Skip to content

definePluginFor

definePluginFor<TEnv>(): <TName, TProvides, TRequires, TClient>(config) => CfastPlugin<TName, Awaited<TProvides>, RequiresFromPlugins<TRequires>, TClient, TEnv>

Defined in: packages/core/src/define-plugin.ts:175

Returns an env-typed definePlugin factory.

The scaffolded Cloudflare.Env type from worker-configuration.d.ts is not known to @cfast/core at build time, so the generic definePlugin exposes ctx.env as the loose Record<string, unknown> shape and consumers have to cast bindings (e.g. ctx.env.DB as D1Database). Apps that want precise bindings call this factory once with their env type and re-export the typed definePlugin from a local module:

app/plugins/define-plugin.ts
import { definePluginFor } from "@cfast/core";
export const definePlugin = definePluginFor<Cloudflare.Env>();

Plugin authors then import from their local module and get ctx.env.DB typed as D1Database without any casting:

import { definePlugin } from "~/plugins/define-plugin";
export const dbPlugin = definePlugin({
name: "db",
setup(ctx) {
const db = ctx.env.DB; // D1Database, no cast
return { client: createDb({ d1: db }) };
},
});

The returned factory has the same shape as definePlugin (with the curried legacy overload preserved) so existing code that switches from the generic form to the env-typed form only needs an import swap.

TEnv

<TName, TProvides, TRequires, TClient>(config): CfastPlugin<TName, Awaited<TProvides>, RequiresFromPlugins<TRequires>, TClient, TEnv>

TName extends string

TProvides

TRequires extends readonly CfastPlugin<string, unknown, any, unknown, any>[] = []

TClient = unknown

TClient

TName

ComponentType<{ children: ReactNode; }>

TRequires

(ctx) => TProvides | Promise<TProvides>

CfastPlugin<TName, Awaited<TProvides>, RequiresFromPlugins<TRequires>, TClient, TEnv>