@noirstack/ledgerbill-sdk 0.1.0
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 +41 -0
- package/dist/client.d.ts +33 -0
- package/dist/client.js +70 -0
- package/dist/generated/api-types.d.ts +2294 -0
- package/dist/generated/api-types.js +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +1 -0
- package/package.json +34 -0
package/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# @ledgerbill/sdk
|
|
2
|
+
|
|
3
|
+
Official TypeScript client scaffold for LedgerBill.
|
|
4
|
+
|
|
5
|
+
## Build flow
|
|
6
|
+
|
|
7
|
+
From repository root:
|
|
8
|
+
|
|
9
|
+
1. Export OpenAPI source of truth:
|
|
10
|
+
- `npm run openapi:export`
|
|
11
|
+
2. Generate strongly-typed API contracts:
|
|
12
|
+
- `npm run sdk:types`
|
|
13
|
+
3. Build SDK package:
|
|
14
|
+
- `npm run sdk:build`
|
|
15
|
+
4. Create local tarball for publishing checks:
|
|
16
|
+
- `npm run sdk:pack`
|
|
17
|
+
|
|
18
|
+
## Basic usage
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { LedgerBillClient } from '@ledgerbill/sdk';
|
|
22
|
+
|
|
23
|
+
const client = new LedgerBillClient({
|
|
24
|
+
baseUrl: 'http://localhost:3107',
|
|
25
|
+
tenantId: 'tenant_123',
|
|
26
|
+
apiKey: 'lb_your_api_key',
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const plans = await client.getPricingPlans();
|
|
30
|
+
const preview = await client.previewUsage({
|
|
31
|
+
customerId: 'cus_123',
|
|
32
|
+
featureKey: 'events_processed',
|
|
33
|
+
amount: 1000,
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Notes
|
|
38
|
+
|
|
39
|
+
- This package is currently scaffolded from OpenAPI and intentionally thin.
|
|
40
|
+
- Any route types come from `src/generated/api-types.ts`.
|
|
41
|
+
- Re-run `npm run sdk:types` after API contract changes.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { paths } from './generated/api-types';
|
|
2
|
+
type FetchLike = (input: RequestInfo | URL, init?: RequestInit) => Promise<Response>;
|
|
3
|
+
type UsagePreviewRequest = paths['/api/v1/usage/preview']['post']['requestBody']['content']['application/json'];
|
|
4
|
+
type UsagePreviewResponse = paths['/api/v1/usage/preview']['post']['responses']['200']['content']['application/json'];
|
|
5
|
+
type UsageReportRequest = paths['/api/v1/usage']['post']['requestBody']['content']['application/json'];
|
|
6
|
+
type UsageReportResponse = paths['/api/v1/usage']['post']['responses']['200']['content']['application/json'];
|
|
7
|
+
type PricingPlansResponse = paths['/api/pricing/plans']['get']['responses']['200']['content']['application/json'];
|
|
8
|
+
type SubscriptionPlansResponse = paths['/api/subscriptions/plans']['get']['responses']['200']['content']['application/json'];
|
|
9
|
+
type CurrentSubscriptionResponse = paths['/api/subscriptions/current']['get']['responses']['200']['content']['application/json'];
|
|
10
|
+
type CheckoutResponse = paths['/api/subscriptions/checkout']['post']['responses']['200']['content']['application/json'];
|
|
11
|
+
export type LedgerBillClientOptions = {
|
|
12
|
+
baseUrl: string;
|
|
13
|
+
tenantId?: string;
|
|
14
|
+
apiKey?: string;
|
|
15
|
+
accessToken?: string;
|
|
16
|
+
fetchImpl?: FetchLike;
|
|
17
|
+
};
|
|
18
|
+
export declare class LedgerBillClient {
|
|
19
|
+
private readonly baseUrl;
|
|
20
|
+
private readonly tenantId?;
|
|
21
|
+
private readonly apiKey?;
|
|
22
|
+
private readonly accessToken?;
|
|
23
|
+
private readonly fetchImpl;
|
|
24
|
+
constructor(options: LedgerBillClientOptions);
|
|
25
|
+
getPricingPlans(): Promise<PricingPlansResponse>;
|
|
26
|
+
getSubscriptionPlans(): Promise<SubscriptionPlansResponse>;
|
|
27
|
+
getCurrentSubscription(): Promise<CurrentSubscriptionResponse>;
|
|
28
|
+
createCheckoutSession(planId: string): Promise<CheckoutResponse>;
|
|
29
|
+
previewUsage(payload: UsagePreviewRequest): Promise<UsagePreviewResponse>;
|
|
30
|
+
reportUsage(payload: UsageReportRequest): Promise<UsageReportResponse>;
|
|
31
|
+
private request;
|
|
32
|
+
}
|
|
33
|
+
export {};
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export class LedgerBillClient {
|
|
2
|
+
baseUrl;
|
|
3
|
+
tenantId;
|
|
4
|
+
apiKey;
|
|
5
|
+
accessToken;
|
|
6
|
+
fetchImpl;
|
|
7
|
+
constructor(options) {
|
|
8
|
+
this.baseUrl = options.baseUrl.replace(/\/$/, '');
|
|
9
|
+
this.tenantId = options.tenantId;
|
|
10
|
+
this.apiKey = options.apiKey;
|
|
11
|
+
this.accessToken = options.accessToken;
|
|
12
|
+
this.fetchImpl = options.fetchImpl ?? fetch;
|
|
13
|
+
}
|
|
14
|
+
async getPricingPlans() {
|
|
15
|
+
return this.request('GET', '/api/pricing/plans');
|
|
16
|
+
}
|
|
17
|
+
async getSubscriptionPlans() {
|
|
18
|
+
return this.request('GET', '/api/subscriptions/plans');
|
|
19
|
+
}
|
|
20
|
+
async getCurrentSubscription() {
|
|
21
|
+
return this.request('GET', '/api/subscriptions/current');
|
|
22
|
+
}
|
|
23
|
+
async createCheckoutSession(planId) {
|
|
24
|
+
return this.request('POST', '/api/subscriptions/checkout', {
|
|
25
|
+
body: { planId },
|
|
26
|
+
idempotencyKey: `sdk-checkout-${Date.now()}`,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
async previewUsage(payload) {
|
|
30
|
+
return this.request('POST', '/api/v1/usage/preview', { body: payload });
|
|
31
|
+
}
|
|
32
|
+
async reportUsage(payload) {
|
|
33
|
+
return this.request('POST', '/api/v1/usage', {
|
|
34
|
+
body: payload,
|
|
35
|
+
idempotencyKey: `sdk-usage-${Date.now()}`,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
async request(method, route, options) {
|
|
39
|
+
const headers = {
|
|
40
|
+
Accept: 'application/json',
|
|
41
|
+
};
|
|
42
|
+
if (this.tenantId)
|
|
43
|
+
headers['x-tenant-id'] = this.tenantId;
|
|
44
|
+
if (this.apiKey)
|
|
45
|
+
headers['x-api-key'] = this.apiKey;
|
|
46
|
+
if (this.accessToken)
|
|
47
|
+
headers.Authorization = `Bearer ${this.accessToken}`;
|
|
48
|
+
if (options?.idempotencyKey)
|
|
49
|
+
headers['Idempotency-Key'] = options.idempotencyKey;
|
|
50
|
+
const hasBody = options?.body != null;
|
|
51
|
+
if (hasBody)
|
|
52
|
+
headers['Content-Type'] = 'application/json';
|
|
53
|
+
const response = await this.fetchImpl(`${this.baseUrl}${route}`, {
|
|
54
|
+
method,
|
|
55
|
+
headers,
|
|
56
|
+
body: hasBody ? JSON.stringify(options?.body) : undefined,
|
|
57
|
+
});
|
|
58
|
+
if (!response.ok) {
|
|
59
|
+
let detail;
|
|
60
|
+
try {
|
|
61
|
+
detail = await response.json();
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
detail = await response.text().catch(() => '');
|
|
65
|
+
}
|
|
66
|
+
throw new Error(`LedgerBill API ${method} ${route} failed: ${response.status} ${response.statusText} ${JSON.stringify(detail)}`);
|
|
67
|
+
}
|
|
68
|
+
return (await response.json());
|
|
69
|
+
}
|
|
70
|
+
}
|