@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.
@@ -0,0 +1,487 @@
1
+ /**
2
+ * ============================================================================
3
+ * @amaster.ai/client - Unified API Client
4
+ * ============================================================================
5
+ *
6
+ * A Supabase-inspired unified client for the Amaster platform.
7
+ *
8
+ * ## Features
9
+ * - Single client instance for all services (auth, entity, bpm, workflow)
10
+ * - Automatic token management and refresh
11
+ * - Auto-attach authentication to all requests
12
+ * - Centralized error handling
13
+ *
14
+ * ## Quick Start
15
+ * ```typescript
16
+ * import { createClient } from '@amaster.ai/client';
17
+ *
18
+ * const client = createClient({
19
+ * baseURL: 'https://api.amaster.ai',
20
+ * onUnauthorized: () => window.location.href = '/login'
21
+ * });
22
+ *
23
+ * // Login
24
+ * await client.auth.login({ email, password });
25
+ *
26
+ * // All subsequent requests automatically include auth token
27
+ * await client.entity.list('default', 'users');
28
+ * await client.bpm.startProcess('approval', {});
29
+ * ```
30
+ *
31
+ * ## Module Documentation
32
+ * For detailed API documentation, see individual module type definitions:
33
+ * - **Authentication**: {@link ./auth.d.ts}
34
+ * - **Entity Operations**: {@link ./entity.d.ts}
35
+ * - **BPM (Business Process)**: {@link ./bpm.d.ts}
36
+ * - **Workflow Execution**: {@link ./workflow.d.ts}
37
+ *
38
+ * @packageDocumentation
39
+ */
40
+
41
+ import type { AuthClientAPI } from './auth';
42
+ import type { EntityClientAPI } from './entity';
43
+ import type { BpmClientAPI } from './bpm';
44
+ import type { WorkflowClientAPI } from './workflow';
45
+
46
+ /**
47
+ * Configuration options for creating an Amaster client
48
+ *
49
+ * @example
50
+ * Basic configuration:
51
+ * ```typescript
52
+ * const client = createClient({
53
+ * baseURL: 'https://api.amaster.ai'
54
+ * });
55
+ * ```
56
+ *
57
+ * @example
58
+ * With authentication callbacks:
59
+ * ```typescript
60
+ * const client = createClient({
61
+ * baseURL: 'https://api.amaster.ai',
62
+ * onUnauthorized: () => {
63
+ * console.log('User is not authenticated');
64
+ * window.location.href = '/login';
65
+ * },
66
+ * onTokenExpired: () => {
67
+ * console.log('Token expired, refreshing...');
68
+ * }
69
+ * });
70
+ * ```
71
+ *
72
+ * @example
73
+ * With custom headers:
74
+ * ```typescript
75
+ * const client = createClient({
76
+ * baseURL: 'https://api.amaster.ai',
77
+ * headers: {
78
+ * 'X-Tenant-ID': 'tenant-123',
79
+ * 'X-App-Version': '1.0.0'
80
+ * }
81
+ * });
82
+ * ```
83
+ */
84
+ export interface AmasterClientOptions {
85
+ /**
86
+ * Base URL for the Amaster API
87
+ *
88
+ * @example 'https://api.amaster.ai'
89
+ * @example 'https://my-app.amaster.ai'
90
+ */
91
+ baseURL: string;
92
+
93
+ /**
94
+ * Optional custom headers to include in ALL requests
95
+ *
96
+ * These headers will be merged with authentication headers.
97
+ * Useful for tenant IDs, API keys, or application metadata.
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * {
102
+ * 'X-Tenant-ID': 'tenant-123',
103
+ * 'X-App-Version': '1.0.0',
104
+ * 'X-Custom-Header': 'custom-value'
105
+ * }
106
+ * ```
107
+ */
108
+ headers?: Record<string, string>;
109
+
110
+ /**
111
+ * Callback triggered when a 401 Unauthorized response is received
112
+ *
113
+ * Use this to redirect to login page or show authentication modal.
114
+ * This callback is invoked BEFORE the request promise rejects.
115
+ *
116
+ * @example
117
+ * Redirect to login page:
118
+ * ```typescript
119
+ * onUnauthorized: () => {
120
+ * window.location.href = '/login';
121
+ * }
122
+ * ```
123
+ *
124
+ * @example
125
+ * Show modal:
126
+ * ```typescript
127
+ * onUnauthorized: () => {
128
+ * showLoginModal();
129
+ * }
130
+ * ```
131
+ */
132
+ onUnauthorized?: () => void;
133
+
134
+ /**
135
+ * Callback triggered when the access token expires
136
+ *
137
+ * Note: Token refresh is handled automatically by the client.
138
+ * This callback is for additional actions like logging or analytics.
139
+ *
140
+ * @example
141
+ * ```typescript
142
+ * onTokenExpired: () => {
143
+ * console.log('Token expired, auto-refreshing...');
144
+ * analytics.track('token_expired');
145
+ * }
146
+ * ```
147
+ */
148
+ onTokenExpired?: () => void;
149
+
150
+ /**
151
+ * Enable automatic token refresh (default: true)
152
+ *
153
+ * When enabled, tokens are automatically refreshed before expiry.
154
+ *
155
+ * @default true
156
+ */
157
+ autoRefresh?: boolean;
158
+
159
+ /**
160
+ * Token refresh threshold in seconds (default: 300)
161
+ *
162
+ * Tokens will be refreshed this many seconds before expiry.
163
+ * For example, if set to 300 (5 minutes), a token expiring at
164
+ * 10:00:00 will be refreshed at 9:55:00.
165
+ *
166
+ * @default 300
167
+ */
168
+ refreshThreshold?: number;
169
+ }
170
+
171
+ /**
172
+ * Unified Amaster Client interface
173
+ *
174
+ * This is the main client object returned by `createClient()`.
175
+ * It provides access to all Amaster services through a unified interface.
176
+ *
177
+ * ## Core Modules
178
+ * - {@link auth} - Authentication and user management
179
+ * - {@link entity} - CRUD operations for entities
180
+ * - {@link bpm} - Business process management
181
+ * - {@link workflow} - Workflow execution
182
+ *
183
+ * ## Token Management
184
+ * - {@link isAuthenticated} - Check if user is authenticated
185
+ * - {@link getAccessToken} - Get current access token
186
+ * - {@link setAccessToken} - Set access token manually
187
+ * - {@link clearAuth} - Clear all authentication state
188
+ *
189
+ * @example
190
+ * Complete authentication flow:
191
+ * ```typescript
192
+ * const client = createClient({ baseURL: 'https://api.amaster.ai' });
193
+ *
194
+ * // 1. Login
195
+ * await client.auth.login({
196
+ * email: 'user@example.com',
197
+ * password: 'password123'
198
+ * });
199
+ *
200
+ * // 2. Check authentication
201
+ * if (client.isAuthenticated()) {
202
+ * console.log('User is logged in');
203
+ * }
204
+ *
205
+ * // 3. Get current token
206
+ * const token = client.getAccessToken();
207
+ *
208
+ * // 4. Use any service (token automatically attached)
209
+ * const users = await client.entity.list('default', 'users');
210
+ *
211
+ * // 5. Logout
212
+ * await client.auth.logout();
213
+ * ```
214
+ */
215
+ export interface AmasterClient {
216
+ /**
217
+ * Authentication module
218
+ *
219
+ * Provides methods for user authentication, registration, and management.
220
+ *
221
+ * For detailed documentation, see {@link ./auth.d.ts}
222
+ *
223
+ * @example
224
+ * Login:
225
+ * ```typescript
226
+ * await client.auth.login({
227
+ * email: 'user@example.com',
228
+ * password: 'password123'
229
+ * });
230
+ * ```
231
+ *
232
+ * @example
233
+ * Register:
234
+ * ```typescript
235
+ * await client.auth.register({
236
+ * username: 'johndoe',
237
+ * email: 'john@example.com',
238
+ * password: 'Password@123'
239
+ * });
240
+ * ```
241
+ *
242
+ * @example
243
+ * Get current user:
244
+ * ```typescript
245
+ * const result = await client.auth.getMe();
246
+ * console.log(result.data); // User object
247
+ * ```
248
+ */
249
+ auth: AuthClientAPI;
250
+
251
+ /**
252
+ * Entity CRUD operations module
253
+ *
254
+ * Provides methods for creating, reading, updating, and deleting entities.
255
+ *
256
+ * For detailed documentation, see {@link ./entity.d.ts}
257
+ *
258
+ * @example
259
+ * List entities:
260
+ * ```typescript
261
+ * const result = await client.entity.list('default', 'users', {
262
+ * page: 1,
263
+ * perPage: 20,
264
+ * orderBy: 'createdAt',
265
+ * orderDir: 'desc'
266
+ * });
267
+ * ```
268
+ *
269
+ * @example
270
+ * Create entity:
271
+ * ```typescript
272
+ * await client.entity.create('default', 'users', {
273
+ * name: 'John Doe',
274
+ * email: 'john@example.com'
275
+ * });
276
+ * ```
277
+ */
278
+ entity: EntityClientAPI;
279
+
280
+ /**
281
+ * Business Process Management (BPM) module
282
+ *
283
+ * Provides methods for managing BPMN processes and tasks.
284
+ *
285
+ * For detailed documentation, see {@link ./bpm.d.ts}
286
+ *
287
+ * @example
288
+ * Start a process:
289
+ * ```typescript
290
+ * const result = await client.bpm.startProcess('approval', {
291
+ * amount: { value: 1000, type: 'Long' },
292
+ * requester: { value: 'user-123', type: 'String' }
293
+ * });
294
+ * ```
295
+ *
296
+ * @example
297
+ * Get tasks:
298
+ * ```typescript
299
+ * const tasks = await client.bpm.getTasks({
300
+ * assignee: 'user-123',
301
+ * processDefinitionKey: 'approval'
302
+ * });
303
+ * ```
304
+ */
305
+ bpm: BpmClientAPI;
306
+
307
+ /**
308
+ * Workflow execution module
309
+ *
310
+ * Provides methods for executing Dify-style workflows.
311
+ *
312
+ * For detailed documentation, see {@link ./workflow.d.ts}
313
+ *
314
+ * @example
315
+ * Run a workflow:
316
+ * ```typescript
317
+ * const result = await client.workflow.run('data-analysis', {
318
+ * input_data: 'sample data',
319
+ * options: { format: 'json' }
320
+ * });
321
+ * console.log(result.data.outputs);
322
+ * ```
323
+ */
324
+ workflow: WorkflowClientAPI;
325
+
326
+ /**
327
+ * Check if the user is currently authenticated
328
+ *
329
+ * @returns `true` if a valid access token exists, `false` otherwise
330
+ *
331
+ * @example
332
+ * ```typescript
333
+ * if (client.isAuthenticated()) {
334
+ * console.log('User is logged in');
335
+ * } else {
336
+ * console.log('Please log in');
337
+ * }
338
+ * ```
339
+ */
340
+ isAuthenticated(): boolean;
341
+
342
+ /**
343
+ * Get the current access token
344
+ *
345
+ * @returns The access token string, or `null` if not authenticated
346
+ *
347
+ * @example
348
+ * ```typescript
349
+ * const token = client.getAccessToken();
350
+ * if (token) {
351
+ * console.log('Token:', token);
352
+ * }
353
+ * ```
354
+ *
355
+ * @example
356
+ * Store token for later use:
357
+ * ```typescript
358
+ * const token = client.getAccessToken();
359
+ * if (token) {
360
+ * localStorage.setItem('backup_token', token);
361
+ * }
362
+ * ```
363
+ */
364
+ getAccessToken(): string | null;
365
+
366
+ /**
367
+ * Manually set the access token
368
+ *
369
+ * Useful when you have a token from another source (e.g., SSO, OAuth).
370
+ * After setting the token, all requests will use it automatically.
371
+ *
372
+ * @param token - The JWT access token to set
373
+ *
374
+ * @example
375
+ * Set token from OAuth:
376
+ * ```typescript
377
+ * const ssoToken = await getSSOToken();
378
+ * client.setAccessToken(ssoToken);
379
+ *
380
+ * // Now all requests will use this token
381
+ * await client.entity.list('default', 'users');
382
+ * ```
383
+ *
384
+ * @example
385
+ * Restore token from storage:
386
+ * ```typescript
387
+ * const savedToken = localStorage.getItem('auth_token');
388
+ * if (savedToken) {
389
+ * client.setAccessToken(savedToken);
390
+ * }
391
+ * ```
392
+ */
393
+ setAccessToken(token: string): void;
394
+
395
+ /**
396
+ * Clear all authentication state
397
+ *
398
+ * This removes:
399
+ * - Access token
400
+ * - User information
401
+ * - Auto-refresh timers
402
+ *
403
+ * After calling this, `isAuthenticated()` will return `false`
404
+ * and all requests will be unauthenticated.
405
+ *
406
+ * @example
407
+ * ```typescript
408
+ * client.clearAuth();
409
+ * console.log(client.isAuthenticated()); // false
410
+ * console.log(client.getAccessToken()); // null
411
+ * ```
412
+ *
413
+ * @example
414
+ * Logout without API call:
415
+ * ```typescript
416
+ * // If you want to clear local state without calling logout API
417
+ * client.clearAuth();
418
+ * window.location.href = '/login';
419
+ * ```
420
+ */
421
+ clearAuth(): void;
422
+ }
423
+
424
+ /**
425
+ * Factory function to create a unified Amaster client
426
+ *
427
+ * This is the main entry point for using the Amaster client library.
428
+ * It returns a client instance that provides access to all Amaster services.
429
+ *
430
+ * @param options - Configuration options for the client
431
+ * @returns A configured Amaster client instance
432
+ *
433
+ * @example
434
+ * Basic usage:
435
+ * ```typescript
436
+ * import { createClient } from '@amaster.ai/client';
437
+ *
438
+ * const client = createClient({
439
+ * baseURL: 'https://api.amaster.ai'
440
+ * });
441
+ *
442
+ * await client.auth.login({ email, password });
443
+ * ```
444
+ *
445
+ * @example
446
+ * With callbacks:
447
+ * ```typescript
448
+ * const client = createClient({
449
+ * baseURL: 'https://api.amaster.ai',
450
+ * onUnauthorized: () => {
451
+ * console.log('Session expired');
452
+ * window.location.href = '/login';
453
+ * },
454
+ * onTokenExpired: () => {
455
+ * console.log('Token expired, refreshing...');
456
+ * }
457
+ * });
458
+ * ```
459
+ *
460
+ * @example
461
+ * With custom headers:
462
+ * ```typescript
463
+ * const client = createClient({
464
+ * baseURL: 'https://api.amaster.ai',
465
+ * headers: {
466
+ * 'X-Tenant-ID': 'tenant-123',
467
+ * 'X-App-Version': '1.0.0'
468
+ * }
469
+ * });
470
+ * ```
471
+ */
472
+ export declare function createClient(options: AmasterClientOptions): AmasterClient;
473
+
474
+ // Re-export shared common types
475
+ export type { ClientResult } from './common';
476
+
477
+ // Re-export main client interfaces
478
+ export type { AuthClientAPI } from './auth';
479
+ export type { EntityClientAPI } from './entity';
480
+ export type { BpmClientAPI } from './bpm';
481
+ export type { WorkflowClientAPI } from './workflow';
482
+
483
+ // For detailed types, import directly from submodules:
484
+ // import type { LoginParams, User } from '@amaster.ai/client/auth'
485
+ // import type { EntityQueryParams } from '@amaster.ai/client/entity'
486
+ // import type { Task, ProcessInstance } from '@amaster.ai/client/bpm'
487
+ // import type { WorkflowRunRequest } from '@amaster.ai/client/workflow'