lago-javascript-client 1.47.0 → 1.47.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -71,6 +71,44 @@ try {
71
71
  }
72
72
  ```
73
73
 
74
+ ### Webhook payload types
75
+
76
+ The SDK exposes TypeScript types for every webhook event Lago emits. The types are generated from the OpenAPI spec, so they stay in sync with the API.
77
+
78
+ Indexed lookup by `webhook_type`:
79
+
80
+ ```typescript
81
+ import type { LagoWebhookPayloads } from 'lago-javascript-client';
82
+
83
+ app.post('/webhooks', (req, res) => {
84
+ const event = req.body as LagoWebhookPayloads['alert.triggered'];
85
+ // event.triggered_alert is fully typed
86
+ console.log(event.triggered_alert.external_customer_id);
87
+ res.sendStatus(200);
88
+ });
89
+ ```
90
+
91
+ Discriminated union for handlers that cover multiple events:
92
+
93
+ ```typescript
94
+ import type { LagoWebhookPayload } from 'lago-javascript-client';
95
+
96
+ function handle(event: LagoWebhookPayload) {
97
+ switch (event.webhook_type) {
98
+ case 'alert.triggered':
99
+ // event.triggered_alert is typed
100
+ return event.triggered_alert;
101
+ case 'invoice.created':
102
+ // event.invoice is typed
103
+ return event.invoice;
104
+ case 'subscription.terminated':
105
+ return event.subscription;
106
+ }
107
+ }
108
+ ```
109
+
110
+ A `WebhookOf<T>` helper is also exported for picking a single payload type by name, and `LagoWebhookType` gives the union of all event-type strings.
111
+
74
112
  ## Development
75
113
 
76
114
  Uses [dnt](https://github.com/denoland/dnt) to build and test for Deno and Node.
package/esm/mod.d.ts CHANGED
@@ -14,3 +14,4 @@ export { parseRateLimitHeaders, parseRateLimitInfo, type RateLimitHeaders, type
14
14
  export { createRateLimitFetch, type RateLimitFetchConfig, type RateLimitInfoCallback, } from "./rate_limit_fetch.js";
15
15
  export { DEFAULT_RATE_LIMIT_THRESHOLDS, loggingRateLimitObserver, type LoggingRateLimitObserverOptions, } from "./logging_rate_limit_observer.js";
16
16
  export * from "./openapi/client.js";
17
+ export type { LagoWebhookPayload, LagoWebhookPayloads, LagoWebhookType, WebhookOf, } from "./webhook_types.js";