From 9ddacb7ae568270d0c7d0d5493ecb8513d1637eb Mon Sep 17 00:00:00 2001 From: dswbx Date: Sat, 25 Jan 2025 11:54:19 +0100 Subject: [PATCH] added adapter exports, generalized env-depending config construction --- app/build.ts | 8 +++++++ app/package.json | 4 ++++ app/src/adapter/astro/astro.adapter.ts | 9 ++++---- .../cloudflare/cloudflare-workers.adapter.ts | 14 +++++------- app/src/adapter/cloudflare/modes/cached.ts | 4 ++-- app/src/adapter/cloudflare/modes/durable.ts | 4 ++-- app/src/adapter/cloudflare/modes/fresh.ts | 4 ++-- app/src/adapter/index.ts | 22 +++++++++---------- app/src/adapter/remix/remix.adapter.ts | 14 ++++++++---- examples/astro/src/pages/api/[...api].ts | 3 ++- examples/cloudflare-worker/src/index.ts | 2 +- 11 files changed, 53 insertions(+), 35 deletions(-) diff --git a/app/build.ts b/app/build.ts index e931021..3c37c6e 100644 --- a/app/build.ts +++ b/app/build.ts @@ -180,6 +180,14 @@ function baseConfig(adapter: string): tsup.Options { }; } +// base adapter handles +await tsup.build({ + ...baseConfig(""), + entry: ["src/adapter/index.ts"], + outDir: "dist/adapter" +}); + +// specific adatpers await tsup.build(baseConfig("remix")); await tsup.build(baseConfig("bun")); await tsup.build(baseConfig("astro")); diff --git a/app/package.json b/app/package.json index a04b686..a2c0893 100644 --- a/app/package.json +++ b/app/package.json @@ -148,6 +148,10 @@ "import": "./dist/adapter/cloudflare/index.js", "require": "./dist/adapter/cloudflare/index.cjs" }, + "./adapter": { + "types": "./dist/types/adapter/index.d.ts", + "import": "./dist/adapter/index.js" + }, "./adapter/vite": { "types": "./dist/types/adapter/vite/index.d.ts", "import": "./dist/adapter/vite/index.js", diff --git a/app/src/adapter/astro/astro.adapter.ts b/app/src/adapter/astro/astro.adapter.ts index 479b873..764e100 100644 --- a/app/src/adapter/astro/astro.adapter.ts +++ b/app/src/adapter/astro/astro.adapter.ts @@ -1,7 +1,7 @@ import { type FrameworkBkndConfig, createFrameworkApp } from "adapter"; import { Api, type ApiOptions, type App } from "bknd"; -export type AstroBkndConfig = FrameworkBkndConfig; +export type AstroBkndConfig = FrameworkBkndConfig; type TAstro = { request: Request; @@ -21,10 +21,11 @@ export function getApi(Astro: TAstro, options: Options = { mode: "static" }) { } let app: App; -export function serve(config: AstroBkndConfig = {}) { - return async (args: TAstro) => { +export function serve(config: AstroBkndConfig = {}) { + return async (args: Context) => { + console.log("args", Object.keys(args)); if (!app) { - app = await createFrameworkApp(config); + app = await createFrameworkApp(config, args); } return app.fetch(args.request); }; diff --git a/app/src/adapter/cloudflare/cloudflare-workers.adapter.ts b/app/src/adapter/cloudflare/cloudflare-workers.adapter.ts index da3c762..cfcba5d 100644 --- a/app/src/adapter/cloudflare/cloudflare-workers.adapter.ts +++ b/app/src/adapter/cloudflare/cloudflare-workers.adapter.ts @@ -1,4 +1,3 @@ -import type { CreateAppConfig } from "bknd"; import { Hono } from "hono"; import { serveStatic } from "hono/cloudflare-workers"; import type { FrameworkBkndConfig } from "../index"; @@ -6,10 +5,9 @@ import { getCached } from "./modes/cached"; import { getDurable } from "./modes/durable"; import { getFresh, getWarm } from "./modes/fresh"; -export type CloudflareBkndConfig = Omit & { - app: CreateAppConfig | ((env: Env) => CreateAppConfig); +export type CloudflareBkndConfig = FrameworkBkndConfig> & { mode?: "warm" | "fresh" | "cache" | "durable"; - bindings?: (env: Env) => { + bindings?: (args: Context) => { kv?: KVNamespace; dobj?: DurableObjectNamespace; }; @@ -21,15 +19,15 @@ export type CloudflareBkndConfig = Omit & html?: string; }; -export type Context = { +export type Context = { request: Request; - env: any; + env: Env; ctx: ExecutionContext; }; -export function serve(config: CloudflareBkndConfig) { +export function serve(config: CloudflareBkndConfig) { return { - async fetch(request: Request, env: any, ctx: ExecutionContext) { + async fetch(request: Request, env: Env, ctx: ExecutionContext) { const url = new URL(request.url); const manifest = config.manifest; diff --git a/app/src/adapter/cloudflare/modes/cached.ts b/app/src/adapter/cloudflare/modes/cached.ts index a238ae0..2c04f51 100644 --- a/app/src/adapter/cloudflare/modes/cached.ts +++ b/app/src/adapter/cloudflare/modes/cached.ts @@ -2,7 +2,7 @@ import { createRuntimeApp } from "adapter"; import { App } from "bknd"; import type { CloudflareBkndConfig, Context } from "../index"; -export async function getCached(config: CloudflareBkndConfig, { env, ctx }: Context) { +export async function getCached(config: CloudflareBkndConfig, { env, ctx, ...args }: Context) { const { kv } = config.bindings?.(env)!; if (!kv) throw new Error("kv namespace is not defined in cloudflare.bindings"); const key = config.key ?? "app"; @@ -37,7 +37,7 @@ export async function getCached(config: CloudflareBkndConfig, { env, ctx }: Cont }, adminOptions: { html: config.html } }, - env + { env, ctx, ...args } ); if (!cachedConfig) { diff --git a/app/src/adapter/cloudflare/modes/durable.ts b/app/src/adapter/cloudflare/modes/durable.ts index 3787b5c..4c7233b 100644 --- a/app/src/adapter/cloudflare/modes/durable.ts +++ b/app/src/adapter/cloudflare/modes/durable.ts @@ -1,5 +1,5 @@ import { DurableObject } from "cloudflare:workers"; -import { createRuntimeApp } from "adapter"; +import { createRuntimeApp, makeConfig } from "adapter"; import type { CloudflareBkndConfig, Context } from "adapter/cloudflare"; import type { App, CreateAppConfig } from "bknd"; @@ -17,7 +17,7 @@ export async function getDurable(config: CloudflareBkndConfig, ctx: Context) { const id = dobj.idFromName(key); const stub = dobj.get(id) as unknown as DurableBkndApp; - const create_config = typeof config.app === "function" ? config.app(ctx.env) : config.app; + const create_config = makeConfig(config, ctx); const res = await stub.fire(ctx.request, { config: create_config, diff --git a/app/src/adapter/cloudflare/modes/fresh.ts b/app/src/adapter/cloudflare/modes/fresh.ts index cb5ece7..30272bf 100644 --- a/app/src/adapter/cloudflare/modes/fresh.ts +++ b/app/src/adapter/cloudflare/modes/fresh.ts @@ -2,13 +2,13 @@ import { createRuntimeApp } from "adapter"; import type { App } from "bknd"; import type { CloudflareBkndConfig, Context } from "../index"; -export async function makeApp(config: CloudflareBkndConfig, { env }: Context) { +export async function makeApp(config: CloudflareBkndConfig, ctx: Context) { return await createRuntimeApp( { ...config, adminOptions: config.html ? { html: config.html } : undefined }, - env + ctx ); } diff --git a/app/src/adapter/index.ts b/app/src/adapter/index.ts index 19bcdef..492f55e 100644 --- a/app/src/adapter/index.ts +++ b/app/src/adapter/index.ts @@ -5,16 +5,16 @@ import type { MiddlewareHandler } from "hono"; import { StorageLocalAdapter } from "media/storage/adapters/StorageLocalAdapter"; import type { AdminControllerOptions } from "modules/server/AdminController"; -export type BkndConfig = CreateAppConfig & { - app?: CreateAppConfig | ((env: Env) => CreateAppConfig); +export type BkndConfig = CreateAppConfig & { + app?: CreateAppConfig | ((args: Args) => CreateAppConfig); onBuilt?: (app: App) => Promise; beforeBuild?: (app: App) => Promise; buildConfig?: Parameters[0]; }; -export type FrameworkBkndConfig = BkndConfig; +export type FrameworkBkndConfig = BkndConfig; -export type RuntimeBkndConfig = BkndConfig & { +export type RuntimeBkndConfig = BkndConfig & { distPath?: string; }; @@ -46,14 +46,14 @@ export function registerLocalMediaAdapter() { registries.media.register("local", StorageLocalAdapter); } -export function makeConfig(config: BkndConfig, env?: Env): CreateAppConfig { +export function makeConfig(config: BkndConfig, args?: Args): CreateAppConfig { let additionalConfig: CreateAppConfig = {}; if ("app" in config && config.app) { if (typeof config.app === "function") { - if (!env) { - throw new Error("env is required when config.app is a function"); + if (!args) { + throw new Error("args is required when config.app is a function"); } - additionalConfig = config.app(env); + additionalConfig = config.app(args); } else { additionalConfig = config.app; } @@ -62,11 +62,11 @@ export function makeConfig(config: BkndConfig, env?: Env): Creat return { ...config, ...additionalConfig }; } -export async function createFrameworkApp( +export async function createFrameworkApp( config: FrameworkBkndConfig, - env?: Env + args?: Args ): Promise { - const app = App.create(makeConfig(config, env)); + const app = App.create(makeConfig(config, args)); if (config.onBuilt) { app.emgr.onEvent( diff --git a/app/src/adapter/remix/remix.adapter.ts b/app/src/adapter/remix/remix.adapter.ts index c3d0c78..7681fd4 100644 --- a/app/src/adapter/remix/remix.adapter.ts +++ b/app/src/adapter/remix/remix.adapter.ts @@ -1,13 +1,19 @@ import { type FrameworkBkndConfig, createFrameworkApp } from "adapter"; import type { App } from "bknd"; -export type RemixBkndConfig = FrameworkBkndConfig; +export type RemixBkndConfig = FrameworkBkndConfig; + +type RemixContext = { + request: Request; +}; let app: App; -export function serve(config: RemixBkndConfig = {}) { - return async (args: { request: Request }) => { +export function serve( + config: RemixBkndConfig = {} +) { + return async (args: Args) => { if (!app) { - app = await createFrameworkApp(config); + app = await createFrameworkApp(config, args); } return app.fetch(args.request); }; diff --git a/examples/astro/src/pages/api/[...api].ts b/examples/astro/src/pages/api/[...api].ts index abbcca7..82d3c4e 100644 --- a/examples/astro/src/pages/api/[...api].ts +++ b/examples/astro/src/pages/api/[...api].ts @@ -1,3 +1,4 @@ +import type { APIContext } from "astro"; import { App } from "bknd"; import { serve } from "bknd/adapter/astro"; import { registerLocalMediaAdapter } from "bknd/adapter/node"; @@ -23,7 +24,7 @@ declare module "bknd/core" { interface DB extends Database {} } -export const ALL = serve({ +export const ALL = serve({ // we can use any libsql config, and if omitted, uses in-memory connection: { type: "libsql", diff --git a/examples/cloudflare-worker/src/index.ts b/examples/cloudflare-worker/src/index.ts index e7a0f5e..dbb2e9b 100644 --- a/examples/cloudflare-worker/src/index.ts +++ b/examples/cloudflare-worker/src/index.ts @@ -3,7 +3,7 @@ import { serve } from "bknd/adapter/cloudflare"; import manifest from "__STATIC_CONTENT_MANIFEST"; export default serve({ - app: (env: Env) => ({ + app: (args) => ({ connection: { type: "libsql", config: {