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", () => { describe("sveltekit adapter", () => {
adapterTestSuite(bunTestRunner, { adapterTestSuite(bunTestRunner, {
makeApp: sveltekit.getApp, makeApp: (c, a) => sveltekit.getApp(c, a ?? ({} as any)),
makeHandler: (c, a) => (request: Request) => sveltekit.serve(c, a)({ request }), 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"; import { type FrameworkBkndConfig, createFrameworkApp } from "bknd/adapter";
type SvelteKitEnv = NodeJS.ProcessEnv;
type TSvelteKit = { type TSvelteKit = {
request: Request; request: Request;
}; };
export type SvelteKitBkndConfig<Env = SvelteKitEnv> = FrameworkBkndConfig<Env>;
export async function getApp<Env = SvelteKitEnv>( export type SvelteKitBkndConfig<Env> = FrameworkBkndConfig<Env>;
config: SvelteKitBkndConfig<Env> = {},
args: Env = process.env as 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); return await createFrameworkApp(config, args);
} }
export function serve<Env = SvelteKitEnv>( /**
config: SvelteKitBkndConfig<Env> = {}, * Create request handler for hooks.server.ts
args: Env = process.env as Env, * @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 async (fnArgs: TSvelteKit) => {
return (await getApp(config, args)).fetch(fnArgs.request); return (await getApp(config, args)).fetch(fnArgs.request);

View File

@@ -1,8 +1,9 @@
import type { Handle } from "@sveltejs/kit"; import type { Handle } from "@sveltejs/kit";
import { serve } from "bknd/adapter/sveltekit"; import { serve } from "bknd/adapter/sveltekit";
import { env } from "$env/dynamic/private";
import config from "../bknd.config"; import config from "../bknd.config";
const bkndHandler = serve(config); const bkndHandler = serve(config, env);
export const handle: Handle = async ({ event, resolve }) => { export const handle: Handle = async ({ event, resolve }) => {
// Handle bknd API requests // Handle bknd API requests

View File

@@ -1,9 +1,10 @@
import type { PageServerLoad } from "./$types"; import type { PageServerLoad } from "./$types";
import { getApp } from "bknd/adapter/sveltekit"; import { getApp } from "bknd/adapter/sveltekit";
import { env } from "$env/dynamic/private";
import config from "../../bknd.config"; import config from "../../bknd.config";
export const load: PageServerLoad = async () => { export const load: PageServerLoad = async () => {
const app = await getApp(config); const app = await getApp(config, env);
const api = app.getApi(); const api = app.getApi();
const todos = await api.data.readMany("todos"); const todos = await api.data.readMany("todos");