mirror of
https://github.com/bknd-io/bknd/
synced 2026-08-03 08:36:01 +00:00
Add web adapter
This commit introduces a new web adapter, allowing applications to be served in a web environment. The web adapter provides functionality to create and manage application instances, handle API requests, and serve the application via a fetch handler. It also includes support for integrating with admin functionalities when admin options are provided.
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import {
|
||||
createFrameworkApp,
|
||||
createRuntimeApp,
|
||||
type FrameworkBkndConfig,
|
||||
} from "bknd/adapter";
|
||||
import type { AdminControllerOptions } from "modules/server/AdminController";
|
||||
import type { App } from "App";
|
||||
|
||||
export type WebBkndConfig<Env = any> = FrameworkBkndConfig<Env> & {
|
||||
adminOptions?: AdminControllerOptions | false;
|
||||
};
|
||||
|
||||
export function createBknd<Env>(config: WebBkndConfig<Env>, args?: Env) {
|
||||
let appPromise: Promise<App> | undefined;
|
||||
|
||||
const { adminOptions, ...frameworkConfig } = config;
|
||||
|
||||
async function getApp() {
|
||||
if (!appPromise) {
|
||||
if (adminOptions != null) {
|
||||
appPromise = createRuntimeApp({ ...frameworkConfig, adminOptions }, args);
|
||||
} else {
|
||||
appPromise = createFrameworkApp(frameworkConfig, args);
|
||||
}
|
||||
}
|
||||
return appPromise;
|
||||
}
|
||||
|
||||
async function getApi(opts?: { headers?: Headers; verify?: boolean }) {
|
||||
const app = await getApp();
|
||||
if (opts?.verify) {
|
||||
const api = app.getApi({ headers: opts.headers });
|
||||
await api.verifyAuth();
|
||||
return api;
|
||||
}
|
||||
return app.getApi();
|
||||
}
|
||||
|
||||
function serve() {
|
||||
return async (req: Request) => {
|
||||
const app = await getApp();
|
||||
return app.fetch(req);
|
||||
};
|
||||
}
|
||||
|
||||
return { getApp, getApi, serve };
|
||||
}
|
||||
Reference in New Issue
Block a user