lago-javascript-client 1.46.1 → 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.
@@ -0,0 +1,33 @@
1
+ import type { RateLimitInfoCallback } from "./rate_limit_fetch.js";
2
+ /**
3
+ * Default usage thresholds (80%, 90%, 95%) at which the observer emits a log.
4
+ */
5
+ export declare const DEFAULT_RATE_LIMIT_THRESHOLDS: readonly number[];
6
+ /**
7
+ * Configuration for the LoggingRateLimitObserver.
8
+ */
9
+ export interface LoggingRateLimitObserverOptions {
10
+ /** Usage fractions (0.0 - 1.0) that should produce a log line. */
11
+ thresholds?: readonly number[];
12
+ /**
13
+ * Function used to emit the log line. Defaults to `console.warn`.
14
+ * Useful when injecting a structured logger.
15
+ */
16
+ log?: (message: string) => void;
17
+ }
18
+ /**
19
+ * Returns a ready-to-use `onRateLimitInfo` callback that logs a warning each
20
+ * time rate limit usage crosses one of the configured thresholds.
21
+ *
22
+ * Example:
23
+ *
24
+ * ```ts
25
+ * import { Client } from "@getlago/lago-javascript-client";
26
+ * import { loggingRateLimitObserver } from "@getlago/lago-javascript-client";
27
+ *
28
+ * const client = Client(apiKey, {
29
+ * rateLimitRetry: { onRateLimitInfo: loggingRateLimitObserver() },
30
+ * });
31
+ * ```
32
+ */
33
+ export declare function loggingRateLimitObserver(options?: LoggingRateLimitObserverOptions): RateLimitInfoCallback;
@@ -0,0 +1,43 @@
1
+ import { rateLimitUsagePct } from "./rate_limit_headers.js";
2
+ /**
3
+ * Default usage thresholds (80%, 90%, 95%) at which the observer emits a log.
4
+ */
5
+ export const DEFAULT_RATE_LIMIT_THRESHOLDS = [
6
+ 0.8,
7
+ 0.9,
8
+ 0.95,
9
+ ];
10
+ /**
11
+ * Returns a ready-to-use `onRateLimitInfo` callback that logs a warning each
12
+ * time rate limit usage crosses one of the configured thresholds.
13
+ *
14
+ * Example:
15
+ *
16
+ * ```ts
17
+ * import { Client } from "@getlago/lago-javascript-client";
18
+ * import { loggingRateLimitObserver } from "@getlago/lago-javascript-client";
19
+ *
20
+ * const client = Client(apiKey, {
21
+ * rateLimitRetry: { onRateLimitInfo: loggingRateLimitObserver() },
22
+ * });
23
+ * ```
24
+ */
25
+ export function loggingRateLimitObserver(options = {}) {
26
+ const thresholds = [...(options.thresholds ?? DEFAULT_RATE_LIMIT_THRESHOLDS)]
27
+ .sort((a, b) => b - a); // descending
28
+ // deno-lint-ignore no-console
29
+ const log = options.log ?? ((m) => console.warn(m));
30
+ return function observe(info) {
31
+ const pct = rateLimitUsagePct(info);
32
+ if (pct === null)
33
+ return;
34
+ for (const threshold of thresholds) {
35
+ if (pct >= threshold) {
36
+ log(`Lago rate limit at ${(pct * 100).toFixed(0)}% (limit=${info.limit}, ` +
37
+ `remaining=${info.remaining}, reset=${info.reset}s, ` +
38
+ `${info.method} ${info.url})`);
39
+ return;
40
+ }
41
+ }
42
+ };
43
+ }
package/esm/mod.d.ts CHANGED
@@ -10,6 +10,8 @@ export declare const Client: (apiKey: string, apiConfig?: LagoClientConfig) => A
10
10
  type ExtractLagoError<E> = E extends (...args: any) => Promise<HttpResponse<infer T, infer P>> ? P : never;
11
11
  export declare function getLagoError<T>(error: any): Promise<ExtractLagoError<T>>;
12
12
  export { LagoRateLimitError } from "./rate_limit_error.js";
13
- export { parseRateLimitHeaders, type RateLimitHeaders } from "./rate_limit_headers.js";
14
- export { createRateLimitFetch, type RateLimitFetchConfig } from "./rate_limit_fetch.js";
13
+ export { parseRateLimitHeaders, parseRateLimitInfo, type RateLimitHeaders, type RateLimitInfo, rateLimitUsagePct, } from "./rate_limit_headers.js";
14
+ export { createRateLimitFetch, type RateLimitFetchConfig, type RateLimitInfoCallback, } from "./rate_limit_fetch.js";
15
+ export { DEFAULT_RATE_LIMIT_THRESHOLDS, loggingRateLimitObserver, type LoggingRateLimitObserverOptions, } from "./logging_rate_limit_observer.js";
15
16
  export * from "./openapi/client.js";
17
+ export type { LagoWebhookPayload, LagoWebhookPayloads, LagoWebhookType, WebhookOf, } from "./webhook_types.js";
package/esm/mod.js CHANGED
@@ -39,6 +39,7 @@ export async function getLagoError(error) {
39
39
  }
40
40
  // Rate limit exports
41
41
  export { LagoRateLimitError } from "./rate_limit_error.js";
42
- export { parseRateLimitHeaders } from "./rate_limit_headers.js";
43
- export { createRateLimitFetch } from "./rate_limit_fetch.js";
42
+ export { parseRateLimitHeaders, parseRateLimitInfo, rateLimitUsagePct, } from "./rate_limit_headers.js";
43
+ export { createRateLimitFetch, } from "./rate_limit_fetch.js";
44
+ export { DEFAULT_RATE_LIMIT_THRESHOLDS, loggingRateLimitObserver, } from "./logging_rate_limit_observer.js";
44
45
  export * from "./openapi/client.js";
@@ -8857,7 +8857,7 @@ export declare class HttpClient<SecurityDataType = unknown> {
8857
8857
  }
8858
8858
  /**
8859
8859
  * @title Lago API documentation
8860
- * @version 1.45.0
8860
+ * @version 1.45.2
8861
8861
  * @license AGPLv3 (https://github.com/getlago/lago-openapi/blob/main/LICENSE)
8862
8862
  * @baseUrl https://api.getlago.com/api/v1
8863
8863
  * @externalDocs https://github.com/getlago
@@ -10302,24 +10302,48 @@ export declare class Api<SecurityDataType extends unknown> extends HttpClient<Se
10302
10302
  * @example true
10303
10303
  */
10304
10304
  apply_taxes?: boolean;
10305
+ /**
10306
+ * Filter usage to a specific charge by its Lago ID (UUID). Replaces deprecated `filter_by_charge_id`.
10307
+ * @format uuid
10308
+ * @example "1a901a90-1a90-1a90-1a90-1a901a901a90"
10309
+ */
10310
+ charge_id?: string;
10311
+ /**
10312
+ * Filter usage to a specific charge by its code. Replaces deprecated `filter_by_charge_code`.
10313
+ * @example "storage"
10314
+ */
10315
+ charge_code?: string;
10316
+ /**
10317
+ * Filter usage to a specific billable metric by its code.
10318
+ * @example "storage"
10319
+ */
10320
+ billable_metric_code?: string;
10321
+ /**
10322
+ * Filter usage by pricing group. Pass key/value pairs as query parameters, e.g. `group[cloud]=aws`. Replaces deprecated `filter_by_group`.
10323
+ * @example {"cloud":"aws"}
10324
+ */
10325
+ group?: Record<string, string>;
10305
10326
  /**
10306
10327
  * Filter usage to a specific charge by its Lago ID (UUID).
10328
+ * @deprecated
10307
10329
  * @format uuid
10308
10330
  * @example "1a901a90-1a90-1a90-1a90-1a901a901a90"
10309
10331
  */
10310
10332
  filter_by_charge_id?: string;
10311
10333
  /**
10312
10334
  * Filter usage to a specific charge by its code.
10335
+ * @deprecated
10313
10336
  * @example "storage"
10314
10337
  */
10315
10338
  filter_by_charge_code?: string;
10316
10339
  /**
10317
10340
  * Filter usage by pricing group. Pass key/value pairs as query parameters, e.g. `filter_by_group[cloud]=aws`.
10341
+ * @deprecated
10318
10342
  * @example {"cloud":"aws"}
10319
10343
  */
10320
10344
  filter_by_group?: Record<string, string>;
10321
10345
  /**
10322
- * When `true`, returns usage since subscription start instead of the current billing period. Requires either `filter_by_charge_id`, `filter_by_charge_code`, or `filter_by_group` to be set.
10346
+ * When `true`, returns usage since subscription start instead of the current billing period. Requires one of `charge_id`, `charge_code`, `group` (or their deprecated `filter_by_*` equivalents) to be set.
10323
10347
  * @default false
10324
10348
  * @example true
10325
10349
  */
@@ -148,7 +148,7 @@ export class HttpClient {
148
148
  }
149
149
  /**
150
150
  * @title Lago API documentation
151
- * @version 1.45.0
151
+ * @version 1.45.2
152
152
  * @license AGPLv3 (https://github.com/getlago/lago-openapi/blob/main/LICENSE)
153
153
  * @baseUrl https://api.getlago.com/api/v1
154
154
  * @externalDocs https://github.com/getlago