web / lib/services/webhook-registry
lib/services/webhook-registry
Generic webhook registry for token-based triggers.
Abstracts webhook token lookup across multiple collections (scheduled-ingests, scrapers) and dispatches to the appropriate job handler. Used by the POST /api/webhooks/trigger/[token] endpoint.
Interfaces
WebhookTarget
Result of resolving a webhook token to a triggerable resource.
Properties
type
type:
"scheduled-ingest"|"scraper"
Which collection the token belongs to.
id
id:
number
The record ID in the source collection.
name
name:
string
Display name for logging.
record
record:
Record<string,unknown>
The full record (for dispatching).
Functions
generateWebhookToken()
generateWebhookToken():
string
Generate a cryptographically random webhook token. 32 bytes = 64 hex characters.
Returns
string
handleWebhookTokenLifecycle()
handleWebhookTokenLifecycle(
data,originalDoc?):void
Handle webhook token generation/rotation for a document.
Reusable across any collection with webhookEnabled and webhookToken fields.
Call from a collection’s beforeChange hook.
Parameters
data
Record<string, unknown>
originalDoc?
Record<string, unknown>
Returns
void
computeWebhookUrl()
computeWebhookUrl(
data):string|null
Compute the webhook URL for display in the admin UI.
Reusable across any collection with webhook fields.
Parameters
data
Record<string, unknown> | undefined
Returns
string | null
resolveWebhookToken()
resolveWebhookToken(
payload,token):Promise<WebhookTarget|null>
Resolve a webhook token to its target resource.
Checks scheduled-ingests first, then scrapers. Returns null if the token is not found or the webhook is disabled on the matching record.
Parameters
payload
BasePayload
token
string
Returns
Promise<WebhookTarget | null>
claimScraperRunning()
claimScraperRunning(
payload,scraperId):Promise<boolean>
Atomically claim “running” status on a scraper to prevent concurrent webhook triggers. Returns true if the claim succeeded, false if already running.
Parameters
payload
BasePayload
scraperId
number
Returns
Promise<boolean>