mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
fix(sveltekit): make adapter runtime-agnostic
Remove process.env default to support all SvelteKit runtimes (Node, Cloudflare Workers, Vercel Edge, Deno). Users now pass env explicitly via $env/dynamic/private.
This commit is contained in:
@@ -9,7 +9,7 @@ afterAll(enableConsoleLog);
|
||||
|
||||
describe("sveltekit adapter", () => {
|
||||
adapterTestSuite(bunTestRunner, {
|
||||
makeApp: sveltekit.getApp,
|
||||
makeHandler: (c, a) => (request: Request) => sveltekit.serve(c, a)({ request }),
|
||||
makeApp: (c, a) => sveltekit.getApp(c, a ?? ({} as any)),
|
||||
makeHandler: (c, a) => (request: Request) => sveltekit.serve(c, a ?? ({} as any))({ request }),
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,21 +1,31 @@
|
||||
import { type FrameworkBkndConfig, createFrameworkApp } from "bknd/adapter";
|
||||
|
||||
type SvelteKitEnv = NodeJS.ProcessEnv;
|
||||
type TSvelteKit = {
|
||||
request: Request;
|
||||
};
|
||||
export type SvelteKitBkndConfig<Env = SvelteKitEnv> = FrameworkBkndConfig<Env>;
|
||||
|
||||
export async function getApp<Env = SvelteKitEnv>(
|
||||
config: SvelteKitBkndConfig<Env> = {},
|
||||
args: Env = process.env as Env,
|
||||
export type SvelteKitBkndConfig<Env> = FrameworkBkndConfig<Env>;
|
||||
|
||||
/**
|
||||
* Get bknd app instance
|
||||
* @param config - bknd configuration
|
||||
* @param args - environment variables (use $env/dynamic/private for universal runtime support)
|
||||
*/
|
||||
export async function getApp<Env>(
|
||||
config: SvelteKitBkndConfig<Env> = {} as SvelteKitBkndConfig<Env>,
|
||||
args: Env,
|
||||
) {
|
||||
return await createFrameworkApp(config, args);
|
||||
}
|
||||
|
||||
export function serve<Env = SvelteKitEnv>(
|
||||
config: SvelteKitBkndConfig<Env> = {},
|
||||
args: Env = process.env as Env,
|
||||
/**
|
||||
* Create request handler for hooks.server.ts
|
||||
* @param config - bknd configuration
|
||||
* @param args - environment variables (use $env/dynamic/private for universal runtime support)
|
||||
*/
|
||||
export function serve<Env>(
|
||||
config: SvelteKitBkndConfig<Env> = {} as SvelteKitBkndConfig<Env>,
|
||||
args: Env,
|
||||
) {
|
||||
return async (fnArgs: TSvelteKit) => {
|
||||
return (await getApp(config, args)).fetch(fnArgs.request);
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import type { Handle } from "@sveltejs/kit";
|
||||
import { serve } from "bknd/adapter/sveltekit";
|
||||
import { env } from "$env/dynamic/private";
|
||||
import config from "../bknd.config";
|
||||
|
||||
const bkndHandler = serve(config);
|
||||
const bkndHandler = serve(config, env);
|
||||
|
||||
export const handle: Handle = async ({ event, resolve }) => {
|
||||
// Handle bknd API requests
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import type { PageServerLoad } from "./$types";
|
||||
import { getApp } from "bknd/adapter/sveltekit";
|
||||
import { env } from "$env/dynamic/private";
|
||||
import config from "../../bknd.config";
|
||||
|
||||
export const load: PageServerLoad = async () => {
|
||||
const app = await getApp(config);
|
||||
const app = await getApp(config, env);
|
||||
const api = app.getApi();
|
||||
|
||||
const todos = await api.data.readMany("todos");
|
||||
|
||||
Reference in New Issue
Block a user