lago-javascript-client 0.19.4-alpha

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Lago
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # Lago JavaScript Client
2
+
3
+ This is a JavaScript wrapper for Lago API. Works in Cloudflare Workers, Deno, and Node.js. Generated from [the Lago Swagger document](https://swagger.getlago.com/#/).
4
+
5
+ [![PyPI version](https://badge.fury.io/js/lago-javascript-client.svg)](https://badge.fury.io/js/lago-javascript-client)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://spdx.org/licenses/MIT.html)
7
+
8
+ ## Installation
9
+
10
+ For npm users:
11
+
12
+ ```bash
13
+ npm install lago-javascript-client
14
+ ```
15
+
16
+ ```typescript
17
+ // npm
18
+ import { Client, getLagoError } from 'lago-javascript-client';
19
+
20
+ // Deno
21
+ import { Client, getLagoError } from 'https://deno.land/x/lago/mod.ts';
22
+
23
+ const lagoClient = Client('__YOUR_API_KEY__');
24
+
25
+ try {
26
+ const { data } = await lagoClient.billableMetrics.createBillableMetric(billableMetric);
27
+ } catch (error) {
28
+ const lagoError = await getLagoError<typeof lagoClient.billableMetrics.createBillableMetric>(error);
29
+ }
30
+ ```
31
+
32
+ ## Compatibility
33
+
34
+ This SDK uses the [Fetch API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) and natively supported Node.js version >= 18. For other Node versions:
35
+
36
+ 1. Ideally, run Node with the [`--experimental-fetch` flag](https://nodejs.org/docs/latest-v16.x/api/cli.html#--experimental-fetch)
37
+
38
+ 1. Otherwise, polyfill the Fetch API by doing both:
39
+
40
+ 1. [Patching globalThis](https://github.com/node-fetch/node-fetch#providing-global-access)
41
+
42
+ 1. [Pass a Fetch instance](https://github.com/node-fetch/node-fetch#loading-and-configuring-the-module) to the Lago client
43
+
44
+ ```typescript
45
+ import { Client } from 'lago-javascript-client';
46
+ import fetch from 'node-fetch';
47
+
48
+ const lagoClient = Client("api_key", { customFetch: fetch });
49
+ ```
50
+
51
+ ## Usage
52
+
53
+ Check the [Lago API reference](https://doc.getlago.com/docs/api/intro)
54
+
55
+ ### Error handling
56
+
57
+ Use the get `getLagoError<>()` utility function to extract the error object and TypeScript type:
58
+
59
+ ```typescript
60
+ try {
61
+ const { data } = await lagoClient.billableMetrics.createBillableMetric(billableMetric);
62
+ } catch (error) {
63
+ const lagoError = await getLagoError<typeof lagoClient.billableMetrics.createBillableMetric>(error);
64
+ }
65
+ ```
66
+
67
+ ## Development
68
+
69
+ Uses [dnt](https://github.com/denoland/dnt) to build and test for Deno and Node.
70
+
71
+ ### Release new version
72
+
73
+ Change the affected fields in `scripts/build_npm.ts` and commit to GitHub. GitHub Actions will build and deploy to npm.
74
+
75
+ ### Dependencies
76
+
77
+ Requires [Deno](https://deno.land/) and [Node.js >= 18](https://nodejs.org/en/)
78
+
79
+ ### Generate client from Swagger
80
+
81
+ ```bash
82
+ deno task generate:swagger
83
+ ```
84
+
85
+ ### Run tests
86
+
87
+ ```bash
88
+ deno task test
89
+ ```
90
+
91
+ ### Build
92
+
93
+ ```bash
94
+ deno task build
95
+ ```
96
+
97
+ ### Publish to npm
98
+
99
+ ```bash
100
+ deno task build
101
+ cd npm
102
+ npm publish
103
+ ```
104
+
105
+ ## Documentation
106
+
107
+ The Lago documentation is available at [doc.getlago.com](https://doc.getlago.com/docs/api/intro).
108
+
109
+ ## Contributing
110
+
111
+ The contribution documentation is available [here](https://github.com/getlago/lago-javascript-client/blob/main/CONTRIBUTING.md)
112
+
113
+ ## License
114
+
115
+ Lago Node.js client is distributed under [AGPL-3.0](LICENSE).
package/esm/mod.js ADDED
@@ -0,0 +1,33 @@
1
+ // deno-lint-ignore-file no-explicit-any
2
+ import { Api } from "./swagger/client.js";
3
+ export const Client = (apiKey, apiConfig) => {
4
+ const api = new Api({
5
+ securityWorker: (apiKey) => apiKey ? { headers: { Authorization: `Bearer ${apiKey}` } } : {},
6
+ // Cloudflare Workers doesn't support some options like credentials so need to override default
7
+ baseApiParams: {
8
+ redirect: "follow",
9
+ },
10
+ ...apiConfig,
11
+ });
12
+ api.setSecurityData(apiKey);
13
+ return api;
14
+ };
15
+ // https://github.com/remix-run/remix/blob/ef26f7671a9619966a6cfa3c39e196d44fbf32cf/packages/remix-server-runtime/responses.ts#L60
16
+ function isResponse(value) {
17
+ return (value != null &&
18
+ typeof value.status === "number" &&
19
+ typeof value.statusText === "string" &&
20
+ typeof value.headers === "object" &&
21
+ typeof value.body !== "undefined");
22
+ }
23
+ export async function getLagoError(error) {
24
+ if (isResponse(error)) {
25
+ if (!error.bodyUsed) {
26
+ const errorJson = await error.json();
27
+ return errorJson;
28
+ }
29
+ return error.error;
30
+ }
31
+ throw new Error(error);
32
+ }
33
+ export * from "./swagger/client.js";
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }