@amaster.ai/client 1.1.0-beta.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/LICENSE +21 -0
- package/README.md +463 -0
- package/dist/index.cjs +62 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +222 -0
- package/dist/index.d.ts +222 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/package.json +84 -0
- package/types/auth.d.ts +674 -0
- package/types/bpm.d.ts +531 -0
- package/types/common.d.ts +132 -0
- package/types/entity.d.ts +710 -0
- package/types/index.d.ts +487 -0
- package/types/workflow.d.ts +291 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Amaster Team
|
|
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,463 @@
|
|
|
1
|
+
# @amaster.ai/client
|
|
2
|
+
|
|
3
|
+
> Unified API client for the Amaster platform - Supabase-style developer experience
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@amaster.ai/client)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## 🤖 AI-Friendly Documentation
|
|
9
|
+
|
|
10
|
+
This package is designed with AI tools (GitHub Copilot, Cursor, etc.) in mind:
|
|
11
|
+
|
|
12
|
+
- **Modular Type Definitions**: API documentation split into focused modules
|
|
13
|
+
- **Rich JSDoc**: Every method includes detailed descriptions and examples
|
|
14
|
+
- **On-Demand Loading**: AI tools only load relevant type files when needed
|
|
15
|
+
- **Type-Driven Learning**: Full TypeScript support helps AI understand API patterns
|
|
16
|
+
|
|
17
|
+
### Type Documentation Structure
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
types/
|
|
21
|
+
├── index.d.ts # Main entry (lightweight)
|
|
22
|
+
├── auth.d.ts # Authentication API (login, register, etc.)
|
|
23
|
+
├── entity.d.ts # Entity CRUD operations
|
|
24
|
+
├── bpm.d.ts # Business Process Management
|
|
25
|
+
└── workflow.d.ts # Workflow execution
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Each module contains:
|
|
29
|
+
- Complete method signatures
|
|
30
|
+
- Parameter descriptions
|
|
31
|
+
- Return type definitions
|
|
32
|
+
- Multiple usage examples
|
|
33
|
+
- Best practices
|
|
34
|
+
|
|
35
|
+
## 🚀 Overview
|
|
36
|
+
|
|
37
|
+
`@amaster.ai/client` is a unified JavaScript/TypeScript client that provides a simple, consistent interface to all Amaster platform services. Inspired by Supabase's elegant API design, it offers:
|
|
38
|
+
|
|
39
|
+
✨ **Single client instance** for all services
|
|
40
|
+
🔐 **Automatic authentication** - tokens attached to all requests
|
|
41
|
+
🔄 **Token auto-refresh** - never worry about expiration
|
|
42
|
+
📦 **Type-safe** - Full TypeScript support with auto-completion
|
|
43
|
+
🎯 **Simple API** - Clean, consistent method naming
|
|
44
|
+
|
|
45
|
+
## 📦 Installation
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# npm
|
|
49
|
+
npm install @amaster.ai/client axios
|
|
50
|
+
|
|
51
|
+
# pnpm
|
|
52
|
+
pnpm add @amaster.ai/client axios
|
|
53
|
+
|
|
54
|
+
# yarn
|
|
55
|
+
yarn add @amaster.ai/client axios
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## 🎯 Quick Start
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { createClient } from '@amaster.ai/client';
|
|
62
|
+
|
|
63
|
+
// 1. Create client instance
|
|
64
|
+
const client = createClient({
|
|
65
|
+
baseURL: 'https://api.amaster.ai',
|
|
66
|
+
onUnauthorized: () => {
|
|
67
|
+
// Handle unauthorized (redirect to login, show modal, etc.)
|
|
68
|
+
window.location.href = '/login';
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// 2. Login
|
|
73
|
+
await client.auth.login({
|
|
74
|
+
email: 'user@example.com',
|
|
75
|
+
password: 'password123'
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
// 3. Use any service - auth token is automatically attached!
|
|
79
|
+
const users = await client.entity.list('default', 'users');
|
|
80
|
+
const tasks = await client.bpm.getMyTasks();
|
|
81
|
+
const result = await client.workflow.execute('my-workflow', { input: {} });
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## 🔑 Authentication
|
|
85
|
+
|
|
86
|
+
### Login
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
// Email/Password login
|
|
90
|
+
const result = await client.auth.login({
|
|
91
|
+
email: 'user@example.com',
|
|
92
|
+
password: 'password123'
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
if (result.success) {
|
|
96
|
+
console.log('Logged in:', result.data.user);
|
|
97
|
+
}
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Register
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
const result = await client.auth.register({
|
|
104
|
+
username: 'johndoe',
|
|
105
|
+
email: 'john@example.com',
|
|
106
|
+
password: 'securePassword123'
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Get Current User
|
|
111
|
+
|
|
112
|
+
```typescript
|
|
113
|
+
const result = await client.auth.getMe();
|
|
114
|
+
if (result.success) {
|
|
115
|
+
console.log('Current user:', result.data);
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Logout
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
await client.auth.logout();
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Check Authentication Status
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
if (client.isAuthenticated()) {
|
|
129
|
+
console.log('User is logged in');
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
## 📊 Entity Operations (CRUD)
|
|
134
|
+
|
|
135
|
+
Full CRUD operations for your data entities with type safety.
|
|
136
|
+
|
|
137
|
+
### List Entities
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
const result = await client.entity.list('default', 'users', {
|
|
141
|
+
page: 1,
|
|
142
|
+
perPage: 20,
|
|
143
|
+
orderBy: 'createdAt',
|
|
144
|
+
orderDir: 'desc',
|
|
145
|
+
// Filters
|
|
146
|
+
'status[eq]': 'active',
|
|
147
|
+
'age[gt]': 18
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
if (result.success) {
|
|
151
|
+
const { items, total, page, perPage } = result.data;
|
|
152
|
+
console.log(`Found ${total} users`);
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Get Single Entity
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
const result = await client.entity.get('default', 'users', 123);
|
|
160
|
+
if (result.success) {
|
|
161
|
+
console.log('User:', result.data);
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
### Create Entity
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
const result = await client.entity.create('default', 'users', {
|
|
169
|
+
name: 'John Doe',
|
|
170
|
+
email: 'john@example.com',
|
|
171
|
+
status: 'active'
|
|
172
|
+
});
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### Update Entity
|
|
176
|
+
|
|
177
|
+
```typescript
|
|
178
|
+
const result = await client.entity.update('default', 'users', 123, {
|
|
179
|
+
name: 'Jane Doe',
|
|
180
|
+
status: 'inactive'
|
|
181
|
+
});
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Delete Entity
|
|
185
|
+
|
|
186
|
+
```typescript
|
|
187
|
+
await client.entity.delete('default', 'users', 123);
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
### Bulk Operations
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
// Bulk update
|
|
194
|
+
await client.entity.bulkUpdate('default', 'users', [
|
|
195
|
+
{ id: 1, status: 'active' },
|
|
196
|
+
{ id: 2, status: 'inactive' }
|
|
197
|
+
]);
|
|
198
|
+
|
|
199
|
+
// Bulk delete
|
|
200
|
+
await client.entity.bulkDelete('default', 'users', [1, 2, 3]);
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
## 🔄 BPM (Business Process Management)
|
|
204
|
+
|
|
205
|
+
Manage business processes powered by Camunda 7.
|
|
206
|
+
|
|
207
|
+
### Start a Process
|
|
208
|
+
|
|
209
|
+
```typescript
|
|
210
|
+
const result = await client.bpm.startProcess({
|
|
211
|
+
processKey: 'approval-process',
|
|
212
|
+
businessKey: 'ORDER-12345',
|
|
213
|
+
variables: {
|
|
214
|
+
amount: 1000,
|
|
215
|
+
requester: 'john@example.com'
|
|
216
|
+
}
|
|
217
|
+
});
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Get My Tasks
|
|
221
|
+
|
|
222
|
+
```typescript
|
|
223
|
+
const result = await client.bpm.getMyTasks({
|
|
224
|
+
page: 1,
|
|
225
|
+
perPage: 20,
|
|
226
|
+
sortBy: 'created',
|
|
227
|
+
sortOrder: 'desc'
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
if (result.success) {
|
|
231
|
+
console.log('Tasks:', result.data.items);
|
|
232
|
+
}
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
### Complete a Task
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
await client.bpm.completeTask('task-id', {
|
|
239
|
+
approved: true,
|
|
240
|
+
comments: 'Looks good!'
|
|
241
|
+
});
|
|
242
|
+
```
|
|
243
|
+
|
|
244
|
+
### Claim a Task
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
await client.bpm.claimTask('task-id');
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## ⚡ Workflow Execution
|
|
251
|
+
|
|
252
|
+
Execute workflows and automation flows.
|
|
253
|
+
|
|
254
|
+
```typescript
|
|
255
|
+
const result = await client.workflow.execute('data-processing-workflow', {
|
|
256
|
+
input: {
|
|
257
|
+
dataSource: 'users',
|
|
258
|
+
filters: { status: 'active' }
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
if (result.success) {
|
|
263
|
+
console.log('Workflow result:', result.data);
|
|
264
|
+
}
|
|
265
|
+
```
|
|
266
|
+
|
|
267
|
+
## ⚙️ Configuration
|
|
268
|
+
|
|
269
|
+
### Client Options
|
|
270
|
+
|
|
271
|
+
```typescript
|
|
272
|
+
interface AmasterClientOptions {
|
|
273
|
+
/** Base URL for the Amaster API */
|
|
274
|
+
baseURL: string;
|
|
275
|
+
|
|
276
|
+
/** Optional custom headers */
|
|
277
|
+
headers?: Record<string, string>;
|
|
278
|
+
|
|
279
|
+
/** Called when receiving 401 Unauthorized */
|
|
280
|
+
onUnauthorized?: () => void;
|
|
281
|
+
|
|
282
|
+
/** Called when token expires (before auto-refresh) */
|
|
283
|
+
onTokenExpired?: () => void;
|
|
284
|
+
|
|
285
|
+
/** Enable automatic token refresh (default: true) */
|
|
286
|
+
autoRefresh?: boolean;
|
|
287
|
+
|
|
288
|
+
/** Refresh threshold in seconds (default: 300) */
|
|
289
|
+
refreshThreshold?: number;
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
### Example with All Options
|
|
294
|
+
|
|
295
|
+
```typescript
|
|
296
|
+
const client = createClient({
|
|
297
|
+
baseURL: 'https://api.amaster.ai',
|
|
298
|
+
|
|
299
|
+
headers: {
|
|
300
|
+
'X-App-Version': '1.0.0'
|
|
301
|
+
},
|
|
302
|
+
|
|
303
|
+
onUnauthorized: () => {
|
|
304
|
+
console.log('Session expired, redirecting to login...');
|
|
305
|
+
window.location.href = '/login';
|
|
306
|
+
},
|
|
307
|
+
|
|
308
|
+
onTokenExpired: () => {
|
|
309
|
+
console.log('Token expired, will auto-refresh');
|
|
310
|
+
},
|
|
311
|
+
|
|
312
|
+
autoRefresh: true,
|
|
313
|
+
refreshThreshold: 300 // Refresh 5 minutes before expiry
|
|
314
|
+
});
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
## 🔧 Advanced Usage
|
|
318
|
+
|
|
319
|
+
### Manual Token Management
|
|
320
|
+
|
|
321
|
+
```typescript
|
|
322
|
+
// Get current access token
|
|
323
|
+
const token = client.getAccessToken();
|
|
324
|
+
|
|
325
|
+
// Set token manually (useful for SSR or external auth)
|
|
326
|
+
client.setAccessToken('your-jwt-token');
|
|
327
|
+
|
|
328
|
+
// Clear all auth data
|
|
329
|
+
client.clearAuth();
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
### Error Handling
|
|
333
|
+
|
|
334
|
+
```typescript
|
|
335
|
+
const result = await client.entity.list('default', 'users');
|
|
336
|
+
|
|
337
|
+
if (result.success) {
|
|
338
|
+
// Success case
|
|
339
|
+
console.log('Data:', result.data);
|
|
340
|
+
} else {
|
|
341
|
+
// Error case
|
|
342
|
+
console.error('Error:', result.error);
|
|
343
|
+
console.error('Status:', result.statusCode);
|
|
344
|
+
}
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
## 🎨 Comparison with Individual Clients
|
|
348
|
+
|
|
349
|
+
### Before (Using separate clients)
|
|
350
|
+
|
|
351
|
+
```typescript
|
|
352
|
+
import { createAuthClient } from '@amaster.ai/auth-client';
|
|
353
|
+
import { createEntityClient } from '@amaster.ai/entity-client';
|
|
354
|
+
import { createBpmClient } from '@amaster.ai/bpm-client';
|
|
355
|
+
|
|
356
|
+
const authClient = createAuthClient({ baseURL });
|
|
357
|
+
const entityClient = createEntityClient({ baseURL });
|
|
358
|
+
const bpmClient = createBpmClient({ baseURL });
|
|
359
|
+
|
|
360
|
+
// Need to manage tokens manually across clients
|
|
361
|
+
await authClient.login({ email, password });
|
|
362
|
+
const token = authClient.getAccessToken();
|
|
363
|
+
|
|
364
|
+
// Pass token to other clients somehow...
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
### After (Using unified client)
|
|
368
|
+
|
|
369
|
+
```typescript
|
|
370
|
+
import { createClient } from '@amaster.ai/client';
|
|
371
|
+
|
|
372
|
+
const client = createClient({ baseURL });
|
|
373
|
+
|
|
374
|
+
// One client, automatic token management
|
|
375
|
+
await client.auth.login({ email, password });
|
|
376
|
+
await client.entity.list('default', 'users'); // Token automatically attached
|
|
377
|
+
await client.bpm.getMyTasks(); // Token automatically attached
|
|
378
|
+
```
|
|
379
|
+
|
|
380
|
+
## 📖 API Reference
|
|
381
|
+
|
|
382
|
+
### `client.auth`
|
|
383
|
+
|
|
384
|
+
Full authentication API from `@amaster.ai/auth-client`:
|
|
385
|
+
|
|
386
|
+
- `login(params)` - Email/password or code-based login
|
|
387
|
+
- `register(params)` - User registration
|
|
388
|
+
- `logout()` - User logout
|
|
389
|
+
- `getMe()` - Get current user profile
|
|
390
|
+
- `updateMe(params)` - Update current user
|
|
391
|
+
- `changePassword(params)` - Change password
|
|
392
|
+
- `refreshToken()` - Manually refresh token
|
|
393
|
+
- `sendCode(params)` - Send verification code
|
|
394
|
+
- `hasPermission(permission)` - Check user permission
|
|
395
|
+
- `hasRole(role)` - Check user role
|
|
396
|
+
|
|
397
|
+
### `client.entity`
|
|
398
|
+
|
|
399
|
+
Full CRUD API from `@amaster.ai/entity-client`:
|
|
400
|
+
|
|
401
|
+
- `list(source, entity, params?)` - List entities with pagination
|
|
402
|
+
- `get(source, entity, id)` - Get single entity
|
|
403
|
+
- `create(source, entity, data)` - Create new entity
|
|
404
|
+
- `update(source, entity, id, data)` - Update entity
|
|
405
|
+
- `delete(source, entity, id)` - Delete entity
|
|
406
|
+
- `bulkUpdate(source, entity, items)` - Bulk update
|
|
407
|
+
- `bulkDelete(source, entity, ids)` - Bulk delete
|
|
408
|
+
- `options(source, entity, fields?)` - Get field options
|
|
409
|
+
|
|
410
|
+
### `client.bpm`
|
|
411
|
+
|
|
412
|
+
Full BPM API from `@amaster.ai/bpm-client`:
|
|
413
|
+
|
|
414
|
+
- `startProcess(params)` - Start process instance
|
|
415
|
+
- `getMyTasks(params?)` - Get current user's tasks
|
|
416
|
+
- `completeTask(taskId, variables?)` - Complete a task
|
|
417
|
+
- `claimTask(taskId)` - Claim a task
|
|
418
|
+
- `unclaimTask(taskId)` - Unclaim a task
|
|
419
|
+
- And more...
|
|
420
|
+
|
|
421
|
+
### `client.workflow`
|
|
422
|
+
|
|
423
|
+
Workflow execution API from `@amaster.ai/workflow-client`:
|
|
424
|
+
|
|
425
|
+
- `execute(workflowId, params)` - Execute a workflow
|
|
426
|
+
- `getStatus(executionId)` - Get execution status
|
|
427
|
+
- And more...
|
|
428
|
+
|
|
429
|
+
## 🔐 Token Management Flow
|
|
430
|
+
|
|
431
|
+
```
|
|
432
|
+
1. Login → Token stored automatically
|
|
433
|
+
2. Request → Token attached to headers
|
|
434
|
+
3. Response 401 → onUnauthorized() called
|
|
435
|
+
4. Token near expiry → Auto-refresh (if enabled)
|
|
436
|
+
5. Logout → Token cleared
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
## 🌐 Environment Support
|
|
440
|
+
|
|
441
|
+
Works in:
|
|
442
|
+
|
|
443
|
+
- ✅ Browser (modern)
|
|
444
|
+
- ✅ Node.js (18+)
|
|
445
|
+
- ✅ React / Vue / Angular
|
|
446
|
+
- ✅ Next.js / Nuxt
|
|
447
|
+
- ✅ React Native (with axios)
|
|
448
|
+
|
|
449
|
+
## 📄 License
|
|
450
|
+
|
|
451
|
+
MIT © Amaster Team
|
|
452
|
+
|
|
453
|
+
## 🤝 Related Packages
|
|
454
|
+
|
|
455
|
+
- `@amaster.ai/auth-client` - Authentication client
|
|
456
|
+
- `@amaster.ai/entity-client` - Entity CRUD client
|
|
457
|
+
- `@amaster.ai/bpm-client` - BPM client
|
|
458
|
+
- `@amaster.ai/workflow-client` - Workflow client
|
|
459
|
+
- `@amaster.ai/http-client` - HTTP client foundation
|
|
460
|
+
|
|
461
|
+
## 💡 Inspiration
|
|
462
|
+
|
|
463
|
+
API design inspired by [Supabase](https://supabase.com) - aiming for the same level of developer experience and simplicity.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var authClient = require('@amaster.ai/auth-client');
|
|
4
|
+
var entityClient = require('@amaster.ai/entity-client');
|
|
5
|
+
var bpmClient = require('@amaster.ai/bpm-client');
|
|
6
|
+
var workflowClient = require('@amaster.ai/workflow-client');
|
|
7
|
+
var httpClient = require('@amaster.ai/http-client');
|
|
8
|
+
|
|
9
|
+
// src/client.ts
|
|
10
|
+
function createClient(options) {
|
|
11
|
+
const { baseURL, headers = {}, onUnauthorized, onTokenExpired } = options;
|
|
12
|
+
const baseHttpClient = httpClient.createHttpClient({
|
|
13
|
+
baseURL,
|
|
14
|
+
headers
|
|
15
|
+
});
|
|
16
|
+
const auth = authClient.createAuthClient({
|
|
17
|
+
baseURL,
|
|
18
|
+
headers,
|
|
19
|
+
onTokenExpired,
|
|
20
|
+
onUnauthorized
|
|
21
|
+
});
|
|
22
|
+
const createAuthenticatedHttpClient = () => {
|
|
23
|
+
return {
|
|
24
|
+
async request(config) {
|
|
25
|
+
const token = auth.getAccessToken();
|
|
26
|
+
const authHeaders = token ? { Authorization: `Bearer ${token}` } : {};
|
|
27
|
+
const mergedConfig = {
|
|
28
|
+
...config,
|
|
29
|
+
headers: {
|
|
30
|
+
...config.headers,
|
|
31
|
+
...authHeaders
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const result = await baseHttpClient.request(mergedConfig);
|
|
35
|
+
if (result.status === 401 && onUnauthorized) {
|
|
36
|
+
onUnauthorized();
|
|
37
|
+
}
|
|
38
|
+
return result;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
const authenticatedHttpClient = createAuthenticatedHttpClient();
|
|
43
|
+
const entity = entityClient.createEntityClient(authenticatedHttpClient);
|
|
44
|
+
const bpm = bpmClient.createBpmClient(authenticatedHttpClient);
|
|
45
|
+
const workflow = workflowClient.createWorkflowClient(authenticatedHttpClient);
|
|
46
|
+
const client = {
|
|
47
|
+
auth,
|
|
48
|
+
entity,
|
|
49
|
+
bpm,
|
|
50
|
+
workflow,
|
|
51
|
+
// Expose token management methods from auth client
|
|
52
|
+
isAuthenticated: () => auth.isAuthenticated(),
|
|
53
|
+
getAccessToken: () => auth.getAccessToken(),
|
|
54
|
+
setAccessToken: (token) => auth.setAccessToken(token),
|
|
55
|
+
clearAuth: () => auth.clearAuth()
|
|
56
|
+
};
|
|
57
|
+
return client;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
exports.createClient = createClient;
|
|
61
|
+
//# sourceMappingURL=index.cjs.map
|
|
62
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/client.ts"],"names":["createHttpClient","createAuthClient","createEntityClient","createBpmClient","createWorkflowClient"],"mappings":";;;;;;;;;AAiFO,SAAS,aAAa,OAAA,EAA8C;AACzE,EAAA,MAAM,EAAE,OAAA,EAAS,OAAA,GAAU,EAAC,EAAG,cAAA,EAAgB,gBAAe,GAAI,OAAA;AAGlE,EAAA,MAAM,iBAAiBA,2BAAA,CAAiB;AAAA,IACtC,OAAA;AAAA,IACA;AAAA,GACD,CAAA;AAGD,EAAA,MAAM,OAAmBC,2BAAA,CAAiB;AAAA,IACxC,OAAA;AAAA,IACA,OAAA;AAAA,IACA,cAAA;AAAA,IACA;AAAA,GACD,CAAA;AAGD,EAAA,MAAM,gCAAgC,MAAkB;AACtD,IAAA,OAAO;AAAA,MACL,MAAM,QAAW,MAAA,EAAiD;AAEhE,QAAA,MAAM,KAAA,GAAQ,KAAK,cAAA,EAAe;AAGlC,QAAA,MAAM,WAAA,GAAc,QAAQ,EAAE,aAAA,EAAe,UAAU,KAAK,CAAA,CAAA,KAAO,EAAC;AACpE,QAAA,MAAM,YAAA,GAA8B;AAAA,UAClC,GAAG,MAAA;AAAA,UACH,OAAA,EAAS;AAAA,YACP,GAAG,MAAA,CAAO,OAAA;AAAA,YACV,GAAG;AAAA;AACL,SACF;AAGA,QAAA,MAAM,MAAA,GAAS,MAAM,cAAA,CAAe,OAAA,CAAW,YAAY,CAAA;AAG3D,QAAA,IAAI,MAAA,CAAO,MAAA,KAAW,GAAA,IAAO,cAAA,EAAgB;AAC3C,UAAA,cAAA,EAAe;AAAA,QACjB;AAEA,QAAA,OAAO,MAAA;AAAA,MACT;AAAA,KACF;AAAA,EACF,CAAA;AAGA,EAAA,MAAM,0BAA0B,6BAAA,EAA8B;AAG9D,EAAA,MAAM,MAAA,GAAuBC,gCAAmB,uBAAuB,CAAA;AACvE,EAAA,MAAM,GAAA,GAAiBC,0BAAgB,uBAAuB,CAAA;AAC9D,EAAA,MAAM,QAAA,GAA2BC,oCAAqB,uBAAuB,CAAA;AAG7E,EAAA,MAAM,MAAA,GAAwB;AAAA,IAC5B,IAAA;AAAA,IACA,MAAA;AAAA,IACA,GAAA;AAAA,IACA,QAAA;AAAA;AAAA,IAGA,eAAA,EAAiB,MAAM,IAAA,CAAK,eAAA,EAAgB;AAAA,IAC5C,cAAA,EAAgB,MAAM,IAAA,CAAK,cAAA,EAAe;AAAA,IAC1C,cAAA,EAAgB,CAAC,KAAA,KAAkB,IAAA,CAAK,eAAe,KAAK,CAAA;AAAA,IAC5D,SAAA,EAAW,MAAM,IAAA,CAAK,SAAA;AAAU,GAClC;AAEA,EAAA,OAAO,MAAA;AACT","file":"index.cjs","sourcesContent":["/**\n * ============================================================================\n * @amaster.ai/client - Unified Amaster Client\n * ============================================================================\n * \n * Supabase-inspired unified API client for the Amaster platform\n * \n * Features:\n * - Single client instance for all services (auth, entity, bpm, workflow)\n * - Automatic token management and refresh\n * - Auto-attach authentication to all requests\n * - Centralized error handling\n * \n * @example\n * ```typescript\n * const client = createClient({\n * baseURL: 'https://api.amaster.ai',\n * onUnauthorized: () => window.location.href = '/login'\n * });\n * \n * // Login\n * await client.auth.login({ email, password });\n * \n * // All subsequent requests automatically include auth token\n * await client.entity.list('default', 'users');\n * await client.bpm.startProcess({ processKey: 'approval' });\n * ```\n */\n\nimport { createAuthClient, type AuthClient } from \"@amaster.ai/auth-client\";\nimport { createEntityClient, type EntityClient } from \"@amaster.ai/entity-client\";\nimport { createBpmClient, type BpmClient } from \"@amaster.ai/bpm-client\";\nimport { createWorkflowClient, type WorkflowClient } from \"@amaster.ai/workflow-client\";\nimport { createHttpClient, type HttpClient, type RequestConfig, type ClientResult } from \"@amaster.ai/http-client\";\nimport type { AmasterClient, AmasterClientOptions } from \"./types\";\n\n/**\n * Create a unified Amaster client instance\n * \n * This function creates a single client that provides access to all Amaster services:\n * - Authentication (login, register, logout)\n * - Entity CRUD operations\n * - BPM (Business Process Management)\n * - Workflow execution\n * \n * All sub-clients automatically share the same HTTP client and authentication state,\n * ensuring that tokens are consistently attached to all requests.\n * \n * @param options - Client configuration options\n * @returns A unified Amaster client instance\n * \n * @example\n * ```typescript\n * // Basic usage\n * const client = createClient({\n * baseURL: 'https://api.amaster.ai'\n * });\n * \n * // With authentication callbacks\n * const client = createClient({\n * baseURL: 'https://api.amaster.ai',\n * onUnauthorized: () => {\n * // Redirect to login or show auth modal\n * window.location.href = '/login';\n * },\n * onTokenExpired: () => {\n * console.log('Token expired, refreshing...');\n * }\n * });\n * \n * // Login\n * await client.auth.login({\n * email: 'user@example.com',\n * password: 'password123'\n * });\n * \n * // Now all requests automatically include the auth token\n * const users = await client.entity.list('default', 'users');\n * const tasks = await client.bpm.getMyTasks();\n * ```\n */\nexport function createClient(options: AmasterClientOptions): AmasterClient {\n const { baseURL, headers = {}, onUnauthorized, onTokenExpired } = options;\n\n // Create the base HTTP client\n const baseHttpClient = createHttpClient({\n baseURL,\n headers,\n });\n\n // Create the auth client first (it manages its own HTTP client internally)\n const auth: AuthClient = createAuthClient({\n baseURL,\n headers,\n onTokenExpired,\n onUnauthorized,\n });\n\n // Create a wrapper HTTP client that automatically adds the auth token\n const createAuthenticatedHttpClient = (): HttpClient => {\n return {\n async request<T>(config: RequestConfig): Promise<ClientResult<T>> {\n // Get the current token from auth client\n const token = auth.getAccessToken();\n \n // Merge Authorization header with existing headers\n const authHeaders = token ? { Authorization: `Bearer ${token}` } : {};\n const mergedConfig: RequestConfig = {\n ...config,\n headers: {\n ...config.headers,\n ...authHeaders,\n },\n };\n\n // Make the request with the updated config\n const result = await baseHttpClient.request<T>(mergedConfig);\n\n // Handle 401 errors\n if (result.status === 401 && onUnauthorized) {\n onUnauthorized();\n }\n\n return result;\n },\n };\n };\n\n // Create the authenticated HTTP client\n const authenticatedHttpClient = createAuthenticatedHttpClient();\n\n // Create other clients using the authenticated HTTP client\n const entity: EntityClient = createEntityClient(authenticatedHttpClient);\n const bpm: BpmClient = createBpmClient(authenticatedHttpClient);\n const workflow: WorkflowClient = createWorkflowClient(authenticatedHttpClient);\n\n // Return unified client interface\n const client: AmasterClient = {\n auth,\n entity,\n bpm,\n workflow,\n\n // Expose token management methods from auth client\n isAuthenticated: () => auth.isAuthenticated(),\n getAccessToken: () => auth.getAccessToken(),\n setAccessToken: (token: string) => auth.setAccessToken(token),\n clearAuth: () => auth.clearAuth(),\n };\n\n return client;\n}\n"]}
|