@insforge/sdk 0.0.58-dev.0 → 0.0.58-dev.10

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.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { UserSchema, CreateUserRequest, CreateUserResponse, CreateSessionRequest, CreateSessionResponse, OAuthProvidersSchema, StorageFileSchema, ListObjectsResponseSchema, ChatCompletionRequest, ImageGenerationRequest } from '@insforge/shared-schemas';
1
+ import { UserSchema, CreateUserRequest, CreateUserResponse, CreateSessionRequest, CreateSessionResponse, OAuthProvidersSchema, GetPublicAuthConfigResponse, UserIdSchema, EmailSchema, RoleSchema, StorageFileSchema, ListObjectsResponseSchema, ChatCompletionRequest, ImageGenerationRequest } from '@insforge/shared-schemas';
2
2
  export { AuthErrorResponse, CreateSessionRequest, CreateUserRequest, UserSchema } from '@insforge/shared-schemas';
3
3
  import * as _supabase_postgrest_js from '@supabase/postgrest-js';
4
4
 
@@ -106,6 +106,21 @@ declare class TokenManager {
106
106
  * Uses shared schemas for type safety
107
107
  */
108
108
 
109
+ /**
110
+ * Dynamic profile type - represents flexible profile data from database
111
+ * Fields can vary based on database schema configuration.
112
+ * All fields are converted from snake_case (database) to camelCase (API)
113
+ */
114
+ type ProfileData = Record<string, any> & {
115
+ id: string;
116
+ createdAt?: string;
117
+ updatedAt?: string;
118
+ };
119
+ /**
120
+ * Dynamic profile update type - for updating profile fields
121
+ * Supports any fields that exist in the profile table
122
+ */
123
+ type UpdateProfileData = Partial<Record<string, any>>;
109
124
  declare class Auth {
110
125
  private http;
111
126
  private tokenManager;
@@ -151,23 +166,47 @@ declare class Auth {
151
166
  signOut(): Promise<{
152
167
  error: InsForgeError | null;
153
168
  }>;
169
+ /**
170
+ * Get all public authentication configuration (OAuth + Email)
171
+ * Returns both OAuth providers and email authentication settings in one request
172
+ * This is a public endpoint that doesn't require authentication
173
+ *
174
+ * @returns Complete public authentication configuration including OAuth providers and email auth settings
175
+ *
176
+ * @example
177
+ * ```ts
178
+ * const { data, error } = await insforge.auth.getPublicAuthConfig();
179
+ * if (data) {
180
+ * console.log(`OAuth providers: ${data.oauth.data.length}`);
181
+ * console.log(`Password min length: ${data.email.passwordMinLength}`);
182
+ * }
183
+ * ```
184
+ */
185
+ getPublicAuthConfig(): Promise<{
186
+ data: GetPublicAuthConfigResponse | null;
187
+ error: InsForgeError | null;
188
+ }>;
154
189
  /**
155
190
  * Get the current user with full profile information
156
- * Returns both auth info (id, email, role) and profile data (nickname, avatar_url, bio, etc.)
191
+ * Returns both auth info (id, email, role) and profile data (dynamic fields from users table)
157
192
  */
158
193
  getCurrentUser(): Promise<{
159
194
  data: {
160
- user: any;
161
- profile: any;
195
+ user: {
196
+ id: UserIdSchema;
197
+ email: EmailSchema;
198
+ role: RoleSchema;
199
+ };
200
+ profile: ProfileData | null;
162
201
  } | null;
163
202
  error: any | null;
164
203
  }>;
165
204
  /**
166
205
  * Get any user's profile by ID
167
- * Returns profile information from the users table (nickname, avatar_url, bio, etc.)
206
+ * Returns profile information from the users table (dynamic fields)
168
207
  */
169
208
  getProfile(userId: string): Promise<{
170
- data: any | null;
209
+ data: ProfileData | null;
171
210
  error: any | null;
172
211
  }>;
173
212
  /**
@@ -182,24 +221,19 @@ declare class Auth {
182
221
  };
183
222
  /**
184
223
  * Set/Update the current user's profile
185
- * Updates profile information in the users table (nickname, avatar_url, bio, etc.)
186
- */
187
- setProfile(profile: {
188
- nickname?: string;
189
- avatar_url?: string;
190
- bio?: string;
191
- birthday?: string;
192
- [key: string]: any;
193
- }): Promise<{
194
- data: any | null;
224
+ * Updates profile information in the users table (supports any dynamic fields)
225
+ */
226
+ setProfile(profile: UpdateProfileData): Promise<{
227
+ data: ProfileData | null;
195
228
  error: any | null;
196
229
  }>;
197
230
  /**
198
- * Send email verification code or link
199
- * @param type - 'code' for numeric OTP (6 digits) or 'link' for verification link
200
- * @param email - Email address to send verification to
231
+ * Send password reset code to user's email
232
+ * Always returns success to prevent user enumeration
201
233
  */
202
- sendEmailVerification(type: 'code' | 'link', email: string): Promise<{
234
+ sendPasswordResetCode(request: {
235
+ email: string;
236
+ }): Promise<{
203
237
  data: {
204
238
  success: boolean;
205
239
  message: string;
@@ -207,38 +241,39 @@ declare class Auth {
207
241
  error: InsForgeError | null;
208
242
  }>;
209
243
  /**
210
- * Verify email with OTP
211
- * @param otp - 6-digit numeric code (with email) or 64-char hex token (without email)
212
- * @param email - Optional email address (required for code verification)
213
- */
214
- verifyEmail(otp: string, email?: string): Promise<{
215
- data: CreateSessionResponse | null;
216
- error: InsForgeError | null;
217
- }>;
218
- /**
219
- * Send password reset code or link
220
- * @param type - 'code' for numeric OTP (6 digits) or 'link' for reset link
221
- * @param email - Email address to send reset instructions to
244
+ * Reset password with OTP token
245
+ * Token can be from magic link or from code verification
222
246
  */
223
- sendResetPasswordEmail(type: 'code' | 'link', email: string): Promise<{
247
+ resetPassword(request: {
248
+ newPassword: string;
249
+ otp: string;
250
+ }): Promise<{
224
251
  data: {
225
- success: boolean;
226
252
  message: string;
253
+ redirectTo?: string;
227
254
  } | null;
228
255
  error: InsForgeError | null;
229
256
  }>;
230
257
  /**
231
- * Reset password with OTP
232
- * @param otp - 6-digit numeric code (with email) or 64-char hex token (without email)
233
- * @param newPassword - New password to set
234
- * @param email - Optional email address (required for code-based reset)
258
+ * Verify email with OTP token
259
+ * If email is provided: uses numeric OTP verification (6-digit code)
260
+ * If email is NOT provided: uses link OTP verification (64-char token)
235
261
  */
236
- resetPassword(otp: string, newPassword: string, email?: string): Promise<{
262
+ verifyEmail(request: {
263
+ email?: string;
264
+ otp: string;
265
+ }): Promise<{
237
266
  data: {
238
- message: string;
267
+ accessToken: string;
268
+ user?: any;
239
269
  } | null;
240
270
  error: InsForgeError | null;
241
271
  }>;
272
+ /**
273
+ * Set the current session
274
+ * This is used to set the session from the OAuth callback
275
+ */
276
+ setSession(session: AuthSession): Promise<void>;
242
277
  }
243
278
 
244
279
  /**
@@ -572,4 +607,4 @@ declare class InsForgeClient {
572
607
 
573
608
  declare function createClient(config: InsForgeConfig): InsForgeClient;
574
609
 
575
- export { AI, type ApiError, Auth, type AuthSession, type InsForgeConfig as ClientOptions, Database, type FunctionInvokeOptions, Functions, HttpClient, InsForgeClient, type InsForgeConfig, InsForgeError, Storage, StorageBucket, type StorageResponse, TokenManager, type TokenStorage, createClient, InsForgeClient as default };
610
+ export { AI, type ApiError, Auth, type AuthSession, type InsForgeConfig as ClientOptions, Database, type FunctionInvokeOptions, Functions, HttpClient, InsForgeClient, type InsForgeConfig, InsForgeError, type ProfileData, Storage, StorageBucket, type StorageResponse, TokenManager, type TokenStorage, type UpdateProfileData, createClient, InsForgeClient as default };
package/dist/index.js CHANGED
@@ -292,6 +292,28 @@ var Database = class {
292
292
  };
293
293
 
294
294
  // src/modules/auth.ts
295
+ function convertDbProfileToCamelCase(dbProfile) {
296
+ const result = {
297
+ id: dbProfile.id
298
+ };
299
+ if (dbProfile.created_at !== void 0) result.createdAt = dbProfile.created_at;
300
+ if (dbProfile.updated_at !== void 0) result.updatedAt = dbProfile.updated_at;
301
+ Object.keys(dbProfile).forEach((key) => {
302
+ if (key === "id" || key === "created_at" || key === "updated_at") return;
303
+ const camelKey = key.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
304
+ result[camelKey] = dbProfile[key];
305
+ });
306
+ return result;
307
+ }
308
+ function convertCamelCaseToDbProfile(profile) {
309
+ const dbProfile = {};
310
+ Object.keys(profile).forEach((key) => {
311
+ if (profile[key] === void 0) return;
312
+ const snakeKey = key.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
313
+ dbProfile[snakeKey] = profile[key];
314
+ });
315
+ return dbProfile;
316
+ }
295
317
  var Auth = class {
296
318
  constructor(http, tokenManager) {
297
319
  this.http = http;
@@ -381,11 +403,18 @@ var Auth = class {
381
403
  try {
382
404
  const response = await this.http.post("/api/auth/sessions", request);
383
405
  const session = {
384
- accessToken: response.accessToken,
385
- user: response.user
406
+ accessToken: response.accessToken || "",
407
+ user: response.user || {
408
+ id: "",
409
+ email: "",
410
+ name: "",
411
+ emailVerified: false,
412
+ createdAt: "",
413
+ updatedAt: ""
414
+ }
386
415
  };
387
416
  this.tokenManager.saveSession(session);
388
- this.http.setAuthToken(response.accessToken);
417
+ this.http.setAuthToken(response.accessToken || "");
389
418
  return {
390
419
  data: response,
391
420
  error: null
@@ -456,9 +485,46 @@ var Auth = class {
456
485
  };
457
486
  }
458
487
  }
488
+ /**
489
+ * Get all public authentication configuration (OAuth + Email)
490
+ * Returns both OAuth providers and email authentication settings in one request
491
+ * This is a public endpoint that doesn't require authentication
492
+ *
493
+ * @returns Complete public authentication configuration including OAuth providers and email auth settings
494
+ *
495
+ * @example
496
+ * ```ts
497
+ * const { data, error } = await insforge.auth.getPublicAuthConfig();
498
+ * if (data) {
499
+ * console.log(`OAuth providers: ${data.oauth.data.length}`);
500
+ * console.log(`Password min length: ${data.email.passwordMinLength}`);
501
+ * }
502
+ * ```
503
+ */
504
+ async getPublicAuthConfig() {
505
+ try {
506
+ const response = await this.http.get("/api/auth/public-config");
507
+ return {
508
+ data: response,
509
+ error: null
510
+ };
511
+ } catch (error) {
512
+ if (error instanceof InsForgeError) {
513
+ return { data: null, error };
514
+ }
515
+ return {
516
+ data: null,
517
+ error: new InsForgeError(
518
+ "An unexpected error occurred while fetching public authentication configuration",
519
+ 500,
520
+ "UNEXPECTED_ERROR"
521
+ )
522
+ };
523
+ }
524
+ }
459
525
  /**
460
526
  * Get the current user with full profile information
461
- * Returns both auth info (id, email, role) and profile data (nickname, avatar_url, bio, etc.)
527
+ * Returns both auth info (id, email, role) and profile data (dynamic fields from users table)
462
528
  */
463
529
  async getCurrentUser() {
464
530
  try {
@@ -475,7 +541,7 @@ var Auth = class {
475
541
  return {
476
542
  data: {
477
543
  user: authResponse.user,
478
- profile
544
+ profile: profile ? convertDbProfileToCamelCase(profile) : null
479
545
  },
480
546
  error: null
481
547
  };
@@ -499,14 +565,17 @@ var Auth = class {
499
565
  }
500
566
  /**
501
567
  * Get any user's profile by ID
502
- * Returns profile information from the users table (nickname, avatar_url, bio, etc.)
568
+ * Returns profile information from the users table (dynamic fields)
503
569
  */
504
570
  async getProfile(userId) {
505
571
  const { data, error } = await this.database.from("users").select("*").eq("id", userId).single();
506
572
  if (error && error.code === "PGRST116") {
507
573
  return { data: null, error: null };
508
574
  }
509
- return { data, error };
575
+ if (data) {
576
+ return { data: convertDbProfileToCamelCase(data), error: null };
577
+ }
578
+ return { data: null, error };
510
579
  }
511
580
  /**
512
581
  * Get the current session (only session data, no API call)
@@ -536,7 +605,7 @@ var Auth = class {
536
605
  }
537
606
  /**
538
607
  * Set/Update the current user's profile
539
- * Updates profile information in the users table (nickname, avatar_url, bio, etc.)
608
+ * Updates profile information in the users table (supports any dynamic fields)
540
609
  */
541
610
  async setProfile(profile) {
542
611
  const session = this.tokenManager.getSession();
@@ -556,23 +625,38 @@ var Auth = class {
556
625
  return { data: null, error: error2 };
557
626
  }
558
627
  if (data2?.user) {
559
- session.user = data2.user;
628
+ session.user = {
629
+ id: data2.user.id,
630
+ email: data2.user.email,
631
+ name: data2.profile?.nickname || "",
632
+ // Fallback - profile structure is dynamic
633
+ emailVerified: false,
634
+ // Not available from API, but required by UserSchema
635
+ createdAt: (/* @__PURE__ */ new Date()).toISOString(),
636
+ // Fallback
637
+ updatedAt: (/* @__PURE__ */ new Date()).toISOString()
638
+ // Fallback
639
+ };
560
640
  this.tokenManager.saveSession(session);
561
641
  }
562
642
  }
563
- const { data, error } = await this.database.from("users").update(profile).eq("id", session.user.id).select().single();
564
- return { data, error };
643
+ const dbProfile = convertCamelCaseToDbProfile(profile);
644
+ const { data, error } = await this.database.from("users").update(dbProfile).eq("id", session.user.id).select().single();
645
+ if (data) {
646
+ return { data: convertDbProfileToCamelCase(data), error: null };
647
+ }
648
+ return { data: null, error };
565
649
  }
566
650
  /**
567
- * Send email verification code or link
568
- * @param type - 'code' for numeric OTP (6 digits) or 'link' for verification link
569
- * @param email - Email address to send verification to
651
+ * Send password reset code to user's email
652
+ * Always returns success to prevent user enumeration
570
653
  */
571
- async sendEmailVerification(type, email) {
654
+ async sendPasswordResetCode(request) {
572
655
  try {
573
- const request = { email };
574
- const endpoint = type === "code" ? "/api/auth/email/send-verification-code" : "/api/auth/email/send-verification-link";
575
- const response = await this.http.post(endpoint, request);
656
+ const response = await this.http.post(
657
+ "/api/auth/email/send-reset-password-code",
658
+ request
659
+ );
576
660
  return {
577
661
  data: response,
578
662
  error: null
@@ -584,7 +668,7 @@ var Auth = class {
584
668
  return {
585
669
  data: null,
586
670
  error: new InsForgeError(
587
- "An unexpected error occurred while sending verification email",
671
+ "An unexpected error occurred while sending password reset code",
588
672
  500,
589
673
  "UNEXPECTED_ERROR"
590
674
  )
@@ -592,20 +676,15 @@ var Auth = class {
592
676
  }
593
677
  }
594
678
  /**
595
- * Verify email with OTP
596
- * @param otp - 6-digit numeric code (with email) or 64-char hex token (without email)
597
- * @param email - Optional email address (required for code verification)
679
+ * Reset password with OTP token
680
+ * Token can be from magic link or from code verification
598
681
  */
599
- async verifyEmail(otp, email) {
682
+ async resetPassword(request) {
600
683
  try {
601
- const request = { otp, email };
602
- const response = await this.http.post("/api/auth/verify-email", request);
603
- const session = {
604
- accessToken: response.accessToken,
605
- user: response.user
606
- };
607
- this.tokenManager.saveSession(session);
608
- this.http.setAuthToken(response.accessToken);
684
+ const response = await this.http.post(
685
+ "/api/auth/reset-password",
686
+ request
687
+ );
609
688
  return {
610
689
  data: response,
611
690
  error: null
@@ -617,7 +696,7 @@ var Auth = class {
617
696
  return {
618
697
  data: null,
619
698
  error: new InsForgeError(
620
- "An unexpected error occurred while verifying email",
699
+ "An unexpected error occurred while resetting password",
621
700
  500,
622
701
  "UNEXPECTED_ERROR"
623
702
  )
@@ -625,15 +704,24 @@ var Auth = class {
625
704
  }
626
705
  }
627
706
  /**
628
- * Send password reset code or link
629
- * @param type - 'code' for numeric OTP (6 digits) or 'link' for reset link
630
- * @param email - Email address to send reset instructions to
707
+ * Verify email with OTP token
708
+ * If email is provided: uses numeric OTP verification (6-digit code)
709
+ * If email is NOT provided: uses link OTP verification (64-char token)
631
710
  */
632
- async sendResetPasswordEmail(type, email) {
711
+ async verifyEmail(request) {
633
712
  try {
634
- const request = { email };
635
- const endpoint = type === "code" ? "/api/auth/email/send-reset-password-code" : "/api/auth/email/send-reset-password-link";
636
- const response = await this.http.post(endpoint, request);
713
+ const response = await this.http.post(
714
+ "/api/auth/verify-email",
715
+ request
716
+ );
717
+ if (response.accessToken) {
718
+ const session = {
719
+ accessToken: response.accessToken,
720
+ user: response.user || {}
721
+ };
722
+ this.tokenManager.saveSession(session);
723
+ this.http.setAuthToken(response.accessToken);
724
+ }
637
725
  return {
638
726
  data: response,
639
727
  error: null
@@ -645,7 +733,7 @@ var Auth = class {
645
733
  return {
646
734
  data: null,
647
735
  error: new InsForgeError(
648
- "An unexpected error occurred while sending reset password email",
736
+ "An unexpected error occurred while verifying email",
649
737
  500,
650
738
  "UNEXPECTED_ERROR"
651
739
  )
@@ -653,31 +741,19 @@ var Auth = class {
653
741
  }
654
742
  }
655
743
  /**
656
- * Reset password with OTP
657
- * @param otp - 6-digit numeric code (with email) or 64-char hex token (without email)
658
- * @param newPassword - New password to set
659
- * @param email - Optional email address (required for code-based reset)
744
+ * Set the current session
745
+ * This is used to set the session from the OAuth callback
660
746
  */
661
- async resetPassword(otp, newPassword, email) {
747
+ async setSession(session) {
662
748
  try {
663
- const request = { otp, newPassword, email };
664
- const response = await this.http.post("/api/auth/reset-password", request);
665
- return {
666
- data: response,
667
- error: null
668
- };
749
+ this.tokenManager.saveSession(session);
750
+ this.http.setAuthToken(session.accessToken);
669
751
  } catch (error) {
670
- if (error instanceof InsForgeError) {
671
- return { data: null, error };
672
- }
673
- return {
674
- data: null,
675
- error: new InsForgeError(
676
- "An unexpected error occurred while resetting password",
677
- 500,
678
- "UNEXPECTED_ERROR"
679
- )
680
- };
752
+ throw new InsForgeError(
753
+ "An unexpected error occurred while setting session",
754
+ 500,
755
+ "UNEXPECTED_ERROR"
756
+ );
681
757
  }
682
758
  }
683
759
  };