mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 04:46:05 +00:00
public commit
This commit is contained in:
63
examples/sw/index.html
Normal file
63
examples/sw/index.html
Normal file
@@ -0,0 +1,63 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<body>
|
||||
<script type="module" src="/main.ts"></script>
|
||||
|
||||
<fieldset>
|
||||
<legend>Params</legend>
|
||||
<div>
|
||||
<label for="entity">Entity:</label>
|
||||
<select id="entity">
|
||||
<option value="todos">Todos</option>
|
||||
<option value="notes">Notes</option>
|
||||
</select>
|
||||
</div>
|
||||
<div>
|
||||
<div>
|
||||
<label for="query">Query:</label>
|
||||
</div>
|
||||
<textarea id="query" rows="5">{"limit": 2}</textarea>
|
||||
</div>
|
||||
|
||||
<br />
|
||||
<button id="btn">Fetch</button>
|
||||
</fieldset>
|
||||
|
||||
<fieldset>
|
||||
<legend>Result</legend>
|
||||
<pre id="out"></pre>
|
||||
</fieldset>
|
||||
|
||||
<script>
|
||||
const btn = document.getElementById('btn');
|
||||
const query = document.getElementById('query');
|
||||
query.onblur = function(e) {
|
||||
try {
|
||||
const formatted = JSON.stringify(JSON.parse(e.target.value), null, 2);
|
||||
query.style.borderColor = '';
|
||||
query.value = formatted
|
||||
btn.disabled = false;
|
||||
} catch (e) {
|
||||
query.style.borderColor = 'red';
|
||||
btn.disabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
btn.addEventListener('click', async () => {
|
||||
const entity = document.getElementById('entity').value;
|
||||
const body = query.value;
|
||||
|
||||
const res = await fetch(`/api/data/${entity}/query`, {
|
||||
method: 'POST',
|
||||
body,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
});
|
||||
const data = await res.json();
|
||||
document.getElementById('out').innerText = JSON.stringify(data, null, 2);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
19
examples/sw/main.ts
Normal file
19
examples/sw/main.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
if ("serviceWorker" in navigator) {
|
||||
navigator.serviceWorker.getRegistrations().then((registrations) => {
|
||||
for (const registration of registrations) {
|
||||
console.log("[-] unregister Service Worker");
|
||||
registration.unregister();
|
||||
}
|
||||
|
||||
navigator.serviceWorker
|
||||
.register("./sw.ts?t=" + Date.now(), {
|
||||
type: "module"
|
||||
})
|
||||
.then(() => console.log("[+] service Worker registered"))
|
||||
.catch((err) => console.error("[!] service Worker registration failed:", err));
|
||||
|
||||
navigator.serviceWorker.ready.then(() => {
|
||||
console.log("[√] service worker is ready and controlling the page.");
|
||||
});
|
||||
});
|
||||
}
|
||||
19
examples/sw/package.json
Normal file
19
examples/sw/package.json
Normal file
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "sw",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "vite dev",
|
||||
"db": "turso dev --db-file test.db",
|
||||
"bknd": "bknd run --db-url http://localhost:8080",
|
||||
"bknd:local": "bknd run --db-url file:test.db",
|
||||
"db:check": "sqlite3 test.db \"PRAGMA wal_checkpoint(FULL);\""
|
||||
},
|
||||
"type": "module",
|
||||
"dependencies": {
|
||||
"hono": "^4.6.9",
|
||||
"bknd": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
"vite": "^5.4.10"
|
||||
}
|
||||
}
|
||||
35
examples/sw/sw.ts
Normal file
35
examples/sw/sw.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
// To support types
|
||||
// https://github.com/microsoft/TypeScript/issues/14877
|
||||
|
||||
declare const self: ServiceWorkerGlobalScope;
|
||||
|
||||
import { App } from "bknd";
|
||||
|
||||
async function getBknd() {
|
||||
const bknd = App.create({
|
||||
connection: {
|
||||
type: "libsql",
|
||||
config: {
|
||||
url: "http://localhost:8080"
|
||||
}
|
||||
}
|
||||
});
|
||||
await bknd.build();
|
||||
return bknd;
|
||||
}
|
||||
|
||||
self.addEventListener("fetch", async (e) => {
|
||||
// only intercept api requests
|
||||
if (e.request.url.includes("/api/")) {
|
||||
e.respondWith(
|
||||
(async () => {
|
||||
try {
|
||||
const bknd = await getBknd();
|
||||
return bknd.modules.server.fetch(e.request);
|
||||
} catch (e) {
|
||||
return new Response(e.message, { status: 500 });
|
||||
}
|
||||
})()
|
||||
);
|
||||
}
|
||||
});
|
||||
10
examples/sw/tsconfig.json
Normal file
10
examples/sw/tsconfig.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2020",
|
||||
"module": "ESNext",
|
||||
"lib": ["ES2020", "DOM", "WebWorker"],
|
||||
"moduleResolution": "bundler"
|
||||
},
|
||||
"include": ["./"],
|
||||
"exclude": ["node_modules"]
|
||||
}
|
||||
Reference in New Issue
Block a user