@insforge/sdk 1.2.6 → 1.2.8-dev.0
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 +74 -78
- package/dist/index.d.mts +45 -14
- package/dist/index.d.ts +45 -14
- package/dist/index.js +267 -125
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +267 -125
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -33,10 +33,10 @@ yarn add @insforge/sdk
|
|
|
33
33
|
### Initialize the Client
|
|
34
34
|
|
|
35
35
|
```javascript
|
|
36
|
-
import { createClient } from
|
|
36
|
+
import { createClient } from "@insforge/sdk";
|
|
37
37
|
|
|
38
38
|
const insforge = createClient({
|
|
39
|
-
baseUrl:
|
|
39
|
+
baseUrl: "http://localhost:7130", // Your InsForge backend URL
|
|
40
40
|
});
|
|
41
41
|
```
|
|
42
42
|
|
|
@@ -45,35 +45,35 @@ const insforge = createClient({
|
|
|
45
45
|
```javascript
|
|
46
46
|
// Sign up a new user
|
|
47
47
|
const { data, error } = await insforge.auth.signUp({
|
|
48
|
-
email:
|
|
49
|
-
password:
|
|
50
|
-
name:
|
|
51
|
-
redirectTo:
|
|
48
|
+
email: "user@example.com",
|
|
49
|
+
password: "securePassword123",
|
|
50
|
+
name: "John Doe", // optional
|
|
51
|
+
redirectTo: "http://localhost:3000/sign-in", // optional, recommended for link-based verification
|
|
52
52
|
});
|
|
53
53
|
|
|
54
54
|
// Sign in with email/password
|
|
55
55
|
const { data, error } = await insforge.auth.signInWithPassword({
|
|
56
|
-
email:
|
|
57
|
-
password:
|
|
56
|
+
email: "user@example.com",
|
|
57
|
+
password: "securePassword123",
|
|
58
58
|
});
|
|
59
59
|
|
|
60
60
|
// OAuth authentication (built-in or custom provider key)
|
|
61
61
|
await insforge.auth.signInWithOAuth({
|
|
62
|
-
provider:
|
|
63
|
-
redirectTo:
|
|
62
|
+
provider: "google", // e.g. built-in: "google", custom: "auth0-acme"
|
|
63
|
+
redirectTo: "http://localhost:3000/dashboard",
|
|
64
64
|
});
|
|
65
65
|
|
|
66
66
|
// Get current user (call this during browser app startup)
|
|
67
67
|
const { data: currentUser } = await insforge.auth.getCurrentUser();
|
|
68
68
|
|
|
69
69
|
// Get any user's profile by ID (public endpoint)
|
|
70
|
-
const { data: profile, error } = await insforge.auth.getProfile(
|
|
70
|
+
const { data: profile, error } = await insforge.auth.getProfile("user-id-here");
|
|
71
71
|
|
|
72
72
|
// Update current user's profile (requires authentication)
|
|
73
73
|
const { data: updatedProfile, error } = await insforge.auth.setProfile({
|
|
74
|
-
displayName:
|
|
75
|
-
bio:
|
|
76
|
-
avatarUrl:
|
|
74
|
+
displayName: "John Doe",
|
|
75
|
+
bio: "Software developer",
|
|
76
|
+
avatarUrl: "https://example.com/avatar.jpg",
|
|
77
77
|
});
|
|
78
78
|
|
|
79
79
|
// Sign out
|
|
@@ -85,37 +85,38 @@ await insforge.auth.signOut();
|
|
|
85
85
|
```javascript
|
|
86
86
|
// Resend a verification email
|
|
87
87
|
await insforge.auth.resendVerificationEmail({
|
|
88
|
-
email:
|
|
89
|
-
redirectTo:
|
|
88
|
+
email: "user@example.com",
|
|
89
|
+
redirectTo: "http://localhost:3000/sign-in", // optional, recommended for link-based verification
|
|
90
90
|
});
|
|
91
91
|
|
|
92
92
|
// Verify email with a 6-digit code
|
|
93
93
|
await insforge.auth.verifyEmail({
|
|
94
|
-
email:
|
|
95
|
-
otp:
|
|
94
|
+
email: "user@example.com",
|
|
95
|
+
otp: "123456",
|
|
96
96
|
});
|
|
97
97
|
|
|
98
98
|
// Send password reset email
|
|
99
99
|
await insforge.auth.sendResetPasswordEmail({
|
|
100
|
-
email:
|
|
101
|
-
redirectTo:
|
|
100
|
+
email: "user@example.com",
|
|
101
|
+
redirectTo: "http://localhost:3000/reset-password", // optional, recommended for link-based reset
|
|
102
102
|
});
|
|
103
103
|
|
|
104
104
|
// Code-based reset flow: exchange the code, then reset the password
|
|
105
105
|
const { data: resetToken } = await insforge.auth.exchangeResetPasswordToken({
|
|
106
|
-
email:
|
|
107
|
-
code:
|
|
106
|
+
email: "user@example.com",
|
|
107
|
+
code: "123456",
|
|
108
108
|
});
|
|
109
109
|
|
|
110
110
|
if (resetToken) {
|
|
111
111
|
await insforge.auth.resetPassword({
|
|
112
|
-
newPassword:
|
|
113
|
-
otp: resetToken.token
|
|
112
|
+
newPassword: "newSecurePassword123",
|
|
113
|
+
otp: resetToken.token,
|
|
114
114
|
});
|
|
115
115
|
}
|
|
116
116
|
```
|
|
117
117
|
|
|
118
118
|
For link-based verification and password reset, users click the emailed browser links:
|
|
119
|
+
|
|
119
120
|
- `GET /api/auth/email/verify-link`
|
|
120
121
|
- `GET /api/auth/email/reset-password-link`
|
|
121
122
|
|
|
@@ -130,28 +131,26 @@ Those backend endpoints validate the token first, then redirect the browser to y
|
|
|
130
131
|
```javascript
|
|
131
132
|
// Insert data
|
|
132
133
|
const { data, error } = await insforge.database
|
|
133
|
-
.from(
|
|
134
|
-
.insert([
|
|
135
|
-
{ title: 'My First Post', content: 'Hello World!' }
|
|
136
|
-
]);
|
|
134
|
+
.from("posts")
|
|
135
|
+
.insert([{ title: "My First Post", content: "Hello World!" }]);
|
|
137
136
|
|
|
138
137
|
// Query data
|
|
139
138
|
const { data, error } = await insforge.database
|
|
140
|
-
.from(
|
|
141
|
-
.select(
|
|
142
|
-
.eq(
|
|
139
|
+
.from("posts")
|
|
140
|
+
.select("*")
|
|
141
|
+
.eq("author_id", userId);
|
|
143
142
|
|
|
144
143
|
// Update data
|
|
145
144
|
const { data, error } = await insforge.database
|
|
146
|
-
.from(
|
|
147
|
-
.update({ title:
|
|
148
|
-
.eq(
|
|
145
|
+
.from("posts")
|
|
146
|
+
.update({ title: "Updated Title" })
|
|
147
|
+
.eq("id", postId);
|
|
149
148
|
|
|
150
149
|
// Delete data
|
|
151
150
|
const { data, error } = await insforge.database
|
|
152
|
-
.from(
|
|
151
|
+
.from("posts")
|
|
153
152
|
.delete()
|
|
154
|
-
.eq(
|
|
153
|
+
.eq("id", postId);
|
|
155
154
|
```
|
|
156
155
|
|
|
157
156
|
### File Storage
|
|
@@ -159,32 +158,28 @@ const { data, error } = await insforge.database
|
|
|
159
158
|
```javascript
|
|
160
159
|
// Upload a file
|
|
161
160
|
const file = document.querySelector('input[type="file"]').files[0];
|
|
162
|
-
const { data, error } = await insforge.storage
|
|
163
|
-
.from('avatars')
|
|
164
|
-
.upload(file);
|
|
161
|
+
const { data, error } = await insforge.storage.from("avatars").upload(file);
|
|
165
162
|
|
|
166
163
|
// Download a file
|
|
167
164
|
const { data, error } = await insforge.storage
|
|
168
|
-
.from(
|
|
169
|
-
.download(
|
|
165
|
+
.from("avatars")
|
|
166
|
+
.download("user-avatar.png");
|
|
170
167
|
|
|
171
168
|
// Delete a file
|
|
172
169
|
const { data, error } = await insforge.storage
|
|
173
|
-
.from(
|
|
174
|
-
.remove([
|
|
170
|
+
.from("avatars")
|
|
171
|
+
.remove(["user-avatar.png"]);
|
|
175
172
|
|
|
176
173
|
// List files
|
|
177
|
-
const { data, error } = await insforge.storage
|
|
178
|
-
.from('avatars')
|
|
179
|
-
.list();
|
|
174
|
+
const { data, error } = await insforge.storage.from("avatars").list();
|
|
180
175
|
```
|
|
181
176
|
|
|
182
177
|
### Edge Functions
|
|
183
178
|
|
|
184
179
|
```javascript
|
|
185
180
|
// Invoke an edge function
|
|
186
|
-
const { data, error } = await insforge.functions.invoke(
|
|
187
|
-
body: { key:
|
|
181
|
+
const { data, error } = await insforge.functions.invoke("my-function", {
|
|
182
|
+
body: { key: "value" },
|
|
188
183
|
});
|
|
189
184
|
```
|
|
190
185
|
|
|
@@ -192,13 +187,12 @@ const { data, error } = await insforge.functions.invoke('my-function', {
|
|
|
192
187
|
|
|
193
188
|
```javascript
|
|
194
189
|
// Create and redirect to a Stripe Checkout Session
|
|
195
|
-
const { data, error } = await insforge.payments.createCheckoutSession({
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
lineItems: [{ stripePriceId: 'price_123', quantity: 1 }],
|
|
190
|
+
const { data, error } = await insforge.payments.createCheckoutSession("test", {
|
|
191
|
+
mode: "payment",
|
|
192
|
+
lineItems: [{ stripePriceId: "price_123", quantity: 1 }],
|
|
199
193
|
successUrl: `${window.location.origin}/success`,
|
|
200
194
|
cancelUrl: `${window.location.origin}/pricing`,
|
|
201
|
-
idempotencyKey:
|
|
195
|
+
idempotencyKey: "cart_123",
|
|
202
196
|
});
|
|
203
197
|
|
|
204
198
|
if (!error && data?.checkoutSession.url) {
|
|
@@ -206,25 +200,27 @@ if (!error && data?.checkoutSession.url) {
|
|
|
206
200
|
}
|
|
207
201
|
|
|
208
202
|
// Create a subscription checkout for an app billing subject
|
|
209
|
-
const { data: subscriptionCheckout } =
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
});
|
|
203
|
+
const { data: subscriptionCheckout } =
|
|
204
|
+
await insforge.payments.createCheckoutSession("test", {
|
|
205
|
+
mode: "subscription",
|
|
206
|
+
subject: { type: "team", id: "team_123" },
|
|
207
|
+
lineItems: [{ stripePriceId: "price_monthly_123", quantity: 1 }],
|
|
208
|
+
successUrl: `${window.location.origin}/billing/success`,
|
|
209
|
+
cancelUrl: `${window.location.origin}/billing`,
|
|
210
|
+
});
|
|
217
211
|
|
|
218
212
|
if (subscriptionCheckout?.checkoutSession.url) {
|
|
219
213
|
window.location.assign(subscriptionCheckout.checkoutSession.url);
|
|
220
214
|
}
|
|
221
215
|
|
|
222
216
|
// Let an authenticated customer manage their subscription in Stripe Billing Portal
|
|
223
|
-
const { data: portal } = await insforge.payments.createCustomerPortalSession(
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
217
|
+
const { data: portal } = await insforge.payments.createCustomerPortalSession(
|
|
218
|
+
"test",
|
|
219
|
+
{
|
|
220
|
+
subject: { type: "team", id: "team_123" },
|
|
221
|
+
returnUrl: `${window.location.origin}/billing`,
|
|
222
|
+
},
|
|
223
|
+
);
|
|
228
224
|
|
|
229
225
|
if (portal?.customerPortalSession.url) {
|
|
230
226
|
window.location.assign(portal.customerPortalSession.url);
|
|
@@ -236,14 +232,14 @@ if (portal?.customerPortalSession.url) {
|
|
|
236
232
|
```javascript
|
|
237
233
|
// Generate text completion
|
|
238
234
|
const { data, error } = await insforge.ai.completion({
|
|
239
|
-
model:
|
|
240
|
-
prompt:
|
|
235
|
+
model: "gpt-3.5-turbo",
|
|
236
|
+
prompt: "Write a hello world program",
|
|
241
237
|
});
|
|
242
238
|
|
|
243
239
|
// Analyze an image
|
|
244
240
|
const { data, error } = await insforge.ai.vision({
|
|
245
|
-
imageUrl:
|
|
246
|
-
prompt:
|
|
241
|
+
imageUrl: "https://example.com/image.jpg",
|
|
242
|
+
prompt: "Describe this image",
|
|
247
243
|
});
|
|
248
244
|
```
|
|
249
245
|
|
|
@@ -260,9 +256,9 @@ The SDK supports the following configuration options:
|
|
|
260
256
|
|
|
261
257
|
```javascript
|
|
262
258
|
const insforge = createClient({
|
|
263
|
-
baseUrl:
|
|
264
|
-
anonKey:
|
|
265
|
-
isServerMode: false
|
|
259
|
+
baseUrl: "http://localhost:7130", // Your InsForge backend URL
|
|
260
|
+
anonKey: "your-anon-key", // Optional
|
|
261
|
+
isServerMode: false, // Optional (set true in SSR/server runtime)
|
|
266
262
|
});
|
|
267
263
|
```
|
|
268
264
|
|
|
@@ -276,7 +272,7 @@ Read your access token from cookies in Next.js and pass it as `edgeFunctionToken
|
|
|
276
272
|
Your app should write/update cookies itself after login/refresh.
|
|
277
273
|
|
|
278
274
|
```typescript
|
|
279
|
-
import { createClient } from
|
|
275
|
+
import { createClient } from "@insforge/sdk";
|
|
280
276
|
const accessToken = /* read access token from request cookies */ null;
|
|
281
277
|
|
|
282
278
|
const insforge = createClient({
|
|
@@ -291,15 +287,14 @@ const insforge = createClient({
|
|
|
291
287
|
The SDK is written in TypeScript and provides full type definitions:
|
|
292
288
|
|
|
293
289
|
```typescript
|
|
294
|
-
import { createClient, InsForgeClient } from
|
|
290
|
+
import { createClient, InsForgeClient } from "@insforge/sdk";
|
|
295
291
|
|
|
296
292
|
const insforge: InsForgeClient = createClient({
|
|
297
|
-
baseUrl:
|
|
293
|
+
baseUrl: "http://localhost:7130",
|
|
298
294
|
});
|
|
299
295
|
|
|
300
296
|
// Type-safe API calls
|
|
301
|
-
const response =
|
|
302
|
-
await insforge.auth.getCurrentUser();
|
|
297
|
+
const response = await insforge.auth.getCurrentUser();
|
|
303
298
|
```
|
|
304
299
|
|
|
305
300
|
## Error Handling
|
|
@@ -320,6 +315,7 @@ if (error) {
|
|
|
320
315
|
## Browser Support
|
|
321
316
|
|
|
322
317
|
The SDK works in all modern browsers that support:
|
|
318
|
+
|
|
323
319
|
- ES6+ features
|
|
324
320
|
- Fetch API
|
|
325
321
|
- Cookies (for refresh token flow)
|
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, ExchangeResetPasswordTokenResponse, ResetPasswordResponse, GetPublicAuthConfigResponse, StorageFileSchema, ListObjectsResponseSchema, ChatCompletionRequest, ImageGenerationRequest, EmbeddingsRequest, SubscribeResponse, SocketMessage, SendRawEmailRequest, SendEmailResponse,
|
|
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, StripeEnvironment, CreateCheckoutSessionBody, CreateCheckoutSessionResponse, CreateCustomerPortalSessionBody, CreateCustomerPortalSessionResponse } 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,6 @@ 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;
|
|
83
77
|
}
|
|
84
78
|
interface AuthSession {
|
|
85
79
|
user: UserSchema;
|
|
@@ -217,6 +211,8 @@ interface RequestOptions extends Omit<RequestInit, 'body'> {
|
|
|
217
211
|
body?: RequestInit['body'] | JsonRequestBody;
|
|
218
212
|
/** Allow retrying non-idempotent requests (POST, PATCH). Off by default to prevent duplicate writes. */
|
|
219
213
|
idempotent?: boolean;
|
|
214
|
+
/** Disable automatic access-token refresh for auth/control-flow requests. */
|
|
215
|
+
skipAuthRefresh?: boolean;
|
|
220
216
|
}
|
|
221
217
|
/**
|
|
222
218
|
* HTTP client with built-in retry, timeout, and exponential backoff support.
|
|
@@ -225,11 +221,11 @@ interface RequestOptions extends Omit<RequestInit, 'body'> {
|
|
|
225
221
|
declare class HttpClient {
|
|
226
222
|
readonly baseUrl: string;
|
|
227
223
|
readonly fetch: typeof fetch;
|
|
224
|
+
private readonly config;
|
|
228
225
|
private defaultHeaders;
|
|
229
226
|
private anonKey;
|
|
230
227
|
private userToken;
|
|
231
228
|
private logger;
|
|
232
|
-
private autoRefreshToken;
|
|
233
229
|
private isRefreshing;
|
|
234
230
|
private refreshPromise;
|
|
235
231
|
private tokenManager;
|
|
@@ -257,6 +253,8 @@ declare class HttpClient {
|
|
|
257
253
|
* @returns Delay in milliseconds.
|
|
258
254
|
*/
|
|
259
255
|
private computeRetryDelay;
|
|
256
|
+
private shouldRefreshAccessToken;
|
|
257
|
+
private fetchWithRetry;
|
|
260
258
|
/**
|
|
261
259
|
* Performs an HTTP request with automatic retry and timeout handling.
|
|
262
260
|
* Retries on network errors and 5xx server errors with exponential backoff.
|
|
@@ -269,6 +267,14 @@ declare class HttpClient {
|
|
|
269
267
|
*/
|
|
270
268
|
private handleRequest;
|
|
271
269
|
request<T>(method: string, path: string, options?: RequestOptions): Promise<T>;
|
|
270
|
+
/**
|
|
271
|
+
* Performs an SDK-configured fetch and returns the raw Response.
|
|
272
|
+
* This is used by clients such as postgrest-js that need to own response
|
|
273
|
+
* parsing while still sharing SDK auth and refresh behavior.
|
|
274
|
+
*/
|
|
275
|
+
rawFetch(input: RequestInfo | URL, init?: RequestInit, options?: {
|
|
276
|
+
skipAuthRefresh?: boolean;
|
|
277
|
+
}): Promise<Response>;
|
|
272
278
|
/** Performs a GET request. */
|
|
273
279
|
get<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
274
280
|
/** Performs a POST request with an optional JSON body. */
|
|
@@ -284,7 +290,9 @@ declare class HttpClient {
|
|
|
284
290
|
setRefreshToken(token: string | null): void;
|
|
285
291
|
/** Returns the current default headers including the authorization header if set. */
|
|
286
292
|
getHeaders(): Record<string, string>;
|
|
287
|
-
|
|
293
|
+
refreshAccessToken(): Promise<AuthRefreshResponse>;
|
|
294
|
+
private refreshAndSaveSession;
|
|
295
|
+
private clearAuthSession;
|
|
288
296
|
}
|
|
289
297
|
|
|
290
298
|
/**
|
|
@@ -433,7 +441,7 @@ declare class Auth {
|
|
|
433
441
|
*/
|
|
434
442
|
declare class Database {
|
|
435
443
|
private postgrest;
|
|
436
|
-
constructor(httpClient: HttpClient
|
|
444
|
+
constructor(httpClient: HttpClient);
|
|
437
445
|
/**
|
|
438
446
|
* Create a query builder for a table
|
|
439
447
|
*
|
|
@@ -993,8 +1001,7 @@ declare class Payments {
|
|
|
993
1001
|
*
|
|
994
1002
|
* @example
|
|
995
1003
|
* ```typescript
|
|
996
|
-
* const { data, error } = await client.payments.createCheckoutSession({
|
|
997
|
-
* environment: 'test',
|
|
1004
|
+
* const { data, error } = await client.payments.createCheckoutSession('test', {
|
|
998
1005
|
* mode: 'payment',
|
|
999
1006
|
* lineItems: [{ stripePriceId: 'price_123', quantity: 1 }],
|
|
1000
1007
|
* successUrl: `${window.location.origin}/success`,
|
|
@@ -1006,11 +1013,11 @@ declare class Payments {
|
|
|
1006
1013
|
* }
|
|
1007
1014
|
* ```
|
|
1008
1015
|
*/
|
|
1009
|
-
createCheckoutSession(request:
|
|
1016
|
+
createCheckoutSession(environment: StripeEnvironment, request: CreateCheckoutSessionBody): Promise<PaymentsResponse<CreateCheckoutSessionResponse>>;
|
|
1010
1017
|
/**
|
|
1011
1018
|
* Create a Stripe Billing Portal Session for a mapped billing subject.
|
|
1012
1019
|
*/
|
|
1013
|
-
createCustomerPortalSession(request:
|
|
1020
|
+
createCustomerPortalSession(environment: StripeEnvironment, request: CreateCustomerPortalSessionBody): Promise<PaymentsResponse<CreateCustomerPortalSessionResponse>>;
|
|
1014
1021
|
}
|
|
1015
1022
|
|
|
1016
1023
|
/**
|
|
@@ -1079,6 +1086,30 @@ declare class InsForgeClient {
|
|
|
1079
1086
|
* ```
|
|
1080
1087
|
*/
|
|
1081
1088
|
getHttpClient(): HttpClient;
|
|
1089
|
+
/**
|
|
1090
|
+
* Set the access token used by every SDK surface. Updates both the HTTP
|
|
1091
|
+
* client (database / storage / functions / AI / emails) and the realtime
|
|
1092
|
+
* token manager (which fires `onTokenChange` to reconnect the WebSocket
|
|
1093
|
+
* with the new bearer). Pass `null` to clear.
|
|
1094
|
+
*
|
|
1095
|
+
* Use this when an external auth provider (Better Auth, Clerk, Auth0,
|
|
1096
|
+
* WorkOS, Kinde, Stytch, …) issues the JWT and you need to keep the
|
|
1097
|
+
* long-lived InsForge client in sync. Without this, you'd have to call
|
|
1098
|
+
* `client.getHttpClient().setAuthToken(token)` AND reach into the private
|
|
1099
|
+
* `client.realtime.tokenManager.setAccessToken(token)` separately —
|
|
1100
|
+
* forgetting the second one silently breaks realtime auth.
|
|
1101
|
+
*
|
|
1102
|
+
* @example
|
|
1103
|
+
* ```typescript
|
|
1104
|
+
* // Refresh a third-party-issued JWT periodically
|
|
1105
|
+
* const { token } = await fetch('/api/insforge-token').then((r) => r.json());
|
|
1106
|
+
* client.setAccessToken(token);
|
|
1107
|
+
*
|
|
1108
|
+
* // Sign-out
|
|
1109
|
+
* client.setAccessToken(null);
|
|
1110
|
+
* ```
|
|
1111
|
+
*/
|
|
1112
|
+
setAccessToken(token: string | null): void;
|
|
1082
1113
|
}
|
|
1083
1114
|
|
|
1084
1115
|
/**
|
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, ExchangeResetPasswordTokenResponse, ResetPasswordResponse, GetPublicAuthConfigResponse, StorageFileSchema, ListObjectsResponseSchema, ChatCompletionRequest, ImageGenerationRequest, EmbeddingsRequest, SubscribeResponse, SocketMessage, SendRawEmailRequest, SendEmailResponse,
|
|
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, StripeEnvironment, CreateCheckoutSessionBody, CreateCheckoutSessionResponse, CreateCustomerPortalSessionBody, CreateCustomerPortalSessionResponse } 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,6 @@ 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;
|
|
83
77
|
}
|
|
84
78
|
interface AuthSession {
|
|
85
79
|
user: UserSchema;
|
|
@@ -217,6 +211,8 @@ interface RequestOptions extends Omit<RequestInit, 'body'> {
|
|
|
217
211
|
body?: RequestInit['body'] | JsonRequestBody;
|
|
218
212
|
/** Allow retrying non-idempotent requests (POST, PATCH). Off by default to prevent duplicate writes. */
|
|
219
213
|
idempotent?: boolean;
|
|
214
|
+
/** Disable automatic access-token refresh for auth/control-flow requests. */
|
|
215
|
+
skipAuthRefresh?: boolean;
|
|
220
216
|
}
|
|
221
217
|
/**
|
|
222
218
|
* HTTP client with built-in retry, timeout, and exponential backoff support.
|
|
@@ -225,11 +221,11 @@ interface RequestOptions extends Omit<RequestInit, 'body'> {
|
|
|
225
221
|
declare class HttpClient {
|
|
226
222
|
readonly baseUrl: string;
|
|
227
223
|
readonly fetch: typeof fetch;
|
|
224
|
+
private readonly config;
|
|
228
225
|
private defaultHeaders;
|
|
229
226
|
private anonKey;
|
|
230
227
|
private userToken;
|
|
231
228
|
private logger;
|
|
232
|
-
private autoRefreshToken;
|
|
233
229
|
private isRefreshing;
|
|
234
230
|
private refreshPromise;
|
|
235
231
|
private tokenManager;
|
|
@@ -257,6 +253,8 @@ declare class HttpClient {
|
|
|
257
253
|
* @returns Delay in milliseconds.
|
|
258
254
|
*/
|
|
259
255
|
private computeRetryDelay;
|
|
256
|
+
private shouldRefreshAccessToken;
|
|
257
|
+
private fetchWithRetry;
|
|
260
258
|
/**
|
|
261
259
|
* Performs an HTTP request with automatic retry and timeout handling.
|
|
262
260
|
* Retries on network errors and 5xx server errors with exponential backoff.
|
|
@@ -269,6 +267,14 @@ declare class HttpClient {
|
|
|
269
267
|
*/
|
|
270
268
|
private handleRequest;
|
|
271
269
|
request<T>(method: string, path: string, options?: RequestOptions): Promise<T>;
|
|
270
|
+
/**
|
|
271
|
+
* Performs an SDK-configured fetch and returns the raw Response.
|
|
272
|
+
* This is used by clients such as postgrest-js that need to own response
|
|
273
|
+
* parsing while still sharing SDK auth and refresh behavior.
|
|
274
|
+
*/
|
|
275
|
+
rawFetch(input: RequestInfo | URL, init?: RequestInit, options?: {
|
|
276
|
+
skipAuthRefresh?: boolean;
|
|
277
|
+
}): Promise<Response>;
|
|
272
278
|
/** Performs a GET request. */
|
|
273
279
|
get<T>(path: string, options?: RequestOptions): Promise<T>;
|
|
274
280
|
/** Performs a POST request with an optional JSON body. */
|
|
@@ -284,7 +290,9 @@ declare class HttpClient {
|
|
|
284
290
|
setRefreshToken(token: string | null): void;
|
|
285
291
|
/** Returns the current default headers including the authorization header if set. */
|
|
286
292
|
getHeaders(): Record<string, string>;
|
|
287
|
-
|
|
293
|
+
refreshAccessToken(): Promise<AuthRefreshResponse>;
|
|
294
|
+
private refreshAndSaveSession;
|
|
295
|
+
private clearAuthSession;
|
|
288
296
|
}
|
|
289
297
|
|
|
290
298
|
/**
|
|
@@ -433,7 +441,7 @@ declare class Auth {
|
|
|
433
441
|
*/
|
|
434
442
|
declare class Database {
|
|
435
443
|
private postgrest;
|
|
436
|
-
constructor(httpClient: HttpClient
|
|
444
|
+
constructor(httpClient: HttpClient);
|
|
437
445
|
/**
|
|
438
446
|
* Create a query builder for a table
|
|
439
447
|
*
|
|
@@ -993,8 +1001,7 @@ declare class Payments {
|
|
|
993
1001
|
*
|
|
994
1002
|
* @example
|
|
995
1003
|
* ```typescript
|
|
996
|
-
* const { data, error } = await client.payments.createCheckoutSession({
|
|
997
|
-
* environment: 'test',
|
|
1004
|
+
* const { data, error } = await client.payments.createCheckoutSession('test', {
|
|
998
1005
|
* mode: 'payment',
|
|
999
1006
|
* lineItems: [{ stripePriceId: 'price_123', quantity: 1 }],
|
|
1000
1007
|
* successUrl: `${window.location.origin}/success`,
|
|
@@ -1006,11 +1013,11 @@ declare class Payments {
|
|
|
1006
1013
|
* }
|
|
1007
1014
|
* ```
|
|
1008
1015
|
*/
|
|
1009
|
-
createCheckoutSession(request:
|
|
1016
|
+
createCheckoutSession(environment: StripeEnvironment, request: CreateCheckoutSessionBody): Promise<PaymentsResponse<CreateCheckoutSessionResponse>>;
|
|
1010
1017
|
/**
|
|
1011
1018
|
* Create a Stripe Billing Portal Session for a mapped billing subject.
|
|
1012
1019
|
*/
|
|
1013
|
-
createCustomerPortalSession(request:
|
|
1020
|
+
createCustomerPortalSession(environment: StripeEnvironment, request: CreateCustomerPortalSessionBody): Promise<PaymentsResponse<CreateCustomerPortalSessionResponse>>;
|
|
1014
1021
|
}
|
|
1015
1022
|
|
|
1016
1023
|
/**
|
|
@@ -1079,6 +1086,30 @@ declare class InsForgeClient {
|
|
|
1079
1086
|
* ```
|
|
1080
1087
|
*/
|
|
1081
1088
|
getHttpClient(): HttpClient;
|
|
1089
|
+
/**
|
|
1090
|
+
* Set the access token used by every SDK surface. Updates both the HTTP
|
|
1091
|
+
* client (database / storage / functions / AI / emails) and the realtime
|
|
1092
|
+
* token manager (which fires `onTokenChange` to reconnect the WebSocket
|
|
1093
|
+
* with the new bearer). Pass `null` to clear.
|
|
1094
|
+
*
|
|
1095
|
+
* Use this when an external auth provider (Better Auth, Clerk, Auth0,
|
|
1096
|
+
* WorkOS, Kinde, Stytch, …) issues the JWT and you need to keep the
|
|
1097
|
+
* long-lived InsForge client in sync. Without this, you'd have to call
|
|
1098
|
+
* `client.getHttpClient().setAuthToken(token)` AND reach into the private
|
|
1099
|
+
* `client.realtime.tokenManager.setAccessToken(token)` separately —
|
|
1100
|
+
* forgetting the second one silently breaks realtime auth.
|
|
1101
|
+
*
|
|
1102
|
+
* @example
|
|
1103
|
+
* ```typescript
|
|
1104
|
+
* // Refresh a third-party-issued JWT periodically
|
|
1105
|
+
* const { token } = await fetch('/api/insforge-token').then((r) => r.json());
|
|
1106
|
+
* client.setAccessToken(token);
|
|
1107
|
+
*
|
|
1108
|
+
* // Sign-out
|
|
1109
|
+
* client.setAccessToken(null);
|
|
1110
|
+
* ```
|
|
1111
|
+
*/
|
|
1112
|
+
setAccessToken(token: string | null): void;
|
|
1082
1113
|
}
|
|
1083
1114
|
|
|
1084
1115
|
/**
|