From ecb12a2e88f4602a7dba0128669d1e3ac5fee3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jyri-Matti=20La=CC=88hteenma=CC=88ki?= Date: Wed, 9 Oct 2024 22:23:55 +0300 Subject: [PATCH] remove hack to clear content on empty result, since it didn't work well with client-side-templates extension etc --- dist/sqlite.js | 4 ++-- dist/sqlite.min.js | 2 +- package.json | 2 +- src/sqlite.js | 4 ++-- test/sqlite.js | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/dist/sqlite.js b/dist/sqlite.js index fbe3bbf..f9225a4 100644 --- a/dist/sqlite.js +++ b/dist/sqlite.js @@ -61,7 +61,7 @@ Extension to use SQLite database backend for Htmx over: evt.detail.xhr = { status: 200, getAllResponseHeaders: function() { - return this.getResponseHeader("Content-Type:application/json"); + return "Content-Type:application/json"; }, getResponseHeader: function(headerName) { if (headerName.toLowerCase() === "content-type") { @@ -128,7 +128,7 @@ Extension to use SQLite database backend for Htmx over: }); evt.detail.xhr.responseJSON = allRows; - evt.detail.xhr.response = allRows.length == 0 ? '' : JSON.stringify(allRows); + evt.detail.xhr.response = JSON.stringify(allRows); onload(); } } diff --git a/dist/sqlite.min.js b/dist/sqlite.min.js index 72653e0..f26f515 100644 --- a/dist/sqlite.min.js +++ b/dist/sqlite.min.js @@ -1 +1 @@ -(function(){var f;var v;var b;htmx.defineExtension("sqlite",{init:function(e){f=e;v={maxPageSize:4096,timeout:1e4,cacheSize:4096};b={rowMode:"object"}},onEvent:function(e,a){if(e==="htmx:beforeRequest"){let r=a.detail.elt;var t=a.detail.requestConfig;var s;if(t.path==="this.value"||t.verb.toUpperCase()==="GET"&&t.path.toUpperCase().startsWith("SELECT ")||t.verb.toUpperCase()==="PUT"&&t.path.toUpperCase().startsWith("UPDATE ")||t.verb.toUpperCase()==="DELETE"&&t.path.toUpperCase().startsWith("DELETE ")||t.verb.toUpperCase()==="POST"&&t.path.toUpperCase().startsWith("INSERT ")||t.verb.toUpperCase()==="POST"&&t.path.toUpperCase().startsWith("ALTER ")||t.verb.toUpperCase()==="POST"&&t.path.toUpperCase().startsWith("CREATE ")||t.verb.toUpperCase()==="POST"&&t.path.toUpperCase().startsWith("DROP ")||t.verb.toUpperCase()==="POST"&&t.path.toUpperCase().startsWith("TRUNCATE ")){s=t.path}else{return true}if(s==="this.value"){s=r.value;if(!a.detail.target){throw new Error("Attribute 'hx-target' is required when value is used is empty")}}var i=htmx.closest(r,"[hx-db]");if(!i){throw new Error("Attribute 'hx-db' is required in the ancestor hierarchy")}var n=i.getAttribute("hx-db");var h=a.detail.xhr.onload;var p=a.detail.xhr.onerror;a.detail.xhr={status:200,getAllResponseHeaders:function(){return this.getResponseHeader("Content-Type:application/json")},getResponseHeader:function(e){if(e.toLowerCase()==="content-type"){return"application/json"}return undefined}};var o={};Object.entries(a.detail.requestConfig.parameters).forEach(function([e,t]){o["$"+e]=t});var l=htmx.closest(r,"[hx-request]");var d=l?JSON.parse(l.getAttribute("hx-request")):{};var c={...v,...b,...d};var u;if(i._htmx_sqlite_http_backend){u=i._htmx_sqlite_http_backend}else{u=n.match(/^https?:/)?{http:sqliteWasmHttp.createHttpBackend(c)}:{};i._htmx_sqlite_http_backend=u}i.addEventListener("htmx:beforeCleanupElement",function(e){if(e.detail.elt==i&&i._htmx_sqlite_http_backend&&i._htmx_sqlite_http_backend.http){i._htmx_sqlite_http_backend.http.close();delete i._htmx_sqlite_http_backend}});var x=[];sqliteWasmHttp.createSQLiteThread(u).then(function(e){e("open",{filename:encodeURI(n.match(/^https?:\/\//)?n:n.replace(/^opfs:/,"").replace(/^https?:/,"file:")),vfs:n.replace(/:.*/,"").replace("https","http")});return e}).then(function(t){f.triggerEvent(r,"htmx:xhr:loadstart",{elt:r,xhr:{...a.detail.xhr,db:t}});t("exec",{sql:s,bind:o,rowMode:c.rowMode,callback:function(e){if(e.row){x.push(e.row);f.triggerEvent(r,"htmx:xhr:progress",{elt:r,xhr:{...a.detail.xhr,db:t,response:JSON.stringify(e.row),responseJSON:e.row}})}else{f.triggerEvent(r,"htmx:xhr:loadend",{elt:r,xhr:{...a.detail.xhr,db:t,response:x.length==0?"":JSON.stringify(x),responseJSON:x}});t("close",{}).then(function(){t.close()});a.detail.xhr.responseJSON=x;a.detail.xhr.response=x.length==0?"":JSON.stringify(x);h()}}}).catch(function(e){if(e.result){a.detail.error=e.result.message}p()})});return false}}})})(); \ No newline at end of file +(function(){var f;var v;var b;htmx.defineExtension("sqlite",{init:function(e){f=e;v={maxPageSize:4096,timeout:1e4,cacheSize:4096};b={rowMode:"object"}},onEvent:function(e,a){if(e==="htmx:beforeRequest"){let r=a.detail.elt;var t=a.detail.requestConfig;var s;if(t.path==="this.value"||t.verb.toUpperCase()==="GET"&&t.path.toUpperCase().startsWith("SELECT ")||t.verb.toUpperCase()==="PUT"&&t.path.toUpperCase().startsWith("UPDATE ")||t.verb.toUpperCase()==="DELETE"&&t.path.toUpperCase().startsWith("DELETE ")||t.verb.toUpperCase()==="POST"&&t.path.toUpperCase().startsWith("INSERT ")||t.verb.toUpperCase()==="POST"&&t.path.toUpperCase().startsWith("ALTER ")||t.verb.toUpperCase()==="POST"&&t.path.toUpperCase().startsWith("CREATE ")||t.verb.toUpperCase()==="POST"&&t.path.toUpperCase().startsWith("DROP ")||t.verb.toUpperCase()==="POST"&&t.path.toUpperCase().startsWith("TRUNCATE ")){s=t.path}else{return true}if(s==="this.value"){s=r.value;if(!a.detail.target){throw new Error("Attribute 'hx-target' is required when value is used is empty")}}var i=htmx.closest(r,"[hx-db]");if(!i){throw new Error("Attribute 'hx-db' is required in the ancestor hierarchy")}var n=i.getAttribute("hx-db");var h=a.detail.xhr.onload;var p=a.detail.xhr.onerror;a.detail.xhr={status:200,getAllResponseHeaders:function(){return"Content-Type:application/json"},getResponseHeader:function(e){if(e.toLowerCase()==="content-type"){return"application/json"}return undefined}};var o={};Object.entries(a.detail.requestConfig.parameters).forEach(function([e,t]){o["$"+e]=t});var l=htmx.closest(r,"[hx-request]");var c=l?JSON.parse(l.getAttribute("hx-request")):{};var d={...v,...b,...c};var u;if(i._htmx_sqlite_http_backend){u=i._htmx_sqlite_http_backend}else{u=n.match(/^https?:/)?{http:sqliteWasmHttp.createHttpBackend(d)}:{};i._htmx_sqlite_http_backend=u}i.addEventListener("htmx:beforeCleanupElement",function(e){if(e.detail.elt==i&&i._htmx_sqlite_http_backend&&i._htmx_sqlite_http_backend.http){i._htmx_sqlite_http_backend.http.close();delete i._htmx_sqlite_http_backend}});var x=[];sqliteWasmHttp.createSQLiteThread(u).then(function(e){e("open",{filename:encodeURI(n.match(/^https?:\/\//)?n:n.replace(/^opfs:/,"").replace(/^https?:/,"file:")),vfs:n.replace(/:.*/,"").replace("https","http")});return e}).then(function(t){f.triggerEvent(r,"htmx:xhr:loadstart",{elt:r,xhr:{...a.detail.xhr,db:t}});t("exec",{sql:s,bind:o,rowMode:d.rowMode,callback:function(e){if(e.row){x.push(e.row);f.triggerEvent(r,"htmx:xhr:progress",{elt:r,xhr:{...a.detail.xhr,db:t,response:JSON.stringify(e.row),responseJSON:e.row}})}else{f.triggerEvent(r,"htmx:xhr:loadend",{elt:r,xhr:{...a.detail.xhr,db:t,response:x.length==0?"":JSON.stringify(x),responseJSON:x}});t("close",{}).then(function(){t.close()});a.detail.xhr.responseJSON=x;a.detail.xhr.response=JSON.stringify(x);h()}}}).catch(function(e){if(e.result){a.detail.error=e.result.message}p()})});return false}}})})(); \ No newline at end of file diff --git a/package.json b/package.json index 2bfd623..b9a6314 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "htmx-sqlite", - "version": "1.9.0", + "version": "1.9.1", "description": "Htmx extension to use SQLite database backend over HTTP or OPFS", "author": "Jyri-Matti Lähteenmäki ", "keywords": [ diff --git a/src/sqlite.js b/src/sqlite.js index fbe3bbf..f9225a4 100644 --- a/src/sqlite.js +++ b/src/sqlite.js @@ -61,7 +61,7 @@ Extension to use SQLite database backend for Htmx over: evt.detail.xhr = { status: 200, getAllResponseHeaders: function() { - return this.getResponseHeader("Content-Type:application/json"); + return "Content-Type:application/json"; }, getResponseHeader: function(headerName) { if (headerName.toLowerCase() === "content-type") { @@ -128,7 +128,7 @@ Extension to use SQLite database backend for Htmx over: }); evt.detail.xhr.responseJSON = allRows; - evt.detail.xhr.response = allRows.length == 0 ? '' : JSON.stringify(allRows); + evt.detail.xhr.response = JSON.stringify(allRows); onload(); } } diff --git a/test/sqlite.js b/test/sqlite.js index d369353..6fc6c5f 100644 --- a/test/sqlite.js +++ b/test/sqlite.js @@ -29,7 +29,7 @@ describe("sqlite extension", function() { it('empty result clears content', function (done) { var div = make('
'); div.addEventListener('htmx:afterSwap', () => { - div.innerText.should.equal(''); + div.innerText.should.equal('[]'); done(); }); });