@tspvivek/baasix-sdk 0.1.0-alpha.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.
Files changed (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +942 -0
  3. package/dist/client-CzF9B60b.d.ts +614 -0
  4. package/dist/client-aXK_gEyr.d.cts +614 -0
  5. package/dist/index.cjs +4159 -0
  6. package/dist/index.cjs.map +1 -0
  7. package/dist/index.d.cts +1498 -0
  8. package/dist/index.d.ts +1498 -0
  9. package/dist/index.js +4135 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/modules/auth.cjs +651 -0
  12. package/dist/modules/auth.cjs.map +1 -0
  13. package/dist/modules/auth.d.cts +384 -0
  14. package/dist/modules/auth.d.ts +384 -0
  15. package/dist/modules/auth.js +649 -0
  16. package/dist/modules/auth.js.map +1 -0
  17. package/dist/modules/files.cjs +266 -0
  18. package/dist/modules/files.cjs.map +1 -0
  19. package/dist/modules/files.d.cts +187 -0
  20. package/dist/modules/files.d.ts +187 -0
  21. package/dist/modules/files.js +264 -0
  22. package/dist/modules/files.js.map +1 -0
  23. package/dist/modules/items.cjs +654 -0
  24. package/dist/modules/items.cjs.map +1 -0
  25. package/dist/modules/items.d.cts +472 -0
  26. package/dist/modules/items.d.ts +472 -0
  27. package/dist/modules/items.js +651 -0
  28. package/dist/modules/items.js.map +1 -0
  29. package/dist/modules/schemas.cjs +269 -0
  30. package/dist/modules/schemas.cjs.map +1 -0
  31. package/dist/modules/schemas.d.cts +239 -0
  32. package/dist/modules/schemas.d.ts +239 -0
  33. package/dist/modules/schemas.js +267 -0
  34. package/dist/modules/schemas.js.map +1 -0
  35. package/dist/storage/index.cjs +162 -0
  36. package/dist/storage/index.cjs.map +1 -0
  37. package/dist/storage/index.d.cts +96 -0
  38. package/dist/storage/index.d.ts +96 -0
  39. package/dist/storage/index.js +157 -0
  40. package/dist/storage/index.js.map +1 -0
  41. package/dist/types-BdjsGANq.d.cts +40 -0
  42. package/dist/types-BdjsGANq.d.ts +40 -0
  43. package/package.json +107 -0
@@ -0,0 +1,651 @@
1
+ 'use strict';
2
+
3
+ // src/storage/types.ts
4
+ var STORAGE_KEYS = {
5
+ ACCESS_TOKEN: "baasix_access_token",
6
+ REFRESH_TOKEN: "baasix_refresh_token",
7
+ TOKEN_EXPIRY: "baasix_token_expiry",
8
+ USER: "baasix_user",
9
+ TENANT: "baasix_tenant"
10
+ };
11
+
12
+ // src/types.ts
13
+ var BaasixError = class _BaasixError extends Error {
14
+ status;
15
+ code;
16
+ details;
17
+ isRetryable;
18
+ constructor(message, status = 500, code, details) {
19
+ super(message);
20
+ this.name = "BaasixError";
21
+ this.status = status;
22
+ this.code = code;
23
+ this.details = details;
24
+ this.isRetryable = status >= 500 || status === 429;
25
+ if (Error.captureStackTrace) {
26
+ Error.captureStackTrace(this, _BaasixError);
27
+ }
28
+ }
29
+ toJSON() {
30
+ return {
31
+ name: this.name,
32
+ message: this.message,
33
+ status: this.status,
34
+ code: this.code,
35
+ details: this.details
36
+ };
37
+ }
38
+ };
39
+
40
+ // src/modules/auth.ts
41
+ var AuthModule = class {
42
+ client;
43
+ storage;
44
+ authMode;
45
+ onAuthStateChange;
46
+ currentUser = null;
47
+ constructor(config) {
48
+ this.client = config.client;
49
+ this.storage = config.storage;
50
+ this.authMode = config.authMode;
51
+ this.onAuthStateChange = config.onAuthStateChange;
52
+ }
53
+ /**
54
+ * Emit an authentication state change event
55
+ */
56
+ emitAuthStateChange(event, user) {
57
+ this.currentUser = user;
58
+ this.onAuthStateChange?.(event, user);
59
+ }
60
+ /**
61
+ * Store authentication tokens
62
+ */
63
+ async storeTokens(response) {
64
+ if (this.authMode === "jwt") {
65
+ await this.storage.set(STORAGE_KEYS.ACCESS_TOKEN, response.token);
66
+ if (response.refreshToken) {
67
+ await this.storage.set(STORAGE_KEYS.REFRESH_TOKEN, response.refreshToken);
68
+ }
69
+ if (response.expiresIn) {
70
+ const expiresAt = Date.now() + response.expiresIn * 1e3;
71
+ await this.storage.set(STORAGE_KEYS.TOKEN_EXPIRY, expiresAt.toString());
72
+ }
73
+ }
74
+ if (response.user) {
75
+ await this.storage.set(STORAGE_KEYS.USER, JSON.stringify(response.user));
76
+ }
77
+ }
78
+ /**
79
+ * Clear stored authentication data
80
+ */
81
+ async clearAuth() {
82
+ await this.storage.remove(STORAGE_KEYS.ACCESS_TOKEN);
83
+ await this.storage.remove(STORAGE_KEYS.REFRESH_TOKEN);
84
+ await this.storage.remove(STORAGE_KEYS.TOKEN_EXPIRY);
85
+ await this.storage.remove(STORAGE_KEYS.USER);
86
+ await this.storage.remove(STORAGE_KEYS.TENANT);
87
+ this.currentUser = null;
88
+ }
89
+ /**
90
+ * Register a new user
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * const { user, token } = await baasix.auth.register({
95
+ * email: 'newuser@example.com',
96
+ * password: 'securepassword',
97
+ * firstName: 'John',
98
+ * lastName: 'Doe'
99
+ * });
100
+ * ```
101
+ */
102
+ async register(data) {
103
+ const response = await this.client.post("/auth/register", data, {
104
+ skipAuth: true
105
+ });
106
+ await this.storeTokens(response);
107
+ this.emitAuthStateChange("SIGNED_IN", response.user);
108
+ return response;
109
+ }
110
+ /**
111
+ * Login with email and password
112
+ *
113
+ * @example
114
+ * ```typescript
115
+ * const { user, token } = await baasix.auth.login({
116
+ * email: 'user@example.com',
117
+ * password: 'password123'
118
+ * });
119
+ *
120
+ * // Login with tenant (multi-tenant mode)
121
+ * const result = await baasix.auth.login({
122
+ * email: 'user@example.com',
123
+ * password: 'password123',
124
+ * tenantId: 'tenant-uuid'
125
+ * });
126
+ * ```
127
+ */
128
+ async login(credentials) {
129
+ const response = await this.client.post(
130
+ "/auth/login",
131
+ {
132
+ email: credentials.email,
133
+ password: credentials.password,
134
+ tenant_Id: credentials.tenantId
135
+ },
136
+ { skipAuth: true }
137
+ );
138
+ await this.storeTokens(response);
139
+ this.emitAuthStateChange("SIGNED_IN", response.user);
140
+ return response;
141
+ }
142
+ /**
143
+ * Logout the current user
144
+ *
145
+ * @example
146
+ * ```typescript
147
+ * await baasix.auth.logout();
148
+ * ```
149
+ */
150
+ async logout() {
151
+ try {
152
+ await this.client.get("/auth/logout");
153
+ } catch {
154
+ }
155
+ await this.clearAuth();
156
+ this.emitAuthStateChange("SIGNED_OUT", null);
157
+ }
158
+ /**
159
+ * Get the current authenticated user from the server
160
+ *
161
+ * @example
162
+ * ```typescript
163
+ * const user = await baasix.auth.getUser();
164
+ * console.log(user?.email);
165
+ * ```
166
+ */
167
+ async getUser() {
168
+ try {
169
+ const response = await this.client.get("/auth/me");
170
+ this.currentUser = response.data;
171
+ await this.storage.set(STORAGE_KEYS.USER, JSON.stringify(response.data));
172
+ return response.data;
173
+ } catch (error) {
174
+ if (error instanceof BaasixError && error.status === 401) {
175
+ await this.clearAuth();
176
+ return null;
177
+ }
178
+ throw error;
179
+ }
180
+ }
181
+ /**
182
+ * Get the cached current user (does not make an API call)
183
+ *
184
+ * @example
185
+ * ```typescript
186
+ * const user = await baasix.auth.getCachedUser();
187
+ * ```
188
+ */
189
+ async getCachedUser() {
190
+ if (this.currentUser) {
191
+ return this.currentUser;
192
+ }
193
+ const userJson = await this.storage.get(STORAGE_KEYS.USER);
194
+ if (userJson) {
195
+ try {
196
+ this.currentUser = JSON.parse(userJson);
197
+ return this.currentUser;
198
+ } catch {
199
+ return null;
200
+ }
201
+ }
202
+ return null;
203
+ }
204
+ /**
205
+ * Check if user is authenticated (has valid token)
206
+ *
207
+ * @example
208
+ * ```typescript
209
+ * if (await baasix.auth.isAuthenticated()) {
210
+ * // User is logged in
211
+ * }
212
+ * ```
213
+ */
214
+ async isAuthenticated() {
215
+ if (this.authMode === "cookie") {
216
+ const user = await this.getCachedUser();
217
+ return user !== null;
218
+ }
219
+ const token = await this.storage.get(STORAGE_KEYS.ACCESS_TOKEN);
220
+ if (!token) return false;
221
+ const expiry = await this.storage.get(STORAGE_KEYS.TOKEN_EXPIRY);
222
+ if (expiry && Date.now() >= parseInt(expiry, 10)) {
223
+ const refreshToken = await this.storage.get(STORAGE_KEYS.REFRESH_TOKEN);
224
+ return !!refreshToken;
225
+ }
226
+ return true;
227
+ }
228
+ /**
229
+ * Get the current access token
230
+ *
231
+ * @example
232
+ * ```typescript
233
+ * const token = await baasix.auth.getToken();
234
+ * ```
235
+ */
236
+ async getToken() {
237
+ if (this.authMode === "cookie") {
238
+ return null;
239
+ }
240
+ return await this.storage.get(STORAGE_KEYS.ACCESS_TOKEN);
241
+ }
242
+ /**
243
+ * Set a static token (useful for server-side or service accounts)
244
+ *
245
+ * @example
246
+ * ```typescript
247
+ * baasix.auth.setToken('your-api-token');
248
+ * ```
249
+ */
250
+ async setToken(token) {
251
+ await this.storage.set(STORAGE_KEYS.ACCESS_TOKEN, token);
252
+ }
253
+ /**
254
+ * Refresh the current token
255
+ *
256
+ * @example
257
+ * ```typescript
258
+ * const tokens = await baasix.auth.refreshToken();
259
+ * ```
260
+ */
261
+ async refreshToken() {
262
+ const refreshToken = await this.storage.get(STORAGE_KEYS.REFRESH_TOKEN);
263
+ const response = await this.client.post(
264
+ "/auth/refresh",
265
+ this.authMode === "jwt" ? { refreshToken } : void 0
266
+ );
267
+ await this.storeTokens(response);
268
+ const tokens = {
269
+ accessToken: response.token,
270
+ refreshToken: response.refreshToken,
271
+ expiresIn: response.expiresIn,
272
+ expiresAt: response.expiresIn ? Date.now() + response.expiresIn * 1e3 : void 0
273
+ };
274
+ this.emitAuthStateChange("TOKEN_REFRESHED", response.user);
275
+ return tokens;
276
+ }
277
+ /**
278
+ * Request a magic link for passwordless login
279
+ *
280
+ * @example
281
+ * ```typescript
282
+ * await baasix.auth.sendMagicLink({
283
+ * email: 'user@example.com',
284
+ * redirectUrl: 'https://myapp.com/auth/callback'
285
+ * });
286
+ * ```
287
+ */
288
+ async sendMagicLink(options) {
289
+ await this.client.post(
290
+ "/auth/magiclink",
291
+ {
292
+ email: options.email,
293
+ link: options.redirectUrl,
294
+ mode: options.mode || "link"
295
+ },
296
+ { skipAuth: true }
297
+ );
298
+ }
299
+ /**
300
+ * Verify magic link/code and complete login
301
+ *
302
+ * @example
303
+ * ```typescript
304
+ * const { user, token } = await baasix.auth.verifyMagicLink('verification-token');
305
+ * ```
306
+ */
307
+ async verifyMagicLink(token) {
308
+ const response = await this.client.post(
309
+ "/auth/magiclink/verify",
310
+ { token },
311
+ { skipAuth: true }
312
+ );
313
+ await this.storeTokens(response);
314
+ this.emitAuthStateChange("SIGNED_IN", response.user);
315
+ return response;
316
+ }
317
+ /**
318
+ * Request a password reset
319
+ *
320
+ * @example
321
+ * ```typescript
322
+ * await baasix.auth.forgotPassword({
323
+ * email: 'user@example.com',
324
+ * redirectUrl: 'https://myapp.com/reset-password'
325
+ * });
326
+ * ```
327
+ */
328
+ async forgotPassword(options) {
329
+ await this.client.post(
330
+ "/auth/forgot-password",
331
+ {
332
+ email: options.email,
333
+ link: options.redirectUrl
334
+ },
335
+ { skipAuth: true }
336
+ );
337
+ }
338
+ /**
339
+ * Reset password using a reset token
340
+ *
341
+ * @example
342
+ * ```typescript
343
+ * await baasix.auth.resetPassword('reset-token', 'newpassword123');
344
+ * ```
345
+ */
346
+ async resetPassword(token, newPassword) {
347
+ await this.client.post(
348
+ "/auth/reset-password",
349
+ { token, password: newPassword },
350
+ { skipAuth: true }
351
+ );
352
+ }
353
+ /**
354
+ * Change the current user's password
355
+ *
356
+ * @example
357
+ * ```typescript
358
+ * await baasix.auth.changePassword('currentPassword', 'newPassword');
359
+ * ```
360
+ */
361
+ async changePassword(currentPassword, newPassword) {
362
+ await this.client.post("/auth/change-password", {
363
+ currentPassword,
364
+ newPassword
365
+ });
366
+ }
367
+ /**
368
+ * Update the current user's profile
369
+ *
370
+ * @example
371
+ * ```typescript
372
+ * const updatedUser = await baasix.auth.updateProfile({
373
+ * firstName: 'Jane',
374
+ * lastName: 'Doe'
375
+ * });
376
+ * ```
377
+ */
378
+ async updateProfile(data) {
379
+ const response = await this.client.patch("/auth/me", data);
380
+ await this.storage.set(STORAGE_KEYS.USER, JSON.stringify(response.data));
381
+ this.emitAuthStateChange("USER_UPDATED", response.data);
382
+ return response.data;
383
+ }
384
+ /**
385
+ * Get available tenants for the current user (multi-tenant mode)
386
+ *
387
+ * @example
388
+ * ```typescript
389
+ * const tenants = await baasix.auth.getTenants();
390
+ * ```
391
+ */
392
+ async getTenants() {
393
+ const response = await this.client.get("/auth/tenants");
394
+ return response.data;
395
+ }
396
+ /**
397
+ * Switch to a different tenant (multi-tenant mode)
398
+ *
399
+ * @example
400
+ * ```typescript
401
+ * const { user, token } = await baasix.auth.switchTenant('tenant-uuid');
402
+ * ```
403
+ */
404
+ async switchTenant(tenantId) {
405
+ const response = await this.client.post("/auth/switch-tenant", {
406
+ tenant_Id: tenantId
407
+ });
408
+ await this.storeTokens(response);
409
+ await this.storage.set(STORAGE_KEYS.TENANT, tenantId);
410
+ this.emitAuthStateChange("TENANT_SWITCHED", response.user);
411
+ return response;
412
+ }
413
+ /**
414
+ * Get the current authentication state
415
+ *
416
+ * @example
417
+ * ```typescript
418
+ * const state = await baasix.auth.getState();
419
+ * console.log(state.isAuthenticated, state.user);
420
+ * ```
421
+ */
422
+ async getState() {
423
+ const isAuthenticated = await this.isAuthenticated();
424
+ const user = await this.getCachedUser();
425
+ return {
426
+ user,
427
+ isAuthenticated,
428
+ isLoading: false,
429
+ error: null
430
+ };
431
+ }
432
+ /**
433
+ * Initialize authentication state from storage
434
+ * Call this on app startup to restore previous session
435
+ *
436
+ * @example
437
+ * ```typescript
438
+ * await baasix.auth.initialize();
439
+ * ```
440
+ */
441
+ async initialize() {
442
+ const state = await this.getState();
443
+ if (state.isAuthenticated && state.user) {
444
+ this.emitAuthStateChange("SIGNED_IN", state.user);
445
+ }
446
+ return state;
447
+ }
448
+ // ===================
449
+ // OAuth / Social Login
450
+ // ===================
451
+ /**
452
+ * Get the OAuth authorization URL for a provider
453
+ * Redirect the user to this URL to start the OAuth flow
454
+ *
455
+ * @example
456
+ * ```typescript
457
+ * const url = baasix.auth.getOAuthUrl({
458
+ * provider: 'google',
459
+ * redirectUrl: 'https://myapp.com/auth/callback'
460
+ * });
461
+ * window.location.href = url;
462
+ * ```
463
+ */
464
+ getOAuthUrl(options) {
465
+ const baseUrl = this.client.getBaseUrl();
466
+ const params = new URLSearchParams({
467
+ redirect_url: options.redirectUrl
468
+ });
469
+ if (options.scopes?.length) {
470
+ params.set("scopes", options.scopes.join(","));
471
+ }
472
+ if (options.state) {
473
+ params.set("state", options.state);
474
+ }
475
+ return `${baseUrl}/auth/signin/${options.provider}?${params.toString()}`;
476
+ }
477
+ /**
478
+ * Handle OAuth callback and complete login
479
+ * Call this from your callback page with the token from URL
480
+ *
481
+ * @example
482
+ * ```typescript
483
+ * // In your callback page
484
+ * const params = new URLSearchParams(window.location.search);
485
+ * const token = params.get('token');
486
+ *
487
+ * if (token) {
488
+ * await baasix.auth.handleOAuthCallback(token);
489
+ * }
490
+ * ```
491
+ */
492
+ async handleOAuthCallback(token) {
493
+ await this.storage.set(STORAGE_KEYS.ACCESS_TOKEN, token);
494
+ const user = await this.getUser();
495
+ const response = {
496
+ token,
497
+ user
498
+ };
499
+ this.emitAuthStateChange("SIGNED_IN", user);
500
+ return response;
501
+ }
502
+ // ===================
503
+ // Email Verification
504
+ // ===================
505
+ /**
506
+ * Request email verification
507
+ * Sends a verification email to the current user
508
+ *
509
+ * @example
510
+ * ```typescript
511
+ * await baasix.auth.requestEmailVerification('https://myapp.com/verify-email');
512
+ * ```
513
+ */
514
+ async requestEmailVerification(redirectUrl) {
515
+ await this.client.post("/auth/request-verify-email", {
516
+ link: redirectUrl
517
+ });
518
+ }
519
+ /**
520
+ * Verify email with token
521
+ *
522
+ * @example
523
+ * ```typescript
524
+ * const params = new URLSearchParams(window.location.search);
525
+ * const token = params.get('token');
526
+ *
527
+ * await baasix.auth.verifyEmail(token);
528
+ * ```
529
+ */
530
+ async verifyEmail(token) {
531
+ await this.client.get("/auth/verify-email", {
532
+ params: { token },
533
+ skipAuth: true
534
+ });
535
+ }
536
+ /**
537
+ * Check if current session/token is valid
538
+ *
539
+ * @example
540
+ * ```typescript
541
+ * const isValid = await baasix.auth.checkSession();
542
+ * ```
543
+ */
544
+ async checkSession() {
545
+ try {
546
+ const response = await this.client.get("/auth/check");
547
+ return response.data.valid;
548
+ } catch {
549
+ return false;
550
+ }
551
+ }
552
+ // ===================
553
+ // Invitation System
554
+ // ===================
555
+ /**
556
+ * Send an invitation to a user (multi-tenant mode)
557
+ *
558
+ * @example
559
+ * ```typescript
560
+ * await baasix.auth.sendInvite({
561
+ * email: 'newuser@example.com',
562
+ * roleId: 'role-uuid',
563
+ * tenantId: 'tenant-uuid',
564
+ * redirectUrl: 'https://myapp.com/accept-invite'
565
+ * });
566
+ * ```
567
+ */
568
+ async sendInvite(options) {
569
+ await this.client.post("/auth/invite", {
570
+ email: options.email,
571
+ role_Id: options.roleId,
572
+ tenant_Id: options.tenantId,
573
+ link: options.redirectUrl
574
+ });
575
+ }
576
+ /**
577
+ * Verify an invitation token
578
+ *
579
+ * @example
580
+ * ```typescript
581
+ * const params = new URLSearchParams(window.location.search);
582
+ * const token = params.get('token');
583
+ *
584
+ * const result = await baasix.auth.verifyInvite(token);
585
+ * if (result.valid) {
586
+ * // Show registration form with pre-filled email
587
+ * }
588
+ * ```
589
+ */
590
+ async verifyInvite(token, redirectUrl) {
591
+ const response = await this.client.get(
592
+ "/auth/verify-invite",
593
+ {
594
+ params: {
595
+ token,
596
+ link: redirectUrl
597
+ },
598
+ skipAuth: true
599
+ }
600
+ );
601
+ return response.data;
602
+ }
603
+ /**
604
+ * Accept an invitation (for existing users)
605
+ *
606
+ * @example
607
+ * ```typescript
608
+ * await baasix.auth.acceptInvite(token);
609
+ * ```
610
+ */
611
+ async acceptInvite(token) {
612
+ const response = await this.client.post(
613
+ "/auth/accept-invite",
614
+ { token }
615
+ );
616
+ await this.storeTokens(response);
617
+ this.emitAuthStateChange("SIGNED_IN", response.user);
618
+ return response;
619
+ }
620
+ /**
621
+ * Register with an invitation token
622
+ *
623
+ * @example
624
+ * ```typescript
625
+ * const { user, token } = await baasix.auth.registerWithInvite({
626
+ * email: 'user@example.com',
627
+ * password: 'password',
628
+ * firstName: 'John',
629
+ * lastName: 'Doe',
630
+ * inviteToken: 'invite-token'
631
+ * });
632
+ * ```
633
+ */
634
+ async registerWithInvite(data) {
635
+ const response = await this.client.post(
636
+ "/auth/register",
637
+ {
638
+ ...data,
639
+ inviteToken: data.inviteToken
640
+ },
641
+ { skipAuth: true }
642
+ );
643
+ await this.storeTokens(response);
644
+ this.emitAuthStateChange("SIGNED_IN", response.user);
645
+ return response;
646
+ }
647
+ };
648
+
649
+ exports.AuthModule = AuthModule;
650
+ //# sourceMappingURL=auth.cjs.map
651
+ //# sourceMappingURL=auth.cjs.map