@blimu/backend 0.6.3 → 1.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 +129 -134
- package/dist/client.d.mts +24 -0
- package/dist/client.d.ts +15 -57
- package/dist/client.js +67 -157
- package/dist/client.js.map +1 -1
- package/dist/client.mjs +48 -0
- package/dist/client.mjs.map +1 -0
- package/dist/index.d.mts +36 -0
- package/dist/index.d.ts +25 -26
- package/dist/index.js +1037 -83
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1005 -0
- package/dist/index.mjs.map +1 -0
- package/dist/main.d.mts +19 -0
- package/dist/main.d.ts +19 -0
- package/dist/main.js +1273 -0
- package/dist/main.js.map +1 -0
- package/dist/main.mjs +1264 -0
- package/dist/main.mjs.map +1 -0
- package/dist/schema-B1usIXCr.d.mts +424 -0
- package/dist/schema-B1usIXCr.d.ts +424 -0
- package/dist/schema.d.mts +2 -0
- package/dist/schema.d.ts +2 -381
- package/dist/schema.js +17 -2
- package/dist/schema.js.map +1 -1
- package/dist/schema.mjs +1 -0
- package/dist/schema.mjs.map +1 -0
- package/dist/schema.zod-CRNAHxbc.d.mts +444 -0
- package/dist/schema.zod-CRNAHxbc.d.ts +444 -0
- package/dist/schema.zod.d.mts +2 -0
- package/dist/schema.zod.d.ts +2 -0
- package/dist/schema.zod.js +562 -0
- package/dist/schema.zod.js.map +1 -0
- package/dist/schema.zod.mjs +496 -0
- package/dist/schema.zod.mjs.map +1 -0
- package/dist/services/bulk_resources.d.mts +12 -0
- package/dist/services/bulk_resources.d.ts +9 -4
- package/dist/services/bulk_resources.js +45 -18
- package/dist/services/bulk_resources.js.map +1 -1
- package/dist/services/bulk_resources.mjs +22 -0
- package/dist/services/bulk_resources.mjs.map +1 -0
- package/dist/services/bulk_roles.d.mts +12 -0
- package/dist/services/bulk_roles.d.ts +9 -4
- package/dist/services/bulk_roles.js +45 -18
- package/dist/services/bulk_roles.js.map +1 -1
- package/dist/services/bulk_roles.mjs +22 -0
- package/dist/services/bulk_roles.mjs.map +1 -0
- package/dist/services/entitlements.d.mts +14 -0
- package/dist/services/entitlements.d.ts +11 -6
- package/dist/services/entitlements.js +69 -34
- package/dist/services/entitlements.js.map +1 -1
- package/dist/services/entitlements.mjs +46 -0
- package/dist/services/entitlements.mjs.map +1 -0
- package/dist/services/plans.d.mts +14 -0
- package/dist/services/plans.d.ts +11 -6
- package/dist/services/plans.js +67 -32
- package/dist/services/plans.js.map +1 -1
- package/dist/services/plans.mjs +44 -0
- package/dist/services/plans.mjs.map +1 -0
- package/dist/services/resource_members.d.mts +12 -0
- package/dist/services/resource_members.d.ts +9 -4
- package/dist/services/resource_members.js +45 -17
- package/dist/services/resource_members.js.map +1 -1
- package/dist/services/resource_members.mjs +22 -0
- package/dist/services/resource_members.mjs.map +1 -0
- package/dist/services/resources.d.mts +16 -0
- package/dist/services/resources.d.ts +13 -8
- package/dist/services/resources.js +91 -49
- package/dist/services/resources.js.map +1 -1
- package/dist/services/resources.mjs +68 -0
- package/dist/services/resources.mjs.map +1 -0
- package/dist/services/roles.d.mts +14 -0
- package/dist/services/roles.d.ts +11 -6
- package/dist/services/roles.js +68 -33
- package/dist/services/roles.js.map +1 -1
- package/dist/services/roles.mjs +45 -0
- package/dist/services/roles.mjs.map +1 -0
- package/dist/services/usage.d.mts +16 -0
- package/dist/services/usage.d.ts +13 -8
- package/dist/services/usage.js +93 -52
- package/dist/services/usage.js.map +1 -1
- package/dist/services/usage.mjs +70 -0
- package/dist/services/usage.mjs.map +1 -0
- package/dist/services/users.d.mts +17 -0
- package/dist/services/users.d.ts +13 -8
- package/dist/services/users.js +102 -56
- package/dist/services/users.js.map +1 -1
- package/dist/services/users.mjs +79 -0
- package/dist/services/users.mjs.map +1 -0
- package/dist/token-verifier.d.mts +35 -0
- package/dist/token-verifier.d.ts +8 -6
- package/dist/token-verifier.js +225 -172
- package/dist/token-verifier.js.map +1 -1
- package/dist/token-verifier.mjs +201 -0
- package/dist/token-verifier.mjs.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils.d.mts +20 -0
- package/dist/utils.d.ts +7 -3
- package/dist/utils.js +50 -20
- package/dist/utils.js.map +1 -1
- package/dist/utils.mjs +29 -0
- package/dist/utils.mjs.map +1 -0
- package/package.json +37 -62
- package/src/client.ts +74 -0
- package/src/index.ts +55 -0
- package/src/main.ts +3 -0
- package/src/schema.ts +430 -0
- package/src/schema.zod.ts +558 -0
- package/src/services/bulk_resources.ts +24 -0
- package/src/services/bulk_roles.ts +22 -0
- package/src/services/entitlements.ts +58 -0
- package/src/services/plans.ts +57 -0
- package/src/services/resource_members.ts +25 -0
- package/src/services/resources.ts +91 -0
- package/src/services/roles.ts +58 -0
- package/src/services/usage.ts +93 -0
- package/src/services/users.ts +100 -0
- package/src/token-verifier.ts +280 -0
- package/src/utils.ts +56 -0
- package/bin/blimu +0 -0
- package/scripts/download-binary.js +0 -243
- package/scripts/postinstall.js +0 -24
package/README.md
CHANGED
|
@@ -17,141 +17,142 @@ import { BlimuClient } from '@blimu/backend';
|
|
|
17
17
|
|
|
18
18
|
// Create a new client
|
|
19
19
|
const client = new BlimuClient({
|
|
20
|
-
baseURL: 'https://api.
|
|
20
|
+
baseURL: 'https://api.blimu.dev',
|
|
21
21
|
timeoutMs: 10000,
|
|
22
|
-
retry: { retries: 2, backoffMs: 300, retryOn: [429, 500, 502, 503, 504] },
|
|
23
|
-
//
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
retry: { retries: 2, strategy: 'exponential', backoffMs: 300, retryOn: [429, 500, 502, 503, 504] },
|
|
23
|
+
// Auth configuration
|
|
24
|
+
auth: {
|
|
25
|
+
strategies: [
|
|
26
|
+
{
|
|
27
|
+
type: 'bearer',
|
|
28
|
+
token: process.env.API_TOKEN,
|
|
29
|
+
},
|
|
30
|
+
],
|
|
28
31
|
},
|
|
29
|
-
// Auth (generic API Key or Bearer header)
|
|
30
|
-
accessToken: process.env.API_TOKEN,
|
|
31
|
-
headerName: 'access_token', // or 'Authorization' (defaults to Authorization: Bearer <token>)
|
|
32
32
|
});
|
|
33
|
+
|
|
33
34
|
// Example: Bulk create resources
|
|
34
35
|
try {
|
|
35
|
-
const result = await client.bulkResources.create(
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
const result = await client.bulkResources.create(
|
|
37
|
+
'resourceType'
|
|
38
|
+
|
|
39
|
+
, {
|
|
40
|
+
// Request body data
|
|
41
|
+
}
|
|
42
|
+
);
|
|
38
43
|
console.log('Result:', result);
|
|
39
44
|
} catch (error) {
|
|
40
|
-
//
|
|
45
|
+
// FetchError with structured data
|
|
46
|
+
console.error(error);
|
|
47
|
+
}
|
|
48
|
+
// Example: Bulk create roles
|
|
49
|
+
try {
|
|
50
|
+
const result = await client.bulkRoles.create(
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
{
|
|
54
|
+
// Request body data
|
|
55
|
+
}
|
|
56
|
+
);
|
|
57
|
+
console.log('Result:', result);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
// FetchError with structured data
|
|
60
|
+
console.error(error);
|
|
61
|
+
}
|
|
62
|
+
// Example: Check if a user has a specific entitlement on a resource
|
|
63
|
+
try {
|
|
64
|
+
const result = await client.entitlements.checkEntitlement(
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
{
|
|
68
|
+
// Request body data
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
console.log('Result:', result);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
// FetchError with structured data
|
|
74
|
+
console.error(error);
|
|
75
|
+
}
|
|
76
|
+
// Example: Remove plan assignment from a tenant resource
|
|
77
|
+
try {
|
|
78
|
+
const result = await client.plans.delete(
|
|
79
|
+
'resourceType', 'resourceId'
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
);
|
|
83
|
+
console.log('Result:', result);
|
|
84
|
+
} catch (error) {
|
|
85
|
+
// FetchError with structured data
|
|
86
|
+
console.error(error);
|
|
87
|
+
}
|
|
88
|
+
// Example: List members for a resource
|
|
89
|
+
try {
|
|
90
|
+
const result = await client.resourceMembers.list(
|
|
91
|
+
'resourceType', 'resourceId'
|
|
92
|
+
, {
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
);
|
|
96
|
+
console.log('Result:', result);
|
|
97
|
+
} catch (error) {
|
|
98
|
+
// FetchError with structured data
|
|
99
|
+
console.error(error);
|
|
100
|
+
}
|
|
101
|
+
// Example: List resources
|
|
102
|
+
try {
|
|
103
|
+
const result = await client.resources.list(
|
|
104
|
+
'resourceType'
|
|
105
|
+
, {
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
);
|
|
109
|
+
console.log('Result:', result);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
// FetchError with structured data
|
|
112
|
+
console.error(error);
|
|
113
|
+
}
|
|
114
|
+
// Example: List user roles
|
|
115
|
+
try {
|
|
116
|
+
const result = await client.roles.list(
|
|
117
|
+
'userId'
|
|
118
|
+
, {
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
);
|
|
122
|
+
console.log('Result:', result);
|
|
123
|
+
} catch (error) {
|
|
124
|
+
// FetchError with structured data
|
|
125
|
+
console.error(error);
|
|
126
|
+
}
|
|
127
|
+
// Example: Get wallet balance
|
|
128
|
+
try {
|
|
129
|
+
const result = await client.usage.getBalance(
|
|
130
|
+
'resourceType', 'resourceId', 'limitType'
|
|
131
|
+
, {
|
|
132
|
+
period: undefined,
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
);
|
|
136
|
+
console.log('Result:', result);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
// FetchError with structured data
|
|
139
|
+
console.error(error);
|
|
140
|
+
}
|
|
141
|
+
// Example: List users
|
|
142
|
+
try {
|
|
143
|
+
const result = await client.users.list(
|
|
144
|
+
|
|
145
|
+
{
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
);
|
|
149
|
+
console.log('Result:', result);
|
|
150
|
+
} catch (error) {
|
|
151
|
+
// FetchError with structured data
|
|
41
152
|
console.error(error);
|
|
42
153
|
}
|
|
43
154
|
```
|
|
44
155
|
|
|
45
|
-
## Environment & Auth
|
|
46
|
-
|
|
47
|
-
```typescript
|
|
48
|
-
const client = new BlimuClient({
|
|
49
|
-
env: 'sandbox',
|
|
50
|
-
envBaseURLs: {
|
|
51
|
-
sandbox: 'https://api-sandbox.example.com',
|
|
52
|
-
production: 'https://api.example.com',
|
|
53
|
-
},
|
|
54
|
-
accessToken: async () => process.env.API_TOKEN!,
|
|
55
|
-
headerName: 'access_token',
|
|
56
|
-
});
|
|
57
|
-
client.setAccessToken('new-token');
|
|
58
|
-
```
|
|
59
|
-
|
|
60
|
-
## Pagination
|
|
61
|
-
|
|
62
|
-
```typescript
|
|
63
|
-
import { listAll } from '@blimu/backend';
|
|
64
|
-
|
|
65
|
-
const allPayments = await listAll((query) => client.payment.listPayments(query), { limit: 100 });
|
|
66
|
-
```
|
|
67
|
-
|
|
68
|
-
## Interceptors
|
|
69
|
-
|
|
70
|
-
```typescript
|
|
71
|
-
const client = new BlimuClient({
|
|
72
|
-
onRequest: ({ url, init }) => console.debug('->', init.method, url),
|
|
73
|
-
onResponse: ({ response }) => console.debug('<-', response.status),
|
|
74
|
-
onError: (err) => console.warn('request error', err),
|
|
75
|
-
});
|
|
76
|
-
```
|
|
77
|
-
|
|
78
|
-
## Authentication
|
|
79
|
-
|
|
80
|
-
This SDK supports the following authentication methods:
|
|
81
|
-
|
|
82
|
-
### ApiKey
|
|
83
|
-
|
|
84
|
-
API Key authentication (header):
|
|
85
|
-
|
|
86
|
-
```typescript
|
|
87
|
-
const client = new BlimuClient({
|
|
88
|
-
apiKey: 'your-api-key',
|
|
89
|
-
});
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
## Subpath imports
|
|
93
|
-
|
|
94
|
-
```typescript
|
|
95
|
-
import { PaymentService, Schema } from '@blimu/backend';
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
## Available Services
|
|
99
|
-
|
|
100
|
-
### BulkResourcesService
|
|
101
|
-
|
|
102
|
-
- **create**: POST /v1/resources/{resourceType}/bulk - Bulk create resources
|
|
103
|
-
|
|
104
|
-
### BulkRolesService
|
|
105
|
-
|
|
106
|
-
- **create**: POST /v1/users/roles/bulk - Bulk create roles
|
|
107
|
-
|
|
108
|
-
### EntitlementsService
|
|
109
|
-
|
|
110
|
-
- **checkEntitlement**: POST /v1/entitlements/check - Check if a user has a specific entitlement on a resource
|
|
111
|
-
- **listForResource**: GET /v1/entitlements/list-for-resource/{resourceType}/{resourceId} - List entitlements for a specific resource
|
|
112
|
-
- **listForTenant**: GET /v1/entitlements/list-for-tenant/{tenantResourceId} - List entitlements for a tenant and all its sub-resources
|
|
113
|
-
|
|
114
|
-
### PlansService
|
|
115
|
-
|
|
116
|
-
- **delete**: DELETE /v1/resources/{resourceType}/{resourceId}/plan - Remove plan assignment from a tenant resource
|
|
117
|
-
- **read**: GET /v1/resources/{resourceType}/{resourceId}/plan - Get the plan assigned to a tenant resource
|
|
118
|
-
- **assign**: POST /v1/resources/{resourceType}/{resourceId}/plan - Assign a plan to a tenant resource
|
|
119
|
-
|
|
120
|
-
### ResourceMembersService
|
|
121
|
-
|
|
122
|
-
- **list**: GET /v1/resources/{resourceType}/{resourceId}/members - List members for a resource
|
|
123
|
-
|
|
124
|
-
### ResourcesService
|
|
125
|
-
|
|
126
|
-
- **list**: GET /v1/resources/{resourceType} - List resources
|
|
127
|
-
- **create**: POST /v1/resources/{resourceType} - Create a resource
|
|
128
|
-
- **delete**: DELETE /v1/resources/{resourceType}/{resourceId} - Delete a resource
|
|
129
|
-
- **read**: GET /v1/resources/{resourceType}/{resourceId} - Read a resource
|
|
130
|
-
- **update**: PUT /v1/resources/{resourceType}/{resourceId} - Update a resource
|
|
131
|
-
|
|
132
|
-
### RolesService
|
|
133
|
-
|
|
134
|
-
- **list**: GET /v1/users/{userId}/roles - List user roles
|
|
135
|
-
- **create**: POST /v1/users/{userId}/roles - Create a role (assign role to user on resource)
|
|
136
|
-
- **delete**: DELETE /v1/users/{userId}/roles/{resourceType}/{resourceId} - Delete a role
|
|
137
|
-
|
|
138
|
-
### UsageService
|
|
139
|
-
|
|
140
|
-
- **getBalance**: GET /v1/usage/balance/{resourceType}/{resourceId}/{limitType} - Get wallet balance
|
|
141
|
-
- **checkLimit**: POST /v1/usage/check - Check if consumption is allowed
|
|
142
|
-
- **consume**: POST /v1/usage/consume - Record consumption (inserts negative amount)
|
|
143
|
-
- **credit**: POST /v1/usage/credit - Add credits to wallet (inserts positive amount)
|
|
144
|
-
- **getTransactionHistory**: GET /v1/usage/transactions/{resourceType}/{resourceId}/{limitType} - Get transaction history
|
|
145
|
-
|
|
146
|
-
### UsersService
|
|
147
|
-
|
|
148
|
-
- **list**: GET /v1/users - List users
|
|
149
|
-
- **create**: POST /v1/users - Create a user
|
|
150
|
-
- **delete**: DELETE /v1/users/{userId} - Delete a user
|
|
151
|
-
- **read**: GET /v1/users/{userId} - Get a user by ID
|
|
152
|
-
- **update**: PUT /v1/users/{userId} - Update a user
|
|
153
|
-
- **listEffectiveUserResourcesRoles**: GET /v1/users/{userId}/effective-user-resources-roles - List effective user resources roles
|
|
154
|
-
|
|
155
156
|
## TypeScript Support
|
|
156
157
|
|
|
157
158
|
This SDK is written in TypeScript and provides full type safety:
|
|
@@ -159,13 +160,9 @@ This SDK is written in TypeScript and provides full type safety:
|
|
|
159
160
|
```typescript
|
|
160
161
|
import { BlimuClient, Schema } from '@blimu/backend';
|
|
161
162
|
|
|
162
|
-
const client = new BlimuClient({
|
|
163
|
-
/* config */
|
|
164
|
-
});
|
|
163
|
+
const client = new BlimuClient({ /* config */ });
|
|
165
164
|
|
|
166
165
|
// All methods are fully typed
|
|
167
|
-
const result: ResourceBulkResult = await client.bulkResources.create(/* ... */);
|
|
168
|
-
|
|
169
166
|
// Schema types are available
|
|
170
167
|
const data: Schema.BalanceResponse = {
|
|
171
168
|
// Fully typed object
|
|
@@ -185,7 +182,7 @@ import { fetch } from 'undici';
|
|
|
185
182
|
import { BlimuClient } from '@blimu/backend';
|
|
186
183
|
|
|
187
184
|
const client = new BlimuClient({
|
|
188
|
-
baseURL: 'https://api.
|
|
185
|
+
baseURL: 'https://api.blimu.dev',
|
|
189
186
|
fetch,
|
|
190
187
|
});
|
|
191
188
|
```
|
|
@@ -236,14 +233,12 @@ All types are available under the `Schema` namespace:
|
|
|
236
233
|
import { Schema } from '@blimu/backend';
|
|
237
234
|
|
|
238
235
|
// Use any model type
|
|
239
|
-
const user: Schema.User = {
|
|
240
|
-
/* ... */
|
|
241
|
-
};
|
|
236
|
+
const user: Schema.User = { /* ... */ };
|
|
242
237
|
```
|
|
243
238
|
|
|
244
239
|
## Contributing
|
|
245
240
|
|
|
246
|
-
This SDK is auto-generated. Please do not edit the generated files directly.
|
|
241
|
+
This SDK is auto-generated. Please do not edit the generated files directly.
|
|
247
242
|
If you find issues, please report them in the main project repository.
|
|
248
243
|
|
|
249
244
|
## License
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { FetchClientConfig, FetchClient } from '@blimu/fetch';
|
|
2
|
+
export { FetchError } from '@blimu/fetch';
|
|
3
|
+
|
|
4
|
+
type ClientOption = FetchClientConfig & {
|
|
5
|
+
apiKey?: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
declare class CoreClient extends FetchClient {
|
|
9
|
+
constructor(cfg?: ClientOption);
|
|
10
|
+
request(init: RequestInit & {
|
|
11
|
+
path: string;
|
|
12
|
+
method: string;
|
|
13
|
+
query?: Record<string, any>;
|
|
14
|
+
}): Promise<any>;
|
|
15
|
+
requestStream<T = any>(init: RequestInit & {
|
|
16
|
+
path: string;
|
|
17
|
+
method: string;
|
|
18
|
+
query?: Record<string, any>;
|
|
19
|
+
contentType: string;
|
|
20
|
+
streamingFormat?: 'sse' | 'ndjson' | 'chunked';
|
|
21
|
+
}): AsyncGenerator<T, void, unknown>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export { type ClientOption, CoreClient };
|
package/dist/client.d.ts
CHANGED
|
@@ -1,66 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
retry?: {
|
|
6
|
-
retries: number;
|
|
7
|
-
backoffMs: number;
|
|
8
|
-
retryOn?: number[];
|
|
9
|
-
};
|
|
10
|
-
onRequest?: (ctx: {
|
|
11
|
-
url: string;
|
|
12
|
-
init: RequestInit & {
|
|
13
|
-
path: string;
|
|
14
|
-
method: string;
|
|
15
|
-
query?: Record<string, any>;
|
|
16
|
-
headers: Headers;
|
|
17
|
-
};
|
|
18
|
-
attempt: number;
|
|
19
|
-
}) => void | Promise<void>;
|
|
20
|
-
onResponse?: (ctx: {
|
|
21
|
-
url: string;
|
|
22
|
-
init: RequestInit & {
|
|
23
|
-
path: string;
|
|
24
|
-
method: string;
|
|
25
|
-
query?: Record<string, any>;
|
|
26
|
-
headers: Headers;
|
|
27
|
-
};
|
|
28
|
-
attempt: number;
|
|
29
|
-
response: Response;
|
|
30
|
-
}) => void | Promise<void>;
|
|
31
|
-
onError?: (err: unknown, ctx: {
|
|
32
|
-
url: string;
|
|
33
|
-
init: RequestInit & {
|
|
34
|
-
path: string;
|
|
35
|
-
method: string;
|
|
36
|
-
query?: Record<string, any>;
|
|
37
|
-
};
|
|
38
|
-
attempt: number;
|
|
39
|
-
}) => void | Promise<void>;
|
|
40
|
-
env?: 'sandbox' | 'production';
|
|
41
|
-
envBaseURLs?: {
|
|
42
|
-
sandbox: string;
|
|
43
|
-
production: string;
|
|
44
|
-
};
|
|
45
|
-
accessToken?: string | undefined | (() => string | undefined | Promise<string | undefined>);
|
|
46
|
-
headerName?: string;
|
|
1
|
+
import { FetchClientConfig, FetchClient } from '@blimu/fetch';
|
|
2
|
+
export { FetchError } from '@blimu/fetch';
|
|
3
|
+
|
|
4
|
+
type ClientOption = FetchClientConfig & {
|
|
47
5
|
apiKey?: string;
|
|
48
|
-
fetch?: typeof fetch;
|
|
49
|
-
credentials?: RequestCredentials;
|
|
50
6
|
};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
readonly data?: T | undefined;
|
|
54
|
-
readonly headers?: Headers | undefined;
|
|
55
|
-
constructor(message: string, status: number, data?: T | undefined, headers?: Headers | undefined);
|
|
56
|
-
}
|
|
57
|
-
export declare class CoreClient {
|
|
58
|
-
private cfg;
|
|
7
|
+
|
|
8
|
+
declare class CoreClient extends FetchClient {
|
|
59
9
|
constructor(cfg?: ClientOption);
|
|
60
|
-
setAccessToken(token: string | undefined | (() => string | undefined | Promise<string | undefined>)): void;
|
|
61
10
|
request(init: RequestInit & {
|
|
62
11
|
path: string;
|
|
63
12
|
method: string;
|
|
64
13
|
query?: Record<string, any>;
|
|
65
14
|
}): Promise<any>;
|
|
15
|
+
requestStream<T = any>(init: RequestInit & {
|
|
16
|
+
path: string;
|
|
17
|
+
method: string;
|
|
18
|
+
query?: Record<string, any>;
|
|
19
|
+
contentType: string;
|
|
20
|
+
streamingFormat?: 'sse' | 'ndjson' | 'chunked';
|
|
21
|
+
}): AsyncGenerator<T, void, unknown>;
|
|
66
22
|
}
|
|
23
|
+
|
|
24
|
+
export { type ClientOption, CoreClient };
|
package/dist/client.js
CHANGED
|
@@ -1,160 +1,70 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/client.ts
|
|
20
|
+
var client_exports = {};
|
|
21
|
+
__export(client_exports, {
|
|
22
|
+
CoreClient: () => CoreClient,
|
|
23
|
+
FetchError: () => import_fetch.FetchError
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(client_exports);
|
|
26
|
+
var import_fetch = require("@blimu/fetch");
|
|
27
|
+
var CoreClient = class extends import_fetch.FetchClient {
|
|
28
|
+
constructor(cfg = {}) {
|
|
29
|
+
const authStrategies = [];
|
|
30
|
+
const { auth: _existingAuth, apiKey, ...restCfg } = cfg;
|
|
31
|
+
if (cfg?.apiKey) {
|
|
32
|
+
const apiKeyValue = cfg.apiKey;
|
|
33
|
+
authStrategies.push({
|
|
34
|
+
type: "apiKey",
|
|
35
|
+
key: () => apiKeyValue,
|
|
36
|
+
location: "header",
|
|
37
|
+
name: "X-API-KEY"
|
|
38
|
+
});
|
|
11
39
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
else {
|
|
25
|
-
this.cfg.baseURL = 'https://api.blimu.dev';
|
|
26
|
-
}
|
|
40
|
+
const finalAuthStrategies = [
|
|
41
|
+
..._existingAuth?.strategies || [],
|
|
42
|
+
...authStrategies
|
|
43
|
+
];
|
|
44
|
+
const fetchConfig = {
|
|
45
|
+
...restCfg,
|
|
46
|
+
baseURL: cfg.baseURL ?? "https://api.blimu.dev",
|
|
47
|
+
// Explicitly set auth after restCfg to ensure it's not overwritten
|
|
48
|
+
// (restCfg might have an auth property that we want to replace)
|
|
49
|
+
...finalAuthStrategies.length > 0 ? {
|
|
50
|
+
auth: {
|
|
51
|
+
strategies: finalAuthStrategies
|
|
27
52
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
url.searchParams.set(k, String(v));
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
const headers = new Headers({
|
|
49
|
-
...(this.cfg.headers || {}),
|
|
50
|
-
...init.headers,
|
|
51
|
-
});
|
|
52
|
-
if (this.cfg.accessToken) {
|
|
53
|
-
const token = typeof this.cfg.accessToken === 'function'
|
|
54
|
-
? await this.cfg.accessToken()
|
|
55
|
-
: this.cfg.accessToken;
|
|
56
|
-
if (token != null) {
|
|
57
|
-
const name = this.cfg.headerName || 'Authorization';
|
|
58
|
-
if (name.toLowerCase() === 'authorization')
|
|
59
|
-
headers.set(name, `Bearer ${String(token)}`);
|
|
60
|
-
else
|
|
61
|
-
headers.set(name, String(token));
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
if (this.cfg?.apiKey)
|
|
65
|
-
headers.set('X-API-KEY', String(this.cfg?.apiKey));
|
|
66
|
-
const doFetch = async (attempt) => {
|
|
67
|
-
const requestHeaders = new Headers(headers);
|
|
68
|
-
const fetchInit = {
|
|
69
|
-
...init,
|
|
70
|
-
headers: requestHeaders,
|
|
71
|
-
};
|
|
72
|
-
if (this.cfg.credentials !== undefined) {
|
|
73
|
-
fetchInit.credentials = this.cfg.credentials;
|
|
74
|
-
}
|
|
75
|
-
if (this.cfg.onRequest)
|
|
76
|
-
await this.cfg.onRequest({ url: url.toString(), init: fetchInit, attempt });
|
|
77
|
-
let controller;
|
|
78
|
-
let timeoutId;
|
|
79
|
-
const existingSignal = fetchInit.signal;
|
|
80
|
-
if (this.cfg.timeoutMs && typeof AbortController !== 'undefined') {
|
|
81
|
-
controller = new AbortController();
|
|
82
|
-
if (existingSignal) {
|
|
83
|
-
if (existingSignal.aborted) {
|
|
84
|
-
controller.abort();
|
|
85
|
-
}
|
|
86
|
-
else {
|
|
87
|
-
existingSignal.addEventListener('abort', () => {
|
|
88
|
-
controller?.abort();
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
fetchInit.signal = controller.signal;
|
|
93
|
-
timeoutId = setTimeout(() => controller?.abort(), this.cfg.timeoutMs);
|
|
94
|
-
}
|
|
95
|
-
try {
|
|
96
|
-
const res = await (this.cfg.fetch || fetch)(url.toString(), fetchInit);
|
|
97
|
-
if (this.cfg.onResponse)
|
|
98
|
-
await this.cfg.onResponse({
|
|
99
|
-
url: url.toString(),
|
|
100
|
-
init: fetchInit,
|
|
101
|
-
attempt,
|
|
102
|
-
response: res,
|
|
103
|
-
});
|
|
104
|
-
const ct = res.headers.get('content-type') || '';
|
|
105
|
-
let parsed;
|
|
106
|
-
if (ct.includes('application/json')) {
|
|
107
|
-
parsed = await res.json();
|
|
108
|
-
}
|
|
109
|
-
else if (ct.startsWith('text/')) {
|
|
110
|
-
parsed = await res.text();
|
|
111
|
-
}
|
|
112
|
-
else {
|
|
113
|
-
parsed = await res.arrayBuffer();
|
|
114
|
-
}
|
|
115
|
-
if (!res.ok) {
|
|
116
|
-
throw new FetchError(parsed?.message || `HTTP ${res.status}`, res.status, parsed, res.headers);
|
|
117
|
-
}
|
|
118
|
-
return parsed;
|
|
119
|
-
}
|
|
120
|
-
catch (err) {
|
|
121
|
-
if (this.cfg.onError)
|
|
122
|
-
await this.cfg.onError(err, { url: url.toString(), init, attempt });
|
|
123
|
-
throw err;
|
|
124
|
-
}
|
|
125
|
-
finally {
|
|
126
|
-
if (timeoutId)
|
|
127
|
-
clearTimeout(timeoutId);
|
|
128
|
-
}
|
|
129
|
-
};
|
|
130
|
-
const retries = this.cfg.retry?.retries ?? 0;
|
|
131
|
-
const baseBackoff = this.cfg.retry?.backoffMs ?? 300;
|
|
132
|
-
const retryOn = this.cfg.retry?.retryOn ?? [429, 500, 502, 503, 504];
|
|
133
|
-
let lastError;
|
|
134
|
-
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
135
|
-
try {
|
|
136
|
-
return await doFetch(attempt);
|
|
137
|
-
}
|
|
138
|
-
catch (err) {
|
|
139
|
-
const status = err?.status;
|
|
140
|
-
const shouldRetry = status ? retryOn.includes(status) : true;
|
|
141
|
-
if (attempt < retries && shouldRetry) {
|
|
142
|
-
const delay = baseBackoff * Math.pow(2, attempt);
|
|
143
|
-
await new Promise((r) => setTimeout(r, delay));
|
|
144
|
-
lastError = err;
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
if (err instanceof DOMException)
|
|
148
|
-
throw err;
|
|
149
|
-
if (err instanceof FetchError)
|
|
150
|
-
throw err;
|
|
151
|
-
if (typeof err === 'string')
|
|
152
|
-
throw new FetchError(err, status ?? 0);
|
|
153
|
-
throw new FetchError(err?.message || 'Network error', status ?? 0);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
throw lastError;
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
exports.CoreClient = CoreClient;
|
|
53
|
+
} : {}
|
|
54
|
+
// Hooks are passed through directly from FetchClientConfig (no mapping needed)
|
|
55
|
+
};
|
|
56
|
+
super(fetchConfig);
|
|
57
|
+
}
|
|
58
|
+
async request(init) {
|
|
59
|
+
return await super.request(init);
|
|
60
|
+
}
|
|
61
|
+
async *requestStream(init) {
|
|
62
|
+
yield* super.requestStream(init);
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
66
|
+
0 && (module.exports = {
|
|
67
|
+
CoreClient,
|
|
68
|
+
FetchError
|
|
69
|
+
});
|
|
160
70
|
//# sourceMappingURL=client.js.map
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/client.ts"],"sourcesContent":["import {\n FetchClient,\n FetchError,\n type FetchClientConfig,\n type AuthStrategy,\n} from '@blimu/fetch';\n\nexport type ClientOption = FetchClientConfig & { apiKey?: string };\n\n// Re-export FetchError for backward compatibility\nexport { FetchError };\n\nexport class CoreClient extends FetchClient {\n constructor(cfg: ClientOption = {}) {\n // Build auth strategies from OpenAPI security schemes\n const authStrategies: AuthStrategy[] = [];\n\n // Extract auth and security scheme properties to avoid passing them to FetchClient\n const { auth: _existingAuth, apiKey, ...restCfg } = cfg;\n if (cfg?.apiKey) {\n const apiKeyValue = cfg.apiKey;\n authStrategies.push({\n type: 'apiKey',\n key: () => apiKeyValue,\n location: 'header',\n name: 'X-API-KEY',\n });\n } // Build final auth config (merge existing with new strategies)\n const finalAuthStrategies = [\n ...(_existingAuth?.strategies || []),\n ...authStrategies,\n ];\n\n // Build fetchConfig, ensuring auth comes after restCfg spread to override any existing auth\n const fetchConfig: FetchClientConfig = {\n ...restCfg,\n baseURL: cfg.baseURL ?? 'https://api.blimu.dev',\n // Explicitly set auth after restCfg to ensure it's not overwritten\n // (restCfg might have an auth property that we want to replace)\n ...(finalAuthStrategies.length > 0\n ? {\n auth: {\n strategies: finalAuthStrategies,\n },\n }\n : {}),\n // Hooks are passed through directly from FetchClientConfig (no mapping needed)\n };\n\n super(fetchConfig);\n }\n\n async request(\n init: RequestInit & {\n path: string;\n method: string;\n query?: Record<string, any>;\n }\n ) {\n return await super.request(init);\n }\n\n async *requestStream<T = any>(\n init: RequestInit & {\n path: string;\n method: string;\n query?: Record<string, any>;\n contentType: string;\n streamingFormat?: 'sse' | 'ndjson' | 'chunked';\n }\n ): AsyncGenerator<T, void, unknown> {\n yield* super.requestStream(init);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAKO;AAOA,IAAM,aAAN,cAAyB,yBAAY;AAAA,EAC1C,YAAY,MAAoB,CAAC,GAAG;AAElC,UAAM,iBAAiC,CAAC;AAGxC,UAAM,EAAE,MAAM,eAAe,QAAQ,GAAG,QAAQ,IAAI;AACpD,QAAI,KAAK,QAAQ;AACf,YAAM,cAAc,IAAI;AACxB,qBAAe,KAAK;AAAA,QAClB,MAAM;AAAA,QACN,KAAK,MAAM;AAAA,QACX,UAAU;AAAA,QACV,MAAM;AAAA,MACR,CAAC;AAAA,IACH;AACA,UAAM,sBAAsB;AAAA,MAC1B,GAAI,eAAe,cAAc,CAAC;AAAA,MAClC,GAAG;AAAA,IACL;AAGA,UAAM,cAAiC;AAAA,MACrC,GAAG;AAAA,MACH,SAAS,IAAI,WAAW;AAAA;AAAA;AAAA,MAGxB,GAAI,oBAAoB,SAAS,IAC7B;AAAA,QACE,MAAM;AAAA,UACJ,YAAY;AAAA,QACd;AAAA,MACF,IACA,CAAC;AAAA;AAAA,IAEP;AAEA,UAAM,WAAW;AAAA,EACnB;AAAA,EAEA,MAAM,QACJ,MAKA;AACA,WAAO,MAAM,MAAM,QAAQ,IAAI;AAAA,EACjC;AAAA,EAEA,OAAO,cACL,MAOkC;AAClC,WAAO,MAAM,cAAc,IAAI;AAAA,EACjC;AACF;","names":[]}
|