@mxraven/mail - v0.6.0
    Preparing search index...

    Class WebhookHandler

    Verifies, decodes, and dispatches mxRaven webhook requests.

    Register one or more listeners with WebhookHandler.on and feed the request to WebhookHandler.handle. Fetch-native runtimes pass the Request directly; other frameworks pass method, URL, headers, and raw body separately. Listeners for an event run sequentially in registration order, and the request is only acknowledged after every listener resolves, so a thrown error produces a 500 and mxRaven retries the delivery.

    Protocol failures are reported through the returned status and never thrown: 204 accepted, 200 reserved for duplicates, 400 malformed request, 401 bad signature, 413 body too large, and 500 when a listener throws.

    A handler is safe for concurrent use; listeners must be too, because a handler may run for several requests at once.

    const webhook = new WebhookHandler({ secret });

    webhook.on(eventType.inboundEmail, async (email) => {
    await store.save(email.task_id, email);
    });

    export default { fetch: (request: Request) => webhook.handle(request) };
    Index
    • Verifies, decodes, and dispatches a request.

      Accepts a Fetch API Request, or the framework-independent WebhookRequestParts for frameworks that expose method, URL, headers, and body separately. The body is read from a clone, so the caller can still read the original request body afterwards.

      Parameters

      Returns Promise<Response>

      A response with the resulting status and an empty body. This method never rejects for protocol failures: 204 accepted, 400 malformed, 401 bad signature, 413 body too large, or 500 when a listener throws.

    • Registers a listener for one event type.

      Listeners for the same event run in registration order. The payload type is inferred from the event name.

      Type Parameters

      Parameters

      • type: K

        The event type, for example eventType.inboundEmail.

      • handler: WebhookEventHandlers[K]

        The listener to invoke after the request is verified.

      Returns this

      This handler, so registrations can be chained.