Skip to content

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.

TConfig extends PolicyConfig = PolicyConfig

PolicyDefinition<TConfig>

Policy name, priority, defaults, and handler.

PolicyFactory<TConfig>

A factory function whose config parameter is required or optional based on TConfig.

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" })