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