mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
feat: add SvelteKit adapter
Add framework adapter for SvelteKit with:
- `getApp()` - get bknd app instance
- `serve()` - request handler for hooks.server.ts
Usage in hooks.server.ts:
```typescript
import { serve } from "bknd/adapter/sveltekit";
import config from "../bknd.config";
const bkndHandler = serve(config);
export const handle = async ({ event, resolve }) => {
if (event.url.pathname.startsWith("/api/")) {
return bkndHandler(event);
}
return resolve(event);
};
```
Includes:
- Adapter implementation (app/src/adapter/sveltekit/)
- Test suite
- Working example (examples/sveltekit/)
- Package exports and types
This commit is contained in:
12
examples/sveltekit/src/app.html
Normal file
12
examples/sveltekit/src/app.html
Normal file
@@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<link rel="icon" href="%sveltekit.assets%/favicon.png" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
%sveltekit.head%
|
||||
</head>
|
||||
<body data-sveltekit-preload-data="hover">
|
||||
<div style="display: contents">%sveltekit.body%</div>
|
||||
</body>
|
||||
</html>
|
||||
14
examples/sveltekit/src/hooks.server.ts
Normal file
14
examples/sveltekit/src/hooks.server.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { Handle } from "@sveltejs/kit";
|
||||
import { serve } from "bknd/adapter/sveltekit";
|
||||
import config from "../bknd.config";
|
||||
|
||||
const bkndHandler = serve(config);
|
||||
|
||||
export const handle: Handle = async ({ event, resolve }) => {
|
||||
// Handle bknd API requests
|
||||
if (event.url.pathname.startsWith("/api/")) {
|
||||
return bkndHandler(event);
|
||||
}
|
||||
|
||||
return resolve(event);
|
||||
};
|
||||
14
examples/sveltekit/src/routes/+page.server.ts
Normal file
14
examples/sveltekit/src/routes/+page.server.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import type { PageServerLoad } from "./$types";
|
||||
import { getApp } from "bknd/adapter/sveltekit";
|
||||
import config from "../../bknd.config";
|
||||
|
||||
export const load: PageServerLoad = async () => {
|
||||
const app = await getApp(config);
|
||||
const api = app.getApi();
|
||||
|
||||
const todos = await api.data.readMany("todos");
|
||||
|
||||
return {
|
||||
todos: todos.data ?? [],
|
||||
};
|
||||
};
|
||||
24
examples/sveltekit/src/routes/+page.svelte
Normal file
24
examples/sveltekit/src/routes/+page.svelte
Normal file
@@ -0,0 +1,24 @@
|
||||
<script lang="ts">
|
||||
import type { PageData } from "./$types";
|
||||
|
||||
let { data }: { data: PageData } = $props();
|
||||
</script>
|
||||
|
||||
<h1>bknd + SvelteKit Example</h1>
|
||||
|
||||
<h2>Todos</h2>
|
||||
<ul>
|
||||
{#each data.todos as todo (todo.id)}
|
||||
<li>{todo.title} - {todo.done === "true" ? "Done" : "Pending"}</li>
|
||||
{/each}
|
||||
</ul>
|
||||
|
||||
<h2>API Endpoints</h2>
|
||||
<ul>
|
||||
<li><a href="/api/data/entity/todos">/api/data/entity/todos</a></li>
|
||||
<li><a href="/api/auth/password/login">/api/auth/password/login</a> (POST)</li>
|
||||
</ul>
|
||||
|
||||
<p>
|
||||
Login credentials: <code>admin@example.com</code> / <code>password</code>
|
||||
</p>
|
||||
Reference in New Issue
Block a user