definePolicy
definePolicy<
TConfig>(definition):PolicyFactory<TConfig>
Defined in: src/policies/sdk/define-policy.ts:204
Create a policy factory from a declarative definition.
The returned factory function accepts user config, merges it with defaults, wires up skip logic, and injects a debug logger at request time.
When TConfig has required keys, the factory requires a config
argument. When all keys are optional, config is optional.
Type Parameters
Section titled “Type Parameters”TConfig
Section titled “TConfig”TConfig extends PolicyConfig = PolicyConfig
Parameters
Section titled “Parameters”definition
Section titled “definition”PolicyDefinition<TConfig>
Policy name, priority, defaults, and handler.
Returns
Section titled “Returns”PolicyFactory<TConfig>
A factory function whose config parameter is required or optional based on TConfig.
Example
Section titled “Example”import { definePolicy, Priority } from "@homegrower-club/stoma";
const myPolicy = definePolicy<MyConfig>({ name: "my-policy", priority: Priority.AUTH, defaults: { headerName: "x-custom" }, handler: async (c, next, { config, debug }) => { debug("checking header"); const value = c.req.header(config.headerName!); if (!value) throw new GatewayError(401, "unauthorized", "Missing header"); await next(); },});
// Usage: myPolicy({ headerName: "x-api-key" })