import { InferSchemaOutput, RegisterOptions, SchemaRouteOptions, WebhookHandler, WebhookRouterOptions } from "./types.js"; import { StandardSchemaV1 } from "@zap-studio/validation"; //#region src/router.d.ts /** * Main webhook router class. * * Register routes with typed schemas and call `handle` with a Web API `Request`. * * @example * ```ts * import { WebhookRouter } from "@zap-studio/webhooks"; * * const router = new WebhookRouter({ prefix: "/webhooks" }); * * router.register("/stripe", { * schema: stripeEventSchema, * verify: createHmacVerifier({ headerName: "stripe-signature", secret: stripeSecret }), * handler: async ({ payload }) => { * console.log("Stripe event:", payload.type); * }, * }); * * // Each route can carry its own verifier for multi-provider setups — * // no router-level `verify` needed unless routes should share a default. * router.register("/github", { * schema: githubEventSchema, * verify: createHmacVerifier({ headerName: "x-hub-signature-256", secret: githubSecret }), * handler: async ({ payload }) => { * console.log("GitHub event:", payload.ref); * }, * }); * * export default { fetch: (request: Request) => router.handle(request) }; * ``` */ export declare class WebhookRouter { private readonly handlers; private readonly verify; private readonly globalBeforeHooks; private readonly globalAfterHooks; private readonly globalErrorHook; private readonly logger; private readonly prefix; private readonly prefixWithSlash; /** * Creates a webhook router with optional global hooks and verification behavior. * * @param opts - Router-level options. * * @example * ```ts * const router = new WebhookRouter({ * prefix: "/webhooks", * verify: createHmacVerifier({ headerName: "x-signature", secret }), * onError: (error) => Response.json({ error: error.message }, { status: 500 }), * }); * ``` */ constructor(opts?: WebhookRouterOptions); /** * Register a webhook handler for a specific path. * * When a schema is provided, `payload` is inferred from the schema output type. * * @param path - Route path relative to configured prefix, starting with `/` (e.g. `"/stripe"`). * @param handlerOrOptions - Handler function or schema-based registration options. * @returns The same router instance with an updated internal route type map. * * @example * ```ts * router.register("/stripe", { * schema: stripeEventSchema, * handler: async ({ payload }) => { * console.log(payload.type); // typed from stripeEventSchema * }, * }); * ``` */ register>(path: Path, handlerOrOptions: SchemaRouteOptions): WebhookRouter>>; /** * Register a webhook handler for a specific path, with schema-less registration options. * * @param path - Route path relative to configured prefix, starting with `/` (e.g. `"/stripe"`). * @param handlerOrOptions - Registration options without a schema. * @returns The same router instance with an updated internal route type map. * * @example * ```ts * router.register("/ping", { * before: (ctx) => console.log("received", ctx.path), * handler: () => Response.json({ ok: true }), * }); * ``` */ register(path: Path, handlerOrOptions: RegisterOptions): WebhookRouter>; /** * Register a webhook handler for a specific path, using a plain handler function. * * @param path - Route path relative to configured prefix, starting with `/` (e.g. `"/stripe"`). * @param handlerOrOptions - Handler function to process the webhook. * @returns The same router instance with an updated internal route type map. * * @example * ```ts * router.register("/health", () => Response.json({ status: "ok" })); * ``` */ register(path: Path, handlerOrOptions: WebhookHandler): WebhookRouter>; /** * Handles an incoming webhook request. * * The request body is read exactly once; hooks and handlers receive the raw * bytes through the webhook context instead of the request stream. * * @param request - Incoming Web API request. * @returns Web API response for the runtime to send back. * * @example * ```ts * // Framework-agnostic: works with any Web API Request/Response runtime. * export async function POST(request: Request): Promise { * return router.handle(request); * } * ``` */ handle(request: Request): Promise; /** Matches the route, runs hooks/verification/validation, and dispatches the handler. */ private dispatch; /** Resolves the incoming request's URL to a registered route key, or `null` if it doesn't match the configured prefix. */ private matchPath; /** Builds the error response for a failed request, deferring to the global error hook when set. */ private handleError; } /** * Factory helper for creating a webhook router instance. * * @param opts - Optional global router options. * @returns A new webhook router. * * @example * ```ts * import { createWebhookRouter } from "@zap-studio/webhooks"; * * const router = createWebhookRouter({ prefix: "/webhooks" }); * router.register("/stripe", { schema: stripeEventSchema, handler }); * ``` */ export declare const createWebhookRouter: (opts?: WebhookRouterOptions) => WebhookRouter; //#endregion //# sourceMappingURL=router.d.ts.map