import { ErrorFactory } from "./errors.mjs"; import { TableBoundary } from "./zod-table.mjs"; import { FunctionReference, GenericDataModel, GenericMutationCtx } from "convex/server"; import { z } from "zod"; //#region src/webhooks.d.ts /** * The host-owned dedup table (the kit gains no tables). Register it in the * schema with the by_eventKey index: * * webhookEvents: webhookEventsTable().table.index('by_eventKey', ['eventKey']) * * Rows are host-owned data: prune them on a retention window comfortably * longer than the provider's redelivery horizon (a cleanup cron over * `receivedAt` — see docs/webhooks.md). */ export declare function webhookEventsTable(): TableBoundary<"webhookEvents", { eventKey: z.ZodString; source: z.ZodString; receivedAt: z.ZodNumber; }, readonly [], readonly [], readonly []>; type DedupContext = Pick, 'db'>; /** * Transactional insert-if-absent dedup guard. Call this FIRST inside the * internal mutation the boundary delegates to — dedup must live in the * mutation (transactional), never in the action. Returns duplicate: true * when the natural key was already recorded; the caller then no-ops. */ export declare function recordWebhookEvent(ctx: DedupContext, input: { key: string; source?: string; table?: string; }): Promise<{ duplicate: boolean; }>; export type WebhookBoundaryConfig = { /** * Verifies the provider signature over the RAW body bytes — the helper * hands it `request.text()` BEFORE any parsing. Implementations must use * a constant-time comparison (e.g. crypto.subtle HMAC verify) and read * the secret from a Convex environment variable. Return false or throw * to reject. */ verify: (raw: string, request: Request) => boolean | Promise; /** * Derives the natural dedup key — the `${event}:${id}:${updatedAt}` * pattern. Receives the raw body and the request; parse inside if needed. */ eventKey: (raw: string, request: Request) => string | Promise; source?: string; errors?: ErrorFactory; }; /** * The sanctioned webhook pattern as code: verify (raw body, fail closed) → * derive natural key → delegate to a host internal mutation whose FIRST act * is recordWebhookEvent (transactional dedup). Use inside the host's * httpAction: * * const boundary = createWebhookBoundary({ verify, eventKey }) * http.route({ path: '/webhooks/vendor', method: 'POST', * handler: httpAction((ctx, request) => * boundary.handle(ctx, request, internal.domain.vendor.functions.applyEvent)) }) */ export declare function createWebhookBoundary(config: WebhookBoundaryConfig): { handle: (ctx: { runMutation: (target: FunctionReference<'mutation', 'internal', WebhookDelivery, Result>, args: WebhookDelivery) => Promise; }, request: Request, target: FunctionReference<'mutation', 'internal', WebhookDelivery, Result>) => Promise; }; export type WebhookDelivery = { eventKey: string; payload: string; source: string; }; //#endregion //# sourceMappingURL=webhooks.d.mts.map