nexus-platform-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +329 -0
- package/dist/index.d.mts +760 -0
- package/dist/index.d.ts +760 -0
- package/dist/index.js +488 -0
- package/dist/index.mjs +460 -0
- package/package.json +56 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,760 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for creating an HttpClient instance.
|
|
3
|
+
*/
|
|
4
|
+
interface HttpClientConfig {
|
|
5
|
+
/** Base URL for all HTTP requests (e.g., 'https://api.nexus.io') */
|
|
6
|
+
baseUrl: string;
|
|
7
|
+
/** Function that returns the current authentication token, or null if not authenticated */
|
|
8
|
+
getToken?: () => string | null;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Options for making HTTP requests.
|
|
12
|
+
*/
|
|
13
|
+
interface RequestOptions {
|
|
14
|
+
/** HTTP method (defaults to 'GET') */
|
|
15
|
+
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH';
|
|
16
|
+
/** Request body (will be JSON stringified) */
|
|
17
|
+
body?: unknown;
|
|
18
|
+
/** Additional headers to include in the request */
|
|
19
|
+
headers?: Record<string, string>;
|
|
20
|
+
/** URL query parameters */
|
|
21
|
+
params?: Record<string, string | number | boolean | undefined>;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* HTTP client for making authenticated API requests.
|
|
25
|
+
*
|
|
26
|
+
* Handles JSON serialization, authentication headers, and error handling.
|
|
27
|
+
* All requests automatically include the Authorization header when a token is available.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* const client = new HttpClient({
|
|
32
|
+
* baseUrl: 'https://api.nexus.io',
|
|
33
|
+
* getToken: () => localStorage.getItem('token'),
|
|
34
|
+
* });
|
|
35
|
+
*
|
|
36
|
+
* const users = await client.get<User[]>('/users');
|
|
37
|
+
* const user = await client.post<User>('/users', { name: 'John' });
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
declare class HttpClient {
|
|
41
|
+
private baseUrl;
|
|
42
|
+
private tokenGetter;
|
|
43
|
+
constructor(config: HttpClientConfig);
|
|
44
|
+
/**
|
|
45
|
+
* Returns the current authentication token.
|
|
46
|
+
* @returns The JWT token if authenticated, null otherwise
|
|
47
|
+
*/
|
|
48
|
+
getToken(): string | null;
|
|
49
|
+
/**
|
|
50
|
+
* Makes an HTTP request with automatic JSON handling and authentication.
|
|
51
|
+
*
|
|
52
|
+
* @param path - API endpoint path (e.g., '/users')
|
|
53
|
+
* @param options - Request options including method, body, headers, and params
|
|
54
|
+
* @returns Promise resolving to the parsed JSON response
|
|
55
|
+
* @throws {NexusApiError} When the API returns an error response
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* const result = await client.request<User>('/users/123', {
|
|
60
|
+
* method: 'PUT',
|
|
61
|
+
* body: { name: 'Updated Name' },
|
|
62
|
+
* });
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
request<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
66
|
+
/**
|
|
67
|
+
* Makes a GET request.
|
|
68
|
+
*
|
|
69
|
+
* @param path - API endpoint path
|
|
70
|
+
* @param params - Optional query parameters
|
|
71
|
+
* @returns Promise resolving to the parsed JSON response
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* const users = await client.get<User[]>('/users', { limit: 10 });
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
get<T>(path: string, params?: Record<string, string | number | boolean | undefined>): Promise<T>;
|
|
79
|
+
/**
|
|
80
|
+
* Makes a POST request.
|
|
81
|
+
*
|
|
82
|
+
* @param path - API endpoint path
|
|
83
|
+
* @param body - Request body (will be JSON stringified)
|
|
84
|
+
* @returns Promise resolving to the parsed JSON response
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* const user = await client.post<User>('/users', { name: 'John', email: 'john@example.com' });
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
post<T>(path: string, body?: unknown): Promise<T>;
|
|
92
|
+
/**
|
|
93
|
+
* Makes a PUT 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.put<User>('/users/123', { name: 'Updated Name' });
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
put<T>(path: string, body?: unknown): Promise<T>;
|
|
105
|
+
/**
|
|
106
|
+
* Makes a DELETE request.
|
|
107
|
+
*
|
|
108
|
+
* @param path - API endpoint path
|
|
109
|
+
* @param params - Optional query parameters
|
|
110
|
+
* @returns Promise resolving to the parsed JSON response (or undefined for 204 responses)
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* await client.delete('/users/123');
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
117
|
+
delete<T>(path: string, params?: Record<string, string | number | boolean | undefined>): Promise<T>;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Error thrown when a Nexus API request fails.
|
|
121
|
+
*
|
|
122
|
+
* Contains detailed information about the error including HTTP status,
|
|
123
|
+
* error code, and additional details from the server response.
|
|
124
|
+
*
|
|
125
|
+
* @example
|
|
126
|
+
* ```typescript
|
|
127
|
+
* try {
|
|
128
|
+
* await nexus.entities.Task.get('invalid-id');
|
|
129
|
+
* } catch (error) {
|
|
130
|
+
* if (error instanceof NexusApiError) {
|
|
131
|
+
* console.log(error.status); // 404
|
|
132
|
+
* console.log(error.message); // "Task not found"
|
|
133
|
+
* console.log(error.code); // "ENTITY_NOT_FOUND"
|
|
134
|
+
* }
|
|
135
|
+
* }
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
declare class NexusApiError extends Error {
|
|
139
|
+
/** HTTP status code (e.g., 400, 401, 404, 500) */
|
|
140
|
+
readonly status: number;
|
|
141
|
+
/** Application-specific error code (e.g., 'ENTITY_NOT_FOUND', 'VALIDATION_ERROR') */
|
|
142
|
+
readonly code?: string | undefined;
|
|
143
|
+
/** Additional error details from the server response */
|
|
144
|
+
readonly details?: unknown | undefined;
|
|
145
|
+
constructor(message: string,
|
|
146
|
+
/** HTTP status code (e.g., 400, 401, 404, 500) */
|
|
147
|
+
status: number,
|
|
148
|
+
/** Application-specific error code (e.g., 'ENTITY_NOT_FOUND', 'VALIDATION_ERROR') */
|
|
149
|
+
code?: string | undefined,
|
|
150
|
+
/** Additional error details from the server response */
|
|
151
|
+
details?: unknown | undefined);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Represents an authenticated user in the Nexus Platform.
|
|
156
|
+
*/
|
|
157
|
+
interface User {
|
|
158
|
+
/** Unique identifier for the user. */
|
|
159
|
+
id: string;
|
|
160
|
+
/** Tenant ID the user belongs to. */
|
|
161
|
+
tenantId: string;
|
|
162
|
+
/** User's email address. */
|
|
163
|
+
email: string;
|
|
164
|
+
/** User's display name (optional). */
|
|
165
|
+
name: string | null;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Credentials required for user sign-in.
|
|
169
|
+
*/
|
|
170
|
+
interface SignInCredentials {
|
|
171
|
+
/** User's email address. */
|
|
172
|
+
email: string;
|
|
173
|
+
/** User's password. */
|
|
174
|
+
password: string;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Data required for user registration.
|
|
178
|
+
*/
|
|
179
|
+
interface SignUpData {
|
|
180
|
+
/** User's email address. */
|
|
181
|
+
email: string;
|
|
182
|
+
/** User's password (minimum 6 characters recommended). */
|
|
183
|
+
password: string;
|
|
184
|
+
/** User's display name (optional). */
|
|
185
|
+
name?: string;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Callback function type for auth state change listeners.
|
|
189
|
+
*/
|
|
190
|
+
interface AuthStateChangeCallback {
|
|
191
|
+
(user: User | null): void;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* @see {@link AuthModuleInterface} in auth.types.ts for full documentation.
|
|
196
|
+
*/
|
|
197
|
+
declare class AuthModule {
|
|
198
|
+
private tenantId;
|
|
199
|
+
private iamBaseUrl;
|
|
200
|
+
private _currentUser;
|
|
201
|
+
private _accessToken;
|
|
202
|
+
private listeners;
|
|
203
|
+
private storageKey;
|
|
204
|
+
constructor(_httpClient: HttpClient, tenantId: string, iamBaseUrl: string);
|
|
205
|
+
get currentUser(): User | null;
|
|
206
|
+
get accessToken(): string | null;
|
|
207
|
+
get isAuthenticated(): boolean;
|
|
208
|
+
signIn(credentials: SignInCredentials): Promise<User>;
|
|
209
|
+
signUp(data: SignUpData): Promise<User>;
|
|
210
|
+
signOut(redirectUrl?: string): void;
|
|
211
|
+
logout(redirectUrl?: string): void;
|
|
212
|
+
me(): Promise<User | null>;
|
|
213
|
+
refreshUser(): Promise<User | null>;
|
|
214
|
+
checkAuth(): Promise<boolean>;
|
|
215
|
+
setToken(token: string, saveToStorage?: boolean): void;
|
|
216
|
+
redirectToLogin(returnUrl?: string): void;
|
|
217
|
+
onAuthStateChange(callback: AuthStateChangeCallback): () => void;
|
|
218
|
+
private setAuthState;
|
|
219
|
+
private clearAuthState;
|
|
220
|
+
private notifyListeners;
|
|
221
|
+
private saveToStorage;
|
|
222
|
+
private loadFromStorage;
|
|
223
|
+
private removeFromStorage;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Options for listing entities with sorting and pagination.
|
|
228
|
+
*/
|
|
229
|
+
interface EntityListOptions {
|
|
230
|
+
/**
|
|
231
|
+
* Field to sort by. Prefix with '-' for descending order.
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* ```typescript
|
|
235
|
+
* // Sort by created_at descending (newest first)
|
|
236
|
+
* { sort: '-created_at' }
|
|
237
|
+
*
|
|
238
|
+
* // Sort by name ascending (A-Z)
|
|
239
|
+
* { sort: 'name' }
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
sort?: string;
|
|
243
|
+
/**
|
|
244
|
+
* Maximum number of records to return.
|
|
245
|
+
* Defaults to 100. Maximum allowed is 1000.
|
|
246
|
+
*/
|
|
247
|
+
limit?: number;
|
|
248
|
+
/**
|
|
249
|
+
* Number of records to skip for pagination.
|
|
250
|
+
* Use with `limit` to implement pagination.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```typescript
|
|
254
|
+
* // Get page 2 (records 11-20)
|
|
255
|
+
* { limit: 10, skip: 10 }
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
skip?: number;
|
|
259
|
+
/**
|
|
260
|
+
* Array of field names to include in the response.
|
|
261
|
+
* If not specified, all fields are returned.
|
|
262
|
+
*
|
|
263
|
+
* @example
|
|
264
|
+
* ```typescript
|
|
265
|
+
* // Only return id, title, and status
|
|
266
|
+
* { fields: ['id', 'title', 'status'] }
|
|
267
|
+
* ```
|
|
268
|
+
*/
|
|
269
|
+
fields?: string[];
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Entity handler providing CRUD operations for a specific entity type.
|
|
273
|
+
*
|
|
274
|
+
* Each table in the database gets a handler with these methods for managing data.
|
|
275
|
+
* Access tables dynamically using `nexus.entities.TableName`.
|
|
276
|
+
*
|
|
277
|
+
* @typeParam T - The shape of records in this table. Defaults to `Record<string, unknown>`.
|
|
278
|
+
*
|
|
279
|
+
* @example
|
|
280
|
+
* ```typescript
|
|
281
|
+
* // Dynamic access (no type safety)
|
|
282
|
+
* const tasks = await nexus.entities.Task.list();
|
|
283
|
+
*
|
|
284
|
+
* // Typed access
|
|
285
|
+
* interface Task {
|
|
286
|
+
* id: string;
|
|
287
|
+
* title: string;
|
|
288
|
+
* status: 'pending' | 'done';
|
|
289
|
+
* }
|
|
290
|
+
* const tasks = nexus.entities.getTable<Task>('Task');
|
|
291
|
+
* const pending = await tasks.filter({ status: 'pending' });
|
|
292
|
+
* ```
|
|
293
|
+
*/
|
|
294
|
+
interface EntityTable<T = Record<string, unknown>> {
|
|
295
|
+
/**
|
|
296
|
+
* Lists records with optional pagination and sorting.
|
|
297
|
+
*
|
|
298
|
+
* Retrieves all records from the table with support for sorting,
|
|
299
|
+
* pagination, and field selection. Supports both positional parameters
|
|
300
|
+
* (for quick usage) and options object (for clarity).
|
|
301
|
+
*
|
|
302
|
+
* @param sortOrOptions - Sort field (e.g., '-created_at') or options object.
|
|
303
|
+
* @param limit - Maximum number of results to return. Defaults to 100.
|
|
304
|
+
* @param skip - Number of results to skip for pagination. Defaults to 0.
|
|
305
|
+
* @param fields - Array of field names to include in the response.
|
|
306
|
+
* @returns Promise resolving to an array of records.
|
|
307
|
+
*
|
|
308
|
+
* @example
|
|
309
|
+
* ```typescript
|
|
310
|
+
* // Get all records
|
|
311
|
+
* const tasks = await nexus.entities.Task.list();
|
|
312
|
+
* ```
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* ```typescript
|
|
316
|
+
* // Get first 10 records sorted by date (newest first)
|
|
317
|
+
* const tasks = await nexus.entities.Task.list('-created_at', 10);
|
|
318
|
+
* ```
|
|
319
|
+
*
|
|
320
|
+
* @example
|
|
321
|
+
* ```typescript
|
|
322
|
+
* // Get paginated results (page 3, 10 items per page)
|
|
323
|
+
* const tasks = await nexus.entities.Task.list('-created_at', 10, 20);
|
|
324
|
+
* ```
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```typescript
|
|
328
|
+
* // Using options object
|
|
329
|
+
* const tasks = await nexus.entities.Task.list({
|
|
330
|
+
* sort: '-created_at',
|
|
331
|
+
* limit: 10,
|
|
332
|
+
* skip: 0,
|
|
333
|
+
* fields: ['id', 'title', 'status'],
|
|
334
|
+
* });
|
|
335
|
+
* ```
|
|
336
|
+
*/
|
|
337
|
+
list(sortOrOptions?: string | EntityListOptions, limit?: number, skip?: number, fields?: string[]): Promise<T[]>;
|
|
338
|
+
/**
|
|
339
|
+
* Filters records based on a query.
|
|
340
|
+
*
|
|
341
|
+
* Retrieves records that match specific criteria with support for
|
|
342
|
+
* sorting, pagination, and field selection. All query conditions
|
|
343
|
+
* are combined with AND logic.
|
|
344
|
+
*
|
|
345
|
+
* @param query - Query object with field-value pairs. Records matching
|
|
346
|
+
* all specified criteria are returned. Field names are case-sensitive.
|
|
347
|
+
* @param sort - Sort field (prefix '-' for descending). Defaults to '-created_at'.
|
|
348
|
+
* @param limit - Maximum number of results to return. Defaults to 100.
|
|
349
|
+
* @param skip - Number of results to skip for pagination. Defaults to 0.
|
|
350
|
+
* @param fields - Array of field names to include in the response.
|
|
351
|
+
* @returns Promise resolving to an array of matching records.
|
|
352
|
+
*
|
|
353
|
+
* @example
|
|
354
|
+
* ```typescript
|
|
355
|
+
* // Filter by single field
|
|
356
|
+
* const doneTasks = await nexus.entities.Task.filter({ status: 'done' });
|
|
357
|
+
* ```
|
|
358
|
+
*
|
|
359
|
+
* @example
|
|
360
|
+
* ```typescript
|
|
361
|
+
* // Filter by multiple fields (AND logic)
|
|
362
|
+
* const urgentTasks = await nexus.entities.Task.filter({
|
|
363
|
+
* status: 'pending',
|
|
364
|
+
* priority: 'high',
|
|
365
|
+
* });
|
|
366
|
+
* ```
|
|
367
|
+
*
|
|
368
|
+
* @example
|
|
369
|
+
* ```typescript
|
|
370
|
+
* // Filter with sorting and pagination
|
|
371
|
+
* const tasks = await nexus.entities.Task.filter(
|
|
372
|
+
* { status: 'pending' },
|
|
373
|
+
* '-priority', // sort by priority descending
|
|
374
|
+
* 10, // limit
|
|
375
|
+
* 0 // skip
|
|
376
|
+
* );
|
|
377
|
+
* ```
|
|
378
|
+
*
|
|
379
|
+
* @example
|
|
380
|
+
* ```typescript
|
|
381
|
+
* // Filter with specific fields
|
|
382
|
+
* const tasks = await nexus.entities.Task.filter(
|
|
383
|
+
* { assignee: 'user-123' },
|
|
384
|
+
* '-created_at',
|
|
385
|
+
* 20,
|
|
386
|
+
* 0,
|
|
387
|
+
* ['id', 'title', 'status']
|
|
388
|
+
* );
|
|
389
|
+
* ```
|
|
390
|
+
*/
|
|
391
|
+
filter(query: Record<string, unknown>, sort?: string, limit?: number, skip?: number, fields?: string[]): Promise<T[]>;
|
|
392
|
+
/**
|
|
393
|
+
* Gets a single record by ID.
|
|
394
|
+
*
|
|
395
|
+
* Retrieves a specific record using its unique identifier.
|
|
396
|
+
*
|
|
397
|
+
* @param id - The unique identifier of the record.
|
|
398
|
+
* @returns Promise resolving to the record.
|
|
399
|
+
* @throws {NexusApiError} When record is not found (404).
|
|
400
|
+
*
|
|
401
|
+
* @example
|
|
402
|
+
* ```typescript
|
|
403
|
+
* const task = await nexus.entities.Task.get('task-123');
|
|
404
|
+
* console.log(task.title);
|
|
405
|
+
* ```
|
|
406
|
+
*
|
|
407
|
+
* @example
|
|
408
|
+
* ```typescript
|
|
409
|
+
* // With error handling
|
|
410
|
+
* try {
|
|
411
|
+
* const task = await nexus.entities.Task.get(taskId);
|
|
412
|
+
* setTask(task);
|
|
413
|
+
* } catch (error) {
|
|
414
|
+
* if (error.status === 404) {
|
|
415
|
+
* console.error('Task not found');
|
|
416
|
+
* }
|
|
417
|
+
* }
|
|
418
|
+
* ```
|
|
419
|
+
*/
|
|
420
|
+
get(id: string | number): Promise<T>;
|
|
421
|
+
/**
|
|
422
|
+
* Creates a new record.
|
|
423
|
+
*
|
|
424
|
+
* Creates a new record in the table with the provided data.
|
|
425
|
+
* The server will generate an `id` and timestamps automatically.
|
|
426
|
+
*
|
|
427
|
+
* @param data - Object containing the record data.
|
|
428
|
+
* @returns Promise resolving to the created record (including generated id).
|
|
429
|
+
* @throws {NexusApiError} When validation fails (400).
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* ```typescript
|
|
433
|
+
* const task = await nexus.entities.Task.create({
|
|
434
|
+
* title: 'Complete documentation',
|
|
435
|
+
* status: 'pending',
|
|
436
|
+
* priority: 'high',
|
|
437
|
+
* });
|
|
438
|
+
* console.log('Created task:', task.id);
|
|
439
|
+
* ```
|
|
440
|
+
*
|
|
441
|
+
* @example
|
|
442
|
+
* ```typescript
|
|
443
|
+
* // With error handling
|
|
444
|
+
* try {
|
|
445
|
+
* const task = await nexus.entities.Task.create(formData);
|
|
446
|
+
* toast.success('Task created!');
|
|
447
|
+
* navigate(`/tasks/${task.id}`);
|
|
448
|
+
* } catch (error) {
|
|
449
|
+
* toast.error(error.message);
|
|
450
|
+
* }
|
|
451
|
+
* ```
|
|
452
|
+
*/
|
|
453
|
+
create(data: Partial<T>): Promise<T>;
|
|
454
|
+
/**
|
|
455
|
+
* Updates an existing record.
|
|
456
|
+
*
|
|
457
|
+
* Updates a record by ID with the provided data. Only the fields
|
|
458
|
+
* included in the data object will be updated; other fields remain
|
|
459
|
+
* unchanged (partial update).
|
|
460
|
+
*
|
|
461
|
+
* @param id - The unique identifier of the record to update.
|
|
462
|
+
* @param data - Object containing the fields to update.
|
|
463
|
+
* @returns Promise resolving to the updated record.
|
|
464
|
+
* @throws {NexusApiError} When record is not found (404).
|
|
465
|
+
*
|
|
466
|
+
* @example
|
|
467
|
+
* ```typescript
|
|
468
|
+
* // Update single field
|
|
469
|
+
* const updated = await nexus.entities.Task.update('task-123', {
|
|
470
|
+
* status: 'completed',
|
|
471
|
+
* });
|
|
472
|
+
* ```
|
|
473
|
+
*
|
|
474
|
+
* @example
|
|
475
|
+
* ```typescript
|
|
476
|
+
* // Update multiple fields
|
|
477
|
+
* const updated = await nexus.entities.Task.update('task-123', {
|
|
478
|
+
* status: 'done',
|
|
479
|
+
* completedAt: new Date().toISOString(),
|
|
480
|
+
* completedBy: currentUser.id,
|
|
481
|
+
* });
|
|
482
|
+
* ```
|
|
483
|
+
*/
|
|
484
|
+
update(id: string | number, data: Partial<T>): Promise<T>;
|
|
485
|
+
/**
|
|
486
|
+
* Deletes a single record by ID.
|
|
487
|
+
*
|
|
488
|
+
* Permanently removes a record from the database. This action cannot
|
|
489
|
+
* be undone.
|
|
490
|
+
*
|
|
491
|
+
* @param id - The unique identifier of the record to delete.
|
|
492
|
+
* @returns Promise resolving when deletion is complete.
|
|
493
|
+
* @throws {NexusApiError} When record is not found (404).
|
|
494
|
+
*
|
|
495
|
+
* @example
|
|
496
|
+
* ```typescript
|
|
497
|
+
* await nexus.entities.Task.delete('task-123');
|
|
498
|
+
* console.log('Task deleted');
|
|
499
|
+
* ```
|
|
500
|
+
*
|
|
501
|
+
* @example
|
|
502
|
+
* ```typescript
|
|
503
|
+
* // With confirmation
|
|
504
|
+
* if (confirm('Delete this task?')) {
|
|
505
|
+
* await nexus.entities.Task.delete(task.id);
|
|
506
|
+
* toast.success('Task deleted');
|
|
507
|
+
* navigate('/tasks');
|
|
508
|
+
* }
|
|
509
|
+
* ```
|
|
510
|
+
*/
|
|
511
|
+
delete(id: string | number): Promise<void>;
|
|
512
|
+
/**
|
|
513
|
+
* Deletes multiple records matching a query.
|
|
514
|
+
*
|
|
515
|
+
* Permanently removes all records that match the provided query.
|
|
516
|
+
* Use with caution as this action cannot be undone.
|
|
517
|
+
*
|
|
518
|
+
* @param query - Query object with field-value pairs. Records matching
|
|
519
|
+
* all specified criteria will be deleted.
|
|
520
|
+
* @returns Promise resolving to object with count of deleted records.
|
|
521
|
+
*
|
|
522
|
+
* @example
|
|
523
|
+
* ```typescript
|
|
524
|
+
* // Delete all completed tasks
|
|
525
|
+
* const result = await nexus.entities.Task.deleteMany({ status: 'done' });
|
|
526
|
+
* console.log(`Deleted ${result.deleted} tasks`);
|
|
527
|
+
* ```
|
|
528
|
+
*
|
|
529
|
+
* @example
|
|
530
|
+
* ```typescript
|
|
531
|
+
* // Delete by multiple criteria
|
|
532
|
+
* const result = await nexus.entities.Task.deleteMany({
|
|
533
|
+
* status: 'archived',
|
|
534
|
+
* createdAt: { $lt: '2024-01-01' },
|
|
535
|
+
* });
|
|
536
|
+
* ```
|
|
537
|
+
*/
|
|
538
|
+
deleteMany(query: Record<string, unknown>): Promise<{
|
|
539
|
+
deleted: number;
|
|
540
|
+
}>;
|
|
541
|
+
/**
|
|
542
|
+
* Creates multiple records in a single request.
|
|
543
|
+
*
|
|
544
|
+
* Efficiently creates multiple records at once. This is faster than
|
|
545
|
+
* calling `create()` multiple times as it uses a single API request.
|
|
546
|
+
*
|
|
547
|
+
* @param data - Array of record data objects.
|
|
548
|
+
* @returns Promise resolving to an array of created records.
|
|
549
|
+
*
|
|
550
|
+
* @example
|
|
551
|
+
* ```typescript
|
|
552
|
+
* const tasks = await nexus.entities.Task.bulkCreate([
|
|
553
|
+
* { title: 'Task 1', status: 'pending' },
|
|
554
|
+
* { title: 'Task 2', status: 'pending' },
|
|
555
|
+
* { title: 'Task 3', status: 'pending' },
|
|
556
|
+
* ]);
|
|
557
|
+
* console.log(`Created ${tasks.length} tasks`);
|
|
558
|
+
* ```
|
|
559
|
+
*
|
|
560
|
+
* @example
|
|
561
|
+
* ```typescript
|
|
562
|
+
* // Import from external source
|
|
563
|
+
* const importedData = parseCSV(csvContent);
|
|
564
|
+
* const records = await nexus.entities.Product.bulkCreate(importedData);
|
|
565
|
+
* toast.success(`Imported ${records.length} products`);
|
|
566
|
+
* ```
|
|
567
|
+
*/
|
|
568
|
+
bulkCreate(data: Partial<T>[]): Promise<T[]>;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
/**
|
|
572
|
+
* @see {@link EntitiesModuleInterface} in entities.types.ts for full documentation.
|
|
573
|
+
*/
|
|
574
|
+
declare class EntitiesModule {
|
|
575
|
+
private httpClient;
|
|
576
|
+
private dataSourceId;
|
|
577
|
+
private tableProxies;
|
|
578
|
+
constructor(httpClient: HttpClient, dataSourceId: string);
|
|
579
|
+
getTable<T = Record<string, unknown>>(tableName: string): EntityTable<T>;
|
|
580
|
+
private createTableAccessor;
|
|
581
|
+
}
|
|
582
|
+
type EntitiesProxy = EntitiesModule & {
|
|
583
|
+
[tableName: string]: EntityTable;
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
/**
|
|
587
|
+
* Result from a function execution.
|
|
588
|
+
*
|
|
589
|
+
* @typeParam T - The type of the data returned by the function.
|
|
590
|
+
* @internal
|
|
591
|
+
*/
|
|
592
|
+
interface FunctionResult<T = unknown> {
|
|
593
|
+
/** Whether the function executed successfully. */
|
|
594
|
+
success: boolean;
|
|
595
|
+
/** The data returned by the function (if successful). */
|
|
596
|
+
data?: T;
|
|
597
|
+
/** Execution time in milliseconds. */
|
|
598
|
+
executionTime?: number;
|
|
599
|
+
}
|
|
600
|
+
/**
|
|
601
|
+
* Information about an available function.
|
|
602
|
+
*/
|
|
603
|
+
interface FunctionInfo {
|
|
604
|
+
/** The function name used to invoke it. */
|
|
605
|
+
name: string;
|
|
606
|
+
/** Optional description of what the function does. */
|
|
607
|
+
description?: string;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* @see {@link FunctionsModuleInterface} in functions.types.ts for full documentation.
|
|
612
|
+
*/
|
|
613
|
+
declare class FunctionsModule {
|
|
614
|
+
private httpClient;
|
|
615
|
+
private appId;
|
|
616
|
+
private functionsBaseUrl;
|
|
617
|
+
constructor(httpClient: HttpClient, appId: string, functionsBaseUrl: string);
|
|
618
|
+
invoke<TInput = unknown, TOutput = unknown>(functionName: string, input?: TInput): Promise<TOutput>;
|
|
619
|
+
list(): Promise<FunctionInfo[]>;
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* Configuration options for creating a Nexus client.
|
|
624
|
+
*/
|
|
625
|
+
interface NexusClientConfig {
|
|
626
|
+
/**
|
|
627
|
+
* Your application's unique identifier.
|
|
628
|
+
* Found in the Nexus Code Studio app settings.
|
|
629
|
+
*/
|
|
630
|
+
appId: string;
|
|
631
|
+
/**
|
|
632
|
+
* Your tenant's unique identifier.
|
|
633
|
+
* Represents your organization in the Nexus Platform.
|
|
634
|
+
*/
|
|
635
|
+
tenantId: string;
|
|
636
|
+
/**
|
|
637
|
+
* The data source ID for your app's database.
|
|
638
|
+
* Found in the Nexus Data Manager settings.
|
|
639
|
+
*/
|
|
640
|
+
dataSourceId: string;
|
|
641
|
+
/**
|
|
642
|
+
* Base URL for the Nexus API.
|
|
643
|
+
* Defaults to 'http://localhost:8080' for local development.
|
|
644
|
+
*
|
|
645
|
+
* @example
|
|
646
|
+
* ```typescript
|
|
647
|
+
* // Local development
|
|
648
|
+
* apiUrl: 'http://localhost:8080'
|
|
649
|
+
*
|
|
650
|
+
* // Production
|
|
651
|
+
* apiUrl: 'https://api.nexus.io'
|
|
652
|
+
* ```
|
|
653
|
+
*/
|
|
654
|
+
apiUrl?: string;
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* The Nexus client instance providing access to all SDK modules.
|
|
658
|
+
*
|
|
659
|
+
* @example
|
|
660
|
+
* ```typescript
|
|
661
|
+
* const nexus = createClient({
|
|
662
|
+
* appId: 'your-app-id',
|
|
663
|
+
* tenantId: 'your-tenant-id',
|
|
664
|
+
* dataSourceId: 'your-datasource-id',
|
|
665
|
+
* });
|
|
666
|
+
*
|
|
667
|
+
* // Authentication
|
|
668
|
+
* await nexus.auth.signIn({ email, password });
|
|
669
|
+
*
|
|
670
|
+
* // Database operations
|
|
671
|
+
* const tasks = await nexus.entities.Task.list();
|
|
672
|
+
*
|
|
673
|
+
* // Serverless functions
|
|
674
|
+
* await nexus.functions.invoke('sendEmail', { to, subject, body });
|
|
675
|
+
* ```
|
|
676
|
+
*/
|
|
677
|
+
interface NexusClient {
|
|
678
|
+
/**
|
|
679
|
+
* Authentication module for managing user sessions.
|
|
680
|
+
*
|
|
681
|
+
* Handles user registration, login, logout, and session persistence.
|
|
682
|
+
*
|
|
683
|
+
* @example
|
|
684
|
+
* ```typescript
|
|
685
|
+
* await nexus.auth.signIn({ email: 'user@example.com', password: 'password' });
|
|
686
|
+
* console.log(nexus.auth.currentUser);
|
|
687
|
+
* ```
|
|
688
|
+
*/
|
|
689
|
+
auth: AuthModule;
|
|
690
|
+
/**
|
|
691
|
+
* Entities module for database CRUD operations.
|
|
692
|
+
*
|
|
693
|
+
* Access any table dynamically using `nexus.entities.TableName`.
|
|
694
|
+
*
|
|
695
|
+
* @example
|
|
696
|
+
* ```typescript
|
|
697
|
+
* const tasks = await nexus.entities.Task.list('-created_at', 10);
|
|
698
|
+
* const task = await nexus.entities.Task.create({ title: 'New task' });
|
|
699
|
+
* ```
|
|
700
|
+
*/
|
|
701
|
+
entities: EntitiesProxy;
|
|
702
|
+
/**
|
|
703
|
+
* Functions module for invoking serverless functions.
|
|
704
|
+
*
|
|
705
|
+
* @example
|
|
706
|
+
* ```typescript
|
|
707
|
+
* const result = await nexus.functions.invoke('processOrder', { orderId });
|
|
708
|
+
* ```
|
|
709
|
+
*/
|
|
710
|
+
functions: FunctionsModule;
|
|
711
|
+
/**
|
|
712
|
+
* The configuration used to create this client.
|
|
713
|
+
*/
|
|
714
|
+
config: NexusClientConfig;
|
|
715
|
+
}
|
|
716
|
+
/**
|
|
717
|
+
* Creates a new Nexus client instance.
|
|
718
|
+
*
|
|
719
|
+
* The client provides access to all Nexus Platform features:
|
|
720
|
+
* - **auth**: User authentication and session management
|
|
721
|
+
* - **entities**: Database CRUD operations
|
|
722
|
+
* - **functions**: Serverless function invocation
|
|
723
|
+
*
|
|
724
|
+
* @param config - Configuration options for the client.
|
|
725
|
+
* @returns A configured NexusClient instance.
|
|
726
|
+
*
|
|
727
|
+
* @example
|
|
728
|
+
* ```typescript
|
|
729
|
+
* import { createClient } from 'nexus-platform-sdk';
|
|
730
|
+
*
|
|
731
|
+
* // Create the client
|
|
732
|
+
* const nexus = createClient({
|
|
733
|
+
* appId: import.meta.env.VITE_NEXUS_APP_ID,
|
|
734
|
+
* tenantId: import.meta.env.VITE_NEXUS_TENANT_ID,
|
|
735
|
+
* dataSourceId: import.meta.env.VITE_NEXUS_DATASOURCE_ID,
|
|
736
|
+
* apiUrl: import.meta.env.VITE_NEXUS_API_URL,
|
|
737
|
+
* });
|
|
738
|
+
*
|
|
739
|
+
* // Use the client
|
|
740
|
+
* await nexus.auth.signIn({ email, password });
|
|
741
|
+
* const tasks = await nexus.entities.Task.list();
|
|
742
|
+
* ```
|
|
743
|
+
*
|
|
744
|
+
* @example
|
|
745
|
+
* ```typescript
|
|
746
|
+
* // Export as singleton for use throughout your app
|
|
747
|
+
* // src/api/nexusClient.ts
|
|
748
|
+
* import { createClient } from 'nexus-platform-sdk';
|
|
749
|
+
*
|
|
750
|
+
* export const nexus = createClient({
|
|
751
|
+
* appId: process.env.NEXUS_APP_ID,
|
|
752
|
+
* tenantId: process.env.NEXUS_TENANT_ID,
|
|
753
|
+
* dataSourceId: process.env.NEXUS_DATASOURCE_ID,
|
|
754
|
+
* apiUrl: process.env.NEXUS_API_URL,
|
|
755
|
+
* });
|
|
756
|
+
* ```
|
|
757
|
+
*/
|
|
758
|
+
declare function createClient(config: NexusClientConfig): NexusClient;
|
|
759
|
+
|
|
760
|
+
export { type EntityListOptions, type EntityTable, type FunctionInfo, type FunctionResult, NexusApiError, type NexusClient, type NexusClientConfig, type SignInCredentials, type SignUpData, type User, createClient };
|