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:
Szymon Rączka
2025-12-28 12:47:09 +01:00
parent 90b4de7093
commit a97a79f11e
4 changed files with 24 additions and 12 deletions

View File

@@ -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 }),
});
});

View File

@@ -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);

View File

@@ -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

View File

@@ -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");