UNPKG

lago-javascript-client

Version:
69 lines (68 loc) 2.13 kB
import type { webhooks } from "./openapi/webhooks.js"; /** Extract the `application/json` body of a webhook's POST request. */ type _PayloadOf<K extends keyof webhooks> = webhooks[K] extends { post: { requestBody?: { content: { "application/json": infer P; }; }; }; } ? P : never; /** Extract the `webhook_type` literal (e.g. `"alert.triggered"`) from a payload. */ type _WebhookTypeOf<K extends keyof webhooks> = _PayloadOf<K> extends { webhook_type: infer T; } ? T extends string ? T : never : never; /** * Map of webhook event name to its typed payload. * * @example * ```ts * import type { LagoWebhookPayloads } from "lago-javascript-client"; * * app.post("/webhooks", (req, res) => { * const event = req.body as LagoWebhookPayloads["alert.triggered"]; * console.log(event.triggered_alert.external_customer_id); * }); * ``` */ export type LagoWebhookPayloads = { [K in keyof webhooks as _WebhookTypeOf<K>]: _PayloadOf<K>; }; /** * Discriminated union of every webhook payload. Narrow with the `webhook_type` * field in a `switch` to get a fully typed branch. * * @example * ```ts * import type { LagoWebhookPayload } from "lago-javascript-client"; * * function handle(event: LagoWebhookPayload) { * switch (event.webhook_type) { * case "alert.triggered": * // event is narrowed; event.triggered_alert is fully typed * break; * case "invoice.created": * // event.invoice is fully typed * break; * } * } * ``` */ export type LagoWebhookPayload = LagoWebhookPayloads[keyof LagoWebhookPayloads]; /** * Union of every `webhook_type` string emitted by Lago, e.g. * `"alert.triggered" | "invoice.created" | ...`. */ export type LagoWebhookType = keyof LagoWebhookPayloads; /** * Helper to look up a single payload type by its `webhook_type` string. * * @example * ```ts * import type { WebhookOf } from "lago-javascript-client"; * type InvoiceCreated = WebhookOf<"invoice.created">; * ``` */ export type WebhookOf<T extends LagoWebhookType> = LagoWebhookPayloads[T]; export {};