@insforge/sdk 1.2.3 → 1.2.4
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 +1049 -0
- package/dist/index.d.ts +1049 -0
- package/dist/index.js +2318 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2278 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +1 -1
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,2278 @@
|
|
|
1
|
+
// src/types.ts
|
|
2
|
+
var InsForgeError = class _InsForgeError extends Error {
|
|
3
|
+
constructor(message, statusCode, error, nextActions) {
|
|
4
|
+
super(message);
|
|
5
|
+
this.name = "InsForgeError";
|
|
6
|
+
this.statusCode = statusCode;
|
|
7
|
+
this.error = error;
|
|
8
|
+
this.nextActions = nextActions;
|
|
9
|
+
}
|
|
10
|
+
static fromApiError(apiError) {
|
|
11
|
+
return new _InsForgeError(
|
|
12
|
+
apiError.message,
|
|
13
|
+
apiError.statusCode,
|
|
14
|
+
apiError.error,
|
|
15
|
+
apiError.nextActions
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// src/lib/logger.ts
|
|
21
|
+
var SENSITIVE_HEADERS = ["authorization", "x-api-key", "cookie", "set-cookie"];
|
|
22
|
+
var SENSITIVE_BODY_KEYS = [
|
|
23
|
+
"password",
|
|
24
|
+
"token",
|
|
25
|
+
"accesstoken",
|
|
26
|
+
"refreshtoken",
|
|
27
|
+
"authorization",
|
|
28
|
+
"secret",
|
|
29
|
+
"apikey",
|
|
30
|
+
"api_key",
|
|
31
|
+
"email",
|
|
32
|
+
"ssn",
|
|
33
|
+
"creditcard",
|
|
34
|
+
"credit_card"
|
|
35
|
+
];
|
|
36
|
+
function redactHeaders(headers) {
|
|
37
|
+
const redacted = {};
|
|
38
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
39
|
+
if (SENSITIVE_HEADERS.includes(key.toLowerCase())) {
|
|
40
|
+
redacted[key] = "***REDACTED***";
|
|
41
|
+
} else {
|
|
42
|
+
redacted[key] = value;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return redacted;
|
|
46
|
+
}
|
|
47
|
+
function sanitizeBody(body) {
|
|
48
|
+
if (body === null || body === void 0) return body;
|
|
49
|
+
if (typeof body === "string") {
|
|
50
|
+
try {
|
|
51
|
+
const parsed = JSON.parse(body);
|
|
52
|
+
return sanitizeBody(parsed);
|
|
53
|
+
} catch {
|
|
54
|
+
return body;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (Array.isArray(body)) return body.map(sanitizeBody);
|
|
58
|
+
if (typeof body === "object") {
|
|
59
|
+
const sanitized = {};
|
|
60
|
+
for (const [key, value] of Object.entries(body)) {
|
|
61
|
+
if (SENSITIVE_BODY_KEYS.includes(key.toLowerCase().replace(/[-_]/g, ""))) {
|
|
62
|
+
sanitized[key] = "***REDACTED***";
|
|
63
|
+
} else {
|
|
64
|
+
sanitized[key] = sanitizeBody(value);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return sanitized;
|
|
68
|
+
}
|
|
69
|
+
return body;
|
|
70
|
+
}
|
|
71
|
+
function formatBody(body) {
|
|
72
|
+
if (body === void 0 || body === null) return "";
|
|
73
|
+
if (typeof body === "string") {
|
|
74
|
+
try {
|
|
75
|
+
return JSON.stringify(JSON.parse(body), null, 2);
|
|
76
|
+
} catch {
|
|
77
|
+
return body;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (typeof FormData !== "undefined" && body instanceof FormData) {
|
|
81
|
+
return "[FormData]";
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
return JSON.stringify(body, null, 2);
|
|
85
|
+
} catch {
|
|
86
|
+
return "[Unserializable body]";
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
var Logger = class {
|
|
90
|
+
/**
|
|
91
|
+
* Creates a new Logger instance.
|
|
92
|
+
* @param debug - Set to true to enable console logging, or pass a custom log function
|
|
93
|
+
*/
|
|
94
|
+
constructor(debug) {
|
|
95
|
+
if (typeof debug === "function") {
|
|
96
|
+
this.enabled = true;
|
|
97
|
+
this.customLog = debug;
|
|
98
|
+
} else {
|
|
99
|
+
this.enabled = !!debug;
|
|
100
|
+
this.customLog = null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Logs a debug message at the info level.
|
|
105
|
+
* @param message - The message to log
|
|
106
|
+
* @param args - Additional arguments to pass to the log function
|
|
107
|
+
*/
|
|
108
|
+
log(message, ...args) {
|
|
109
|
+
if (!this.enabled) return;
|
|
110
|
+
const formatted = `[InsForge Debug] ${message}`;
|
|
111
|
+
if (this.customLog) {
|
|
112
|
+
this.customLog(formatted, ...args);
|
|
113
|
+
} else {
|
|
114
|
+
console.log(formatted, ...args);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Logs a debug message at the warning level.
|
|
119
|
+
* @param message - The message to log
|
|
120
|
+
* @param args - Additional arguments to pass to the log function
|
|
121
|
+
*/
|
|
122
|
+
warn(message, ...args) {
|
|
123
|
+
if (!this.enabled) return;
|
|
124
|
+
const formatted = `[InsForge Debug] ${message}`;
|
|
125
|
+
if (this.customLog) {
|
|
126
|
+
this.customLog(formatted, ...args);
|
|
127
|
+
} else {
|
|
128
|
+
console.warn(formatted, ...args);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Logs a debug message at the error level.
|
|
133
|
+
* @param message - The message to log
|
|
134
|
+
* @param args - Additional arguments to pass to the log function
|
|
135
|
+
*/
|
|
136
|
+
error(message, ...args) {
|
|
137
|
+
if (!this.enabled) return;
|
|
138
|
+
const formatted = `[InsForge Debug] ${message}`;
|
|
139
|
+
if (this.customLog) {
|
|
140
|
+
this.customLog(formatted, ...args);
|
|
141
|
+
} else {
|
|
142
|
+
console.error(formatted, ...args);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Logs an outgoing HTTP request with method, URL, headers, and body.
|
|
147
|
+
* Sensitive headers and body fields are automatically redacted.
|
|
148
|
+
* @param method - HTTP method (GET, POST, etc.)
|
|
149
|
+
* @param url - The full request URL
|
|
150
|
+
* @param headers - Request headers (sensitive values will be redacted)
|
|
151
|
+
* @param body - Request body (sensitive fields will be masked)
|
|
152
|
+
*/
|
|
153
|
+
logRequest(method, url, headers, body) {
|
|
154
|
+
if (!this.enabled) return;
|
|
155
|
+
const parts = [
|
|
156
|
+
`\u2192 ${method} ${url}`
|
|
157
|
+
];
|
|
158
|
+
if (headers && Object.keys(headers).length > 0) {
|
|
159
|
+
parts.push(` Headers: ${JSON.stringify(redactHeaders(headers))}`);
|
|
160
|
+
}
|
|
161
|
+
const formattedBody = formatBody(sanitizeBody(body));
|
|
162
|
+
if (formattedBody) {
|
|
163
|
+
const truncated = formattedBody.length > 1e3 ? formattedBody.slice(0, 1e3) + "... [truncated]" : formattedBody;
|
|
164
|
+
parts.push(` Body: ${truncated}`);
|
|
165
|
+
}
|
|
166
|
+
this.log(parts.join("\n"));
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Logs an incoming HTTP response with method, URL, status, duration, and body.
|
|
170
|
+
* Error responses (4xx/5xx) are logged at the error level.
|
|
171
|
+
* @param method - HTTP method (GET, POST, etc.)
|
|
172
|
+
* @param url - The full request URL
|
|
173
|
+
* @param status - HTTP response status code
|
|
174
|
+
* @param durationMs - Request duration in milliseconds
|
|
175
|
+
* @param body - Response body (sensitive fields will be masked, large bodies truncated)
|
|
176
|
+
*/
|
|
177
|
+
logResponse(method, url, status, durationMs, body) {
|
|
178
|
+
if (!this.enabled) return;
|
|
179
|
+
const parts = [
|
|
180
|
+
`\u2190 ${method} ${url} ${status} (${durationMs}ms)`
|
|
181
|
+
];
|
|
182
|
+
const formattedBody = formatBody(sanitizeBody(body));
|
|
183
|
+
if (formattedBody) {
|
|
184
|
+
const truncated = formattedBody.length > 1e3 ? formattedBody.slice(0, 1e3) + "... [truncated]" : formattedBody;
|
|
185
|
+
parts.push(` Body: ${truncated}`);
|
|
186
|
+
}
|
|
187
|
+
if (status >= 400) {
|
|
188
|
+
this.error(parts.join("\n"));
|
|
189
|
+
} else {
|
|
190
|
+
this.log(parts.join("\n"));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
};
|
|
194
|
+
|
|
195
|
+
// src/lib/token-manager.ts
|
|
196
|
+
var CSRF_TOKEN_COOKIE = "insforge_csrf_token";
|
|
197
|
+
function getCsrfToken() {
|
|
198
|
+
if (typeof document === "undefined") return null;
|
|
199
|
+
const match = document.cookie.split(";").find((c) => c.trim().startsWith(`${CSRF_TOKEN_COOKIE}=`));
|
|
200
|
+
if (!match) return null;
|
|
201
|
+
return match.split("=")[1] || null;
|
|
202
|
+
}
|
|
203
|
+
function setCsrfToken(token) {
|
|
204
|
+
if (typeof document === "undefined") return;
|
|
205
|
+
const maxAge = 7 * 24 * 60 * 60;
|
|
206
|
+
const secure = typeof window !== "undefined" && window.location.protocol === "https:" ? "; Secure" : "";
|
|
207
|
+
document.cookie = `${CSRF_TOKEN_COOKIE}=${encodeURIComponent(token)}; path=/; max-age=${maxAge}; SameSite=Lax${secure}`;
|
|
208
|
+
}
|
|
209
|
+
function clearCsrfToken() {
|
|
210
|
+
if (typeof document === "undefined") return;
|
|
211
|
+
const secure = typeof window !== "undefined" && window.location.protocol === "https:" ? "; Secure" : "";
|
|
212
|
+
document.cookie = `${CSRF_TOKEN_COOKIE}=; path=/; max-age=0; SameSite=Lax${secure}`;
|
|
213
|
+
}
|
|
214
|
+
var TokenManager = class {
|
|
215
|
+
constructor() {
|
|
216
|
+
// In-memory storage
|
|
217
|
+
this.accessToken = null;
|
|
218
|
+
this.user = null;
|
|
219
|
+
// Callback for token changes (used by realtime to reconnect with new token)
|
|
220
|
+
this.onTokenChange = null;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Save session in memory
|
|
224
|
+
*/
|
|
225
|
+
saveSession(session) {
|
|
226
|
+
const tokenChanged = session.accessToken !== this.accessToken;
|
|
227
|
+
this.accessToken = session.accessToken;
|
|
228
|
+
this.user = session.user;
|
|
229
|
+
if (tokenChanged && this.onTokenChange) {
|
|
230
|
+
this.onTokenChange();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Get current session
|
|
235
|
+
*/
|
|
236
|
+
getSession() {
|
|
237
|
+
if (!this.accessToken || !this.user) return null;
|
|
238
|
+
return {
|
|
239
|
+
accessToken: this.accessToken,
|
|
240
|
+
user: this.user
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Get access token
|
|
245
|
+
*/
|
|
246
|
+
getAccessToken() {
|
|
247
|
+
return this.accessToken;
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Set access token
|
|
251
|
+
*/
|
|
252
|
+
setAccessToken(token) {
|
|
253
|
+
const tokenChanged = token !== this.accessToken;
|
|
254
|
+
this.accessToken = token;
|
|
255
|
+
if (tokenChanged && this.onTokenChange) {
|
|
256
|
+
this.onTokenChange();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Get user
|
|
261
|
+
*/
|
|
262
|
+
getUser() {
|
|
263
|
+
return this.user;
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Set user
|
|
267
|
+
*/
|
|
268
|
+
setUser(user) {
|
|
269
|
+
this.user = user;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Clear in-memory session
|
|
273
|
+
*/
|
|
274
|
+
clearSession() {
|
|
275
|
+
const hadToken = this.accessToken !== null;
|
|
276
|
+
this.accessToken = null;
|
|
277
|
+
this.user = null;
|
|
278
|
+
if (hadToken && this.onTokenChange) {
|
|
279
|
+
this.onTokenChange();
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
// src/lib/http-client.ts
|
|
285
|
+
var RETRYABLE_STATUS_CODES = /* @__PURE__ */ new Set([500, 502, 503, 504]);
|
|
286
|
+
var IDEMPOTENT_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "PUT", "DELETE", "OPTIONS"]);
|
|
287
|
+
var HttpClient = class {
|
|
288
|
+
/**
|
|
289
|
+
* Creates a new HttpClient instance.
|
|
290
|
+
* @param config - SDK configuration including baseUrl, timeout, retry settings, and fetch implementation.
|
|
291
|
+
* @param tokenManager - Token manager for session persistence.
|
|
292
|
+
* @param logger - Optional logger instance for request/response debugging.
|
|
293
|
+
*/
|
|
294
|
+
constructor(config, tokenManager, logger) {
|
|
295
|
+
this.userToken = null;
|
|
296
|
+
this.autoRefreshToken = true;
|
|
297
|
+
this.isRefreshing = false;
|
|
298
|
+
this.refreshPromise = null;
|
|
299
|
+
this.refreshToken = null;
|
|
300
|
+
this.baseUrl = config.baseUrl || "http://localhost:7130";
|
|
301
|
+
this.autoRefreshToken = config.autoRefreshToken ?? true;
|
|
302
|
+
this.fetch = config.fetch || (globalThis.fetch ? globalThis.fetch.bind(globalThis) : void 0);
|
|
303
|
+
this.anonKey = config.anonKey;
|
|
304
|
+
this.defaultHeaders = {
|
|
305
|
+
...config.headers
|
|
306
|
+
};
|
|
307
|
+
this.tokenManager = tokenManager ?? new TokenManager();
|
|
308
|
+
this.logger = logger || new Logger(false);
|
|
309
|
+
this.timeout = config.timeout ?? 3e4;
|
|
310
|
+
this.retryCount = config.retryCount ?? 3;
|
|
311
|
+
this.retryDelay = config.retryDelay ?? 500;
|
|
312
|
+
if (!this.fetch) {
|
|
313
|
+
throw new Error(
|
|
314
|
+
"Fetch is not available. Please provide a fetch implementation in the config."
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Builds a full URL from a path and optional query parameters.
|
|
320
|
+
* Normalizes PostgREST select parameters for proper syntax.
|
|
321
|
+
*/
|
|
322
|
+
buildUrl(path, params) {
|
|
323
|
+
const url = new URL(path, this.baseUrl);
|
|
324
|
+
if (params) {
|
|
325
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
326
|
+
if (key === "select") {
|
|
327
|
+
let normalizedValue = value.replace(/\s+/g, " ").trim();
|
|
328
|
+
normalizedValue = normalizedValue.replace(/\s*\(\s*/g, "(").replace(/\s*\)\s*/g, ")").replace(/\(\s+/g, "(").replace(/\s+\)/g, ")").replace(/,\s+(?=[^()]*\))/g, ",");
|
|
329
|
+
url.searchParams.append(key, normalizedValue);
|
|
330
|
+
} else {
|
|
331
|
+
url.searchParams.append(key, value);
|
|
332
|
+
}
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
return url.toString();
|
|
336
|
+
}
|
|
337
|
+
/** Checks if an HTTP status code is eligible for retry (5xx server errors). */
|
|
338
|
+
isRetryableStatus(status) {
|
|
339
|
+
return RETRYABLE_STATUS_CODES.has(status);
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Computes the delay before the next retry using exponential backoff with jitter.
|
|
343
|
+
* @param attempt - The current retry attempt number (1-based).
|
|
344
|
+
* @returns Delay in milliseconds.
|
|
345
|
+
*/
|
|
346
|
+
computeRetryDelay(attempt) {
|
|
347
|
+
const base = this.retryDelay * Math.pow(2, attempt - 1);
|
|
348
|
+
const jitter = base * (0.85 + Math.random() * 0.3);
|
|
349
|
+
return Math.round(jitter);
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* Performs an HTTP request with automatic retry and timeout handling.
|
|
353
|
+
* Retries on network errors and 5xx server errors with exponential backoff.
|
|
354
|
+
* Client errors (4xx) and timeouts are thrown immediately without retry.
|
|
355
|
+
* @param method - HTTP method (GET, POST, PUT, PATCH, DELETE).
|
|
356
|
+
* @param path - API path relative to the base URL.
|
|
357
|
+
* @param options - Optional request configuration including headers, body, and query params.
|
|
358
|
+
* @returns Parsed response data.
|
|
359
|
+
* @throws {InsForgeError} On timeout, network failure, or HTTP error responses.
|
|
360
|
+
*/
|
|
361
|
+
async handleRequest(method, path, options = {}) {
|
|
362
|
+
const {
|
|
363
|
+
params,
|
|
364
|
+
headers = {},
|
|
365
|
+
body,
|
|
366
|
+
signal: callerSignal,
|
|
367
|
+
...fetchOptions
|
|
368
|
+
} = options;
|
|
369
|
+
const url = this.buildUrl(path, params);
|
|
370
|
+
const startTime = Date.now();
|
|
371
|
+
const canRetry = IDEMPOTENT_METHODS.has(method.toUpperCase()) || options.idempotent === true;
|
|
372
|
+
const maxAttempts = canRetry ? this.retryCount : 0;
|
|
373
|
+
const requestHeaders = {
|
|
374
|
+
...this.defaultHeaders
|
|
375
|
+
};
|
|
376
|
+
const authToken = this.userToken || this.anonKey;
|
|
377
|
+
if (authToken) {
|
|
378
|
+
requestHeaders["Authorization"] = `Bearer ${authToken}`;
|
|
379
|
+
}
|
|
380
|
+
let processedBody;
|
|
381
|
+
if (body !== void 0) {
|
|
382
|
+
if (typeof FormData !== "undefined" && body instanceof FormData) {
|
|
383
|
+
processedBody = body;
|
|
384
|
+
} else {
|
|
385
|
+
if (method !== "GET") {
|
|
386
|
+
requestHeaders["Content-Type"] = "application/json;charset=UTF-8";
|
|
387
|
+
}
|
|
388
|
+
processedBody = JSON.stringify(body);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
if (headers instanceof Headers) {
|
|
392
|
+
headers.forEach((value, key) => {
|
|
393
|
+
requestHeaders[key] = value;
|
|
394
|
+
});
|
|
395
|
+
} else if (Array.isArray(headers)) {
|
|
396
|
+
headers.forEach(([key, value]) => {
|
|
397
|
+
requestHeaders[key] = value;
|
|
398
|
+
});
|
|
399
|
+
} else {
|
|
400
|
+
Object.assign(requestHeaders, headers);
|
|
401
|
+
}
|
|
402
|
+
this.logger.logRequest(method, url, requestHeaders, processedBody);
|
|
403
|
+
let lastError;
|
|
404
|
+
for (let attempt = 0; attempt <= maxAttempts; attempt++) {
|
|
405
|
+
if (attempt > 0) {
|
|
406
|
+
const delay = this.computeRetryDelay(attempt);
|
|
407
|
+
this.logger.warn(
|
|
408
|
+
`Retry ${attempt}/${maxAttempts} for ${method} ${url} in ${delay}ms`
|
|
409
|
+
);
|
|
410
|
+
if (callerSignal?.aborted) throw callerSignal.reason;
|
|
411
|
+
await new Promise((resolve, reject) => {
|
|
412
|
+
const onAbort = () => {
|
|
413
|
+
clearTimeout(timer2);
|
|
414
|
+
reject(callerSignal.reason);
|
|
415
|
+
};
|
|
416
|
+
const timer2 = setTimeout(() => {
|
|
417
|
+
if (callerSignal)
|
|
418
|
+
callerSignal.removeEventListener("abort", onAbort);
|
|
419
|
+
resolve();
|
|
420
|
+
}, delay);
|
|
421
|
+
if (callerSignal) {
|
|
422
|
+
callerSignal.addEventListener("abort", onAbort, { once: true });
|
|
423
|
+
}
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
let controller;
|
|
427
|
+
let timer;
|
|
428
|
+
if (this.timeout > 0 || callerSignal) {
|
|
429
|
+
controller = new AbortController();
|
|
430
|
+
if (this.timeout > 0) {
|
|
431
|
+
timer = setTimeout(() => controller.abort(), this.timeout);
|
|
432
|
+
}
|
|
433
|
+
if (callerSignal) {
|
|
434
|
+
if (callerSignal.aborted) {
|
|
435
|
+
controller.abort(callerSignal.reason);
|
|
436
|
+
} else {
|
|
437
|
+
const onCallerAbort = () => controller.abort(callerSignal.reason);
|
|
438
|
+
callerSignal.addEventListener("abort", onCallerAbort, {
|
|
439
|
+
once: true
|
|
440
|
+
});
|
|
441
|
+
controller.signal.addEventListener(
|
|
442
|
+
"abort",
|
|
443
|
+
() => {
|
|
444
|
+
callerSignal.removeEventListener("abort", onCallerAbort);
|
|
445
|
+
},
|
|
446
|
+
{ once: true }
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
try {
|
|
452
|
+
const response = await this.fetch(url, {
|
|
453
|
+
method,
|
|
454
|
+
headers: requestHeaders,
|
|
455
|
+
body: processedBody,
|
|
456
|
+
...fetchOptions,
|
|
457
|
+
...controller ? { signal: controller.signal } : {}
|
|
458
|
+
});
|
|
459
|
+
if (this.isRetryableStatus(response.status) && attempt < maxAttempts) {
|
|
460
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
461
|
+
await response.body?.cancel();
|
|
462
|
+
lastError = new InsForgeError(
|
|
463
|
+
`Server error: ${response.status} ${response.statusText}`,
|
|
464
|
+
response.status,
|
|
465
|
+
"SERVER_ERROR"
|
|
466
|
+
);
|
|
467
|
+
continue;
|
|
468
|
+
}
|
|
469
|
+
if (response.status === 204) {
|
|
470
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
471
|
+
return void 0;
|
|
472
|
+
}
|
|
473
|
+
let data;
|
|
474
|
+
const contentType = response.headers.get("content-type");
|
|
475
|
+
try {
|
|
476
|
+
if (contentType?.includes("json")) {
|
|
477
|
+
data = await response.json();
|
|
478
|
+
} else {
|
|
479
|
+
data = await response.text();
|
|
480
|
+
}
|
|
481
|
+
} catch (parseErr) {
|
|
482
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
483
|
+
throw new InsForgeError(
|
|
484
|
+
`Failed to parse response body: ${parseErr?.message || "Unknown error"}`,
|
|
485
|
+
response.status,
|
|
486
|
+
response.ok ? "PARSE_ERROR" : "REQUEST_FAILED"
|
|
487
|
+
);
|
|
488
|
+
}
|
|
489
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
490
|
+
if (!response.ok) {
|
|
491
|
+
this.logger.logResponse(
|
|
492
|
+
method,
|
|
493
|
+
url,
|
|
494
|
+
response.status,
|
|
495
|
+
Date.now() - startTime,
|
|
496
|
+
data
|
|
497
|
+
);
|
|
498
|
+
if (data && typeof data === "object" && "error" in data) {
|
|
499
|
+
if (!data.statusCode && !data.status) {
|
|
500
|
+
data.statusCode = response.status;
|
|
501
|
+
}
|
|
502
|
+
const error = InsForgeError.fromApiError(data);
|
|
503
|
+
Object.keys(data).forEach((key) => {
|
|
504
|
+
if (key !== "error" && key !== "message" && key !== "statusCode") {
|
|
505
|
+
error[key] = data[key];
|
|
506
|
+
}
|
|
507
|
+
});
|
|
508
|
+
throw error;
|
|
509
|
+
}
|
|
510
|
+
throw new InsForgeError(
|
|
511
|
+
`Request failed: ${response.statusText}`,
|
|
512
|
+
response.status,
|
|
513
|
+
"REQUEST_FAILED"
|
|
514
|
+
);
|
|
515
|
+
}
|
|
516
|
+
this.logger.logResponse(
|
|
517
|
+
method,
|
|
518
|
+
url,
|
|
519
|
+
response.status,
|
|
520
|
+
Date.now() - startTime,
|
|
521
|
+
data
|
|
522
|
+
);
|
|
523
|
+
return data;
|
|
524
|
+
} catch (err) {
|
|
525
|
+
if (timer !== void 0) clearTimeout(timer);
|
|
526
|
+
if (err?.name === "AbortError") {
|
|
527
|
+
if (controller && controller.signal.aborted && this.timeout > 0 && !callerSignal?.aborted) {
|
|
528
|
+
throw new InsForgeError(
|
|
529
|
+
`Request timed out after ${this.timeout}ms`,
|
|
530
|
+
408,
|
|
531
|
+
"REQUEST_TIMEOUT"
|
|
532
|
+
);
|
|
533
|
+
}
|
|
534
|
+
throw err;
|
|
535
|
+
}
|
|
536
|
+
if (err instanceof InsForgeError) {
|
|
537
|
+
throw err;
|
|
538
|
+
}
|
|
539
|
+
if (attempt < maxAttempts) {
|
|
540
|
+
lastError = err;
|
|
541
|
+
continue;
|
|
542
|
+
}
|
|
543
|
+
throw new InsForgeError(
|
|
544
|
+
`Network request failed: ${err?.message || "Unknown error"}`,
|
|
545
|
+
0,
|
|
546
|
+
"NETWORK_ERROR"
|
|
547
|
+
);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
throw lastError || new InsForgeError(
|
|
551
|
+
"Request failed after all retry attempts",
|
|
552
|
+
0,
|
|
553
|
+
"NETWORK_ERROR"
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
async request(method, path, options = {}) {
|
|
557
|
+
try {
|
|
558
|
+
return await this.handleRequest(method, path, { ...options });
|
|
559
|
+
} catch (error) {
|
|
560
|
+
if (error instanceof InsForgeError && error.statusCode === 401 && error.error === "INVALID_TOKEN" && this.autoRefreshToken) {
|
|
561
|
+
try {
|
|
562
|
+
const newTokenData = await this.handleTokenRefresh();
|
|
563
|
+
this.setAuthToken(newTokenData.accessToken);
|
|
564
|
+
this.tokenManager.saveSession(newTokenData);
|
|
565
|
+
if (newTokenData.csrfToken) {
|
|
566
|
+
setCsrfToken(newTokenData.csrfToken);
|
|
567
|
+
}
|
|
568
|
+
if (newTokenData.refreshToken) {
|
|
569
|
+
this.setRefreshToken(newTokenData.refreshToken);
|
|
570
|
+
}
|
|
571
|
+
return await this.handleRequest(method, path, { ...options });
|
|
572
|
+
} catch (error2) {
|
|
573
|
+
this.tokenManager.clearSession();
|
|
574
|
+
this.userToken = null;
|
|
575
|
+
this.refreshToken = null;
|
|
576
|
+
clearCsrfToken();
|
|
577
|
+
throw error2;
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
throw error;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
/** Performs a GET request. */
|
|
584
|
+
get(path, options) {
|
|
585
|
+
return this.request("GET", path, options);
|
|
586
|
+
}
|
|
587
|
+
/** Performs a POST request with an optional JSON body. */
|
|
588
|
+
post(path, body, options) {
|
|
589
|
+
return this.request("POST", path, { ...options, body });
|
|
590
|
+
}
|
|
591
|
+
/** Performs a PUT request with an optional JSON body. */
|
|
592
|
+
put(path, body, options) {
|
|
593
|
+
return this.request("PUT", path, { ...options, body });
|
|
594
|
+
}
|
|
595
|
+
/** Performs a PATCH request with an optional JSON body. */
|
|
596
|
+
patch(path, body, options) {
|
|
597
|
+
return this.request("PATCH", path, { ...options, body });
|
|
598
|
+
}
|
|
599
|
+
/** Performs a DELETE request. */
|
|
600
|
+
delete(path, options) {
|
|
601
|
+
return this.request("DELETE", path, options);
|
|
602
|
+
}
|
|
603
|
+
/** Sets or clears the user authentication token for subsequent requests. */
|
|
604
|
+
setAuthToken(token) {
|
|
605
|
+
this.userToken = token;
|
|
606
|
+
}
|
|
607
|
+
setRefreshToken(token) {
|
|
608
|
+
this.refreshToken = token;
|
|
609
|
+
}
|
|
610
|
+
/** Returns the current default headers including the authorization header if set. */
|
|
611
|
+
getHeaders() {
|
|
612
|
+
const headers = { ...this.defaultHeaders };
|
|
613
|
+
const authToken = this.userToken || this.anonKey;
|
|
614
|
+
if (authToken) {
|
|
615
|
+
headers["Authorization"] = `Bearer ${authToken}`;
|
|
616
|
+
}
|
|
617
|
+
return headers;
|
|
618
|
+
}
|
|
619
|
+
async handleTokenRefresh() {
|
|
620
|
+
if (this.isRefreshing) {
|
|
621
|
+
return this.refreshPromise;
|
|
622
|
+
}
|
|
623
|
+
this.isRefreshing = true;
|
|
624
|
+
this.refreshPromise = (async () => {
|
|
625
|
+
try {
|
|
626
|
+
const csrfToken = getCsrfToken();
|
|
627
|
+
const body = this.refreshToken ? { refreshToken: this.refreshToken } : void 0;
|
|
628
|
+
const response = await this.handleRequest(
|
|
629
|
+
"POST",
|
|
630
|
+
"/api/auth/sessions/current",
|
|
631
|
+
{
|
|
632
|
+
body,
|
|
633
|
+
headers: csrfToken ? { "X-CSRF-Token": csrfToken } : {},
|
|
634
|
+
credentials: "include"
|
|
635
|
+
}
|
|
636
|
+
);
|
|
637
|
+
return response;
|
|
638
|
+
} finally {
|
|
639
|
+
this.isRefreshing = false;
|
|
640
|
+
this.refreshPromise = null;
|
|
641
|
+
}
|
|
642
|
+
})();
|
|
643
|
+
return this.refreshPromise;
|
|
644
|
+
}
|
|
645
|
+
};
|
|
646
|
+
|
|
647
|
+
// src/modules/auth/helpers.ts
|
|
648
|
+
var PKCE_VERIFIER_KEY = "insforge_pkce_verifier";
|
|
649
|
+
function base64UrlEncode(buffer) {
|
|
650
|
+
const base64 = btoa(String.fromCharCode(...buffer));
|
|
651
|
+
return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
652
|
+
}
|
|
653
|
+
function generateCodeVerifier() {
|
|
654
|
+
const array = new Uint8Array(32);
|
|
655
|
+
crypto.getRandomValues(array);
|
|
656
|
+
return base64UrlEncode(array);
|
|
657
|
+
}
|
|
658
|
+
async function generateCodeChallenge(verifier) {
|
|
659
|
+
const encoder = new TextEncoder();
|
|
660
|
+
const data = encoder.encode(verifier);
|
|
661
|
+
const hash = await crypto.subtle.digest("SHA-256", data);
|
|
662
|
+
return base64UrlEncode(new Uint8Array(hash));
|
|
663
|
+
}
|
|
664
|
+
function storePkceVerifier(verifier) {
|
|
665
|
+
if (typeof sessionStorage !== "undefined") {
|
|
666
|
+
sessionStorage.setItem(PKCE_VERIFIER_KEY, verifier);
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
function retrievePkceVerifier() {
|
|
670
|
+
if (typeof sessionStorage === "undefined") {
|
|
671
|
+
return null;
|
|
672
|
+
}
|
|
673
|
+
const verifier = sessionStorage.getItem(PKCE_VERIFIER_KEY);
|
|
674
|
+
if (verifier) {
|
|
675
|
+
sessionStorage.removeItem(PKCE_VERIFIER_KEY);
|
|
676
|
+
}
|
|
677
|
+
return verifier;
|
|
678
|
+
}
|
|
679
|
+
function wrapError(error, fallbackMessage) {
|
|
680
|
+
if (error instanceof InsForgeError) {
|
|
681
|
+
return { data: null, error };
|
|
682
|
+
}
|
|
683
|
+
return {
|
|
684
|
+
data: null,
|
|
685
|
+
error: new InsForgeError(
|
|
686
|
+
error instanceof Error ? error.message : fallbackMessage,
|
|
687
|
+
500,
|
|
688
|
+
"UNEXPECTED_ERROR"
|
|
689
|
+
)
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
function cleanUrlParams(...params) {
|
|
693
|
+
if (typeof window === "undefined") {
|
|
694
|
+
return;
|
|
695
|
+
}
|
|
696
|
+
const url = new URL(window.location.href);
|
|
697
|
+
params.forEach((p) => url.searchParams.delete(p));
|
|
698
|
+
window.history.replaceState({}, document.title, url.toString());
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// src/modules/auth/auth.ts
|
|
702
|
+
import { oAuthProvidersSchema } from "@insforge/shared-schemas";
|
|
703
|
+
var Auth = class {
|
|
704
|
+
constructor(http, tokenManager, options = {}) {
|
|
705
|
+
this.http = http;
|
|
706
|
+
this.tokenManager = tokenManager;
|
|
707
|
+
this.options = options;
|
|
708
|
+
this.authCallbackHandled = this.detectAuthCallback();
|
|
709
|
+
}
|
|
710
|
+
isServerMode() {
|
|
711
|
+
return !!this.options.isServerMode;
|
|
712
|
+
}
|
|
713
|
+
/**
|
|
714
|
+
* Save session from API response
|
|
715
|
+
* Handles token storage, CSRF token, and HTTP auth header
|
|
716
|
+
*/
|
|
717
|
+
saveSessionFromResponse(response) {
|
|
718
|
+
if (!response.accessToken || !response.user) {
|
|
719
|
+
return false;
|
|
720
|
+
}
|
|
721
|
+
const session = {
|
|
722
|
+
accessToken: response.accessToken,
|
|
723
|
+
user: response.user
|
|
724
|
+
};
|
|
725
|
+
if (!this.isServerMode() && response.csrfToken) {
|
|
726
|
+
setCsrfToken(response.csrfToken);
|
|
727
|
+
}
|
|
728
|
+
if (!this.isServerMode()) {
|
|
729
|
+
this.tokenManager.saveSession(session);
|
|
730
|
+
}
|
|
731
|
+
this.http.setAuthToken(response.accessToken);
|
|
732
|
+
this.http.setRefreshToken(response.refreshToken ?? null);
|
|
733
|
+
return true;
|
|
734
|
+
}
|
|
735
|
+
// ============================================================================
|
|
736
|
+
// OAuth Callback Detection (runs on initialization)
|
|
737
|
+
// ============================================================================
|
|
738
|
+
/**
|
|
739
|
+
* Detect and handle OAuth callback parameters in URL
|
|
740
|
+
* Supports PKCE flow (insforge_code)
|
|
741
|
+
*/
|
|
742
|
+
async detectAuthCallback() {
|
|
743
|
+
if (this.isServerMode() || typeof window === "undefined") return;
|
|
744
|
+
try {
|
|
745
|
+
const params = new URLSearchParams(window.location.search);
|
|
746
|
+
const error = params.get("error");
|
|
747
|
+
if (error) {
|
|
748
|
+
cleanUrlParams("error");
|
|
749
|
+
console.debug("OAuth callback error:", error);
|
|
750
|
+
return;
|
|
751
|
+
}
|
|
752
|
+
const code = params.get("insforge_code");
|
|
753
|
+
if (code) {
|
|
754
|
+
cleanUrlParams("insforge_code");
|
|
755
|
+
const { error: exchangeError } = await this.exchangeOAuthCode(code);
|
|
756
|
+
if (exchangeError) {
|
|
757
|
+
console.debug("OAuth code exchange failed:", exchangeError.message);
|
|
758
|
+
}
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
761
|
+
} catch (error) {
|
|
762
|
+
console.debug("OAuth callback detection skipped:", error);
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
// ============================================================================
|
|
766
|
+
// Sign Up / Sign In / Sign Out
|
|
767
|
+
// ============================================================================
|
|
768
|
+
async signUp(request) {
|
|
769
|
+
try {
|
|
770
|
+
const response = await this.http.post(
|
|
771
|
+
this.isServerMode() ? "/api/auth/users?client_type=mobile" : "/api/auth/users",
|
|
772
|
+
request,
|
|
773
|
+
{ credentials: "include" }
|
|
774
|
+
);
|
|
775
|
+
if (response.accessToken && response.user) {
|
|
776
|
+
this.saveSessionFromResponse(response);
|
|
777
|
+
}
|
|
778
|
+
if (response.refreshToken) {
|
|
779
|
+
this.http.setRefreshToken(response.refreshToken);
|
|
780
|
+
}
|
|
781
|
+
return { data: response, error: null };
|
|
782
|
+
} catch (error) {
|
|
783
|
+
return wrapError(error, "An unexpected error occurred during sign up");
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
async signInWithPassword(request) {
|
|
787
|
+
try {
|
|
788
|
+
const response = await this.http.post(
|
|
789
|
+
this.isServerMode() ? "/api/auth/sessions?client_type=mobile" : "/api/auth/sessions",
|
|
790
|
+
request,
|
|
791
|
+
{ credentials: "include" }
|
|
792
|
+
);
|
|
793
|
+
this.saveSessionFromResponse(response);
|
|
794
|
+
if (response.refreshToken) {
|
|
795
|
+
this.http.setRefreshToken(response.refreshToken);
|
|
796
|
+
}
|
|
797
|
+
return { data: response, error: null };
|
|
798
|
+
} catch (error) {
|
|
799
|
+
return wrapError(error, "An unexpected error occurred during sign in");
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
async signOut() {
|
|
803
|
+
try {
|
|
804
|
+
try {
|
|
805
|
+
await this.http.post(
|
|
806
|
+
this.isServerMode() ? "/api/auth/logout?client_type=mobile" : "/api/auth/logout",
|
|
807
|
+
void 0,
|
|
808
|
+
{ credentials: "include" }
|
|
809
|
+
);
|
|
810
|
+
} catch {
|
|
811
|
+
}
|
|
812
|
+
this.tokenManager.clearSession();
|
|
813
|
+
this.http.setAuthToken(null);
|
|
814
|
+
this.http.setRefreshToken(null);
|
|
815
|
+
if (!this.isServerMode()) {
|
|
816
|
+
clearCsrfToken();
|
|
817
|
+
}
|
|
818
|
+
return { error: null };
|
|
819
|
+
} catch {
|
|
820
|
+
return {
|
|
821
|
+
error: new InsForgeError("Failed to sign out", 500, "SIGNOUT_ERROR")
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
// ============================================================================
|
|
826
|
+
// OAuth Authentication
|
|
827
|
+
// ============================================================================
|
|
828
|
+
/**
|
|
829
|
+
* Sign in with OAuth provider using PKCE flow
|
|
830
|
+
*/
|
|
831
|
+
async signInWithOAuth(options) {
|
|
832
|
+
try {
|
|
833
|
+
const { provider, redirectTo, skipBrowserRedirect } = options;
|
|
834
|
+
const providerKey = encodeURIComponent(provider.toLowerCase());
|
|
835
|
+
const codeVerifier = generateCodeVerifier();
|
|
836
|
+
const codeChallenge = await generateCodeChallenge(codeVerifier);
|
|
837
|
+
storePkceVerifier(codeVerifier);
|
|
838
|
+
const params = { code_challenge: codeChallenge };
|
|
839
|
+
if (redirectTo) params.redirect_uri = redirectTo;
|
|
840
|
+
const isBuiltInProvider = oAuthProvidersSchema.options.includes(
|
|
841
|
+
providerKey
|
|
842
|
+
);
|
|
843
|
+
const oauthPath = isBuiltInProvider ? `/api/auth/oauth/${providerKey}` : `/api/auth/oauth/custom/${providerKey}`;
|
|
844
|
+
const response = await this.http.get(oauthPath, {
|
|
845
|
+
params
|
|
846
|
+
});
|
|
847
|
+
if (!this.isServerMode() && typeof window !== "undefined" && !skipBrowserRedirect) {
|
|
848
|
+
window.location.href = response.authUrl;
|
|
849
|
+
return { data: {}, error: null };
|
|
850
|
+
}
|
|
851
|
+
return {
|
|
852
|
+
data: { url: response.authUrl, provider: providerKey, codeVerifier },
|
|
853
|
+
error: null
|
|
854
|
+
};
|
|
855
|
+
} catch (error) {
|
|
856
|
+
if (error instanceof InsForgeError) {
|
|
857
|
+
return { data: {}, error };
|
|
858
|
+
}
|
|
859
|
+
return {
|
|
860
|
+
data: {},
|
|
861
|
+
error: new InsForgeError(
|
|
862
|
+
"An unexpected error occurred during OAuth initialization",
|
|
863
|
+
500,
|
|
864
|
+
"UNEXPECTED_ERROR"
|
|
865
|
+
)
|
|
866
|
+
};
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* Exchange OAuth authorization code for tokens (PKCE flow)
|
|
871
|
+
* Called automatically on initialization when insforge_code is in URL
|
|
872
|
+
*/
|
|
873
|
+
async exchangeOAuthCode(code, codeVerifier) {
|
|
874
|
+
try {
|
|
875
|
+
const verifier = codeVerifier ?? retrievePkceVerifier();
|
|
876
|
+
if (!verifier) {
|
|
877
|
+
return {
|
|
878
|
+
data: null,
|
|
879
|
+
error: new InsForgeError(
|
|
880
|
+
"PKCE code verifier not found. Ensure signInWithOAuth was called in the same browser session.",
|
|
881
|
+
400,
|
|
882
|
+
"PKCE_VERIFIER_MISSING"
|
|
883
|
+
)
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
const request = {
|
|
887
|
+
code,
|
|
888
|
+
code_verifier: verifier
|
|
889
|
+
};
|
|
890
|
+
const response = await this.http.post(
|
|
891
|
+
this.isServerMode() ? "/api/auth/oauth/exchange?client_type=mobile" : "/api/auth/oauth/exchange",
|
|
892
|
+
request,
|
|
893
|
+
{ credentials: "include" }
|
|
894
|
+
);
|
|
895
|
+
this.saveSessionFromResponse(response);
|
|
896
|
+
return {
|
|
897
|
+
data: response,
|
|
898
|
+
error: null
|
|
899
|
+
};
|
|
900
|
+
} catch (error) {
|
|
901
|
+
return wrapError(
|
|
902
|
+
error,
|
|
903
|
+
"An unexpected error occurred during OAuth code exchange"
|
|
904
|
+
);
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
/**
|
|
908
|
+
* Sign in with an ID token from a native SDK (Google One Tap, etc.)
|
|
909
|
+
* Use this for native mobile apps or Google One Tap on web.
|
|
910
|
+
*
|
|
911
|
+
* @param credentials.provider - The identity provider (currently only 'google' is supported)
|
|
912
|
+
* @param credentials.token - The ID token from the native SDK
|
|
913
|
+
*/
|
|
914
|
+
async signInWithIdToken(credentials) {
|
|
915
|
+
try {
|
|
916
|
+
const { provider, token } = credentials;
|
|
917
|
+
const response = await this.http.post(
|
|
918
|
+
"/api/auth/id-token?client_type=mobile",
|
|
919
|
+
{ provider, token },
|
|
920
|
+
{ credentials: "include" }
|
|
921
|
+
);
|
|
922
|
+
this.saveSessionFromResponse(response);
|
|
923
|
+
if (response.refreshToken) {
|
|
924
|
+
this.http.setRefreshToken(response.refreshToken);
|
|
925
|
+
}
|
|
926
|
+
return {
|
|
927
|
+
data: response,
|
|
928
|
+
error: null
|
|
929
|
+
};
|
|
930
|
+
} catch (error) {
|
|
931
|
+
return wrapError(
|
|
932
|
+
error,
|
|
933
|
+
"An unexpected error occurred during ID token sign in"
|
|
934
|
+
);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
// ============================================================================
|
|
938
|
+
// Session Management
|
|
939
|
+
// ============================================================================
|
|
940
|
+
/**
|
|
941
|
+
* Refresh the current auth session.
|
|
942
|
+
*
|
|
943
|
+
* Browser mode:
|
|
944
|
+
* - Uses httpOnly refresh cookie and optional CSRF header.
|
|
945
|
+
*
|
|
946
|
+
* Server mode (`isServerMode: true`):
|
|
947
|
+
* - Uses mobile auth flow and requires `refreshToken` in request body.
|
|
948
|
+
*/
|
|
949
|
+
async refreshSession(options) {
|
|
950
|
+
try {
|
|
951
|
+
if (this.isServerMode() && !options?.refreshToken) {
|
|
952
|
+
return {
|
|
953
|
+
data: null,
|
|
954
|
+
error: new InsForgeError(
|
|
955
|
+
"refreshToken is required when refreshing session in server mode",
|
|
956
|
+
400,
|
|
957
|
+
"REFRESH_TOKEN_REQUIRED"
|
|
958
|
+
)
|
|
959
|
+
};
|
|
960
|
+
}
|
|
961
|
+
const csrfToken = !this.isServerMode() ? getCsrfToken() : null;
|
|
962
|
+
const response = await this.http.post(
|
|
963
|
+
this.isServerMode() ? "/api/auth/refresh?client_type=mobile" : "/api/auth/refresh",
|
|
964
|
+
this.isServerMode() ? { refresh_token: options?.refreshToken } : void 0,
|
|
965
|
+
{
|
|
966
|
+
headers: csrfToken ? { "X-CSRF-Token": csrfToken } : {},
|
|
967
|
+
credentials: "include"
|
|
968
|
+
}
|
|
969
|
+
);
|
|
970
|
+
if (response.accessToken) {
|
|
971
|
+
this.saveSessionFromResponse(response);
|
|
972
|
+
}
|
|
973
|
+
return { data: response, error: null };
|
|
974
|
+
} catch (error) {
|
|
975
|
+
return wrapError(
|
|
976
|
+
error,
|
|
977
|
+
"An unexpected error occurred during session refresh"
|
|
978
|
+
);
|
|
979
|
+
}
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* Get current user, automatically waits for pending OAuth callback
|
|
983
|
+
*/
|
|
984
|
+
async getCurrentUser() {
|
|
985
|
+
await this.authCallbackHandled;
|
|
986
|
+
try {
|
|
987
|
+
if (this.isServerMode()) {
|
|
988
|
+
const accessToken = this.tokenManager.getAccessToken();
|
|
989
|
+
if (!accessToken) return { data: { user: null }, error: null };
|
|
990
|
+
this.http.setAuthToken(accessToken);
|
|
991
|
+
const response = await this.http.get(
|
|
992
|
+
"/api/auth/sessions/current"
|
|
993
|
+
);
|
|
994
|
+
const user = response.user ?? null;
|
|
995
|
+
return { data: { user }, error: null };
|
|
996
|
+
}
|
|
997
|
+
const session = this.tokenManager.getSession();
|
|
998
|
+
if (session) {
|
|
999
|
+
this.http.setAuthToken(session.accessToken);
|
|
1000
|
+
return { data: { user: session.user }, error: null };
|
|
1001
|
+
}
|
|
1002
|
+
if (typeof window !== "undefined") {
|
|
1003
|
+
const { data: refreshed, error: refreshError } = await this.refreshSession();
|
|
1004
|
+
if (refreshError) {
|
|
1005
|
+
return { data: { user: null }, error: refreshError };
|
|
1006
|
+
}
|
|
1007
|
+
if (refreshed?.accessToken) {
|
|
1008
|
+
return { data: { user: refreshed.user ?? null }, error: null };
|
|
1009
|
+
}
|
|
1010
|
+
}
|
|
1011
|
+
return { data: { user: null }, error: null };
|
|
1012
|
+
} catch (error) {
|
|
1013
|
+
if (error instanceof InsForgeError) {
|
|
1014
|
+
return { data: { user: null }, error };
|
|
1015
|
+
}
|
|
1016
|
+
return {
|
|
1017
|
+
data: { user: null },
|
|
1018
|
+
error: new InsForgeError(
|
|
1019
|
+
"An unexpected error occurred while getting user",
|
|
1020
|
+
500,
|
|
1021
|
+
"UNEXPECTED_ERROR"
|
|
1022
|
+
)
|
|
1023
|
+
};
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
// ============================================================================
|
|
1027
|
+
// Profile Management
|
|
1028
|
+
// ============================================================================
|
|
1029
|
+
async getProfile(userId) {
|
|
1030
|
+
try {
|
|
1031
|
+
const response = await this.http.get(
|
|
1032
|
+
`/api/auth/profiles/${userId}`
|
|
1033
|
+
);
|
|
1034
|
+
return { data: response, error: null };
|
|
1035
|
+
} catch (error) {
|
|
1036
|
+
return wrapError(
|
|
1037
|
+
error,
|
|
1038
|
+
"An unexpected error occurred while fetching user profile"
|
|
1039
|
+
);
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
async setProfile(profile) {
|
|
1043
|
+
try {
|
|
1044
|
+
const response = await this.http.patch(
|
|
1045
|
+
"/api/auth/profiles/current",
|
|
1046
|
+
{
|
|
1047
|
+
profile
|
|
1048
|
+
}
|
|
1049
|
+
);
|
|
1050
|
+
const currentUser = this.tokenManager.getUser();
|
|
1051
|
+
if (!this.isServerMode() && currentUser && response.profile !== void 0) {
|
|
1052
|
+
this.tokenManager.setUser({
|
|
1053
|
+
...currentUser,
|
|
1054
|
+
profile: response.profile
|
|
1055
|
+
});
|
|
1056
|
+
}
|
|
1057
|
+
return { data: response, error: null };
|
|
1058
|
+
} catch (error) {
|
|
1059
|
+
return wrapError(
|
|
1060
|
+
error,
|
|
1061
|
+
"An unexpected error occurred while updating user profile"
|
|
1062
|
+
);
|
|
1063
|
+
}
|
|
1064
|
+
}
|
|
1065
|
+
// ============================================================================
|
|
1066
|
+
// Email Verification
|
|
1067
|
+
// ============================================================================
|
|
1068
|
+
async resendVerificationEmail(request) {
|
|
1069
|
+
try {
|
|
1070
|
+
const response = await this.http.post("/api/auth/email/send-verification", request);
|
|
1071
|
+
return { data: response, error: null };
|
|
1072
|
+
} catch (error) {
|
|
1073
|
+
return wrapError(
|
|
1074
|
+
error,
|
|
1075
|
+
"An unexpected error occurred while sending verification email"
|
|
1076
|
+
);
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
async verifyEmail(request) {
|
|
1080
|
+
try {
|
|
1081
|
+
const response = await this.http.post(
|
|
1082
|
+
this.isServerMode() ? "/api/auth/email/verify?client_type=mobile" : "/api/auth/email/verify",
|
|
1083
|
+
request,
|
|
1084
|
+
{ credentials: "include" }
|
|
1085
|
+
);
|
|
1086
|
+
this.saveSessionFromResponse(response);
|
|
1087
|
+
if (response.refreshToken) {
|
|
1088
|
+
this.http.setRefreshToken(response.refreshToken);
|
|
1089
|
+
}
|
|
1090
|
+
return { data: response, error: null };
|
|
1091
|
+
} catch (error) {
|
|
1092
|
+
return wrapError(
|
|
1093
|
+
error,
|
|
1094
|
+
"An unexpected error occurred while verifying email"
|
|
1095
|
+
);
|
|
1096
|
+
}
|
|
1097
|
+
}
|
|
1098
|
+
// ============================================================================
|
|
1099
|
+
// Password Reset
|
|
1100
|
+
// ============================================================================
|
|
1101
|
+
async sendResetPasswordEmail(request) {
|
|
1102
|
+
try {
|
|
1103
|
+
const response = await this.http.post("/api/auth/email/send-reset-password", request);
|
|
1104
|
+
return { data: response, error: null };
|
|
1105
|
+
} catch (error) {
|
|
1106
|
+
return wrapError(
|
|
1107
|
+
error,
|
|
1108
|
+
"An unexpected error occurred while sending password reset email"
|
|
1109
|
+
);
|
|
1110
|
+
}
|
|
1111
|
+
}
|
|
1112
|
+
async exchangeResetPasswordToken(request) {
|
|
1113
|
+
try {
|
|
1114
|
+
const response = await this.http.post(
|
|
1115
|
+
"/api/auth/email/exchange-reset-password-token",
|
|
1116
|
+
request
|
|
1117
|
+
);
|
|
1118
|
+
return { data: response, error: null };
|
|
1119
|
+
} catch (error) {
|
|
1120
|
+
return wrapError(
|
|
1121
|
+
error,
|
|
1122
|
+
"An unexpected error occurred while verifying reset code"
|
|
1123
|
+
);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
async resetPassword(request) {
|
|
1127
|
+
try {
|
|
1128
|
+
const response = await this.http.post(
|
|
1129
|
+
"/api/auth/email/reset-password",
|
|
1130
|
+
request
|
|
1131
|
+
);
|
|
1132
|
+
return { data: response, error: null };
|
|
1133
|
+
} catch (error) {
|
|
1134
|
+
return wrapError(
|
|
1135
|
+
error,
|
|
1136
|
+
"An unexpected error occurred while resetting password"
|
|
1137
|
+
);
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
// ============================================================================
|
|
1141
|
+
// Configuration
|
|
1142
|
+
// ============================================================================
|
|
1143
|
+
async getPublicAuthConfig() {
|
|
1144
|
+
try {
|
|
1145
|
+
const response = await this.http.get(
|
|
1146
|
+
"/api/auth/public-config"
|
|
1147
|
+
);
|
|
1148
|
+
return { data: response, error: null };
|
|
1149
|
+
} catch (error) {
|
|
1150
|
+
return wrapError(
|
|
1151
|
+
error,
|
|
1152
|
+
"An unexpected error occurred while fetching auth configuration"
|
|
1153
|
+
);
|
|
1154
|
+
}
|
|
1155
|
+
}
|
|
1156
|
+
};
|
|
1157
|
+
|
|
1158
|
+
// src/modules/database-postgrest.ts
|
|
1159
|
+
import { PostgrestClient } from "@supabase/postgrest-js";
|
|
1160
|
+
function createInsForgePostgrestFetch(httpClient, tokenManager) {
|
|
1161
|
+
return async (input, init) => {
|
|
1162
|
+
const url = typeof input === "string" ? input : input.toString();
|
|
1163
|
+
const urlObj = new URL(url);
|
|
1164
|
+
const pathname = urlObj.pathname.slice(1);
|
|
1165
|
+
const rpcMatch = pathname.match(/^rpc\/(.+)$/);
|
|
1166
|
+
const endpoint = rpcMatch ? `/api/database/rpc/${rpcMatch[1]}` : `/api/database/records/${pathname}`;
|
|
1167
|
+
const insforgeUrl = `${httpClient.baseUrl}${endpoint}${urlObj.search}`;
|
|
1168
|
+
const token = tokenManager.getAccessToken();
|
|
1169
|
+
const httpHeaders = httpClient.getHeaders();
|
|
1170
|
+
const authToken = token || httpHeaders["Authorization"]?.replace("Bearer ", "");
|
|
1171
|
+
const headers = new Headers(init?.headers);
|
|
1172
|
+
if (authToken && !headers.has("Authorization")) {
|
|
1173
|
+
headers.set("Authorization", `Bearer ${authToken}`);
|
|
1174
|
+
}
|
|
1175
|
+
const response = await fetch(insforgeUrl, {
|
|
1176
|
+
...init,
|
|
1177
|
+
headers
|
|
1178
|
+
});
|
|
1179
|
+
return response;
|
|
1180
|
+
};
|
|
1181
|
+
}
|
|
1182
|
+
var Database = class {
|
|
1183
|
+
constructor(httpClient, tokenManager) {
|
|
1184
|
+
this.postgrest = new PostgrestClient("http://dummy", {
|
|
1185
|
+
fetch: createInsForgePostgrestFetch(httpClient, tokenManager),
|
|
1186
|
+
headers: {}
|
|
1187
|
+
});
|
|
1188
|
+
}
|
|
1189
|
+
/**
|
|
1190
|
+
* Create a query builder for a table
|
|
1191
|
+
*
|
|
1192
|
+
* @example
|
|
1193
|
+
* // Basic query
|
|
1194
|
+
* const { data, error } = await client.database
|
|
1195
|
+
* .from('posts')
|
|
1196
|
+
* .select('*')
|
|
1197
|
+
* .eq('user_id', userId);
|
|
1198
|
+
*
|
|
1199
|
+
* // With count (Supabase style!)
|
|
1200
|
+
* const { data, error, count } = await client.database
|
|
1201
|
+
* .from('posts')
|
|
1202
|
+
* .select('*', { count: 'exact' })
|
|
1203
|
+
* .range(0, 9);
|
|
1204
|
+
*
|
|
1205
|
+
* // Just get count, no data
|
|
1206
|
+
* const { count } = await client.database
|
|
1207
|
+
* .from('posts')
|
|
1208
|
+
* .select('*', { count: 'exact', head: true });
|
|
1209
|
+
*
|
|
1210
|
+
* // Complex queries with OR
|
|
1211
|
+
* const { data } = await client.database
|
|
1212
|
+
* .from('posts')
|
|
1213
|
+
* .select('*, users!inner(*)')
|
|
1214
|
+
* .or('status.eq.active,status.eq.pending');
|
|
1215
|
+
*
|
|
1216
|
+
* // All features work:
|
|
1217
|
+
* - Nested selects
|
|
1218
|
+
* - Foreign key expansion
|
|
1219
|
+
* - OR/AND/NOT conditions
|
|
1220
|
+
* - Count with head
|
|
1221
|
+
* - Range pagination
|
|
1222
|
+
* - Upserts
|
|
1223
|
+
*/
|
|
1224
|
+
from(table) {
|
|
1225
|
+
return this.postgrest.from(table);
|
|
1226
|
+
}
|
|
1227
|
+
/**
|
|
1228
|
+
* Call a PostgreSQL function (RPC)
|
|
1229
|
+
*
|
|
1230
|
+
* @example
|
|
1231
|
+
* // Call a function with parameters
|
|
1232
|
+
* const { data, error } = await client.database
|
|
1233
|
+
* .rpc('get_user_stats', { user_id: 123 });
|
|
1234
|
+
*
|
|
1235
|
+
* // Call a function with no parameters
|
|
1236
|
+
* const { data, error } = await client.database
|
|
1237
|
+
* .rpc('get_all_active_users');
|
|
1238
|
+
*
|
|
1239
|
+
* // With options (head, count, get)
|
|
1240
|
+
* const { data, count } = await client.database
|
|
1241
|
+
* .rpc('search_posts', { query: 'hello' }, { count: 'exact' });
|
|
1242
|
+
*/
|
|
1243
|
+
rpc(fn, args, options) {
|
|
1244
|
+
return this.postgrest.rpc(fn, args, options);
|
|
1245
|
+
}
|
|
1246
|
+
};
|
|
1247
|
+
|
|
1248
|
+
// src/modules/storage.ts
|
|
1249
|
+
var StorageBucket = class {
|
|
1250
|
+
constructor(bucketName, http) {
|
|
1251
|
+
this.bucketName = bucketName;
|
|
1252
|
+
this.http = http;
|
|
1253
|
+
}
|
|
1254
|
+
/**
|
|
1255
|
+
* Upload a file with a specific key
|
|
1256
|
+
* Uses the upload strategy from backend (direct or presigned)
|
|
1257
|
+
* @param path - The object key/path
|
|
1258
|
+
* @param file - File or Blob to upload
|
|
1259
|
+
*/
|
|
1260
|
+
async upload(path, file) {
|
|
1261
|
+
try {
|
|
1262
|
+
const strategyResponse = await this.http.post(
|
|
1263
|
+
`/api/storage/buckets/${this.bucketName}/upload-strategy`,
|
|
1264
|
+
{
|
|
1265
|
+
filename: path,
|
|
1266
|
+
contentType: file.type || "application/octet-stream",
|
|
1267
|
+
size: file.size
|
|
1268
|
+
}
|
|
1269
|
+
);
|
|
1270
|
+
if (strategyResponse.method === "presigned") {
|
|
1271
|
+
return await this.uploadWithPresignedUrl(strategyResponse, file);
|
|
1272
|
+
}
|
|
1273
|
+
if (strategyResponse.method === "direct") {
|
|
1274
|
+
const formData = new FormData();
|
|
1275
|
+
formData.append("file", file);
|
|
1276
|
+
const response = await this.http.request(
|
|
1277
|
+
"PUT",
|
|
1278
|
+
`/api/storage/buckets/${this.bucketName}/objects/${encodeURIComponent(path)}`,
|
|
1279
|
+
{
|
|
1280
|
+
body: formData,
|
|
1281
|
+
headers: {
|
|
1282
|
+
// Don't set Content-Type, let browser set multipart boundary
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
);
|
|
1286
|
+
return { data: response, error: null };
|
|
1287
|
+
}
|
|
1288
|
+
throw new InsForgeError(
|
|
1289
|
+
`Unsupported upload method: ${strategyResponse.method}`,
|
|
1290
|
+
500,
|
|
1291
|
+
"STORAGE_ERROR"
|
|
1292
|
+
);
|
|
1293
|
+
} catch (error) {
|
|
1294
|
+
return {
|
|
1295
|
+
data: null,
|
|
1296
|
+
error: error instanceof InsForgeError ? error : new InsForgeError(
|
|
1297
|
+
"Upload failed",
|
|
1298
|
+
500,
|
|
1299
|
+
"STORAGE_ERROR"
|
|
1300
|
+
)
|
|
1301
|
+
};
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
/**
|
|
1305
|
+
* Upload a file with auto-generated key
|
|
1306
|
+
* Uses the upload strategy from backend (direct or presigned)
|
|
1307
|
+
* @param file - File or Blob to upload
|
|
1308
|
+
*/
|
|
1309
|
+
async uploadAuto(file) {
|
|
1310
|
+
try {
|
|
1311
|
+
const filename = file instanceof File ? file.name : "file";
|
|
1312
|
+
const strategyResponse = await this.http.post(
|
|
1313
|
+
`/api/storage/buckets/${this.bucketName}/upload-strategy`,
|
|
1314
|
+
{
|
|
1315
|
+
filename,
|
|
1316
|
+
contentType: file.type || "application/octet-stream",
|
|
1317
|
+
size: file.size
|
|
1318
|
+
}
|
|
1319
|
+
);
|
|
1320
|
+
if (strategyResponse.method === "presigned") {
|
|
1321
|
+
return await this.uploadWithPresignedUrl(strategyResponse, file);
|
|
1322
|
+
}
|
|
1323
|
+
if (strategyResponse.method === "direct") {
|
|
1324
|
+
const formData = new FormData();
|
|
1325
|
+
formData.append("file", file);
|
|
1326
|
+
const response = await this.http.request(
|
|
1327
|
+
"POST",
|
|
1328
|
+
`/api/storage/buckets/${this.bucketName}/objects`,
|
|
1329
|
+
{
|
|
1330
|
+
body: formData,
|
|
1331
|
+
headers: {
|
|
1332
|
+
// Don't set Content-Type, let browser set multipart boundary
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
);
|
|
1336
|
+
return { data: response, error: null };
|
|
1337
|
+
}
|
|
1338
|
+
throw new InsForgeError(
|
|
1339
|
+
`Unsupported upload method: ${strategyResponse.method}`,
|
|
1340
|
+
500,
|
|
1341
|
+
"STORAGE_ERROR"
|
|
1342
|
+
);
|
|
1343
|
+
} catch (error) {
|
|
1344
|
+
return {
|
|
1345
|
+
data: null,
|
|
1346
|
+
error: error instanceof InsForgeError ? error : new InsForgeError(
|
|
1347
|
+
"Upload failed",
|
|
1348
|
+
500,
|
|
1349
|
+
"STORAGE_ERROR"
|
|
1350
|
+
)
|
|
1351
|
+
};
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
/**
|
|
1355
|
+
* Internal method to handle presigned URL uploads
|
|
1356
|
+
*/
|
|
1357
|
+
async uploadWithPresignedUrl(strategy, file) {
|
|
1358
|
+
try {
|
|
1359
|
+
const formData = new FormData();
|
|
1360
|
+
if (strategy.fields) {
|
|
1361
|
+
Object.entries(strategy.fields).forEach(([key, value]) => {
|
|
1362
|
+
formData.append(key, value);
|
|
1363
|
+
});
|
|
1364
|
+
}
|
|
1365
|
+
formData.append("file", file);
|
|
1366
|
+
const uploadResponse = await fetch(strategy.uploadUrl, {
|
|
1367
|
+
method: "POST",
|
|
1368
|
+
body: formData
|
|
1369
|
+
});
|
|
1370
|
+
if (!uploadResponse.ok) {
|
|
1371
|
+
throw new InsForgeError(
|
|
1372
|
+
`Upload to storage failed: ${uploadResponse.statusText}`,
|
|
1373
|
+
uploadResponse.status,
|
|
1374
|
+
"STORAGE_ERROR"
|
|
1375
|
+
);
|
|
1376
|
+
}
|
|
1377
|
+
if (strategy.confirmRequired && strategy.confirmUrl) {
|
|
1378
|
+
const confirmResponse = await this.http.post(
|
|
1379
|
+
strategy.confirmUrl,
|
|
1380
|
+
{
|
|
1381
|
+
size: file.size,
|
|
1382
|
+
contentType: file.type || "application/octet-stream"
|
|
1383
|
+
}
|
|
1384
|
+
);
|
|
1385
|
+
return { data: confirmResponse, error: null };
|
|
1386
|
+
}
|
|
1387
|
+
return {
|
|
1388
|
+
data: {
|
|
1389
|
+
key: strategy.key,
|
|
1390
|
+
bucket: this.bucketName,
|
|
1391
|
+
size: file.size,
|
|
1392
|
+
mimeType: file.type || "application/octet-stream",
|
|
1393
|
+
uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1394
|
+
url: this.getPublicUrl(strategy.key)
|
|
1395
|
+
},
|
|
1396
|
+
error: null
|
|
1397
|
+
};
|
|
1398
|
+
} catch (error) {
|
|
1399
|
+
throw error instanceof InsForgeError ? error : new InsForgeError(
|
|
1400
|
+
"Presigned upload failed",
|
|
1401
|
+
500,
|
|
1402
|
+
"STORAGE_ERROR"
|
|
1403
|
+
);
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
/**
|
|
1407
|
+
* Download a file
|
|
1408
|
+
* Uses the download strategy from backend (direct or presigned)
|
|
1409
|
+
* @param path - The object key/path
|
|
1410
|
+
* Returns the file as a Blob
|
|
1411
|
+
*/
|
|
1412
|
+
async download(path) {
|
|
1413
|
+
try {
|
|
1414
|
+
const strategyResponse = await this.http.post(
|
|
1415
|
+
`/api/storage/buckets/${this.bucketName}/objects/${encodeURIComponent(path)}/download-strategy`,
|
|
1416
|
+
{ expiresIn: 3600 }
|
|
1417
|
+
);
|
|
1418
|
+
const downloadUrl = strategyResponse.url;
|
|
1419
|
+
const headers = {};
|
|
1420
|
+
if (strategyResponse.method === "direct") {
|
|
1421
|
+
Object.assign(headers, this.http.getHeaders());
|
|
1422
|
+
}
|
|
1423
|
+
const response = await fetch(downloadUrl, {
|
|
1424
|
+
method: "GET",
|
|
1425
|
+
headers
|
|
1426
|
+
});
|
|
1427
|
+
if (!response.ok) {
|
|
1428
|
+
try {
|
|
1429
|
+
const error = await response.json();
|
|
1430
|
+
throw InsForgeError.fromApiError(error);
|
|
1431
|
+
} catch {
|
|
1432
|
+
throw new InsForgeError(
|
|
1433
|
+
`Download failed: ${response.statusText}`,
|
|
1434
|
+
response.status,
|
|
1435
|
+
"STORAGE_ERROR"
|
|
1436
|
+
);
|
|
1437
|
+
}
|
|
1438
|
+
}
|
|
1439
|
+
const blob = await response.blob();
|
|
1440
|
+
return { data: blob, error: null };
|
|
1441
|
+
} catch (error) {
|
|
1442
|
+
return {
|
|
1443
|
+
data: null,
|
|
1444
|
+
error: error instanceof InsForgeError ? error : new InsForgeError(
|
|
1445
|
+
"Download failed",
|
|
1446
|
+
500,
|
|
1447
|
+
"STORAGE_ERROR"
|
|
1448
|
+
)
|
|
1449
|
+
};
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
/**
|
|
1453
|
+
* Get public URL for a file
|
|
1454
|
+
* @param path - The object key/path
|
|
1455
|
+
*/
|
|
1456
|
+
getPublicUrl(path) {
|
|
1457
|
+
return `${this.http.baseUrl}/api/storage/buckets/${this.bucketName}/objects/${encodeURIComponent(path)}`;
|
|
1458
|
+
}
|
|
1459
|
+
/**
|
|
1460
|
+
* List objects in the bucket
|
|
1461
|
+
* @param prefix - Filter by key prefix
|
|
1462
|
+
* @param search - Search in file names
|
|
1463
|
+
* @param limit - Maximum number of results (default: 100, max: 1000)
|
|
1464
|
+
* @param offset - Number of results to skip
|
|
1465
|
+
*/
|
|
1466
|
+
async list(options) {
|
|
1467
|
+
try {
|
|
1468
|
+
const params = {};
|
|
1469
|
+
if (options?.prefix) params.prefix = options.prefix;
|
|
1470
|
+
if (options?.search) params.search = options.search;
|
|
1471
|
+
if (options?.limit) params.limit = options.limit.toString();
|
|
1472
|
+
if (options?.offset) params.offset = options.offset.toString();
|
|
1473
|
+
const response = await this.http.get(
|
|
1474
|
+
`/api/storage/buckets/${this.bucketName}/objects`,
|
|
1475
|
+
{ params }
|
|
1476
|
+
);
|
|
1477
|
+
return { data: response, error: null };
|
|
1478
|
+
} catch (error) {
|
|
1479
|
+
return {
|
|
1480
|
+
data: null,
|
|
1481
|
+
error: error instanceof InsForgeError ? error : new InsForgeError(
|
|
1482
|
+
"List failed",
|
|
1483
|
+
500,
|
|
1484
|
+
"STORAGE_ERROR"
|
|
1485
|
+
)
|
|
1486
|
+
};
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
/**
|
|
1490
|
+
* Delete a file
|
|
1491
|
+
* @param path - The object key/path
|
|
1492
|
+
*/
|
|
1493
|
+
async remove(path) {
|
|
1494
|
+
try {
|
|
1495
|
+
const response = await this.http.delete(
|
|
1496
|
+
`/api/storage/buckets/${this.bucketName}/objects/${encodeURIComponent(path)}`
|
|
1497
|
+
);
|
|
1498
|
+
return { data: response, error: null };
|
|
1499
|
+
} catch (error) {
|
|
1500
|
+
return {
|
|
1501
|
+
data: null,
|
|
1502
|
+
error: error instanceof InsForgeError ? error : new InsForgeError(
|
|
1503
|
+
"Delete failed",
|
|
1504
|
+
500,
|
|
1505
|
+
"STORAGE_ERROR"
|
|
1506
|
+
)
|
|
1507
|
+
};
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
};
|
|
1511
|
+
var Storage = class {
|
|
1512
|
+
constructor(http) {
|
|
1513
|
+
this.http = http;
|
|
1514
|
+
}
|
|
1515
|
+
/**
|
|
1516
|
+
* Get a bucket instance for operations
|
|
1517
|
+
* @param bucketName - Name of the bucket
|
|
1518
|
+
*/
|
|
1519
|
+
from(bucketName) {
|
|
1520
|
+
return new StorageBucket(bucketName, this.http);
|
|
1521
|
+
}
|
|
1522
|
+
};
|
|
1523
|
+
|
|
1524
|
+
// src/modules/ai.ts
|
|
1525
|
+
var AI = class {
|
|
1526
|
+
constructor(http) {
|
|
1527
|
+
this.http = http;
|
|
1528
|
+
this.chat = new Chat(http);
|
|
1529
|
+
this.images = new Images(http);
|
|
1530
|
+
this.embeddings = new Embeddings(http);
|
|
1531
|
+
}
|
|
1532
|
+
};
|
|
1533
|
+
var Chat = class {
|
|
1534
|
+
constructor(http) {
|
|
1535
|
+
this.completions = new ChatCompletions(http);
|
|
1536
|
+
}
|
|
1537
|
+
};
|
|
1538
|
+
var ChatCompletions = class {
|
|
1539
|
+
constructor(http) {
|
|
1540
|
+
this.http = http;
|
|
1541
|
+
}
|
|
1542
|
+
/**
|
|
1543
|
+
* Create a chat completion - OpenAI-like response format
|
|
1544
|
+
*
|
|
1545
|
+
* @example
|
|
1546
|
+
* ```typescript
|
|
1547
|
+
* // Non-streaming
|
|
1548
|
+
* const completion = await client.ai.chat.completions.create({
|
|
1549
|
+
* model: 'gpt-4',
|
|
1550
|
+
* messages: [{ role: 'user', content: 'Hello!' }]
|
|
1551
|
+
* });
|
|
1552
|
+
* console.log(completion.choices[0].message.content);
|
|
1553
|
+
*
|
|
1554
|
+
* // With images (OpenAI-compatible format)
|
|
1555
|
+
* const response = await client.ai.chat.completions.create({
|
|
1556
|
+
* model: 'gpt-4-vision',
|
|
1557
|
+
* messages: [{
|
|
1558
|
+
* role: 'user',
|
|
1559
|
+
* content: [
|
|
1560
|
+
* { type: 'text', text: 'What is in this image?' },
|
|
1561
|
+
* { type: 'image_url', image_url: { url: 'https://example.com/image.jpg' } }
|
|
1562
|
+
* ]
|
|
1563
|
+
* }]
|
|
1564
|
+
* });
|
|
1565
|
+
*
|
|
1566
|
+
* // With PDF files
|
|
1567
|
+
* const pdfResponse = await client.ai.chat.completions.create({
|
|
1568
|
+
* model: 'anthropic/claude-3.5-sonnet',
|
|
1569
|
+
* messages: [{
|
|
1570
|
+
* role: 'user',
|
|
1571
|
+
* content: [
|
|
1572
|
+
* { type: 'text', text: 'Summarize this document' },
|
|
1573
|
+
* { type: 'file', file: { filename: 'doc.pdf', file_data: 'https://example.com/doc.pdf' } }
|
|
1574
|
+
* ]
|
|
1575
|
+
* }],
|
|
1576
|
+
* fileParser: { enabled: true, pdf: { engine: 'mistral-ocr' } }
|
|
1577
|
+
* });
|
|
1578
|
+
*
|
|
1579
|
+
* // With web search
|
|
1580
|
+
* const searchResponse = await client.ai.chat.completions.create({
|
|
1581
|
+
* model: 'openai/gpt-4',
|
|
1582
|
+
* messages: [{ role: 'user', content: 'What are the latest news about AI?' }],
|
|
1583
|
+
* webSearch: { enabled: true, maxResults: 5 }
|
|
1584
|
+
* });
|
|
1585
|
+
* // Access citations from response.choices[0].message.annotations
|
|
1586
|
+
*
|
|
1587
|
+
* // With thinking/reasoning mode (Anthropic models)
|
|
1588
|
+
* const thinkingResponse = await client.ai.chat.completions.create({
|
|
1589
|
+
* model: 'anthropic/claude-3.5-sonnet',
|
|
1590
|
+
* messages: [{ role: 'user', content: 'Solve this complex math problem...' }],
|
|
1591
|
+
* thinking: true
|
|
1592
|
+
* });
|
|
1593
|
+
*
|
|
1594
|
+
* // Streaming - returns async iterable
|
|
1595
|
+
* const stream = await client.ai.chat.completions.create({
|
|
1596
|
+
* model: 'gpt-4',
|
|
1597
|
+
* messages: [{ role: 'user', content: 'Tell me a story' }],
|
|
1598
|
+
* stream: true
|
|
1599
|
+
* });
|
|
1600
|
+
*
|
|
1601
|
+
* for await (const chunk of stream) {
|
|
1602
|
+
* if (chunk.choices[0]?.delta?.content) {
|
|
1603
|
+
* process.stdout.write(chunk.choices[0].delta.content);
|
|
1604
|
+
* }
|
|
1605
|
+
* }
|
|
1606
|
+
* ```
|
|
1607
|
+
*/
|
|
1608
|
+
async create(params) {
|
|
1609
|
+
const backendParams = {
|
|
1610
|
+
model: params.model,
|
|
1611
|
+
messages: params.messages,
|
|
1612
|
+
temperature: params.temperature,
|
|
1613
|
+
maxTokens: params.maxTokens,
|
|
1614
|
+
topP: params.topP,
|
|
1615
|
+
stream: params.stream,
|
|
1616
|
+
// New plugin options
|
|
1617
|
+
webSearch: params.webSearch,
|
|
1618
|
+
fileParser: params.fileParser,
|
|
1619
|
+
thinking: params.thinking,
|
|
1620
|
+
// Tool calling options
|
|
1621
|
+
tools: params.tools,
|
|
1622
|
+
toolChoice: params.toolChoice,
|
|
1623
|
+
parallelToolCalls: params.parallelToolCalls
|
|
1624
|
+
};
|
|
1625
|
+
if (params.stream) {
|
|
1626
|
+
const headers = this.http.getHeaders();
|
|
1627
|
+
headers["Content-Type"] = "application/json";
|
|
1628
|
+
const response2 = await this.http.fetch(
|
|
1629
|
+
`${this.http.baseUrl}/api/ai/chat/completion`,
|
|
1630
|
+
{
|
|
1631
|
+
method: "POST",
|
|
1632
|
+
headers,
|
|
1633
|
+
body: JSON.stringify(backendParams)
|
|
1634
|
+
}
|
|
1635
|
+
);
|
|
1636
|
+
if (!response2.ok) {
|
|
1637
|
+
const error = await response2.json();
|
|
1638
|
+
throw new Error(error.error || "Stream request failed");
|
|
1639
|
+
}
|
|
1640
|
+
return this.parseSSEStream(response2, params.model);
|
|
1641
|
+
}
|
|
1642
|
+
const response = await this.http.post(
|
|
1643
|
+
"/api/ai/chat/completion",
|
|
1644
|
+
backendParams
|
|
1645
|
+
);
|
|
1646
|
+
const content = response.text || "";
|
|
1647
|
+
return {
|
|
1648
|
+
id: `chatcmpl-${Date.now()}`,
|
|
1649
|
+
object: "chat.completion",
|
|
1650
|
+
created: Math.floor(Date.now() / 1e3),
|
|
1651
|
+
model: response.metadata?.model,
|
|
1652
|
+
choices: [
|
|
1653
|
+
{
|
|
1654
|
+
index: 0,
|
|
1655
|
+
message: {
|
|
1656
|
+
role: "assistant",
|
|
1657
|
+
content,
|
|
1658
|
+
// Include tool_calls if present (from tool calling)
|
|
1659
|
+
...response.tool_calls?.length && { tool_calls: response.tool_calls },
|
|
1660
|
+
// Include annotations if present (from web search or file parsing)
|
|
1661
|
+
...response.annotations?.length && { annotations: response.annotations }
|
|
1662
|
+
},
|
|
1663
|
+
finish_reason: response.tool_calls?.length ? "tool_calls" : "stop"
|
|
1664
|
+
}
|
|
1665
|
+
],
|
|
1666
|
+
usage: response.metadata?.usage || {
|
|
1667
|
+
prompt_tokens: 0,
|
|
1668
|
+
completion_tokens: 0,
|
|
1669
|
+
total_tokens: 0
|
|
1670
|
+
}
|
|
1671
|
+
};
|
|
1672
|
+
}
|
|
1673
|
+
/**
|
|
1674
|
+
* Parse SSE stream into async iterable of OpenAI-like chunks
|
|
1675
|
+
*/
|
|
1676
|
+
async *parseSSEStream(response, model) {
|
|
1677
|
+
const reader = response.body.getReader();
|
|
1678
|
+
const decoder = new TextDecoder();
|
|
1679
|
+
let buffer = "";
|
|
1680
|
+
try {
|
|
1681
|
+
while (true) {
|
|
1682
|
+
const { done, value } = await reader.read();
|
|
1683
|
+
if (done) break;
|
|
1684
|
+
buffer += decoder.decode(value, { stream: true });
|
|
1685
|
+
const lines = buffer.split("\n");
|
|
1686
|
+
buffer = lines.pop() || "";
|
|
1687
|
+
for (const line of lines) {
|
|
1688
|
+
if (line.startsWith("data: ")) {
|
|
1689
|
+
const dataStr = line.slice(6).trim();
|
|
1690
|
+
if (dataStr) {
|
|
1691
|
+
try {
|
|
1692
|
+
const data = JSON.parse(dataStr);
|
|
1693
|
+
if (data.chunk || data.content) {
|
|
1694
|
+
yield {
|
|
1695
|
+
id: `chatcmpl-${Date.now()}`,
|
|
1696
|
+
object: "chat.completion.chunk",
|
|
1697
|
+
created: Math.floor(Date.now() / 1e3),
|
|
1698
|
+
model,
|
|
1699
|
+
choices: [
|
|
1700
|
+
{
|
|
1701
|
+
index: 0,
|
|
1702
|
+
delta: {
|
|
1703
|
+
content: data.chunk || data.content
|
|
1704
|
+
},
|
|
1705
|
+
finish_reason: null
|
|
1706
|
+
}
|
|
1707
|
+
]
|
|
1708
|
+
};
|
|
1709
|
+
}
|
|
1710
|
+
if (data.tool_calls?.length) {
|
|
1711
|
+
yield {
|
|
1712
|
+
id: `chatcmpl-${Date.now()}`,
|
|
1713
|
+
object: "chat.completion.chunk",
|
|
1714
|
+
created: Math.floor(Date.now() / 1e3),
|
|
1715
|
+
model,
|
|
1716
|
+
choices: [
|
|
1717
|
+
{
|
|
1718
|
+
index: 0,
|
|
1719
|
+
delta: {
|
|
1720
|
+
tool_calls: data.tool_calls
|
|
1721
|
+
},
|
|
1722
|
+
finish_reason: "tool_calls"
|
|
1723
|
+
}
|
|
1724
|
+
]
|
|
1725
|
+
};
|
|
1726
|
+
}
|
|
1727
|
+
if (data.done) {
|
|
1728
|
+
reader.releaseLock();
|
|
1729
|
+
return;
|
|
1730
|
+
}
|
|
1731
|
+
} catch (e) {
|
|
1732
|
+
console.warn("Failed to parse SSE data:", dataStr);
|
|
1733
|
+
}
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
}
|
|
1737
|
+
}
|
|
1738
|
+
} finally {
|
|
1739
|
+
reader.releaseLock();
|
|
1740
|
+
}
|
|
1741
|
+
}
|
|
1742
|
+
};
|
|
1743
|
+
var Embeddings = class {
|
|
1744
|
+
constructor(http) {
|
|
1745
|
+
this.http = http;
|
|
1746
|
+
}
|
|
1747
|
+
/**
|
|
1748
|
+
* Create embeddings for text input - OpenAI-like response format
|
|
1749
|
+
*
|
|
1750
|
+
* @example
|
|
1751
|
+
* ```typescript
|
|
1752
|
+
* // Single text input
|
|
1753
|
+
* const response = await client.ai.embeddings.create({
|
|
1754
|
+
* model: 'openai/text-embedding-3-small',
|
|
1755
|
+
* input: 'Hello world'
|
|
1756
|
+
* });
|
|
1757
|
+
* console.log(response.data[0].embedding); // number[]
|
|
1758
|
+
*
|
|
1759
|
+
* // Multiple text inputs
|
|
1760
|
+
* const response = await client.ai.embeddings.create({
|
|
1761
|
+
* model: 'openai/text-embedding-3-small',
|
|
1762
|
+
* input: ['Hello world', 'Goodbye world']
|
|
1763
|
+
* });
|
|
1764
|
+
* response.data.forEach((item, i) => {
|
|
1765
|
+
* console.log(`Embedding ${i}:`, item.embedding.slice(0, 5)); // First 5 dimensions
|
|
1766
|
+
* });
|
|
1767
|
+
*
|
|
1768
|
+
* // With custom dimensions (if supported by model)
|
|
1769
|
+
* const response = await client.ai.embeddings.create({
|
|
1770
|
+
* model: 'openai/text-embedding-3-small',
|
|
1771
|
+
* input: 'Hello world',
|
|
1772
|
+
* dimensions: 256
|
|
1773
|
+
* });
|
|
1774
|
+
*
|
|
1775
|
+
* // With base64 encoding format
|
|
1776
|
+
* const response = await client.ai.embeddings.create({
|
|
1777
|
+
* model: 'openai/text-embedding-3-small',
|
|
1778
|
+
* input: 'Hello world',
|
|
1779
|
+
* encoding_format: 'base64'
|
|
1780
|
+
* });
|
|
1781
|
+
* ```
|
|
1782
|
+
*/
|
|
1783
|
+
async create(params) {
|
|
1784
|
+
const response = await this.http.post(
|
|
1785
|
+
"/api/ai/embeddings",
|
|
1786
|
+
params
|
|
1787
|
+
);
|
|
1788
|
+
return {
|
|
1789
|
+
object: response.object,
|
|
1790
|
+
data: response.data,
|
|
1791
|
+
model: response.metadata?.model,
|
|
1792
|
+
usage: response.metadata?.usage ? {
|
|
1793
|
+
prompt_tokens: response.metadata.usage.promptTokens || 0,
|
|
1794
|
+
total_tokens: response.metadata.usage.totalTokens || 0
|
|
1795
|
+
} : {
|
|
1796
|
+
prompt_tokens: 0,
|
|
1797
|
+
total_tokens: 0
|
|
1798
|
+
}
|
|
1799
|
+
};
|
|
1800
|
+
}
|
|
1801
|
+
};
|
|
1802
|
+
var Images = class {
|
|
1803
|
+
constructor(http) {
|
|
1804
|
+
this.http = http;
|
|
1805
|
+
}
|
|
1806
|
+
/**
|
|
1807
|
+
* Generate images - OpenAI-like response format
|
|
1808
|
+
*
|
|
1809
|
+
* @example
|
|
1810
|
+
* ```typescript
|
|
1811
|
+
* // Text-to-image
|
|
1812
|
+
* const response = await client.ai.images.generate({
|
|
1813
|
+
* model: 'dall-e-3',
|
|
1814
|
+
* prompt: 'A sunset over mountains',
|
|
1815
|
+
* });
|
|
1816
|
+
* console.log(response.images[0].url);
|
|
1817
|
+
*
|
|
1818
|
+
* // Image-to-image (with input images)
|
|
1819
|
+
* const response = await client.ai.images.generate({
|
|
1820
|
+
* model: 'stable-diffusion-xl',
|
|
1821
|
+
* prompt: 'Transform this into a watercolor painting',
|
|
1822
|
+
* images: [
|
|
1823
|
+
* { url: 'https://example.com/input.jpg' },
|
|
1824
|
+
* // or base64-encoded Data URI:
|
|
1825
|
+
* { url: 'data:image/jpeg;base64,/9j/4AAQ...' }
|
|
1826
|
+
* ]
|
|
1827
|
+
* });
|
|
1828
|
+
* ```
|
|
1829
|
+
*/
|
|
1830
|
+
async generate(params) {
|
|
1831
|
+
const response = await this.http.post(
|
|
1832
|
+
"/api/ai/image/generation",
|
|
1833
|
+
params
|
|
1834
|
+
);
|
|
1835
|
+
let data = [];
|
|
1836
|
+
if (response.images && response.images.length > 0) {
|
|
1837
|
+
data = response.images.map((img) => ({
|
|
1838
|
+
b64_json: img.imageUrl.replace(/^data:image\/\w+;base64,/, ""),
|
|
1839
|
+
content: response.text
|
|
1840
|
+
}));
|
|
1841
|
+
} else if (response.text) {
|
|
1842
|
+
data = [{ content: response.text }];
|
|
1843
|
+
}
|
|
1844
|
+
return {
|
|
1845
|
+
created: Math.floor(Date.now() / 1e3),
|
|
1846
|
+
data,
|
|
1847
|
+
...response.metadata?.usage && {
|
|
1848
|
+
usage: {
|
|
1849
|
+
total_tokens: response.metadata.usage.totalTokens || 0,
|
|
1850
|
+
input_tokens: response.metadata.usage.promptTokens || 0,
|
|
1851
|
+
output_tokens: response.metadata.usage.completionTokens || 0
|
|
1852
|
+
}
|
|
1853
|
+
}
|
|
1854
|
+
};
|
|
1855
|
+
}
|
|
1856
|
+
};
|
|
1857
|
+
|
|
1858
|
+
// src/modules/functions.ts
|
|
1859
|
+
var Functions = class _Functions {
|
|
1860
|
+
constructor(http, functionsUrl) {
|
|
1861
|
+
this.http = http;
|
|
1862
|
+
this.functionsUrl = functionsUrl || _Functions.deriveSubhostingUrl(http.baseUrl);
|
|
1863
|
+
}
|
|
1864
|
+
/**
|
|
1865
|
+
* Derive the subhosting URL from the base URL.
|
|
1866
|
+
* Base URL pattern: https://{appKey}.{region}.insforge.app
|
|
1867
|
+
* Functions URL: https://{appKey}.functions.insforge.app
|
|
1868
|
+
* Only applies to .insforge.app domains.
|
|
1869
|
+
*/
|
|
1870
|
+
static deriveSubhostingUrl(baseUrl) {
|
|
1871
|
+
try {
|
|
1872
|
+
const { hostname } = new URL(baseUrl);
|
|
1873
|
+
if (!hostname.endsWith(".insforge.app")) return void 0;
|
|
1874
|
+
const appKey = hostname.split(".")[0];
|
|
1875
|
+
return `https://${appKey}.functions.insforge.app`;
|
|
1876
|
+
} catch {
|
|
1877
|
+
return void 0;
|
|
1878
|
+
}
|
|
1879
|
+
}
|
|
1880
|
+
/**
|
|
1881
|
+
* Invokes an Edge Function
|
|
1882
|
+
*
|
|
1883
|
+
* If functionsUrl is configured, tries direct subhosting first.
|
|
1884
|
+
* Falls back to proxy URL if subhosting returns 404.
|
|
1885
|
+
*
|
|
1886
|
+
* @param slug The function slug to invoke
|
|
1887
|
+
* @param options Request options
|
|
1888
|
+
*/
|
|
1889
|
+
async invoke(slug, options = {}) {
|
|
1890
|
+
const { method = "POST", body, headers = {} } = options;
|
|
1891
|
+
if (this.functionsUrl) {
|
|
1892
|
+
try {
|
|
1893
|
+
const data = await this.http.request(method, `${this.functionsUrl}/${slug}`, {
|
|
1894
|
+
body,
|
|
1895
|
+
headers
|
|
1896
|
+
});
|
|
1897
|
+
return { data, error: null };
|
|
1898
|
+
} catch (error) {
|
|
1899
|
+
if (error instanceof Error && error.name === "AbortError") throw error;
|
|
1900
|
+
if (error instanceof InsForgeError && error.statusCode === 404) {
|
|
1901
|
+
} else {
|
|
1902
|
+
return {
|
|
1903
|
+
data: null,
|
|
1904
|
+
error: error instanceof InsForgeError ? error : new InsForgeError(
|
|
1905
|
+
error instanceof Error ? error.message : "Function invocation failed",
|
|
1906
|
+
500,
|
|
1907
|
+
"FUNCTION_ERROR"
|
|
1908
|
+
)
|
|
1909
|
+
};
|
|
1910
|
+
}
|
|
1911
|
+
}
|
|
1912
|
+
}
|
|
1913
|
+
try {
|
|
1914
|
+
const path = `/functions/${slug}`;
|
|
1915
|
+
const data = await this.http.request(method, path, { body, headers });
|
|
1916
|
+
return { data, error: null };
|
|
1917
|
+
} catch (error) {
|
|
1918
|
+
if (error instanceof Error && error.name === "AbortError") throw error;
|
|
1919
|
+
return {
|
|
1920
|
+
data: null,
|
|
1921
|
+
error: error instanceof InsForgeError ? error : new InsForgeError(
|
|
1922
|
+
error instanceof Error ? error.message : "Function invocation failed",
|
|
1923
|
+
500,
|
|
1924
|
+
"FUNCTION_ERROR"
|
|
1925
|
+
)
|
|
1926
|
+
};
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
};
|
|
1930
|
+
|
|
1931
|
+
// src/modules/realtime.ts
|
|
1932
|
+
import { io } from "socket.io-client";
|
|
1933
|
+
var CONNECT_TIMEOUT = 1e4;
|
|
1934
|
+
var Realtime = class {
|
|
1935
|
+
constructor(baseUrl, tokenManager, anonKey) {
|
|
1936
|
+
this.socket = null;
|
|
1937
|
+
this.connectPromise = null;
|
|
1938
|
+
this.subscribedChannels = /* @__PURE__ */ new Set();
|
|
1939
|
+
this.eventListeners = /* @__PURE__ */ new Map();
|
|
1940
|
+
this.baseUrl = baseUrl;
|
|
1941
|
+
this.tokenManager = tokenManager;
|
|
1942
|
+
this.anonKey = anonKey;
|
|
1943
|
+
this.tokenManager.onTokenChange = () => this.onTokenChange();
|
|
1944
|
+
}
|
|
1945
|
+
notifyListeners(event, payload) {
|
|
1946
|
+
const listeners = this.eventListeners.get(event);
|
|
1947
|
+
if (!listeners) return;
|
|
1948
|
+
for (const cb of listeners) {
|
|
1949
|
+
try {
|
|
1950
|
+
cb(payload);
|
|
1951
|
+
} catch (err) {
|
|
1952
|
+
console.error(`Error in ${event} callback:`, err);
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
1955
|
+
}
|
|
1956
|
+
/**
|
|
1957
|
+
* Connect to the realtime server
|
|
1958
|
+
* @returns Promise that resolves when connected
|
|
1959
|
+
*/
|
|
1960
|
+
connect() {
|
|
1961
|
+
if (this.socket?.connected) {
|
|
1962
|
+
return Promise.resolve();
|
|
1963
|
+
}
|
|
1964
|
+
if (this.connectPromise) {
|
|
1965
|
+
return this.connectPromise;
|
|
1966
|
+
}
|
|
1967
|
+
this.connectPromise = new Promise((resolve, reject) => {
|
|
1968
|
+
const token = this.tokenManager.getAccessToken() ?? this.anonKey;
|
|
1969
|
+
this.socket = io(this.baseUrl, {
|
|
1970
|
+
transports: ["websocket"],
|
|
1971
|
+
auth: token ? { token } : void 0
|
|
1972
|
+
});
|
|
1973
|
+
let initialConnection = true;
|
|
1974
|
+
let timeoutId = null;
|
|
1975
|
+
const cleanup = () => {
|
|
1976
|
+
if (timeoutId) {
|
|
1977
|
+
clearTimeout(timeoutId);
|
|
1978
|
+
timeoutId = null;
|
|
1979
|
+
}
|
|
1980
|
+
};
|
|
1981
|
+
timeoutId = setTimeout(() => {
|
|
1982
|
+
if (initialConnection) {
|
|
1983
|
+
initialConnection = false;
|
|
1984
|
+
this.connectPromise = null;
|
|
1985
|
+
this.socket?.disconnect();
|
|
1986
|
+
this.socket = null;
|
|
1987
|
+
reject(new Error(`Connection timeout after ${CONNECT_TIMEOUT}ms`));
|
|
1988
|
+
}
|
|
1989
|
+
}, CONNECT_TIMEOUT);
|
|
1990
|
+
this.socket.on("connect", () => {
|
|
1991
|
+
cleanup();
|
|
1992
|
+
for (const channel of this.subscribedChannels) {
|
|
1993
|
+
this.socket.emit("realtime:subscribe", { channel });
|
|
1994
|
+
}
|
|
1995
|
+
this.notifyListeners("connect");
|
|
1996
|
+
if (initialConnection) {
|
|
1997
|
+
initialConnection = false;
|
|
1998
|
+
this.connectPromise = null;
|
|
1999
|
+
resolve();
|
|
2000
|
+
}
|
|
2001
|
+
});
|
|
2002
|
+
this.socket.on("connect_error", (error) => {
|
|
2003
|
+
cleanup();
|
|
2004
|
+
this.notifyListeners("connect_error", error);
|
|
2005
|
+
if (initialConnection) {
|
|
2006
|
+
initialConnection = false;
|
|
2007
|
+
this.connectPromise = null;
|
|
2008
|
+
reject(error);
|
|
2009
|
+
}
|
|
2010
|
+
});
|
|
2011
|
+
this.socket.on("disconnect", (reason) => {
|
|
2012
|
+
this.notifyListeners("disconnect", reason);
|
|
2013
|
+
});
|
|
2014
|
+
this.socket.on("realtime:error", (error) => {
|
|
2015
|
+
this.notifyListeners("error", error);
|
|
2016
|
+
});
|
|
2017
|
+
this.socket.onAny((event, message) => {
|
|
2018
|
+
if (event === "realtime:error") return;
|
|
2019
|
+
this.notifyListeners(event, message);
|
|
2020
|
+
});
|
|
2021
|
+
});
|
|
2022
|
+
return this.connectPromise;
|
|
2023
|
+
}
|
|
2024
|
+
/**
|
|
2025
|
+
* Disconnect from the realtime server
|
|
2026
|
+
*/
|
|
2027
|
+
disconnect() {
|
|
2028
|
+
if (this.socket) {
|
|
2029
|
+
this.socket.disconnect();
|
|
2030
|
+
this.socket = null;
|
|
2031
|
+
}
|
|
2032
|
+
this.subscribedChannels.clear();
|
|
2033
|
+
}
|
|
2034
|
+
/**
|
|
2035
|
+
* Handle token changes (e.g., after auth refresh)
|
|
2036
|
+
* Updates socket auth so reconnects use the new token
|
|
2037
|
+
* If connected, triggers reconnect to apply new token immediately
|
|
2038
|
+
*/
|
|
2039
|
+
onTokenChange() {
|
|
2040
|
+
const token = this.tokenManager.getAccessToken() ?? this.anonKey;
|
|
2041
|
+
if (this.socket) {
|
|
2042
|
+
this.socket.auth = token ? { token } : {};
|
|
2043
|
+
}
|
|
2044
|
+
if (this.socket && (this.socket.connected || this.connectPromise)) {
|
|
2045
|
+
this.socket.disconnect();
|
|
2046
|
+
this.socket.connect();
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
/**
|
|
2050
|
+
* Check if connected to the realtime server
|
|
2051
|
+
*/
|
|
2052
|
+
get isConnected() {
|
|
2053
|
+
return this.socket?.connected ?? false;
|
|
2054
|
+
}
|
|
2055
|
+
/**
|
|
2056
|
+
* Get the current connection state
|
|
2057
|
+
*/
|
|
2058
|
+
get connectionState() {
|
|
2059
|
+
if (!this.socket) return "disconnected";
|
|
2060
|
+
if (this.socket.connected) return "connected";
|
|
2061
|
+
return "connecting";
|
|
2062
|
+
}
|
|
2063
|
+
/**
|
|
2064
|
+
* Get the socket ID (if connected)
|
|
2065
|
+
*/
|
|
2066
|
+
get socketId() {
|
|
2067
|
+
return this.socket?.id;
|
|
2068
|
+
}
|
|
2069
|
+
/**
|
|
2070
|
+
* Subscribe to a channel
|
|
2071
|
+
*
|
|
2072
|
+
* Automatically connects if not already connected.
|
|
2073
|
+
*
|
|
2074
|
+
* @param channel - Channel name (e.g., 'orders:123', 'broadcast')
|
|
2075
|
+
* @returns Promise with the subscription response
|
|
2076
|
+
*/
|
|
2077
|
+
async subscribe(channel) {
|
|
2078
|
+
if (this.subscribedChannels.has(channel)) {
|
|
2079
|
+
return { ok: true, channel };
|
|
2080
|
+
}
|
|
2081
|
+
if (!this.socket?.connected) {
|
|
2082
|
+
try {
|
|
2083
|
+
await this.connect();
|
|
2084
|
+
} catch (error) {
|
|
2085
|
+
const message = error instanceof Error ? error.message : "Connection failed";
|
|
2086
|
+
return { ok: false, channel, error: { code: "CONNECTION_FAILED", message } };
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
return new Promise((resolve) => {
|
|
2090
|
+
this.socket.emit("realtime:subscribe", { channel }, (response) => {
|
|
2091
|
+
if (response.ok) {
|
|
2092
|
+
this.subscribedChannels.add(channel);
|
|
2093
|
+
}
|
|
2094
|
+
resolve(response);
|
|
2095
|
+
});
|
|
2096
|
+
});
|
|
2097
|
+
}
|
|
2098
|
+
/**
|
|
2099
|
+
* Unsubscribe from a channel (fire-and-forget)
|
|
2100
|
+
*
|
|
2101
|
+
* @param channel - Channel name to unsubscribe from
|
|
2102
|
+
*/
|
|
2103
|
+
unsubscribe(channel) {
|
|
2104
|
+
this.subscribedChannels.delete(channel);
|
|
2105
|
+
if (this.socket?.connected) {
|
|
2106
|
+
this.socket.emit("realtime:unsubscribe", { channel });
|
|
2107
|
+
}
|
|
2108
|
+
}
|
|
2109
|
+
/**
|
|
2110
|
+
* Publish a message to a channel
|
|
2111
|
+
*
|
|
2112
|
+
* @param channel - Channel name
|
|
2113
|
+
* @param event - Event name
|
|
2114
|
+
* @param payload - Message payload
|
|
2115
|
+
*/
|
|
2116
|
+
async publish(channel, event, payload) {
|
|
2117
|
+
if (!this.socket?.connected) {
|
|
2118
|
+
throw new Error("Not connected to realtime server. Call connect() first.");
|
|
2119
|
+
}
|
|
2120
|
+
this.socket.emit("realtime:publish", { channel, event, payload });
|
|
2121
|
+
}
|
|
2122
|
+
/**
|
|
2123
|
+
* Listen for events
|
|
2124
|
+
*
|
|
2125
|
+
* Reserved event names:
|
|
2126
|
+
* - 'connect' - Fired when connected to the server
|
|
2127
|
+
* - 'connect_error' - Fired when connection fails (payload: Error)
|
|
2128
|
+
* - 'disconnect' - Fired when disconnected (payload: reason string)
|
|
2129
|
+
* - 'error' - Fired when a realtime error occurs (payload: RealtimeErrorPayload)
|
|
2130
|
+
*
|
|
2131
|
+
* All other events receive a `SocketMessage` payload with metadata.
|
|
2132
|
+
*
|
|
2133
|
+
* @param event - Event name to listen for
|
|
2134
|
+
* @param callback - Callback function when event is received
|
|
2135
|
+
*/
|
|
2136
|
+
on(event, callback) {
|
|
2137
|
+
if (!this.eventListeners.has(event)) {
|
|
2138
|
+
this.eventListeners.set(event, /* @__PURE__ */ new Set());
|
|
2139
|
+
}
|
|
2140
|
+
this.eventListeners.get(event).add(callback);
|
|
2141
|
+
}
|
|
2142
|
+
/**
|
|
2143
|
+
* Remove a listener for a specific event
|
|
2144
|
+
*
|
|
2145
|
+
* @param event - Event name
|
|
2146
|
+
* @param callback - The callback function to remove
|
|
2147
|
+
*/
|
|
2148
|
+
off(event, callback) {
|
|
2149
|
+
const listeners = this.eventListeners.get(event);
|
|
2150
|
+
if (listeners) {
|
|
2151
|
+
listeners.delete(callback);
|
|
2152
|
+
if (listeners.size === 0) {
|
|
2153
|
+
this.eventListeners.delete(event);
|
|
2154
|
+
}
|
|
2155
|
+
}
|
|
2156
|
+
}
|
|
2157
|
+
/**
|
|
2158
|
+
* Listen for an event only once, then automatically remove the listener
|
|
2159
|
+
*
|
|
2160
|
+
* @param event - Event name to listen for
|
|
2161
|
+
* @param callback - Callback function when event is received
|
|
2162
|
+
*/
|
|
2163
|
+
once(event, callback) {
|
|
2164
|
+
const wrapper = (payload) => {
|
|
2165
|
+
this.off(event, wrapper);
|
|
2166
|
+
callback(payload);
|
|
2167
|
+
};
|
|
2168
|
+
this.on(event, wrapper);
|
|
2169
|
+
}
|
|
2170
|
+
/**
|
|
2171
|
+
* Get all currently subscribed channels
|
|
2172
|
+
*
|
|
2173
|
+
* @returns Array of channel names
|
|
2174
|
+
*/
|
|
2175
|
+
getSubscribedChannels() {
|
|
2176
|
+
return Array.from(this.subscribedChannels);
|
|
2177
|
+
}
|
|
2178
|
+
};
|
|
2179
|
+
|
|
2180
|
+
// src/modules/email.ts
|
|
2181
|
+
var Emails = class {
|
|
2182
|
+
constructor(http) {
|
|
2183
|
+
this.http = http;
|
|
2184
|
+
}
|
|
2185
|
+
/**
|
|
2186
|
+
* Send a custom HTML email
|
|
2187
|
+
* @param options Email options including recipients, subject, and HTML content
|
|
2188
|
+
*/
|
|
2189
|
+
async send(options) {
|
|
2190
|
+
try {
|
|
2191
|
+
const data = await this.http.post(
|
|
2192
|
+
"/api/email/send-raw",
|
|
2193
|
+
options
|
|
2194
|
+
);
|
|
2195
|
+
return { data, error: null };
|
|
2196
|
+
} catch (error) {
|
|
2197
|
+
if (error instanceof Error && error.name === "AbortError") throw error;
|
|
2198
|
+
return {
|
|
2199
|
+
data: null,
|
|
2200
|
+
error: error instanceof InsForgeError ? error : new InsForgeError(
|
|
2201
|
+
error instanceof Error ? error.message : "Email send failed",
|
|
2202
|
+
500,
|
|
2203
|
+
"EMAIL_ERROR"
|
|
2204
|
+
)
|
|
2205
|
+
};
|
|
2206
|
+
}
|
|
2207
|
+
}
|
|
2208
|
+
};
|
|
2209
|
+
|
|
2210
|
+
// src/client.ts
|
|
2211
|
+
var InsForgeClient = class {
|
|
2212
|
+
constructor(config = {}) {
|
|
2213
|
+
const logger = new Logger(config.debug);
|
|
2214
|
+
this.tokenManager = new TokenManager();
|
|
2215
|
+
this.http = new HttpClient(config, this.tokenManager, logger);
|
|
2216
|
+
if (config.edgeFunctionToken) {
|
|
2217
|
+
this.http.setAuthToken(config.edgeFunctionToken);
|
|
2218
|
+
this.tokenManager.setAccessToken(config.edgeFunctionToken);
|
|
2219
|
+
}
|
|
2220
|
+
this.auth = new Auth(this.http, this.tokenManager, {
|
|
2221
|
+
isServerMode: config.isServerMode ?? !!config.edgeFunctionToken
|
|
2222
|
+
});
|
|
2223
|
+
this.database = new Database(this.http, this.tokenManager);
|
|
2224
|
+
this.storage = new Storage(this.http);
|
|
2225
|
+
this.ai = new AI(this.http);
|
|
2226
|
+
this.functions = new Functions(this.http, config.functionsUrl);
|
|
2227
|
+
this.realtime = new Realtime(
|
|
2228
|
+
this.http.baseUrl,
|
|
2229
|
+
this.tokenManager,
|
|
2230
|
+
config.anonKey
|
|
2231
|
+
);
|
|
2232
|
+
this.emails = new Emails(this.http);
|
|
2233
|
+
}
|
|
2234
|
+
/**
|
|
2235
|
+
* Get the underlying HTTP client for custom requests
|
|
2236
|
+
*
|
|
2237
|
+
* @example
|
|
2238
|
+
* ```typescript
|
|
2239
|
+
* const httpClient = client.getHttpClient();
|
|
2240
|
+
* const customData = await httpClient.get('/api/custom-endpoint');
|
|
2241
|
+
* ```
|
|
2242
|
+
*/
|
|
2243
|
+
getHttpClient() {
|
|
2244
|
+
return this.http;
|
|
2245
|
+
}
|
|
2246
|
+
/**
|
|
2247
|
+
* Future modules will be added here:
|
|
2248
|
+
* - database: Database operations
|
|
2249
|
+
* - storage: File storage operations
|
|
2250
|
+
* - functions: Serverless functions
|
|
2251
|
+
* - tables: Table management
|
|
2252
|
+
* - metadata: Backend metadata
|
|
2253
|
+
*/
|
|
2254
|
+
};
|
|
2255
|
+
|
|
2256
|
+
// src/index.ts
|
|
2257
|
+
function createClient(config) {
|
|
2258
|
+
return new InsForgeClient(config);
|
|
2259
|
+
}
|
|
2260
|
+
var index_default = InsForgeClient;
|
|
2261
|
+
export {
|
|
2262
|
+
AI,
|
|
2263
|
+
Auth,
|
|
2264
|
+
Database,
|
|
2265
|
+
Emails,
|
|
2266
|
+
Functions,
|
|
2267
|
+
HttpClient,
|
|
2268
|
+
InsForgeClient,
|
|
2269
|
+
InsForgeError,
|
|
2270
|
+
Logger,
|
|
2271
|
+
Realtime,
|
|
2272
|
+
Storage,
|
|
2273
|
+
StorageBucket,
|
|
2274
|
+
TokenManager,
|
|
2275
|
+
createClient,
|
|
2276
|
+
index_default as default
|
|
2277
|
+
};
|
|
2278
|
+
//# sourceMappingURL=index.mjs.map
|