mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
refactored adapters to run test suites (#126)
* refactored adapters to run test suites * fix bun version for tests * added missing adapter tests and refactored examples to use `bknd.config.ts` where applicable
This commit is contained in:
@@ -1,68 +1,76 @@
|
||||
import type { App } from "bknd";
|
||||
import { handle } from "hono/aws-lambda";
|
||||
import { type RuntimeBkndConfig, createRuntimeApp } from "bknd/adapter";
|
||||
import { serveStatic } from "@hono/node-server/serve-static";
|
||||
import { type RuntimeBkndConfig, createRuntimeApp, type RuntimeOptions } from "bknd/adapter";
|
||||
|
||||
export type AwsLambdaBkndConfig = RuntimeBkndConfig & {
|
||||
assets?:
|
||||
| {
|
||||
mode: "local";
|
||||
root: string;
|
||||
}
|
||||
| {
|
||||
mode: "url";
|
||||
url: string;
|
||||
};
|
||||
};
|
||||
type AwsLambdaEnv = object;
|
||||
export type AwsLambdaBkndConfig<Env extends AwsLambdaEnv = AwsLambdaEnv> =
|
||||
RuntimeBkndConfig<Env> & {
|
||||
assets?:
|
||||
| {
|
||||
mode: "local";
|
||||
root: string;
|
||||
}
|
||||
| {
|
||||
mode: "url";
|
||||
url: string;
|
||||
};
|
||||
};
|
||||
|
||||
let app: App;
|
||||
export async function createApp({
|
||||
adminOptions = false,
|
||||
assets,
|
||||
...config
|
||||
}: AwsLambdaBkndConfig = {}) {
|
||||
if (!app) {
|
||||
let additional: Partial<RuntimeBkndConfig> = {
|
||||
adminOptions,
|
||||
};
|
||||
export async function createApp<Env extends AwsLambdaEnv = AwsLambdaEnv>(
|
||||
{ adminOptions = false, assets, ...config }: AwsLambdaBkndConfig<Env> = {},
|
||||
args: Env = {} as Env,
|
||||
opts?: RuntimeOptions,
|
||||
): Promise<App> {
|
||||
let additional: Partial<RuntimeBkndConfig> = {
|
||||
adminOptions,
|
||||
};
|
||||
|
||||
if (assets?.mode) {
|
||||
switch (assets.mode) {
|
||||
case "local":
|
||||
// @todo: serve static outside app context
|
||||
additional = {
|
||||
adminOptions: adminOptions === false ? undefined : adminOptions,
|
||||
serveStatic: (await import("@hono/node-server/serve-static")).serveStatic({
|
||||
root: assets.root,
|
||||
onFound: (path, c) => {
|
||||
c.res.headers.set("Cache-Control", "public, max-age=31536000");
|
||||
},
|
||||
}),
|
||||
};
|
||||
break;
|
||||
case "url":
|
||||
additional.adminOptions = {
|
||||
...(typeof adminOptions === "object" ? adminOptions : {}),
|
||||
assets_path: assets.url,
|
||||
};
|
||||
break;
|
||||
default:
|
||||
throw new Error("Invalid assets mode");
|
||||
}
|
||||
if (assets?.mode) {
|
||||
switch (assets.mode) {
|
||||
case "local":
|
||||
// @todo: serve static outside app context
|
||||
additional = {
|
||||
adminOptions: adminOptions === false ? undefined : adminOptions,
|
||||
serveStatic: serveStatic({
|
||||
root: assets.root,
|
||||
onFound: (path, c) => {
|
||||
c.res.headers.set("Cache-Control", "public, max-age=31536000");
|
||||
},
|
||||
}),
|
||||
};
|
||||
break;
|
||||
case "url":
|
||||
additional.adminOptions = {
|
||||
...(typeof adminOptions === "object" ? adminOptions : {}),
|
||||
assets_path: assets.url,
|
||||
};
|
||||
break;
|
||||
default:
|
||||
throw new Error("Invalid assets mode");
|
||||
}
|
||||
|
||||
app = await createRuntimeApp({
|
||||
...config,
|
||||
...additional,
|
||||
});
|
||||
}
|
||||
|
||||
return app;
|
||||
return await createRuntimeApp(
|
||||
{
|
||||
...config,
|
||||
...additional,
|
||||
},
|
||||
args ?? process.env,
|
||||
opts,
|
||||
);
|
||||
}
|
||||
|
||||
export function serveLambda(config: AwsLambdaBkndConfig = {}) {
|
||||
console.log("serving lambda");
|
||||
export function serve<Env extends AwsLambdaEnv = AwsLambdaEnv>(
|
||||
config: AwsLambdaBkndConfig<Env> = {},
|
||||
args: Env = {} as Env,
|
||||
opts?: RuntimeOptions,
|
||||
) {
|
||||
return async (event) => {
|
||||
const app = await createApp(config);
|
||||
const app = await createApp(config, args, opts);
|
||||
return await handle(app.server)(event);
|
||||
};
|
||||
}
|
||||
|
||||
// compatibility with old code
|
||||
export const serveLambda = serve;
|
||||
|
||||
Reference in New Issue
Block a user