@insforge/sdk 1.2.1-dev.0 → 1.2.1-dev.2

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 CHANGED
@@ -46,7 +46,8 @@ const insforge = createClient({
46
46
  const { data, error } = await insforge.auth.signUp({
47
47
  email: 'user@example.com',
48
48
  password: 'securePassword123',
49
- name: 'John Doe' // optional
49
+ name: 'John Doe', // optional
50
+ redirectTo: 'http://localhost:3000/verify-email' // optional, for link-based verification
50
51
  });
51
52
 
52
53
  // Sign in with email/password
@@ -78,6 +79,47 @@ const { data: updatedProfile, error } = await insforge.auth.setProfile({
78
79
  await insforge.auth.signOut();
79
80
  ```
80
81
 
82
+ ### Email Verification And Password Reset
83
+
84
+ ```javascript
85
+ // Resend a verification email
86
+ await insforge.auth.resendVerificationEmail({
87
+ email: 'user@example.com',
88
+ redirectTo: 'http://localhost:3000/verify-email' // optional, for link-based verification
89
+ });
90
+
91
+ // Verify email with a 6-digit code
92
+ await insforge.auth.verifyEmail({
93
+ email: 'user@example.com',
94
+ otp: '123456'
95
+ });
96
+
97
+ // Send password reset email
98
+ await insforge.auth.sendResetPasswordEmail({
99
+ email: 'user@example.com',
100
+ redirectTo: 'http://localhost:3000/reset-password' // optional, for link-based reset
101
+ });
102
+
103
+ // Code-based reset flow: exchange the code, then reset the password
104
+ const { data: resetToken } = await insforge.auth.exchangeResetPasswordToken({
105
+ email: 'user@example.com',
106
+ code: '123456'
107
+ });
108
+
109
+ if (resetToken) {
110
+ await insforge.auth.resetPassword({
111
+ newPassword: 'newSecurePassword123',
112
+ otp: resetToken.token
113
+ });
114
+ }
115
+ ```
116
+
117
+ For link-based verification and password reset, users click the emailed browser links:
118
+ - `GET /api/auth/email/verify-link`
119
+ - `GET /api/auth/email/reset-password-link`
120
+
121
+ Those backend endpoints validate the token first, then redirect the browser to your `redirectTo` URL.
122
+
81
123
  ### Database Operations
82
124
 
83
125
  ```javascript
@@ -201,14 +243,14 @@ const insforge = createClient({
201
243
  The SDK is written in TypeScript and provides full type definitions:
202
244
 
203
245
  ```typescript
204
- import { createClient, InsForgeClient, User, Session } from '@insforge/sdk';
246
+ import { createClient, InsForgeClient } from '@insforge/sdk';
205
247
 
206
248
  const insforge: InsForgeClient = createClient({
207
249
  baseUrl: 'http://localhost:7130'
208
250
  });
209
251
 
210
252
  // Type-safe API calls
211
- const response: { data: User | null; error: Error | null } =
253
+ const response =
212
254
  await insforge.auth.getCurrentUser();
213
255
  ```
214
256
 
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { UserSchema, CreateUserRequest, CreateUserResponse, CreateSessionRequest, CreateSessionResponse, OAuthProvidersSchema, RefreshSessionResponse, GetProfileResponse, SendVerificationEmailRequest, VerifyEmailRequest, VerifyEmailResponse, SendResetPasswordEmailRequest, ExchangeResetPasswordTokenRequest, GetPublicAuthConfigResponse, StorageFileSchema, ListObjectsResponseSchema, ChatCompletionRequest, ImageGenerationRequest, EmbeddingsRequest, SubscribeResponse, SocketMessage, SendRawEmailRequest, SendEmailResponse } from '@insforge/shared-schemas';
1
+ import { UserSchema, CreateUserRequest, CreateUserResponse, CreateSessionRequest, CreateSessionResponse, OAuthProvidersSchema, RefreshSessionResponse, GetProfileResponse, SendVerificationEmailRequest, VerifyEmailRequest, VerifyEmailResponse, SendResetPasswordEmailRequest, ExchangeResetPasswordTokenRequest, ExchangeResetPasswordTokenResponse, ResetPasswordResponse, GetPublicAuthConfigResponse, StorageFileSchema, ListObjectsResponseSchema, ChatCompletionRequest, ImageGenerationRequest, EmbeddingsRequest, SubscribeResponse, SocketMessage, SendRawEmailRequest, SendEmailResponse } from '@insforge/shared-schemas';
2
2
  export { AuthErrorResponse, CreateSessionRequest, CreateUserRequest, RealtimeErrorPayload, SendRawEmailRequest as SendEmailOptions, SendEmailResponse, SocketMessage, SubscribeResponse, UserSchema } from '@insforge/shared-schemas';
3
3
  import * as _supabase_postgrest_js from '@supabase/postgrest-js';
4
4
 
@@ -74,12 +74,24 @@ interface InsForgeConfig {
74
74
  * @default 500
75
75
  */
76
76
  retryDelay?: number;
77
+ /**
78
+ * Automatically refresh the access token when a request fails with 401 INVALID_TOKEN.
79
+ * When true, the SDK will attempt a token refresh and retry the original request.
80
+ * @default true
81
+ */
82
+ autoRefreshToken?: boolean;
77
83
  }
78
84
  interface AuthSession {
79
85
  user: UserSchema;
80
86
  accessToken: string;
81
87
  expiresAt?: Date;
82
88
  }
89
+ interface AuthRefreshResponse {
90
+ user: UserSchema;
91
+ accessToken: string;
92
+ csrfToken?: string;
93
+ refreshToken?: string;
94
+ }
83
95
  interface ApiError {
84
96
  error: string;
85
97
  message: string;
@@ -158,8 +170,51 @@ declare class Logger {
158
170
  logResponse(method: string, url: string, status: number, durationMs: number, body?: any): void;
159
171
  }
160
172
 
161
- interface RequestOptions extends RequestInit {
173
+ /**
174
+ * Token Manager for InsForge SDK
175
+ *
176
+ * Memory-only token storage.
177
+ */
178
+
179
+ declare class TokenManager {
180
+ private accessToken;
181
+ private user;
182
+ onTokenChange: (() => void) | null;
183
+ constructor();
184
+ /**
185
+ * Save session in memory
186
+ */
187
+ saveSession(session: AuthSession): void;
188
+ /**
189
+ * Get current session
190
+ */
191
+ getSession(): AuthSession | null;
192
+ /**
193
+ * Get access token
194
+ */
195
+ getAccessToken(): string | null;
196
+ /**
197
+ * Set access token
198
+ */
199
+ setAccessToken(token: string): void;
200
+ /**
201
+ * Get user
202
+ */
203
+ getUser(): UserSchema | null;
204
+ /**
205
+ * Set user
206
+ */
207
+ setUser(user: UserSchema): void;
208
+ /**
209
+ * Clear in-memory session
210
+ */
211
+ clearSession(): void;
212
+ }
213
+
214
+ type JsonRequestBody = Record<string, unknown> | unknown[] | null;
215
+ interface RequestOptions extends Omit<RequestInit, 'body'> {
162
216
  params?: Record<string, string>;
217
+ body?: RequestInit['body'] | JsonRequestBody;
163
218
  /** Allow retrying non-idempotent requests (POST, PATCH). Off by default to prevent duplicate writes. */
164
219
  idempotent?: boolean;
165
220
  }
@@ -174,15 +229,21 @@ declare class HttpClient {
174
229
  private anonKey;
175
230
  private userToken;
176
231
  private logger;
232
+ private autoRefreshToken;
233
+ private isRefreshing;
234
+ private refreshPromise;
235
+ private tokenManager;
236
+ private refreshToken;
177
237
  private timeout;
178
238
  private retryCount;
179
239
  private retryDelay;
180
240
  /**
181
241
  * Creates a new HttpClient instance.
182
242
  * @param config - SDK configuration including baseUrl, timeout, retry settings, and fetch implementation.
243
+ * @param tokenManager - Token manager for session persistence.
183
244
  * @param logger - Optional logger instance for request/response debugging.
184
245
  */
185
- constructor(config: InsForgeConfig, logger?: Logger);
246
+ constructor(config: InsForgeConfig, tokenManager?: TokenManager, logger?: Logger);
186
247
  /**
187
248
  * Builds a full URL from a path and optional query parameters.
188
249
  * Normalizes PostgREST select parameters for proper syntax.
@@ -206,6 +267,7 @@ declare class HttpClient {
206
267
  * @returns Parsed response data.
207
268
  * @throws {InsForgeError} On timeout, network failure, or HTTP error responses.
208
269
  */
270
+ private handleRequest;
209
271
  request<T>(method: string, path: string, options?: RequestOptions): Promise<T>;
210
272
  /** Performs a GET request. */
211
273
  get<T>(path: string, options?: RequestOptions): Promise<T>;
@@ -219,49 +281,10 @@ declare class HttpClient {
219
281
  delete<T>(path: string, options?: RequestOptions): Promise<T>;
220
282
  /** Sets or clears the user authentication token for subsequent requests. */
221
283
  setAuthToken(token: string | null): void;
284
+ setRefreshToken(token: string | null): void;
222
285
  /** Returns the current default headers including the authorization header if set. */
223
286
  getHeaders(): Record<string, string>;
224
- }
225
-
226
- /**
227
- * Token Manager for InsForge SDK
228
- *
229
- * Memory-only token storage.
230
- */
231
-
232
- declare class TokenManager {
233
- private accessToken;
234
- private user;
235
- onTokenChange: (() => void) | null;
236
- constructor();
237
- /**
238
- * Save session in memory
239
- */
240
- saveSession(session: AuthSession): void;
241
- /**
242
- * Get current session
243
- */
244
- getSession(): AuthSession | null;
245
- /**
246
- * Get access token
247
- */
248
- getAccessToken(): string | null;
249
- /**
250
- * Set access token
251
- */
252
- setAccessToken(token: string): void;
253
- /**
254
- * Get user
255
- */
256
- getUser(): UserSchema | null;
257
- /**
258
- * Set user
259
- */
260
- setUser(user: UserSchema): void;
261
- /**
262
- * Clear in-memory session
263
- */
264
- clearSession(): void;
287
+ handleTokenRefresh(): Promise<AuthRefreshResponse>;
265
288
  }
266
289
 
267
290
  /**
@@ -286,7 +309,7 @@ declare class Auth {
286
309
  private saveSessionFromResponse;
287
310
  /**
288
311
  * Detect and handle OAuth callback parameters in URL
289
- * Supports PKCE flow (insforge_code) and legacy flow (access_token in URL)
312
+ * Supports PKCE flow (insforge_code)
290
313
  */
291
314
  private detectAuthCallback;
292
315
  signUp(request: CreateUserRequest): Promise<{
@@ -320,12 +343,7 @@ declare class Auth {
320
343
  * Called automatically on initialization when insforge_code is in URL
321
344
  */
322
345
  exchangeOAuthCode(code: string, codeVerifier?: string): Promise<{
323
- data: {
324
- accessToken: string;
325
- refreshToken?: string;
326
- user: UserSchema;
327
- redirectTo?: string;
328
- } | null;
346
+ data: CreateSessionResponse | null;
329
347
  error: InsForgeError | null;
330
348
  }>;
331
349
  /**
@@ -339,11 +357,7 @@ declare class Auth {
339
357
  provider: 'google';
340
358
  token: string;
341
359
  }): Promise<{
342
- data: {
343
- accessToken: string;
344
- refreshToken?: string;
345
- user: UserSchema;
346
- } | null;
360
+ data: CreateSessionResponse | null;
347
361
  error: InsForgeError | null;
348
362
  }>;
349
363
  /**
@@ -385,14 +399,6 @@ declare class Auth {
385
399
  } | null;
386
400
  error: InsForgeError | null;
387
401
  }>;
388
- /** @deprecated Use `resendVerificationEmail` instead */
389
- sendVerificationEmail(request: SendVerificationEmailRequest): Promise<{
390
- data: {
391
- success: boolean;
392
- message: string;
393
- } | null;
394
- error: InsForgeError | null;
395
- }>;
396
402
  verifyEmail(request: VerifyEmailRequest): Promise<{
397
403
  data: VerifyEmailResponse | null;
398
404
  error: InsForgeError | null;
@@ -405,20 +411,14 @@ declare class Auth {
405
411
  error: InsForgeError | null;
406
412
  }>;
407
413
  exchangeResetPasswordToken(request: ExchangeResetPasswordTokenRequest): Promise<{
408
- data: {
409
- token: string;
410
- expiresAt: string;
411
- } | null;
414
+ data: ExchangeResetPasswordTokenResponse | null;
412
415
  error: InsForgeError | null;
413
416
  }>;
414
417
  resetPassword(request: {
415
418
  newPassword: string;
416
419
  otp: string;
417
420
  }): Promise<{
418
- data: {
419
- message: string;
420
- redirectTo?: string;
421
- } | null;
421
+ data: ResetPasswordResponse | null;
422
422
  error: InsForgeError | null;
423
423
  }>;
424
424
  getPublicAuthConfig(): Promise<{
@@ -792,7 +792,7 @@ declare class Functions {
792
792
  */
793
793
  invoke<T = any>(slug: string, options?: FunctionInvokeOptions): Promise<{
794
794
  data: T | null;
795
- error: Error | null;
795
+ error: InsForgeError | null;
796
796
  }>;
797
797
  }
798
798
 
@@ -967,7 +967,7 @@ declare class Emails {
967
967
  */
968
968
  send(options: SendRawEmailRequest): Promise<{
969
969
  data: SendEmailResponse | null;
970
- error: Error | null;
970
+ error: InsForgeError | null;
971
971
  }>;
972
972
  }
973
973
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { UserSchema, CreateUserRequest, CreateUserResponse, CreateSessionRequest, CreateSessionResponse, OAuthProvidersSchema, RefreshSessionResponse, GetProfileResponse, SendVerificationEmailRequest, VerifyEmailRequest, VerifyEmailResponse, SendResetPasswordEmailRequest, ExchangeResetPasswordTokenRequest, GetPublicAuthConfigResponse, StorageFileSchema, ListObjectsResponseSchema, ChatCompletionRequest, ImageGenerationRequest, EmbeddingsRequest, SubscribeResponse, SocketMessage, SendRawEmailRequest, SendEmailResponse } from '@insforge/shared-schemas';
1
+ import { UserSchema, CreateUserRequest, CreateUserResponse, CreateSessionRequest, CreateSessionResponse, OAuthProvidersSchema, RefreshSessionResponse, GetProfileResponse, SendVerificationEmailRequest, VerifyEmailRequest, VerifyEmailResponse, SendResetPasswordEmailRequest, ExchangeResetPasswordTokenRequest, ExchangeResetPasswordTokenResponse, ResetPasswordResponse, GetPublicAuthConfigResponse, StorageFileSchema, ListObjectsResponseSchema, ChatCompletionRequest, ImageGenerationRequest, EmbeddingsRequest, SubscribeResponse, SocketMessage, SendRawEmailRequest, SendEmailResponse } from '@insforge/shared-schemas';
2
2
  export { AuthErrorResponse, CreateSessionRequest, CreateUserRequest, RealtimeErrorPayload, SendRawEmailRequest as SendEmailOptions, SendEmailResponse, SocketMessage, SubscribeResponse, UserSchema } from '@insforge/shared-schemas';
3
3
  import * as _supabase_postgrest_js from '@supabase/postgrest-js';
4
4
 
@@ -74,12 +74,24 @@ interface InsForgeConfig {
74
74
  * @default 500
75
75
  */
76
76
  retryDelay?: number;
77
+ /**
78
+ * Automatically refresh the access token when a request fails with 401 INVALID_TOKEN.
79
+ * When true, the SDK will attempt a token refresh and retry the original request.
80
+ * @default true
81
+ */
82
+ autoRefreshToken?: boolean;
77
83
  }
78
84
  interface AuthSession {
79
85
  user: UserSchema;
80
86
  accessToken: string;
81
87
  expiresAt?: Date;
82
88
  }
89
+ interface AuthRefreshResponse {
90
+ user: UserSchema;
91
+ accessToken: string;
92
+ csrfToken?: string;
93
+ refreshToken?: string;
94
+ }
83
95
  interface ApiError {
84
96
  error: string;
85
97
  message: string;
@@ -158,8 +170,51 @@ declare class Logger {
158
170
  logResponse(method: string, url: string, status: number, durationMs: number, body?: any): void;
159
171
  }
160
172
 
161
- interface RequestOptions extends RequestInit {
173
+ /**
174
+ * Token Manager for InsForge SDK
175
+ *
176
+ * Memory-only token storage.
177
+ */
178
+
179
+ declare class TokenManager {
180
+ private accessToken;
181
+ private user;
182
+ onTokenChange: (() => void) | null;
183
+ constructor();
184
+ /**
185
+ * Save session in memory
186
+ */
187
+ saveSession(session: AuthSession): void;
188
+ /**
189
+ * Get current session
190
+ */
191
+ getSession(): AuthSession | null;
192
+ /**
193
+ * Get access token
194
+ */
195
+ getAccessToken(): string | null;
196
+ /**
197
+ * Set access token
198
+ */
199
+ setAccessToken(token: string): void;
200
+ /**
201
+ * Get user
202
+ */
203
+ getUser(): UserSchema | null;
204
+ /**
205
+ * Set user
206
+ */
207
+ setUser(user: UserSchema): void;
208
+ /**
209
+ * Clear in-memory session
210
+ */
211
+ clearSession(): void;
212
+ }
213
+
214
+ type JsonRequestBody = Record<string, unknown> | unknown[] | null;
215
+ interface RequestOptions extends Omit<RequestInit, 'body'> {
162
216
  params?: Record<string, string>;
217
+ body?: RequestInit['body'] | JsonRequestBody;
163
218
  /** Allow retrying non-idempotent requests (POST, PATCH). Off by default to prevent duplicate writes. */
164
219
  idempotent?: boolean;
165
220
  }
@@ -174,15 +229,21 @@ declare class HttpClient {
174
229
  private anonKey;
175
230
  private userToken;
176
231
  private logger;
232
+ private autoRefreshToken;
233
+ private isRefreshing;
234
+ private refreshPromise;
235
+ private tokenManager;
236
+ private refreshToken;
177
237
  private timeout;
178
238
  private retryCount;
179
239
  private retryDelay;
180
240
  /**
181
241
  * Creates a new HttpClient instance.
182
242
  * @param config - SDK configuration including baseUrl, timeout, retry settings, and fetch implementation.
243
+ * @param tokenManager - Token manager for session persistence.
183
244
  * @param logger - Optional logger instance for request/response debugging.
184
245
  */
185
- constructor(config: InsForgeConfig, logger?: Logger);
246
+ constructor(config: InsForgeConfig, tokenManager?: TokenManager, logger?: Logger);
186
247
  /**
187
248
  * Builds a full URL from a path and optional query parameters.
188
249
  * Normalizes PostgREST select parameters for proper syntax.
@@ -206,6 +267,7 @@ declare class HttpClient {
206
267
  * @returns Parsed response data.
207
268
  * @throws {InsForgeError} On timeout, network failure, or HTTP error responses.
208
269
  */
270
+ private handleRequest;
209
271
  request<T>(method: string, path: string, options?: RequestOptions): Promise<T>;
210
272
  /** Performs a GET request. */
211
273
  get<T>(path: string, options?: RequestOptions): Promise<T>;
@@ -219,49 +281,10 @@ declare class HttpClient {
219
281
  delete<T>(path: string, options?: RequestOptions): Promise<T>;
220
282
  /** Sets or clears the user authentication token for subsequent requests. */
221
283
  setAuthToken(token: string | null): void;
284
+ setRefreshToken(token: string | null): void;
222
285
  /** Returns the current default headers including the authorization header if set. */
223
286
  getHeaders(): Record<string, string>;
224
- }
225
-
226
- /**
227
- * Token Manager for InsForge SDK
228
- *
229
- * Memory-only token storage.
230
- */
231
-
232
- declare class TokenManager {
233
- private accessToken;
234
- private user;
235
- onTokenChange: (() => void) | null;
236
- constructor();
237
- /**
238
- * Save session in memory
239
- */
240
- saveSession(session: AuthSession): void;
241
- /**
242
- * Get current session
243
- */
244
- getSession(): AuthSession | null;
245
- /**
246
- * Get access token
247
- */
248
- getAccessToken(): string | null;
249
- /**
250
- * Set access token
251
- */
252
- setAccessToken(token: string): void;
253
- /**
254
- * Get user
255
- */
256
- getUser(): UserSchema | null;
257
- /**
258
- * Set user
259
- */
260
- setUser(user: UserSchema): void;
261
- /**
262
- * Clear in-memory session
263
- */
264
- clearSession(): void;
287
+ handleTokenRefresh(): Promise<AuthRefreshResponse>;
265
288
  }
266
289
 
267
290
  /**
@@ -286,7 +309,7 @@ declare class Auth {
286
309
  private saveSessionFromResponse;
287
310
  /**
288
311
  * Detect and handle OAuth callback parameters in URL
289
- * Supports PKCE flow (insforge_code) and legacy flow (access_token in URL)
312
+ * Supports PKCE flow (insforge_code)
290
313
  */
291
314
  private detectAuthCallback;
292
315
  signUp(request: CreateUserRequest): Promise<{
@@ -320,12 +343,7 @@ declare class Auth {
320
343
  * Called automatically on initialization when insforge_code is in URL
321
344
  */
322
345
  exchangeOAuthCode(code: string, codeVerifier?: string): Promise<{
323
- data: {
324
- accessToken: string;
325
- refreshToken?: string;
326
- user: UserSchema;
327
- redirectTo?: string;
328
- } | null;
346
+ data: CreateSessionResponse | null;
329
347
  error: InsForgeError | null;
330
348
  }>;
331
349
  /**
@@ -339,11 +357,7 @@ declare class Auth {
339
357
  provider: 'google';
340
358
  token: string;
341
359
  }): Promise<{
342
- data: {
343
- accessToken: string;
344
- refreshToken?: string;
345
- user: UserSchema;
346
- } | null;
360
+ data: CreateSessionResponse | null;
347
361
  error: InsForgeError | null;
348
362
  }>;
349
363
  /**
@@ -385,14 +399,6 @@ declare class Auth {
385
399
  } | null;
386
400
  error: InsForgeError | null;
387
401
  }>;
388
- /** @deprecated Use `resendVerificationEmail` instead */
389
- sendVerificationEmail(request: SendVerificationEmailRequest): Promise<{
390
- data: {
391
- success: boolean;
392
- message: string;
393
- } | null;
394
- error: InsForgeError | null;
395
- }>;
396
402
  verifyEmail(request: VerifyEmailRequest): Promise<{
397
403
  data: VerifyEmailResponse | null;
398
404
  error: InsForgeError | null;
@@ -405,20 +411,14 @@ declare class Auth {
405
411
  error: InsForgeError | null;
406
412
  }>;
407
413
  exchangeResetPasswordToken(request: ExchangeResetPasswordTokenRequest): Promise<{
408
- data: {
409
- token: string;
410
- expiresAt: string;
411
- } | null;
414
+ data: ExchangeResetPasswordTokenResponse | null;
412
415
  error: InsForgeError | null;
413
416
  }>;
414
417
  resetPassword(request: {
415
418
  newPassword: string;
416
419
  otp: string;
417
420
  }): Promise<{
418
- data: {
419
- message: string;
420
- redirectTo?: string;
421
- } | null;
421
+ data: ResetPasswordResponse | null;
422
422
  error: InsForgeError | null;
423
423
  }>;
424
424
  getPublicAuthConfig(): Promise<{
@@ -792,7 +792,7 @@ declare class Functions {
792
792
  */
793
793
  invoke<T = any>(slug: string, options?: FunctionInvokeOptions): Promise<{
794
794
  data: T | null;
795
- error: Error | null;
795
+ error: InsForgeError | null;
796
796
  }>;
797
797
  }
798
798
 
@@ -967,7 +967,7 @@ declare class Emails {
967
967
  */
968
968
  send(options: SendRawEmailRequest): Promise<{
969
969
  data: SendEmailResponse | null;
970
- error: Error | null;
970
+ error: InsForgeError | null;
971
971
  }>;
972
972
  }
973
973