Skip to content

feat: export TypeScript types for webhook payloads#88

Open
mikeh-lago wants to merge 1 commit into
mainfrom
issue-1791-webhook-types
Open

feat: export TypeScript types for webhook payloads#88
mikeh-lago wants to merge 1 commit into
mainfrom
issue-1791-webhook-types

Conversation

@mikeh-lago
Copy link
Copy Markdown
Contributor

What
Adds TypeScript types for webhook payloads to the JS SDK. Customers can now import strongly typed webhook event types instead of treating req.body as any in their webhook handlers.

Why
Today, users handling Lago webhooks get zero type safety on the payload. They have to: Cast req.body to a hand-written interface they maintain themselves, or read the docs every time they touch a webhook handler.

The OpenAPI spec already defines every webhook event under the OAS 3.1 webhooks: key, complete with webhook_type discriminators and $refs to the existing components/schemas objects. The schemas exist, they just weren't surfaced.

After this PR:

import type { LagoWebhookPayloads, LagoWebhookPayload } from 'lago-javascript-client';
 
// Indexed lookup by event name
app.post('/webhooks', (req, res) => {
  const event = req.body as LagoWebhookPayloads['alert.triggered'];
  console.log(event.triggered_alert.external_customer_id); // fully typed
});

// Discriminated union for multi-event handlers
function handle(event: LagoWebhookPayload) {
  switch (event.webhook_type) {
    case 'alert.triggered':         return event.triggered_alert;
    case 'invoice.created':         return event.invoice;
    case 'subscription.terminated': return event.subscription;
  }
}

How

  • swagger-typescript-api (existing) continues to generate openapi/client.ts from paths:.

  • openapi-typescript@7.13.0 (new) generates openapi/webhooks.ts from the webhooks: key. swagger-typescript-api does not support that key, which is why a second tool is needed. openapi-typescript is the most adopted OAS-to-TS tool that supports OAS 3.1 natively

Verification

  • Ran openapi-typescript@7.13.0 against the live https://swagger.getlago.com/openapi.yaml. 224 ms, 50 events, no errors.
  • Ran tsc --strict against webhook_types.ts plus the generated file. Zero errors.
  • Verified four usage patterns compile cleanly:
    • LagoWebhookPayloads["alert.triggered"] indexed lookup
    • WebhookOf<"invoice.created"> helper
    • LagoWebhookType union
    • switch (event.webhook_type) narrowing on LagoWebhookPayload
  • Confirmed no name collisions: openapi-typescript exports five top-level names (paths, webhooks, components, $defs, operations). None overlap with swagger-typescript-api's output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants