@insforge/sdk 0.0.10 → 0.0.11

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/dist/index.mjs CHANGED
@@ -166,265 +166,6 @@ var TokenManager = class {
166
166
  }
167
167
  };
168
168
 
169
- // src/modules/auth.ts
170
- var Auth = class {
171
- constructor(http, tokenManager) {
172
- this.http = http;
173
- this.tokenManager = tokenManager;
174
- }
175
- /**
176
- * Sign up a new user
177
- */
178
- async signUp(request) {
179
- try {
180
- const response = await this.http.post("/api/auth/users", request);
181
- const session = {
182
- accessToken: response.accessToken,
183
- user: response.user
184
- };
185
- this.tokenManager.saveSession(session);
186
- this.http.setAuthToken(response.accessToken);
187
- return {
188
- data: response,
189
- error: null
190
- };
191
- } catch (error) {
192
- if (error instanceof InsForgeError) {
193
- return { data: null, error };
194
- }
195
- return {
196
- data: null,
197
- error: new InsForgeError(
198
- error instanceof Error ? error.message : "An unexpected error occurred during sign up",
199
- 500,
200
- "UNEXPECTED_ERROR"
201
- )
202
- };
203
- }
204
- }
205
- /**
206
- * Sign in with email and password
207
- */
208
- async signInWithPassword(request) {
209
- try {
210
- const response = await this.http.post("/api/auth/sessions", request);
211
- const session = {
212
- accessToken: response.accessToken,
213
- user: response.user
214
- };
215
- this.tokenManager.saveSession(session);
216
- this.http.setAuthToken(response.accessToken);
217
- return {
218
- data: response,
219
- error: null
220
- };
221
- } catch (error) {
222
- if (error instanceof InsForgeError) {
223
- return { data: null, error };
224
- }
225
- return {
226
- data: null,
227
- error: new InsForgeError(
228
- "An unexpected error occurred during sign in",
229
- 500,
230
- "UNEXPECTED_ERROR"
231
- )
232
- };
233
- }
234
- }
235
- /**
236
- * Sign in with OAuth provider
237
- */
238
- async signInWithOAuth(options) {
239
- try {
240
- const { provider, redirectTo, skipBrowserRedirect } = options;
241
- const params = redirectTo ? { redirect_uri: redirectTo } : void 0;
242
- const endpoint = `/api/auth/oauth/${provider}`;
243
- const response = await this.http.get(endpoint, { params });
244
- if (typeof window !== "undefined" && !skipBrowserRedirect) {
245
- window.location.href = response.authUrl;
246
- return { data: {}, error: null };
247
- }
248
- return {
249
- data: {
250
- url: response.authUrl,
251
- provider
252
- },
253
- error: null
254
- };
255
- } catch (error) {
256
- if (error instanceof InsForgeError) {
257
- return { data: {}, error };
258
- }
259
- return {
260
- data: {},
261
- error: new InsForgeError(
262
- "An unexpected error occurred during OAuth initialization",
263
- 500,
264
- "UNEXPECTED_ERROR"
265
- )
266
- };
267
- }
268
- }
269
- /**
270
- * Sign out the current user
271
- */
272
- async signOut() {
273
- try {
274
- this.tokenManager.clearSession();
275
- this.http.setAuthToken(null);
276
- return { error: null };
277
- } catch (error) {
278
- return {
279
- error: new InsForgeError(
280
- "Failed to sign out",
281
- 500,
282
- "SIGNOUT_ERROR"
283
- )
284
- };
285
- }
286
- }
287
- /**
288
- * Get the current user with full profile information
289
- * Returns both auth info (id, email, role) and profile data (nickname, avatar_url, bio, etc.)
290
- */
291
- async getCurrentUser() {
292
- try {
293
- const session = this.tokenManager.getSession();
294
- if (!session?.accessToken) {
295
- return { data: null, error: null };
296
- }
297
- this.http.setAuthToken(session.accessToken);
298
- const authResponse = await this.http.get("/api/auth/sessions/current");
299
- const profileResponse = await this.http.get(
300
- `/api/database/records/users?id=eq.${authResponse.user.id}`
301
- );
302
- const profile = profileResponse && profileResponse.length > 0 ? profileResponse[0] : null;
303
- return {
304
- data: {
305
- user: authResponse.user,
306
- profile
307
- },
308
- error: null
309
- };
310
- } catch (error) {
311
- if (error instanceof InsForgeError && error.statusCode === 401) {
312
- await this.signOut();
313
- return { data: null, error: null };
314
- }
315
- if (error instanceof InsForgeError) {
316
- return { data: null, error };
317
- }
318
- return {
319
- data: null,
320
- error: new InsForgeError(
321
- "An unexpected error occurred while fetching user",
322
- 500,
323
- "UNEXPECTED_ERROR"
324
- )
325
- };
326
- }
327
- }
328
- /**
329
- * Get any user's profile by ID
330
- * Returns profile information from the users table (nickname, avatar_url, bio, etc.)
331
- */
332
- async getProfile(userId) {
333
- try {
334
- const response = await this.http.get(
335
- `/api/database/records/users?id=eq.${userId}`
336
- );
337
- const profile = response && response.length > 0 ? response[0] : null;
338
- return {
339
- data: profile,
340
- error: null
341
- };
342
- } catch (error) {
343
- if (error instanceof InsForgeError) {
344
- return { data: null, error };
345
- }
346
- return {
347
- data: null,
348
- error: new InsForgeError(
349
- "An unexpected error occurred while fetching profile",
350
- 500,
351
- "UNEXPECTED_ERROR"
352
- )
353
- };
354
- }
355
- }
356
- /**
357
- * Get the current session (only session data, no API call)
358
- * Returns the stored JWT token and basic user info from local storage
359
- */
360
- async getCurrentSession() {
361
- try {
362
- const session = this.tokenManager.getSession();
363
- if (session?.accessToken) {
364
- this.http.setAuthToken(session.accessToken);
365
- return { data: { session }, error: null };
366
- }
367
- return { data: { session: null }, error: null };
368
- } catch (error) {
369
- if (error instanceof InsForgeError) {
370
- return { data: { session: null }, error };
371
- }
372
- return {
373
- data: { session: null },
374
- error: new InsForgeError(
375
- "An unexpected error occurred while getting session",
376
- 500,
377
- "UNEXPECTED_ERROR"
378
- )
379
- };
380
- }
381
- }
382
- /**
383
- * Set/Update the current user's profile
384
- * Updates profile information in the users table (nickname, avatar_url, bio, etc.)
385
- */
386
- async setProfile(profile) {
387
- try {
388
- const session = this.tokenManager.getSession();
389
- if (!session?.user?.id) {
390
- return {
391
- data: null,
392
- error: new InsForgeError(
393
- "No authenticated user found",
394
- 401,
395
- "UNAUTHENTICATED"
396
- )
397
- };
398
- }
399
- const response = await this.http.patch(
400
- `/api/database/records/users?id=eq.${session.user.id}`,
401
- profile,
402
- {
403
- headers: {
404
- "Prefer": "return=representation"
405
- }
406
- }
407
- );
408
- return {
409
- data: response,
410
- error: null
411
- };
412
- } catch (error) {
413
- if (error instanceof InsForgeError) {
414
- return { data: null, error };
415
- }
416
- return {
417
- data: null,
418
- error: new InsForgeError(
419
- "An unexpected error occurred while updating profile",
420
- 500,
421
- "UNEXPECTED_ERROR"
422
- )
423
- };
424
- }
425
- }
426
- };
427
-
428
169
  // src/modules/database.ts
429
170
  var QueryBuilder = class {
430
171
  constructor(table, http) {
@@ -712,6 +453,223 @@ var Database = class {
712
453
  }
713
454
  };
714
455
 
456
+ // src/modules/auth.ts
457
+ var Auth = class {
458
+ constructor(http, tokenManager) {
459
+ this.http = http;
460
+ this.tokenManager = tokenManager;
461
+ this.database = new Database(http);
462
+ }
463
+ /**
464
+ * Sign up a new user
465
+ */
466
+ async signUp(request) {
467
+ try {
468
+ const response = await this.http.post("/api/auth/users", request);
469
+ const session = {
470
+ accessToken: response.accessToken,
471
+ user: response.user
472
+ };
473
+ this.tokenManager.saveSession(session);
474
+ this.http.setAuthToken(response.accessToken);
475
+ return {
476
+ data: response,
477
+ error: null
478
+ };
479
+ } catch (error) {
480
+ if (error instanceof InsForgeError) {
481
+ return { data: null, error };
482
+ }
483
+ return {
484
+ data: null,
485
+ error: new InsForgeError(
486
+ error instanceof Error ? error.message : "An unexpected error occurred during sign up",
487
+ 500,
488
+ "UNEXPECTED_ERROR"
489
+ )
490
+ };
491
+ }
492
+ }
493
+ /**
494
+ * Sign in with email and password
495
+ */
496
+ async signInWithPassword(request) {
497
+ try {
498
+ const response = await this.http.post("/api/auth/sessions", request);
499
+ const session = {
500
+ accessToken: response.accessToken,
501
+ user: response.user
502
+ };
503
+ this.tokenManager.saveSession(session);
504
+ this.http.setAuthToken(response.accessToken);
505
+ return {
506
+ data: response,
507
+ error: null
508
+ };
509
+ } catch (error) {
510
+ if (error instanceof InsForgeError) {
511
+ return { data: null, error };
512
+ }
513
+ return {
514
+ data: null,
515
+ error: new InsForgeError(
516
+ "An unexpected error occurred during sign in",
517
+ 500,
518
+ "UNEXPECTED_ERROR"
519
+ )
520
+ };
521
+ }
522
+ }
523
+ /**
524
+ * Sign in with OAuth provider
525
+ */
526
+ async signInWithOAuth(options) {
527
+ try {
528
+ const { provider, redirectTo, skipBrowserRedirect } = options;
529
+ const params = redirectTo ? { redirect_uri: redirectTo } : void 0;
530
+ const endpoint = `/api/auth/oauth/${provider}`;
531
+ const response = await this.http.get(endpoint, { params });
532
+ if (typeof window !== "undefined" && !skipBrowserRedirect) {
533
+ window.location.href = response.authUrl;
534
+ return { data: {}, error: null };
535
+ }
536
+ return {
537
+ data: {
538
+ url: response.authUrl,
539
+ provider
540
+ },
541
+ error: null
542
+ };
543
+ } catch (error) {
544
+ if (error instanceof InsForgeError) {
545
+ return { data: {}, error };
546
+ }
547
+ return {
548
+ data: {},
549
+ error: new InsForgeError(
550
+ "An unexpected error occurred during OAuth initialization",
551
+ 500,
552
+ "UNEXPECTED_ERROR"
553
+ )
554
+ };
555
+ }
556
+ }
557
+ /**
558
+ * Sign out the current user
559
+ */
560
+ async signOut() {
561
+ try {
562
+ this.tokenManager.clearSession();
563
+ this.http.setAuthToken(null);
564
+ return { error: null };
565
+ } catch (error) {
566
+ return {
567
+ error: new InsForgeError(
568
+ "Failed to sign out",
569
+ 500,
570
+ "SIGNOUT_ERROR"
571
+ )
572
+ };
573
+ }
574
+ }
575
+ /**
576
+ * Get the current user with full profile information
577
+ * Returns both auth info (id, email, role) and profile data (nickname, avatar_url, bio, etc.)
578
+ */
579
+ async getCurrentUser() {
580
+ try {
581
+ const session = this.tokenManager.getSession();
582
+ if (!session?.accessToken) {
583
+ return { data: null, error: null };
584
+ }
585
+ this.http.setAuthToken(session.accessToken);
586
+ const authResponse = await this.http.get("/api/auth/sessions/current");
587
+ const { data: profile, error: profileError } = await this.database.from("users").select("*").eq("id", authResponse.user.id).single();
588
+ if (profileError && profileError.statusCode !== 406) {
589
+ return { data: null, error: profileError };
590
+ }
591
+ return {
592
+ data: {
593
+ user: authResponse.user,
594
+ profile
595
+ },
596
+ error: null
597
+ };
598
+ } catch (error) {
599
+ if (error instanceof InsForgeError && error.statusCode === 401) {
600
+ await this.signOut();
601
+ return { data: null, error: null };
602
+ }
603
+ if (error instanceof InsForgeError) {
604
+ return { data: null, error };
605
+ }
606
+ return {
607
+ data: null,
608
+ error: new InsForgeError(
609
+ "An unexpected error occurred while fetching user",
610
+ 500,
611
+ "UNEXPECTED_ERROR"
612
+ )
613
+ };
614
+ }
615
+ }
616
+ /**
617
+ * Get any user's profile by ID
618
+ * Returns profile information from the users table (nickname, avatar_url, bio, etc.)
619
+ */
620
+ async getProfile(userId) {
621
+ const { data, error } = await this.database.from("users").select("*").eq("id", userId).single();
622
+ if (error && error.statusCode === 406) {
623
+ return { data: null, error: null };
624
+ }
625
+ return { data, error };
626
+ }
627
+ /**
628
+ * Get the current session (only session data, no API call)
629
+ * Returns the stored JWT token and basic user info from local storage
630
+ */
631
+ async getCurrentSession() {
632
+ try {
633
+ const session = this.tokenManager.getSession();
634
+ if (session?.accessToken) {
635
+ this.http.setAuthToken(session.accessToken);
636
+ return { data: { session }, error: null };
637
+ }
638
+ return { data: { session: null }, error: null };
639
+ } catch (error) {
640
+ if (error instanceof InsForgeError) {
641
+ return { data: { session: null }, error };
642
+ }
643
+ return {
644
+ data: { session: null },
645
+ error: new InsForgeError(
646
+ "An unexpected error occurred while getting session",
647
+ 500,
648
+ "UNEXPECTED_ERROR"
649
+ )
650
+ };
651
+ }
652
+ }
653
+ /**
654
+ * Set/Update the current user's profile
655
+ * Updates profile information in the users table (nickname, avatar_url, bio, etc.)
656
+ */
657
+ async setProfile(profile) {
658
+ const session = this.tokenManager.getSession();
659
+ if (!session?.user?.id) {
660
+ return {
661
+ data: null,
662
+ error: new InsForgeError(
663
+ "No authenticated user found",
664
+ 401,
665
+ "UNAUTHENTICATED"
666
+ )
667
+ };
668
+ }
669
+ return await this.database.from("users").update(profile).eq("id", session.user.id).select().single();
670
+ }
671
+ };
672
+
715
673
  // src/modules/storage.ts
716
674
  var StorageBucket = class {
717
675
  constructor(bucketName, http) {