@mitralab.io/platform-sdk 1.0.1

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,1140 @@
1
+ /** Allowed query parameter value types. */
2
+ type QueryParamValue = string | number | boolean | undefined;
3
+ /**
4
+ * Configuration options for creating an HttpClient instance.
5
+ */
6
+ interface HttpClientConfig {
7
+ /** Base URL for all HTTP requests (e.g., 'https://api.mitra.io') */
8
+ baseUrl: string;
9
+ /** Function that returns the current authentication token, or null if not authenticated */
10
+ getToken?: () => string | null;
11
+ /** Callback invoked on 401 responses. Should attempt token refresh and return true if successful. */
12
+ onUnauthorized?: () => Promise<boolean>;
13
+ /** Called whenever an API request fails. Useful for global error handling (e.g., toast notifications). */
14
+ onError?: (error: MitraApiError) => void;
15
+ /** Headers included in every request (e.g., X-App-Id for tracing). */
16
+ defaultHeaders?: Record<string, string>;
17
+ }
18
+ /**
19
+ * Options for making HTTP requests.
20
+ */
21
+ interface RequestOptions {
22
+ /** HTTP method (defaults to 'GET') */
23
+ method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
24
+ /** Request body (will be JSON stringified) */
25
+ body?: unknown;
26
+ /** Additional headers to include in the request */
27
+ headers?: Record<string, string>;
28
+ /** URL query parameters */
29
+ params?: Record<string, QueryParamValue>;
30
+ /** @internal Flag to prevent infinite retry loops on 401 */
31
+ isRetry?: boolean;
32
+ }
33
+ /**
34
+ * HTTP client for making authenticated API requests.
35
+ *
36
+ * Handles JSON serialization, authentication headers, and error handling.
37
+ * All requests automatically include the Authorization header when a token is available.
38
+ *
39
+ * @example
40
+ * ```typescript
41
+ * const client = new HttpClient({
42
+ * baseUrl: 'https://api.mitra.io',
43
+ * getToken: () => localStorage.getItem('token'),
44
+ * });
45
+ *
46
+ * const users = await client.get<User[]>('/users');
47
+ * const user = await client.post<User>('/users', { name: 'John' });
48
+ * ```
49
+ */
50
+ declare class HttpClient {
51
+ private readonly baseUrl;
52
+ private readonly tokenGetter;
53
+ private readonly onUnauthorized?;
54
+ private readonly onError?;
55
+ private readonly defaultHeaders;
56
+ constructor(config: HttpClientConfig);
57
+ /**
58
+ * Returns the current authentication token.
59
+ * @returns The JWT token if authenticated, null otherwise
60
+ */
61
+ getToken(): string | null;
62
+ /**
63
+ * Makes an HTTP request with automatic JSON handling and authentication.
64
+ *
65
+ * @param path - API endpoint path (e.g., '/users')
66
+ * @param options - Request options including method, body, headers, and params
67
+ * @returns Promise resolving to the parsed JSON response
68
+ * @throws {MitraApiError} When the API returns an error response
69
+ *
70
+ * @example
71
+ * ```typescript
72
+ * const result = await client.request<User>('/users/123', {
73
+ * method: 'PUT',
74
+ * body: { name: 'Updated Name' },
75
+ * });
76
+ * ```
77
+ */
78
+ request<T>(path: string, options?: RequestOptions): Promise<T>;
79
+ /**
80
+ * Makes a GET request.
81
+ *
82
+ * @param path - API endpoint path
83
+ * @param params - Optional query parameters
84
+ * @returns Promise resolving to the parsed JSON response
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * const users = await client.get<User[]>('/users', { limit: 10 });
89
+ * ```
90
+ */
91
+ get<T>(path: string, params?: Record<string, QueryParamValue>): Promise<T>;
92
+ /**
93
+ * Makes a POST request.
94
+ *
95
+ * @param path - API endpoint path
96
+ * @param body - Request body (will be JSON stringified)
97
+ * @returns Promise resolving to the parsed JSON response
98
+ *
99
+ * @example
100
+ * ```typescript
101
+ * const user = await client.post<User>('/users', { name: 'John', email: 'john@example.com' });
102
+ * ```
103
+ */
104
+ post<T>(path: string, body?: unknown): Promise<T>;
105
+ /**
106
+ * Makes a PUT request.
107
+ *
108
+ * @param path - API endpoint path
109
+ * @param body - Request body (will be JSON stringified)
110
+ * @returns Promise resolving to the parsed JSON response
111
+ *
112
+ * @example
113
+ * ```typescript
114
+ * const user = await client.put<User>('/users/123', { name: 'Updated Name' });
115
+ * ```
116
+ */
117
+ put<T>(path: string, body?: unknown): Promise<T>;
118
+ /**
119
+ * Makes a DELETE request.
120
+ *
121
+ * @param path - API endpoint path
122
+ * @param params - Optional query parameters
123
+ * @returns Promise resolving to the parsed JSON response (or undefined for 204 responses)
124
+ *
125
+ * @example
126
+ * ```typescript
127
+ * await client.delete('/users/123');
128
+ * ```
129
+ */
130
+ delete<T>(path: string, params?: Record<string, QueryParamValue>): Promise<T>;
131
+ }
132
+ /**
133
+ * Error thrown when a Mitra API request fails.
134
+ *
135
+ * Contains detailed information about the error including HTTP status,
136
+ * error code, and additional details from the server response.
137
+ *
138
+ * @example
139
+ * ```typescript
140
+ * try {
141
+ * await mitra.entities.Task.get('invalid-id');
142
+ * } catch (error) {
143
+ * if (error instanceof MitraApiError) {
144
+ * console.log(error.status); // 404
145
+ * console.log(error.message); // "Task not found"
146
+ * console.log(error.code); // "ENTITY_NOT_FOUND"
147
+ * }
148
+ * }
149
+ * ```
150
+ */
151
+ declare class MitraApiError extends Error {
152
+ /** HTTP status code (e.g., 400, 401, 404, 500) */
153
+ readonly status: number;
154
+ /** Application-specific error code (e.g., 'ENTITY_NOT_FOUND', 'VALIDATION_ERROR') */
155
+ readonly code?: string | undefined;
156
+ /** Additional error details from the server response */
157
+ readonly details?: unknown | undefined;
158
+ constructor(message: string,
159
+ /** HTTP status code (e.g., 400, 401, 404, 500) */
160
+ status: number,
161
+ /** Application-specific error code (e.g., 'ENTITY_NOT_FOUND', 'VALIDATION_ERROR') */
162
+ code?: string | undefined,
163
+ /** Additional error details from the server response */
164
+ details?: unknown | undefined);
165
+ }
166
+
167
+ /** Authenticated user in the Mitra Platform. */
168
+ interface User {
169
+ /** Unique identifier. */
170
+ id: string;
171
+ /** Tenant the user belongs to. */
172
+ tenantId: string;
173
+ /** Email address. */
174
+ email: string;
175
+ /** Display name (optional). */
176
+ name: string | null;
177
+ }
178
+ /** Credentials for sign-in. */
179
+ interface SignInCredentials {
180
+ email: string;
181
+ password: string;
182
+ }
183
+ /** Data for user registration. */
184
+ interface SignUpData {
185
+ email: string;
186
+ password: string;
187
+ name?: string;
188
+ }
189
+ /** Callback for auth state changes. Receives the user on login, null on logout. */
190
+ type AuthStateChangeCallback = (user: User | null) => void;
191
+
192
+ /**
193
+ * Authentication module for managing user sessions.
194
+ *
195
+ * Handles sign-in, sign-up, sign-out, and automatic token refresh.
196
+ * Auth state is persisted to localStorage with key `mitra_auth_{appId}`
197
+ * and restored on page reload.
198
+ *
199
+ * @example
200
+ * ```typescript
201
+ * await mitra.auth.signIn({ email: 'user@example.com', password: 'password' });
202
+ * console.log(mitra.auth.currentUser);
203
+ * ```
204
+ */
205
+ declare class AuthModule {
206
+ private readonly appId;
207
+ private _currentUser;
208
+ private _accessToken;
209
+ private _refreshToken;
210
+ private refreshPromise;
211
+ private readonly listeners;
212
+ private readonly storageKey;
213
+ private readonly publicClient;
214
+ private readonly authedClient;
215
+ constructor(appId: string, iamBaseUrl: string);
216
+ /** The currently authenticated user, or null. */
217
+ get currentUser(): User | null;
218
+ /** The current JWT access token, or null. */
219
+ get accessToken(): string | null;
220
+ /** Whether a user is currently authenticated (local check, not server-validated). */
221
+ get isAuthenticated(): boolean;
222
+ /**
223
+ * Signs in a user with email and password.
224
+ *
225
+ * On success, stores access token, refresh token, and user data.
226
+ * Subsequent API requests use the token automatically.
227
+ *
228
+ * @param credentials - Email and password.
229
+ * @returns The authenticated user.
230
+ * @throws {MitraApiError} On invalid credentials (401).
231
+ *
232
+ * @example
233
+ * ```typescript
234
+ * const user = await mitra.auth.signIn({
235
+ * email: 'user@example.com',
236
+ * password: 'password123',
237
+ * });
238
+ * ```
239
+ */
240
+ signIn(credentials: SignInCredentials): Promise<User>;
241
+ /**
242
+ * Registers a new user and signs them in automatically.
243
+ *
244
+ * @param data - Email, password, and optional name.
245
+ * @returns The newly created and authenticated user.
246
+ * @throws {MitraApiError} On duplicate email (409) or validation error (400).
247
+ *
248
+ * @example
249
+ * ```typescript
250
+ * const user = await mitra.auth.signUp({
251
+ * email: 'new@example.com',
252
+ * password: 'securepassword',
253
+ * name: 'Jane Doe',
254
+ * });
255
+ * ```
256
+ */
257
+ signUp(data: SignUpData): Promise<User>;
258
+ /**
259
+ * Signs out the current user, clearing all auth state and localStorage.
260
+ *
261
+ * @param redirectUrl - Optional URL to navigate to after sign-out.
262
+ *
263
+ * @example
264
+ * ```typescript
265
+ * mitra.auth.signOut();
266
+ * mitra.auth.signOut('/login');
267
+ * ```
268
+ */
269
+ signOut(redirectUrl?: string): void;
270
+ /**
271
+ * Refreshes the session using the stored refresh token.
272
+ *
273
+ * Called automatically by the SDK on 401 responses. Can also be called
274
+ * manually. Multiple concurrent calls are deduplicated (only one refresh
275
+ * request is made).
276
+ *
277
+ * @returns `true` if refresh succeeded, `false` otherwise.
278
+ *
279
+ * @example
280
+ * ```typescript
281
+ * const ok = await mitra.auth.refreshSession();
282
+ * if (!ok) mitra.auth.redirectToLogin();
283
+ * ```
284
+ */
285
+ refreshSession(): Promise<boolean>;
286
+ /**
287
+ * Fetches the current user from the server and updates local state.
288
+ *
289
+ * Only clears auth state on 401 (expired/invalid token).
290
+ * Transient errors (500, network) return null without clearing the session.
291
+ *
292
+ * @returns The user if authenticated, `null` otherwise.
293
+ *
294
+ * @example
295
+ * ```typescript
296
+ * const user = await mitra.auth.me();
297
+ * if (!user) console.log('Not authenticated');
298
+ * ```
299
+ */
300
+ me(): Promise<User | null>;
301
+ /**
302
+ * Validates the current session with the server.
303
+ *
304
+ * @returns `true` if the session is valid, `false` otherwise.
305
+ *
306
+ * @example
307
+ * ```typescript
308
+ * const valid = await mitra.auth.checkAuth();
309
+ * if (!valid) mitra.auth.redirectToLogin();
310
+ * ```
311
+ */
312
+ checkAuth(): Promise<boolean>;
313
+ /**
314
+ * Sets the access token manually (e.g., from SSO/OAuth callback).
315
+ *
316
+ * Call `me()` afterwards to fetch the associated user data.
317
+ *
318
+ * @param token - JWT access token.
319
+ * @param saveToStorage - Whether to persist to localStorage (default: true).
320
+ *
321
+ * @example
322
+ * ```typescript
323
+ * mitra.auth.setToken(tokenFromCallback);
324
+ * await mitra.auth.me();
325
+ * ```
326
+ */
327
+ setToken(token: string, saveToStorage?: boolean): void;
328
+ /**
329
+ * Redirects to `/login?returnUrl=...` for unauthenticated users.
330
+ *
331
+ * @param returnUrl - URL to return to after login (default: '/').
332
+ *
333
+ * @example
334
+ * ```typescript
335
+ * if (!mitra.auth.isAuthenticated) {
336
+ * mitra.auth.redirectToLogin(window.location.pathname);
337
+ * }
338
+ * ```
339
+ */
340
+ redirectToLogin(returnUrl?: string): void;
341
+ /**
342
+ * Registers a callback for auth state changes.
343
+ *
344
+ * Called immediately with the current state, then on every sign-in/sign-out.
345
+ *
346
+ * @param callback - Receives the User on login, null on logout.
347
+ * @returns Unsubscribe function.
348
+ *
349
+ * @example
350
+ * ```typescript
351
+ * useEffect(() => {
352
+ * const unsub = mitra.auth.onAuthStateChange((user) => {
353
+ * setUser(user);
354
+ * setLoading(false);
355
+ * });
356
+ * return unsub;
357
+ * }, []);
358
+ * ```
359
+ */
360
+ onAuthStateChange(callback: AuthStateChangeCallback): () => void;
361
+ private doRefresh;
362
+ private setAuthState;
363
+ private clearAuthState;
364
+ private notifyListeners;
365
+ private saveToStorage;
366
+ private loadFromStorage;
367
+ private removeFromStorage;
368
+ }
369
+
370
+ /**
371
+ * Options for listing entities with sorting and pagination.
372
+ */
373
+ interface EntityListOptions {
374
+ /**
375
+ * Field to sort by. Prefix with '-' for descending order.
376
+ *
377
+ * @example
378
+ * ```typescript
379
+ * // Sort by created_at descending (newest first)
380
+ * { sort: '-created_at' }
381
+ *
382
+ * // Sort by name ascending (A-Z)
383
+ * { sort: 'name' }
384
+ * ```
385
+ */
386
+ sort?: string;
387
+ /**
388
+ * Maximum number of records to return.
389
+ * Defaults to 100. Maximum allowed is 1000.
390
+ */
391
+ limit?: number;
392
+ /**
393
+ * Number of records to skip for pagination.
394
+ * Use with `limit` to implement pagination.
395
+ *
396
+ * @example
397
+ * ```typescript
398
+ * // Get page 2 (records 11-20)
399
+ * { limit: 10, skip: 10 }
400
+ * ```
401
+ */
402
+ skip?: number;
403
+ /**
404
+ * Array of field names to include in the response.
405
+ * If not specified, all fields are returned.
406
+ *
407
+ * @example
408
+ * ```typescript
409
+ * // Only return id, title, and status
410
+ * { fields: ['id', 'title', 'status'] }
411
+ * ```
412
+ */
413
+ fields?: string[];
414
+ }
415
+ /**
416
+ * Entity handler providing CRUD operations for a specific entity type.
417
+ *
418
+ * Each table in the database gets a handler with these methods for managing data.
419
+ * Access tables dynamically using `mitra.entities.TableName`.
420
+ *
421
+ * @typeParam T - The shape of records in this table. Defaults to `Record<string, unknown>`.
422
+ *
423
+ * @example
424
+ * ```typescript
425
+ * // Dynamic access (no type safety)
426
+ * const tasks = await mitra.entities.Task.list();
427
+ *
428
+ * // Typed access
429
+ * interface Task {
430
+ * id: string;
431
+ * title: string;
432
+ * status: 'pending' | 'done';
433
+ * }
434
+ * const tasks = mitra.entities.getTable<Task>('Task');
435
+ * const pending = await tasks.filter({ status: 'pending' });
436
+ * ```
437
+ */
438
+ interface EntityTable<T = Record<string, unknown>> {
439
+ /**
440
+ * Lists records with optional pagination and sorting.
441
+ *
442
+ * Retrieves all records from the table with support for sorting,
443
+ * pagination, and field selection. Supports both positional parameters
444
+ * (for quick usage) and options object (for clarity).
445
+ *
446
+ * @param sortOrOptions - Sort field (e.g., '-created_at') or options object.
447
+ * @param limit - Maximum number of results to return. Defaults to 100.
448
+ * @param skip - Number of results to skip for pagination. Defaults to 0.
449
+ * @param fields - Array of field names to include in the response.
450
+ * @returns Promise resolving to an array of records.
451
+ *
452
+ * @example
453
+ * ```typescript
454
+ * // Get all records
455
+ * const tasks = await mitra.entities.Task.list();
456
+ * ```
457
+ *
458
+ * @example
459
+ * ```typescript
460
+ * // Get first 10 records sorted by date (newest first)
461
+ * const tasks = await mitra.entities.Task.list('-created_at', 10);
462
+ * ```
463
+ *
464
+ * @example
465
+ * ```typescript
466
+ * // Get paginated results (page 3, 10 items per page)
467
+ * const tasks = await mitra.entities.Task.list('-created_at', 10, 20);
468
+ * ```
469
+ *
470
+ * @example
471
+ * ```typescript
472
+ * // Using options object
473
+ * const tasks = await mitra.entities.Task.list({
474
+ * sort: '-created_at',
475
+ * limit: 10,
476
+ * skip: 0,
477
+ * fields: ['id', 'title', 'status'],
478
+ * });
479
+ * ```
480
+ */
481
+ list(sortOrOptions?: string | EntityListOptions, limit?: number, skip?: number, fields?: string[]): Promise<T[]>;
482
+ /**
483
+ * Filters records based on a query.
484
+ *
485
+ * Retrieves records that match specific criteria with support for
486
+ * sorting, pagination, and field selection. All query conditions
487
+ * are combined with AND logic.
488
+ *
489
+ * @param query - Query object with field-value pairs. Records matching
490
+ * all specified criteria are returned. Field names are case-sensitive.
491
+ * @param sort - Sort field (prefix '-' for descending). Defaults to '-created_at'.
492
+ * @param limit - Maximum number of results to return. Defaults to 100.
493
+ * @param skip - Number of results to skip for pagination. Defaults to 0.
494
+ * @param fields - Array of field names to include in the response.
495
+ * @returns Promise resolving to an array of matching records.
496
+ *
497
+ * @example
498
+ * ```typescript
499
+ * // Filter by single field
500
+ * const doneTasks = await mitra.entities.Task.filter({ status: 'done' });
501
+ * ```
502
+ *
503
+ * @example
504
+ * ```typescript
505
+ * // Filter by multiple fields (AND logic)
506
+ * const urgentTasks = await mitra.entities.Task.filter({
507
+ * status: 'pending',
508
+ * priority: 'high',
509
+ * });
510
+ * ```
511
+ *
512
+ * @example
513
+ * ```typescript
514
+ * // Filter with sorting and pagination
515
+ * const tasks = await mitra.entities.Task.filter(
516
+ * { status: 'pending' },
517
+ * '-priority', // sort by priority descending
518
+ * 10, // limit
519
+ * 0 // skip
520
+ * );
521
+ * ```
522
+ *
523
+ * @example
524
+ * ```typescript
525
+ * // Filter with specific fields
526
+ * const tasks = await mitra.entities.Task.filter(
527
+ * { assignee: 'user-123' },
528
+ * '-created_at',
529
+ * 20,
530
+ * 0,
531
+ * ['id', 'title', 'status']
532
+ * );
533
+ * ```
534
+ *
535
+ * @example
536
+ * ```typescript
537
+ * // Comparison operators: $gt, $gte, $lt, $lte, $ne
538
+ * const expensive = await mitra.entities.Product.filter({ price: { $gt: 100 } });
539
+ * const recent = await mitra.entities.Order.filter({ created_at: { $gte: '2025-01-01' } });
540
+ * const notDone = await mitra.entities.Task.filter({ status: { $ne: 'done' } });
541
+ * ```
542
+ *
543
+ * @example
544
+ * ```typescript
545
+ * // Range query (combines with AND)
546
+ * const midRange = await mitra.entities.Product.filter({
547
+ * price: { $gte: 50, $lte: 200 },
548
+ * });
549
+ * ```
550
+ *
551
+ * @example
552
+ * ```typescript
553
+ * // Mix equality and operators
554
+ * const results = await mitra.entities.Order.filter({
555
+ * status: 'shipped',
556
+ * total: { $gt: 1000 },
557
+ * });
558
+ * ```
559
+ */
560
+ filter(query: Record<string, unknown>, sort?: string, limit?: number, skip?: number, fields?: string[]): Promise<T[]>;
561
+ /**
562
+ * Gets a single record by ID.
563
+ *
564
+ * Retrieves a specific record using its unique identifier.
565
+ *
566
+ * @param id - The unique identifier of the record.
567
+ * @returns Promise resolving to the record.
568
+ * @throws {MitraApiError} When record is not found (404).
569
+ *
570
+ * @example
571
+ * ```typescript
572
+ * const task = await mitra.entities.Task.get('task-123');
573
+ * console.log(task.title);
574
+ * ```
575
+ *
576
+ * @example
577
+ * ```typescript
578
+ * // With error handling
579
+ * try {
580
+ * const task = await mitra.entities.Task.get(taskId);
581
+ * setTask(task);
582
+ * } catch (error) {
583
+ * if (error.status === 404) {
584
+ * console.error('Task not found');
585
+ * }
586
+ * }
587
+ * ```
588
+ */
589
+ get(id: string | number): Promise<T>;
590
+ /**
591
+ * Creates a new record.
592
+ *
593
+ * Creates a new record in the table with the provided data.
594
+ * The server will generate an `id` and timestamps automatically.
595
+ *
596
+ * @param data - Object containing the record data.
597
+ * @returns Promise resolving to the created record (including generated id).
598
+ * @throws {MitraApiError} When validation fails (400).
599
+ *
600
+ * @example
601
+ * ```typescript
602
+ * const task = await mitra.entities.Task.create({
603
+ * title: 'Complete documentation',
604
+ * status: 'pending',
605
+ * priority: 'high',
606
+ * });
607
+ * console.log('Created task:', task.id);
608
+ * ```
609
+ *
610
+ * @example
611
+ * ```typescript
612
+ * // With error handling
613
+ * try {
614
+ * const task = await mitra.entities.Task.create(formData);
615
+ * toast.success('Task created!');
616
+ * navigate(`/tasks/${task.id}`);
617
+ * } catch (error) {
618
+ * toast.error(error.message);
619
+ * }
620
+ * ```
621
+ */
622
+ create(data: Partial<T>): Promise<T>;
623
+ /**
624
+ * Updates an existing record.
625
+ *
626
+ * Updates a record by ID with the provided data. Only the fields
627
+ * included in the data object will be updated; other fields remain
628
+ * unchanged (partial update).
629
+ *
630
+ * @param id - The unique identifier of the record to update.
631
+ * @param data - Object containing the fields to update.
632
+ * @returns Promise resolving to the updated record.
633
+ * @throws {MitraApiError} When record is not found (404).
634
+ *
635
+ * @example
636
+ * ```typescript
637
+ * // Update single field
638
+ * const updated = await mitra.entities.Task.update('task-123', {
639
+ * status: 'completed',
640
+ * });
641
+ * ```
642
+ *
643
+ * @example
644
+ * ```typescript
645
+ * // Update multiple fields
646
+ * const updated = await mitra.entities.Task.update('task-123', {
647
+ * status: 'done',
648
+ * completedAt: new Date().toISOString(),
649
+ * completedBy: currentUser.id,
650
+ * });
651
+ * ```
652
+ */
653
+ update(id: string | number, data: Partial<T>): Promise<T>;
654
+ /**
655
+ * Deletes a single record by ID.
656
+ *
657
+ * Permanently removes a record from the database. This action cannot
658
+ * be undone.
659
+ *
660
+ * @param id - The unique identifier of the record to delete.
661
+ * @returns Promise resolving when deletion is complete.
662
+ * @throws {MitraApiError} When record is not found (404).
663
+ *
664
+ * @example
665
+ * ```typescript
666
+ * await mitra.entities.Task.delete('task-123');
667
+ * console.log('Task deleted');
668
+ * ```
669
+ *
670
+ * @example
671
+ * ```typescript
672
+ * // With confirmation
673
+ * if (confirm('Delete this task?')) {
674
+ * await mitra.entities.Task.delete(task.id);
675
+ * toast.success('Task deleted');
676
+ * navigate('/tasks');
677
+ * }
678
+ * ```
679
+ */
680
+ delete(id: string | number): Promise<void>;
681
+ /**
682
+ * Deletes multiple records matching a query.
683
+ *
684
+ * Permanently removes all records that match the provided query.
685
+ * Use with caution as this action cannot be undone.
686
+ *
687
+ * @param query - Query object with field-value pairs. Records matching
688
+ * all specified criteria will be deleted.
689
+ * @returns Promise resolving to object with count of deleted records.
690
+ *
691
+ * @example
692
+ * ```typescript
693
+ * // Delete all completed tasks
694
+ * const result = await mitra.entities.Task.deleteMany({ status: 'done' });
695
+ * console.log(`Deleted ${result.deleted} tasks`);
696
+ * ```
697
+ *
698
+ * @example
699
+ * ```typescript
700
+ * // Delete by multiple criteria
701
+ * const result = await mitra.entities.Task.deleteMany({
702
+ * status: 'archived',
703
+ * createdAt: { $lt: '2024-01-01' },
704
+ * });
705
+ * ```
706
+ */
707
+ deleteMany(query: Record<string, unknown>): Promise<{
708
+ deleted: number;
709
+ }>;
710
+ /**
711
+ * Creates multiple records in a single request.
712
+ *
713
+ * Efficiently creates multiple records at once. This is faster than
714
+ * calling `create()` multiple times as it uses a single API request.
715
+ *
716
+ * @param data - Array of record data objects.
717
+ * @returns Promise resolving to an array of created records.
718
+ *
719
+ * @example
720
+ * ```typescript
721
+ * const tasks = await mitra.entities.Task.bulkCreate([
722
+ * { title: 'Task 1', status: 'pending' },
723
+ * { title: 'Task 2', status: 'pending' },
724
+ * { title: 'Task 3', status: 'pending' },
725
+ * ]);
726
+ * console.log(`Created ${tasks.length} tasks`);
727
+ * ```
728
+ *
729
+ * @example
730
+ * ```typescript
731
+ * // Import from external source
732
+ * const importedData = parseCSV(csvContent);
733
+ * const records = await mitra.entities.Product.bulkCreate(importedData);
734
+ * toast.success(`Imported ${records.length} products`);
735
+ * ```
736
+ */
737
+ bulkCreate(data: Partial<T>[]): Promise<T[]>;
738
+ }
739
+
740
+ /**
741
+ * Module for database CRUD operations.
742
+ *
743
+ * Access any table dynamically: `mitra.entities.TableName.method()`.
744
+ * Table names are case-sensitive and must match the Data Manager config.
745
+ *
746
+ * @example
747
+ * ```typescript
748
+ * // Dynamic access
749
+ * const tasks = await mitra.entities.Task.list('-created_at', 10);
750
+ * const task = await mitra.entities.Task.create({ title: 'New task' });
751
+ *
752
+ * // Typed access
753
+ * const typed = mitra.entities.getTable<Task>('Task');
754
+ * const pending = await typed.filter({ status: 'pending' });
755
+ * ```
756
+ */
757
+ declare class EntitiesModule {
758
+ private readonly httpClient;
759
+ private dataSourceId;
760
+ private readonly tableProxies;
761
+ constructor(httpClient: HttpClient, dataSourceId: string);
762
+ static createProxy(httpClient: HttpClient, dataSourceId: string): EntitiesModule;
763
+ setDataSourceId(dataSourceId: string): void;
764
+ getTable<T = Record<string, unknown>>(tableName: string): EntityTable<T>;
765
+ private createTableAccessor;
766
+ }
767
+ type EntitiesProxy = EntitiesModule & {
768
+ [tableName: string]: EntityTable;
769
+ };
770
+
771
+ /** Result of a serverless function execution. */
772
+ interface FunctionExecution {
773
+ /** Unique execution ID. */
774
+ id: string;
775
+ /** ID of the executed function. */
776
+ functionId: string;
777
+ /** ID of the function version that was executed. */
778
+ functionVersionId: string;
779
+ /** Execution status: PENDING, RUNNING, COMPLETED, or FAILED. */
780
+ status: string;
781
+ /** Input data passed to the function. */
782
+ input: Record<string, unknown>;
783
+ /** Output data returned by the function. */
784
+ output: Record<string, unknown> | null;
785
+ /** Error message if execution failed. */
786
+ errorMessage: string | null;
787
+ /** Execution logs. */
788
+ logs: string | null;
789
+ /** Duration in milliseconds. */
790
+ durationMs: number | null;
791
+ /** When execution started (ISO 8601). */
792
+ startedAt: string | null;
793
+ /** When execution finished (ISO 8601). */
794
+ finishedAt: string | null;
795
+ /** When the execution record was created (ISO 8601). */
796
+ createdAt: string;
797
+ }
798
+
799
+ /**
800
+ * Module for executing serverless functions.
801
+ *
802
+ * @example
803
+ * ```typescript
804
+ * const result = await mitra.functions.execute('function-id', { orderId: '123' });
805
+ * if (result.status === 'COMPLETED') {
806
+ * console.log(result.output);
807
+ * }
808
+ * ```
809
+ */
810
+ declare class FunctionsModule {
811
+ private readonly httpClient;
812
+ constructor(httpClient: HttpClient);
813
+ /**
814
+ * Executes a serverless function by ID.
815
+ *
816
+ * Triggers the function's current published version with the provided input.
817
+ *
818
+ * @param functionId - UUID of the function to execute.
819
+ * @param input - Input data to pass to the function.
820
+ * @returns The execution result with status, output, and metadata.
821
+ * @throws {MitraApiError} On function not found (404) or unauthorized (401).
822
+ *
823
+ * @example
824
+ * ```typescript
825
+ * const execution = await mitra.functions.execute('fn-id', { key: 'value' });
826
+ * console.log(execution.status, execution.output);
827
+ * ```
828
+ */
829
+ execute(functionId: string, input?: Record<string, unknown>): Promise<FunctionExecution>;
830
+ }
831
+
832
+ /** Input for a proxied HTTP request through an integration. */
833
+ interface ProxyInput {
834
+ /** HTTP method (GET, POST, PUT, DELETE, etc.). */
835
+ method: string;
836
+ /** API endpoint path (appended to the template's baseUrl). */
837
+ endpoint: string;
838
+ /** Additional headers. */
839
+ headers?: Record<string, string>;
840
+ /** Request body. */
841
+ body?: unknown;
842
+ /** Query parameters. */
843
+ queryParams?: Record<string, string>;
844
+ }
845
+ /** Result of a proxied HTTP request. */
846
+ interface ProxyResult {
847
+ /** HTTP status code from the external API. */
848
+ status: number;
849
+ /** Response headers. */
850
+ headers: Record<string, string>;
851
+ /** Response body. */
852
+ body: unknown;
853
+ /** Execution time in milliseconds. */
854
+ durationMs: number;
855
+ /** Unique execution record ID. */
856
+ executionId: string;
857
+ }
858
+
859
+ /**
860
+ * Module for proxying HTTP requests to external APIs.
861
+ *
862
+ * Sends requests through the Mitra server, which handles authentication
863
+ * and credential injection based on the integration config.
864
+ *
865
+ * @example
866
+ * ```typescript
867
+ * const result = await mitra.integration.executeResource('resource-id', {
868
+ * descricao: 'Notebook',
869
+ * limit: 10,
870
+ * });
871
+ * console.log(result.body);
872
+ * ```
873
+ */
874
+ declare class IntegrationModule {
875
+ private readonly httpClient;
876
+ constructor(httpClient: HttpClient);
877
+ /**
878
+ * Executes a pre-defined integration resource by ID.
879
+ *
880
+ * The resource's endpoint, method, and body are resolved server-side
881
+ * using the provided parameters. Only declared parameters can be passed.
882
+ *
883
+ * @param resourceId - UUID of the integration resource.
884
+ * @param params - Named parameters declared in the resource's params schema.
885
+ * @returns Proxy result with status, headers, body, and execution metadata.
886
+ * @throws {MitraApiError} On resource not found (404) or external API failure.
887
+ *
888
+ * @example
889
+ * ```typescript
890
+ * const result = await mitra.integration.executeResource('resource-id', {
891
+ * descricao: 'Notebook',
892
+ * limit: 10,
893
+ * });
894
+ * console.log(result.body);
895
+ * ```
896
+ */
897
+ executeResource(resourceId: string, params?: Record<string, unknown>): Promise<ProxyResult>;
898
+ /**
899
+ * Executes a proxied HTTP request through an integration config.
900
+ *
901
+ * The Mitra server handles authentication and injects credentials automatically.
902
+ * Note: integrations configured with RESOURCE_ONLY mode will block direct proxy access.
903
+ *
904
+ * @param configId - UUID of the integration config.
905
+ * @param request - The HTTP request to proxy (method, endpoint, body, etc.).
906
+ * @returns Proxy result with status, headers, body, and execution metadata.
907
+ * @throws {MitraApiError} On config not found (404) or external API failure.
908
+ */
909
+ execute(configId: string, request: ProxyInput): Promise<ProxyResult>;
910
+ }
911
+
912
+ /** Result of executing a custom query. */
913
+ interface QueryResult {
914
+ /** Array of row objects returned by the query. */
915
+ rows: Record<string, unknown>[];
916
+ /** Number of affected rows (null for SELECT). */
917
+ affectedRows: number | null;
918
+ }
919
+
920
+ /**
921
+ * Module for executing reusable named queries.
922
+ *
923
+ * @example
924
+ * ```typescript
925
+ * const result = await mitra.queries.execute('query-id', { status: 'active' });
926
+ * console.log(result.rows);
927
+ * ```
928
+ */
929
+ declare class QueriesModule {
930
+ private readonly httpClient;
931
+ private dataSourceId;
932
+ constructor(httpClient: HttpClient);
933
+ /** @internal Called by client.init() to set the resolved data source. */
934
+ setDataSourceId(dataSourceId: string): void;
935
+ /**
936
+ * Executes a named query.
937
+ *
938
+ * @param id - UUID of the custom query.
939
+ * @param parameters - Named parameters for the prepared statement.
940
+ * @returns Query result with rows and affected row count.
941
+ * @throws {MitraApiError} On query not found (404).
942
+ *
943
+ * @example
944
+ * ```typescript
945
+ * const result = await mitra.queries.execute('query-id', { status: 'active' });
946
+ * console.log(`Found ${result.rows.length} rows`);
947
+ * ```
948
+ */
949
+ execute(id: string, parameters?: Record<string, unknown>): Promise<QueryResult>;
950
+ }
951
+
952
+ /**
953
+ * Configuration options for creating a Mitra client.
954
+ */
955
+ interface MitraClientConfig {
956
+ /**
957
+ * Your app's unique identifier.
958
+ * Found in the Mitra Code Studio dashboard.
959
+ */
960
+ appId: string;
961
+ /**
962
+ * Base URL for the Mitra API (Kong Gateway).
963
+ * Injected automatically via `VITE_MITRA_API_URL` environment variable
964
+ * during the Code Studio build process.
965
+ *
966
+ * @example
967
+ * ```typescript
968
+ * apiUrl: import.meta.env.VITE_MITRA_API_URL
969
+ * ```
970
+ */
971
+ apiUrl: string;
972
+ /**
973
+ * Global error handler called whenever an API request fails.
974
+ * Useful for displaying toast notifications or logging errors.
975
+ *
976
+ * @example
977
+ * ```typescript
978
+ * const mitra = createClient({
979
+ * appId: 'your-app-id',
980
+ * onError: (error) => toast.error(error.message),
981
+ * });
982
+ * ```
983
+ */
984
+ onError?: (error: MitraApiError) => void;
985
+ }
986
+ /**
987
+ * The Mitra client instance providing access to all SDK modules.
988
+ *
989
+ * @example
990
+ * ```typescript
991
+ * const mitra = createClient({
992
+ * appId: 'your-app-id',
993
+ * });
994
+ *
995
+ * // Initialize (resolves app config automatically)
996
+ * await mitra.init();
997
+ *
998
+ * // Authentication
999
+ * await mitra.auth.signIn({ email, password });
1000
+ *
1001
+ * // Database operations
1002
+ * const tasks = await mitra.entities.Task.list();
1003
+ *
1004
+ * // Serverless functions
1005
+ * const execution = await mitra.functions.execute('function-id', { orderId });
1006
+ * ```
1007
+ */
1008
+ interface MitraClient {
1009
+ /**
1010
+ * Initializes the client by resolving app config from the server.
1011
+ *
1012
+ * Must be called before using `auth.signUp()` or `entities`.
1013
+ * Fetches dataSourceId and allowSignup from the public app info endpoint.
1014
+ *
1015
+ * Safe to call multiple times — subsequent calls are no-ops.
1016
+ *
1017
+ * @example
1018
+ * ```typescript
1019
+ * const mitra = createClient({ appId: 'your-app-id' });
1020
+ * await mitra.init();
1021
+ * ```
1022
+ */
1023
+ init(): Promise<void>;
1024
+ /**
1025
+ * Authentication module for managing user sessions.
1026
+ *
1027
+ * Handles user registration, login, logout, and session persistence.
1028
+ *
1029
+ * @example
1030
+ * ```typescript
1031
+ * await mitra.auth.signIn({ email: 'user@example.com', password: 'password' });
1032
+ * console.log(mitra.auth.currentUser);
1033
+ * ```
1034
+ */
1035
+ auth: AuthModule;
1036
+ /**
1037
+ * Entities module for database CRUD operations.
1038
+ *
1039
+ * Access any table dynamically using `mitra.entities.TableName`.
1040
+ *
1041
+ * @example
1042
+ * ```typescript
1043
+ * const tasks = await mitra.entities.Task.list('-created_at', 10);
1044
+ * const task = await mitra.entities.Task.create({ title: 'New task' });
1045
+ * ```
1046
+ */
1047
+ entities: EntitiesProxy;
1048
+ /**
1049
+ * Functions module for executing serverless functions.
1050
+ *
1051
+ * @example
1052
+ * ```typescript
1053
+ * const execution = await mitra.functions.execute('function-id', { orderId });
1054
+ * console.log(execution.status, execution.output);
1055
+ * ```
1056
+ */
1057
+ functions: FunctionsModule;
1058
+ /**
1059
+ * Integration module for proxying HTTP requests to external APIs.
1060
+ *
1061
+ * Sends requests through the Mitra server, which handles authentication
1062
+ * and credential injection automatically based on the template config.
1063
+ *
1064
+ * @example
1065
+ * ```typescript
1066
+ * const result = await mitra.integration.execute('config-id', {
1067
+ * method: 'GET',
1068
+ * endpoint: '/users',
1069
+ * });
1070
+ * console.log(result.body);
1071
+ * ```
1072
+ */
1073
+ integration: IntegrationModule;
1074
+ /**
1075
+ * Queries module for executing reusable named SELECT queries.
1076
+ *
1077
+ * @example
1078
+ * ```typescript
1079
+ * const result = await mitra.queries.execute('query-id', { status: 'active' });
1080
+ * console.log(result.rows);
1081
+ * ```
1082
+ */
1083
+ queries: QueriesModule;
1084
+ /**
1085
+ * Whether this app allows public user registration.
1086
+ * Defaults to `true` before `init()` is called.
1087
+ */
1088
+ readonly allowSignup: boolean;
1089
+ /**
1090
+ * The configuration used to create this client.
1091
+ */
1092
+ config: MitraClientConfig;
1093
+ }
1094
+ /**
1095
+ * Creates a new Mitra client instance.
1096
+ *
1097
+ * The client provides access to all Mitra Platform features:
1098
+ * - **auth**: User authentication and session management
1099
+ * - **entities**: Database CRUD operations
1100
+ * - **functions**: Serverless function invocation
1101
+ * - **integration**: Proxy HTTP requests to external APIs
1102
+ * - **queries**: Custom query management and execution
1103
+ *
1104
+ * After creating the client, call `init()` to resolve the app's config
1105
+ * (dataSourceId, allowSignup) automatically from the server.
1106
+ *
1107
+ * @param config - Configuration options for the client.
1108
+ * @returns A configured MitraClient instance.
1109
+ *
1110
+ * @example
1111
+ * ```typescript
1112
+ * import { createClient } from 'mitra-platform-sdk';
1113
+ *
1114
+ * const mitra = createClient({
1115
+ * appId: import.meta.env.VITE_MITRA_APP_ID,
1116
+ * apiUrl: import.meta.env.VITE_MITRA_API_URL,
1117
+ * });
1118
+ *
1119
+ * await mitra.init();
1120
+ *
1121
+ * // Use the client
1122
+ * await mitra.auth.signIn({ email, password });
1123
+ * const tasks = await mitra.entities.Task.list();
1124
+ * ```
1125
+ *
1126
+ * @example
1127
+ * ```typescript
1128
+ * // Export as singleton for use throughout your app
1129
+ * // src/api/mitraClient.ts
1130
+ * import { createClient } from 'mitra-platform-sdk';
1131
+ *
1132
+ * export const mitra = createClient({
1133
+ * appId: import.meta.env.VITE_MITRA_APP_ID,
1134
+ * apiUrl: import.meta.env.VITE_MITRA_API_URL,
1135
+ * });
1136
+ * ```
1137
+ */
1138
+ declare function createClient(config: MitraClientConfig): MitraClient;
1139
+
1140
+ export { type EntityListOptions, type EntityTable, type FunctionExecution, MitraApiError, type MitraClient, type MitraClientConfig, type ProxyInput, type ProxyResult, type QueryResult, type SignInCredentials, type SignUpData, type User, createClient };