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 +38 -0
- package/esm/mod.d.ts +1 -0
- package/esm/openapi/webhooks.d.ts +22385 -0
- package/esm/openapi/webhooks.js +5 -0
- package/esm/webhook_types.d.ts +68 -0
- package/esm/webhook_types.js +23 -0
- package/package.json +1 -1
- package/script/mod.d.ts +1 -0
- package/script/openapi/webhooks.d.ts +22385 -0
- package/script/openapi/webhooks.js +6 -0
- package/script/webhook_types.d.ts +68 -0
- package/script/webhook_types.js +24 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { webhooks } from "./openapi/webhooks.js";
|
|
2
|
+
/** Extract the `application/json` body of a webhook's POST request. */
|
|
3
|
+
type _PayloadOf<K extends keyof webhooks> = webhooks[K] extends {
|
|
4
|
+
post: {
|
|
5
|
+
requestBody?: {
|
|
6
|
+
content: {
|
|
7
|
+
"application/json": infer P;
|
|
8
|
+
};
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
} ? P : never;
|
|
12
|
+
/** Extract the `webhook_type` literal (e.g. `"alert.triggered"`) from a payload. */
|
|
13
|
+
type _WebhookTypeOf<K extends keyof webhooks> = _PayloadOf<K> extends {
|
|
14
|
+
webhook_type: infer T;
|
|
15
|
+
} ? T extends string ? T : never : never;
|
|
16
|
+
/**
|
|
17
|
+
* Map of webhook event name to its typed payload.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* import type { LagoWebhookPayloads } from "lago-javascript-client";
|
|
22
|
+
*
|
|
23
|
+
* app.post("/webhooks", (req, res) => {
|
|
24
|
+
* const event = req.body as LagoWebhookPayloads["alert.triggered"];
|
|
25
|
+
* console.log(event.triggered_alert.external_customer_id);
|
|
26
|
+
* });
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export type LagoWebhookPayloads = {
|
|
30
|
+
[K in keyof webhooks as _WebhookTypeOf<K>]: _PayloadOf<K>;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Discriminated union of every webhook payload. Narrow with the `webhook_type`
|
|
34
|
+
* field in a `switch` to get a fully typed branch.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```ts
|
|
38
|
+
* import type { LagoWebhookPayload } from "lago-javascript-client";
|
|
39
|
+
*
|
|
40
|
+
* function handle(event: LagoWebhookPayload) {
|
|
41
|
+
* switch (event.webhook_type) {
|
|
42
|
+
* case "alert.triggered":
|
|
43
|
+
* // event is narrowed; event.triggered_alert is fully typed
|
|
44
|
+
* break;
|
|
45
|
+
* case "invoice.created":
|
|
46
|
+
* // event.invoice is fully typed
|
|
47
|
+
* break;
|
|
48
|
+
* }
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
export type LagoWebhookPayload = LagoWebhookPayloads[keyof LagoWebhookPayloads];
|
|
53
|
+
/**
|
|
54
|
+
* Union of every `webhook_type` string emitted by Lago, e.g.
|
|
55
|
+
* `"alert.triggered" | "invoice.created" | ...`.
|
|
56
|
+
*/
|
|
57
|
+
export type LagoWebhookType = keyof LagoWebhookPayloads;
|
|
58
|
+
/**
|
|
59
|
+
* Helper to look up a single payload type by its `webhook_type` string.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* import type { WebhookOf } from "lago-javascript-client";
|
|
64
|
+
* type InvoiceCreated = WebhookOf<"invoice.created">;
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export type WebhookOf<T extends LagoWebhookType> = LagoWebhookPayloads[T];
|
|
68
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// User-facing TypeScript types for Lago webhook payloads.
|
|
3
|
+
//
|
|
4
|
+
// Backed by ./openapi/webhooks.ts, which is auto-generated from the Lago
|
|
5
|
+
// OpenAPI 3.1 spec (`webhooks:` key) via `deno task generate:webhooks`.
|
|
6
|
+
// This file is hand-written and is safe across regenerations.
|
|
7
|
+
//
|
|
8
|
+
// The generator output keys events by their snake_case operationId
|
|
9
|
+
// (e.g. `alert_triggered`). We re-key here by the dotted `webhook_type`
|
|
10
|
+
// literal (e.g. `alert.triggered`) so that the public API matches the
|
|
11
|
+
// string customers actually see on the wire.
|
|
12
|
+
//
|
|
13
|
+
// Note on nested types: the payload types here are produced by
|
|
14
|
+
// `openapi-typescript`, while the SDK's component exports (e.g.
|
|
15
|
+
// `CustomerObject`, `InvoiceObjectExtended`) are produced by
|
|
16
|
+
// `swagger-typescript-api`. The two generators interpret the same schema
|
|
17
|
+
// slightly differently for nullable fields, so a value typed as
|
|
18
|
+
// `LagoWebhookPayloads['customer.created']['customer']` is not always
|
|
19
|
+
// structurally equal to `CustomerObject` from the main SDK exports. Both
|
|
20
|
+
// are valid views of the same wire shape; the openapi-typescript view is
|
|
21
|
+
// the more accurate one for what actually arrives over the network. If
|
|
22
|
+
// you pass webhook objects into helpers typed with SDK components, a
|
|
23
|
+
// narrowing cast may be required.
|
|
24
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|