@insforge/sdk 1.2.10 → 1.3.0-ssr.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/dist/ssr.mjs ADDED
@@ -0,0 +1,3119 @@
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 REFRESHABLE_AUTH_ERROR_CODES = /* @__PURE__ */ new Set([
288
+ "AUTH_UNAUTHORIZED",
289
+ "PGRST301"
290
+ ]);
291
+ function serializeBody(method, body, headers) {
292
+ if (body === void 0) return void 0;
293
+ if (method === "GET" || method === "HEAD") return void 0;
294
+ if (typeof FormData !== "undefined" && body instanceof FormData) {
295
+ return body;
296
+ }
297
+ headers["Content-Type"] = "application/json;charset=UTF-8";
298
+ return JSON.stringify(body);
299
+ }
300
+ async function parseResponse(response) {
301
+ if (response.status === 204) return void 0;
302
+ let data;
303
+ const contentType = response.headers.get("content-type");
304
+ try {
305
+ if (contentType?.includes("json")) {
306
+ data = await response.json();
307
+ } else {
308
+ data = await response.text();
309
+ }
310
+ } catch (parseErr) {
311
+ throw new InsForgeError(
312
+ `Failed to parse response body: ${parseErr?.message || "Unknown error"}`,
313
+ response.status,
314
+ response.ok ? "PARSE_ERROR" : "REQUEST_FAILED"
315
+ );
316
+ }
317
+ if (!response.ok) {
318
+ if (data && typeof data === "object" && "error" in data) {
319
+ data.statusCode ?? (data.statusCode = data.status ?? response.status);
320
+ const error = InsForgeError.fromApiError(data);
321
+ Object.keys(data).forEach((key) => {
322
+ if (key !== "error" && key !== "message" && key !== "statusCode") {
323
+ error[key] = data[key];
324
+ }
325
+ });
326
+ throw error;
327
+ }
328
+ throw new InsForgeError(
329
+ `Request failed: ${response.statusText}`,
330
+ response.status,
331
+ "REQUEST_FAILED"
332
+ );
333
+ }
334
+ return data;
335
+ }
336
+ var HttpClient = class {
337
+ /**
338
+ * Creates a new HttpClient instance.
339
+ * @param config - SDK configuration including baseUrl, timeout, retry settings, and fetch implementation.
340
+ * @param tokenManager - Token manager for session persistence.
341
+ * @param logger - Optional logger instance for request/response debugging.
342
+ */
343
+ constructor(config, tokenManager, logger) {
344
+ this.userToken = null;
345
+ this.isRefreshing = false;
346
+ this.refreshPromise = null;
347
+ this.refreshToken = null;
348
+ this.config = config;
349
+ this.baseUrl = config.baseUrl || "http://localhost:7130";
350
+ this.fetch = config.fetch || (globalThis.fetch ? globalThis.fetch.bind(globalThis) : void 0);
351
+ this.anonKey = config.anonKey;
352
+ this.defaultHeaders = {
353
+ ...config.headers
354
+ };
355
+ this.tokenManager = tokenManager ?? new TokenManager();
356
+ this.logger = logger || new Logger(false);
357
+ this.timeout = config.timeout ?? 3e4;
358
+ this.retryCount = config.retryCount ?? 3;
359
+ this.retryDelay = config.retryDelay ?? 500;
360
+ if (!this.fetch) {
361
+ throw new Error(
362
+ "Fetch is not available. Please provide a fetch implementation in the config."
363
+ );
364
+ }
365
+ }
366
+ /**
367
+ * Builds a full URL from a path and optional query parameters.
368
+ * Normalizes PostgREST select parameters for proper syntax.
369
+ */
370
+ buildUrl(path, params) {
371
+ const url = new URL(path, this.baseUrl);
372
+ if (params) {
373
+ Object.entries(params).forEach(([key, value]) => {
374
+ if (key === "select") {
375
+ let normalizedValue = value.replace(/\s+/g, " ").trim();
376
+ normalizedValue = normalizedValue.replace(/\s*\(\s*/g, "(").replace(/\s*\)\s*/g, ")").replace(/\(\s+/g, "(").replace(/\s+\)/g, ")").replace(/,\s+(?=[^()]*\))/g, ",");
377
+ url.searchParams.append(key, normalizedValue);
378
+ } else {
379
+ url.searchParams.append(key, value);
380
+ }
381
+ });
382
+ }
383
+ return url.toString();
384
+ }
385
+ /** Checks if an HTTP status code is eligible for retry (5xx server errors). */
386
+ isRetryableStatus(status) {
387
+ return RETRYABLE_STATUS_CODES.has(status);
388
+ }
389
+ /**
390
+ * Computes the delay before the next retry using exponential backoff with jitter.
391
+ * @param attempt - The current retry attempt number (1-based).
392
+ * @returns Delay in milliseconds.
393
+ */
394
+ computeRetryDelay(attempt) {
395
+ const base = this.retryDelay * Math.pow(2, attempt - 1);
396
+ const jitter = base * (0.85 + Math.random() * 0.3);
397
+ return Math.round(jitter);
398
+ }
399
+ shouldRefreshAccessToken(statusCode, errorCode, authToken, options = {}) {
400
+ return statusCode === 401 && REFRESHABLE_AUTH_ERROR_CODES.has(errorCode ?? "") && !this.config.isServerMode && !this.config.edgeFunctionToken && !options.skipAuthRefresh && authToken !== null;
401
+ }
402
+ async fetchWithRetry(args) {
403
+ const {
404
+ method,
405
+ url,
406
+ headers,
407
+ body,
408
+ fetchOptions,
409
+ callerSignal,
410
+ maxAttempts
411
+ } = args;
412
+ let lastError;
413
+ for (let attempt = 0; attempt <= maxAttempts; attempt++) {
414
+ if (attempt > 0) {
415
+ const delay = this.computeRetryDelay(attempt);
416
+ this.logger.warn(
417
+ `Retry ${attempt}/${maxAttempts} for ${method} ${url} in ${delay}ms`
418
+ );
419
+ if (callerSignal?.aborted) throw callerSignal.reason;
420
+ await new Promise((resolve, reject) => {
421
+ const onAbort = () => {
422
+ clearTimeout(timer2);
423
+ reject(callerSignal.reason);
424
+ };
425
+ const timer2 = setTimeout(() => {
426
+ if (callerSignal)
427
+ callerSignal.removeEventListener("abort", onAbort);
428
+ resolve();
429
+ }, delay);
430
+ if (callerSignal) {
431
+ callerSignal.addEventListener("abort", onAbort, { once: true });
432
+ }
433
+ });
434
+ }
435
+ let controller;
436
+ let timer;
437
+ if (this.timeout > 0 || callerSignal) {
438
+ controller = new AbortController();
439
+ if (this.timeout > 0) {
440
+ timer = setTimeout(() => controller.abort(), this.timeout);
441
+ }
442
+ if (callerSignal) {
443
+ if (callerSignal.aborted) {
444
+ controller.abort(callerSignal.reason);
445
+ } else {
446
+ const onCallerAbort = () => controller.abort(callerSignal.reason);
447
+ callerSignal.addEventListener("abort", onCallerAbort, {
448
+ once: true
449
+ });
450
+ controller.signal.addEventListener(
451
+ "abort",
452
+ () => {
453
+ callerSignal.removeEventListener("abort", onCallerAbort);
454
+ },
455
+ { once: true }
456
+ );
457
+ }
458
+ }
459
+ }
460
+ try {
461
+ const response = await this.fetch(url, {
462
+ method,
463
+ headers,
464
+ body,
465
+ ...fetchOptions,
466
+ ...controller ? { signal: controller.signal } : {}
467
+ });
468
+ if (this.isRetryableStatus(response.status) && attempt < maxAttempts) {
469
+ if (timer !== void 0) clearTimeout(timer);
470
+ await response.body?.cancel();
471
+ lastError = new InsForgeError(
472
+ `Server error: ${response.status} ${response.statusText}`,
473
+ response.status,
474
+ "SERVER_ERROR"
475
+ );
476
+ continue;
477
+ }
478
+ if (timer !== void 0) clearTimeout(timer);
479
+ return response;
480
+ } catch (err) {
481
+ if (timer !== void 0) clearTimeout(timer);
482
+ if (err?.name === "AbortError") {
483
+ if (controller && controller.signal.aborted && this.timeout > 0 && !callerSignal?.aborted) {
484
+ throw new InsForgeError(
485
+ `Request timed out after ${this.timeout}ms`,
486
+ 408,
487
+ "REQUEST_TIMEOUT"
488
+ );
489
+ }
490
+ throw err;
491
+ }
492
+ if (attempt < maxAttempts) {
493
+ lastError = err;
494
+ continue;
495
+ }
496
+ throw new InsForgeError(
497
+ `Network request failed: ${err?.message || "Unknown error"}`,
498
+ 0,
499
+ "NETWORK_ERROR"
500
+ );
501
+ }
502
+ }
503
+ throw lastError || new InsForgeError(
504
+ "Request failed after all retry attempts",
505
+ 0,
506
+ "NETWORK_ERROR"
507
+ );
508
+ }
509
+ /**
510
+ * Performs an HTTP request with automatic retry and timeout handling.
511
+ * Retries on network errors and 5xx server errors with exponential backoff.
512
+ * Client errors (4xx) and timeouts are thrown immediately without retry.
513
+ * @param method - HTTP method (GET, POST, PUT, PATCH, DELETE).
514
+ * @param path - API path relative to the base URL.
515
+ * @param options - Optional request configuration including headers, body, and query params.
516
+ * @returns Parsed response data.
517
+ * @throws {InsForgeError} On timeout, network failure, or HTTP error responses.
518
+ */
519
+ async handleRequest(method, path, options = {}, tokenOverride) {
520
+ const {
521
+ params,
522
+ headers = {},
523
+ body,
524
+ skipAuthRefresh: _skipAuthRefresh,
525
+ signal: callerSignal,
526
+ ...fetchOptions
527
+ } = options;
528
+ const url = this.buildUrl(path, params);
529
+ const startTime = Date.now();
530
+ const canRetry = IDEMPOTENT_METHODS.has(method.toUpperCase()) || options.idempotent === true;
531
+ const maxAttempts = canRetry ? this.retryCount : 0;
532
+ const requestHeaders = {
533
+ ...this.defaultHeaders
534
+ };
535
+ const authToken = tokenOverride ?? this.userToken ?? this.anonKey;
536
+ if (authToken) {
537
+ requestHeaders["Authorization"] = `Bearer ${authToken}`;
538
+ }
539
+ const processedBody = serializeBody(method, body, requestHeaders);
540
+ const setRequestHeader = (key, value) => {
541
+ if (key.toLowerCase() === "authorization") {
542
+ delete requestHeaders["Authorization"];
543
+ delete requestHeaders["authorization"];
544
+ requestHeaders["Authorization"] = value;
545
+ return;
546
+ }
547
+ requestHeaders[key] = value;
548
+ };
549
+ if (headers instanceof Headers) {
550
+ headers.forEach((value, key) => {
551
+ setRequestHeader(key, value);
552
+ });
553
+ } else if (Array.isArray(headers)) {
554
+ headers.forEach(([key, value]) => {
555
+ setRequestHeader(key, value);
556
+ });
557
+ } else {
558
+ Object.entries(headers).forEach(([key, value]) => {
559
+ setRequestHeader(key, value);
560
+ });
561
+ }
562
+ this.logger.logRequest(method, url, requestHeaders, processedBody);
563
+ const response = await this.fetchWithRetry({
564
+ method,
565
+ url,
566
+ headers: requestHeaders,
567
+ body: processedBody,
568
+ fetchOptions,
569
+ callerSignal,
570
+ maxAttempts
571
+ });
572
+ let data;
573
+ try {
574
+ data = await parseResponse(response);
575
+ } catch (err) {
576
+ if (err instanceof InsForgeError) {
577
+ this.logger.logResponse(
578
+ method,
579
+ url,
580
+ err.statusCode || response.status,
581
+ Date.now() - startTime,
582
+ err
583
+ );
584
+ }
585
+ throw err;
586
+ }
587
+ this.logger.logResponse(
588
+ method,
589
+ url,
590
+ response.status,
591
+ Date.now() - startTime,
592
+ data
593
+ );
594
+ return data;
595
+ }
596
+ async request(method, path, options = {}) {
597
+ const tokenUsed = this.userToken;
598
+ try {
599
+ return await this.handleRequest(
600
+ method,
601
+ path,
602
+ { ...options },
603
+ tokenUsed
604
+ );
605
+ } catch (error) {
606
+ if (!(error instanceof InsForgeError) || !this.shouldRefreshAccessToken(
607
+ error.statusCode,
608
+ error.error,
609
+ tokenUsed,
610
+ options
611
+ )) {
612
+ throw error;
613
+ }
614
+ if (tokenUsed !== this.userToken) {
615
+ if (this.userToken === null) {
616
+ throw error;
617
+ }
618
+ return await this.handleRequest(
619
+ method,
620
+ path,
621
+ {
622
+ ...options,
623
+ skipAuthRefresh: true
624
+ },
625
+ this.userToken
626
+ );
627
+ }
628
+ try {
629
+ await this.refreshAndSaveSession();
630
+ } catch (error2) {
631
+ if (error2 instanceof InsForgeError && (error2.statusCode === 401 || error2.statusCode === 403)) {
632
+ this.clearAuthSession();
633
+ }
634
+ throw error2;
635
+ }
636
+ return await this.handleRequest(method, path, {
637
+ ...options,
638
+ skipAuthRefresh: true
639
+ });
640
+ }
641
+ }
642
+ /**
643
+ * Performs an SDK-configured fetch and returns the raw Response.
644
+ * This is used by clients such as postgrest-js that need to own response
645
+ * parsing while still sharing SDK auth and refresh behavior.
646
+ */
647
+ async rawFetch(input, init, options = {}) {
648
+ const request = typeof Request !== "undefined" && input instanceof Request ? input : void 0;
649
+ const {
650
+ method: initMethod,
651
+ headers: initHeaders,
652
+ body: initBody,
653
+ signal: initSignal,
654
+ ...fetchOptions
655
+ } = init ?? {};
656
+ const method = initMethod ?? request?.method ?? "GET";
657
+ const url = request?.url ?? input.toString();
658
+ const startTime = Date.now();
659
+ const tokenUsed = this.userToken;
660
+ const headers = new Headers({
661
+ ...this.defaultHeaders
662
+ });
663
+ const authToken = tokenUsed ?? this.anonKey;
664
+ if (authToken) {
665
+ headers.set("Authorization", `Bearer ${authToken}`);
666
+ }
667
+ request?.headers.forEach((value, key) => {
668
+ headers.set(key, value);
669
+ });
670
+ new Headers(initHeaders).forEach((value, key) => {
671
+ headers.set(key, value);
672
+ });
673
+ const requestHeaders = {};
674
+ headers.forEach((value, key) => {
675
+ requestHeaders[key] = value;
676
+ });
677
+ const sourceBody = initBody ?? request?.body ?? void 0;
678
+ let body = sourceBody;
679
+ let retryInit = init;
680
+ if (typeof ReadableStream !== "undefined" && sourceBody instanceof ReadableStream) {
681
+ body = await new Response(sourceBody).arrayBuffer();
682
+ retryInit = { ...init ?? {}, body };
683
+ }
684
+ const callerSignal = initSignal ?? request?.signal;
685
+ const maxAttempts = IDEMPOTENT_METHODS.has(method.toUpperCase()) ? this.retryCount : 0;
686
+ this.logger.logRequest(method, url, requestHeaders, body);
687
+ const response = await this.fetchWithRetry({
688
+ method,
689
+ url,
690
+ headers: requestHeaders,
691
+ body,
692
+ fetchOptions,
693
+ callerSignal,
694
+ maxAttempts
695
+ });
696
+ this.logger.logResponse(
697
+ method,
698
+ url,
699
+ response.status,
700
+ Date.now() - startTime
701
+ );
702
+ let errorCode = null;
703
+ if (response.status === 401) {
704
+ try {
705
+ const data = await response.clone().json();
706
+ if (data && typeof data === "object") {
707
+ const candidate = data.error ?? data.code;
708
+ if (typeof candidate === "string") {
709
+ errorCode = candidate;
710
+ }
711
+ }
712
+ } catch {
713
+ }
714
+ }
715
+ if (!this.shouldRefreshAccessToken(
716
+ response.status,
717
+ errorCode,
718
+ tokenUsed,
719
+ options
720
+ )) {
721
+ return response;
722
+ }
723
+ if (tokenUsed !== this.userToken) {
724
+ if (this.userToken === null) {
725
+ return response;
726
+ }
727
+ const retryHeaders2 = new Headers(initHeaders);
728
+ retryHeaders2.set("Authorization", `Bearer ${this.userToken}`);
729
+ return await this.rawFetch(
730
+ input,
731
+ { ...retryInit, headers: retryHeaders2 },
732
+ { skipAuthRefresh: true }
733
+ );
734
+ }
735
+ let newTokenData;
736
+ try {
737
+ newTokenData = await this.refreshAndSaveSession();
738
+ } catch (error) {
739
+ if (error instanceof InsForgeError && (error.statusCode === 401 || error.statusCode === 403)) {
740
+ this.clearAuthSession();
741
+ }
742
+ throw error;
743
+ }
744
+ const retryHeaders = new Headers(initHeaders);
745
+ retryHeaders.set("Authorization", `Bearer ${newTokenData.accessToken}`);
746
+ return await this.rawFetch(
747
+ input,
748
+ { ...retryInit, headers: retryHeaders },
749
+ { skipAuthRefresh: true }
750
+ );
751
+ }
752
+ /** Performs a GET request. */
753
+ get(path, options) {
754
+ return this.request("GET", path, options);
755
+ }
756
+ /** Performs a POST request with an optional JSON body. */
757
+ post(path, body, options) {
758
+ return this.request("POST", path, { ...options, body });
759
+ }
760
+ /** Performs a PUT request with an optional JSON body. */
761
+ put(path, body, options) {
762
+ return this.request("PUT", path, { ...options, body });
763
+ }
764
+ /** Performs a PATCH request with an optional JSON body. */
765
+ patch(path, body, options) {
766
+ return this.request("PATCH", path, { ...options, body });
767
+ }
768
+ /** Performs a DELETE request. */
769
+ delete(path, options) {
770
+ return this.request("DELETE", path, options);
771
+ }
772
+ /** Sets or clears the user authentication token for subsequent requests. */
773
+ setAuthToken(token) {
774
+ this.userToken = token;
775
+ }
776
+ setRefreshToken(token) {
777
+ this.refreshToken = token;
778
+ }
779
+ /** Returns the current default headers including the authorization header if set. */
780
+ getHeaders() {
781
+ const headers = { ...this.defaultHeaders };
782
+ const authToken = this.userToken || this.anonKey;
783
+ if (authToken) {
784
+ headers["Authorization"] = `Bearer ${authToken}`;
785
+ }
786
+ return headers;
787
+ }
788
+ async refreshAccessToken() {
789
+ if (this.isRefreshing) {
790
+ return this.refreshPromise;
791
+ }
792
+ this.isRefreshing = true;
793
+ this.refreshPromise = (async () => {
794
+ try {
795
+ const csrfToken = getCsrfToken();
796
+ const body = this.refreshToken ? { refreshToken: this.refreshToken } : void 0;
797
+ const response = await this.handleRequest(
798
+ "POST",
799
+ this.refreshToken ? "/api/auth/refresh?client_type=mobile" : "/api/auth/refresh",
800
+ {
801
+ body,
802
+ headers: csrfToken ? { "X-CSRF-Token": csrfToken } : {},
803
+ credentials: "include"
804
+ }
805
+ );
806
+ return response;
807
+ } finally {
808
+ this.isRefreshing = false;
809
+ this.refreshPromise = null;
810
+ }
811
+ })();
812
+ return this.refreshPromise;
813
+ }
814
+ async refreshAndSaveSession() {
815
+ const newTokenData = await this.refreshAccessToken();
816
+ this.setAuthToken(newTokenData.accessToken);
817
+ this.tokenManager.saveSession(newTokenData);
818
+ if (newTokenData.csrfToken) {
819
+ setCsrfToken(newTokenData.csrfToken);
820
+ }
821
+ if (newTokenData.refreshToken) {
822
+ this.setRefreshToken(newTokenData.refreshToken);
823
+ }
824
+ return newTokenData;
825
+ }
826
+ clearAuthSession() {
827
+ this.tokenManager.clearSession();
828
+ this.userToken = null;
829
+ this.refreshToken = null;
830
+ clearCsrfToken();
831
+ }
832
+ };
833
+
834
+ // src/modules/auth/helpers.ts
835
+ var PKCE_VERIFIER_KEY = "insforge_pkce_verifier";
836
+ function base64UrlEncode(buffer) {
837
+ const base64 = btoa(String.fromCharCode(...buffer));
838
+ return base64.replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
839
+ }
840
+ function generateCodeVerifier() {
841
+ const array = new Uint8Array(32);
842
+ crypto.getRandomValues(array);
843
+ return base64UrlEncode(array);
844
+ }
845
+ async function generateCodeChallenge(verifier) {
846
+ const encoder = new TextEncoder();
847
+ const data = encoder.encode(verifier);
848
+ const hash = await crypto.subtle.digest("SHA-256", data);
849
+ return base64UrlEncode(new Uint8Array(hash));
850
+ }
851
+ function storePkceVerifier(verifier) {
852
+ if (typeof sessionStorage !== "undefined") {
853
+ sessionStorage.setItem(PKCE_VERIFIER_KEY, verifier);
854
+ }
855
+ }
856
+ function retrievePkceVerifier() {
857
+ if (typeof sessionStorage === "undefined") {
858
+ return null;
859
+ }
860
+ const verifier = sessionStorage.getItem(PKCE_VERIFIER_KEY);
861
+ if (verifier) {
862
+ sessionStorage.removeItem(PKCE_VERIFIER_KEY);
863
+ }
864
+ return verifier;
865
+ }
866
+ function wrapError(error, fallbackMessage) {
867
+ if (error instanceof InsForgeError) {
868
+ return { data: null, error };
869
+ }
870
+ return {
871
+ data: null,
872
+ error: new InsForgeError(
873
+ error instanceof Error ? error.message : fallbackMessage,
874
+ 500,
875
+ "UNEXPECTED_ERROR"
876
+ )
877
+ };
878
+ }
879
+ function cleanUrlParams(...params) {
880
+ if (typeof window === "undefined") {
881
+ return;
882
+ }
883
+ const url = new URL(window.location.href);
884
+ params.forEach((p) => url.searchParams.delete(p));
885
+ window.history.replaceState({}, document.title, url.toString());
886
+ }
887
+
888
+ // src/modules/auth/auth.ts
889
+ import { ERROR_CODES, oAuthProvidersSchema } from "@insforge/shared-schemas";
890
+ var Auth = class {
891
+ constructor(http, tokenManager, options = {}) {
892
+ this.http = http;
893
+ this.tokenManager = tokenManager;
894
+ this.options = options;
895
+ this.authCallbackHandled = this.detectAuthCallback();
896
+ }
897
+ isServerMode() {
898
+ return !!this.options.isServerMode;
899
+ }
900
+ /**
901
+ * Save session from API response
902
+ * Handles token storage, CSRF token, and HTTP auth header
903
+ */
904
+ saveSessionFromResponse(response) {
905
+ if (!response.accessToken || !response.user) {
906
+ return false;
907
+ }
908
+ const session = {
909
+ accessToken: response.accessToken,
910
+ user: response.user
911
+ };
912
+ if (!this.isServerMode() && response.csrfToken) {
913
+ setCsrfToken(response.csrfToken);
914
+ }
915
+ if (!this.isServerMode()) {
916
+ this.tokenManager.saveSession(session);
917
+ }
918
+ this.http.setAuthToken(response.accessToken);
919
+ this.http.setRefreshToken(response.refreshToken ?? null);
920
+ return true;
921
+ }
922
+ // ============================================================================
923
+ // OAuth Callback Detection (runs on initialization)
924
+ // ============================================================================
925
+ /**
926
+ * Detect and handle OAuth callback parameters in URL
927
+ * Supports PKCE flow (insforge_code)
928
+ */
929
+ async detectAuthCallback() {
930
+ if (this.isServerMode() || typeof window === "undefined") return;
931
+ try {
932
+ const params = new URLSearchParams(window.location.search);
933
+ const error = params.get("error");
934
+ if (error) {
935
+ cleanUrlParams("error");
936
+ console.debug("OAuth callback error:", error);
937
+ return;
938
+ }
939
+ const code = params.get("insforge_code");
940
+ if (code) {
941
+ cleanUrlParams("insforge_code");
942
+ const { error: exchangeError } = await this.exchangeOAuthCode(code);
943
+ if (exchangeError) {
944
+ console.debug("OAuth code exchange failed:", exchangeError.message);
945
+ }
946
+ return;
947
+ }
948
+ } catch (error) {
949
+ console.debug("OAuth callback detection skipped:", error);
950
+ }
951
+ }
952
+ // ============================================================================
953
+ // Sign Up / Sign In / Sign Out
954
+ // ============================================================================
955
+ async signUp(request) {
956
+ try {
957
+ const response = await this.http.post(
958
+ this.isServerMode() ? "/api/auth/users?client_type=mobile" : "/api/auth/users",
959
+ request,
960
+ { credentials: "include", skipAuthRefresh: true }
961
+ );
962
+ if (response.accessToken && response.user) {
963
+ this.saveSessionFromResponse(response);
964
+ }
965
+ if (response.refreshToken) {
966
+ this.http.setRefreshToken(response.refreshToken);
967
+ }
968
+ return { data: response, error: null };
969
+ } catch (error) {
970
+ return wrapError(error, "An unexpected error occurred during sign up");
971
+ }
972
+ }
973
+ async signInWithPassword(request) {
974
+ try {
975
+ const response = await this.http.post(
976
+ this.isServerMode() ? "/api/auth/sessions?client_type=mobile" : "/api/auth/sessions",
977
+ request,
978
+ { credentials: "include", skipAuthRefresh: true }
979
+ );
980
+ this.saveSessionFromResponse(response);
981
+ if (response.refreshToken) {
982
+ this.http.setRefreshToken(response.refreshToken);
983
+ }
984
+ return { data: response, error: null };
985
+ } catch (error) {
986
+ return wrapError(error, "An unexpected error occurred during sign in");
987
+ }
988
+ }
989
+ async signOut() {
990
+ try {
991
+ try {
992
+ await this.http.post(
993
+ this.isServerMode() ? "/api/auth/logout?client_type=mobile" : "/api/auth/logout",
994
+ void 0,
995
+ { credentials: "include", skipAuthRefresh: true }
996
+ );
997
+ } catch {
998
+ }
999
+ this.tokenManager.clearSession();
1000
+ this.http.setAuthToken(null);
1001
+ this.http.setRefreshToken(null);
1002
+ if (!this.isServerMode()) {
1003
+ clearCsrfToken();
1004
+ }
1005
+ return { error: null };
1006
+ } catch {
1007
+ return {
1008
+ error: new InsForgeError("Failed to sign out", 500, "SIGNOUT_ERROR")
1009
+ };
1010
+ }
1011
+ }
1012
+ // ============================================================================
1013
+ // OAuth Authentication
1014
+ // ============================================================================
1015
+ /**
1016
+ * Sign in with OAuth provider using PKCE flow
1017
+ */
1018
+ async signInWithOAuth(options) {
1019
+ try {
1020
+ const { provider, redirectTo, skipBrowserRedirect } = options;
1021
+ const providerKey = encodeURIComponent(provider.toLowerCase());
1022
+ const codeVerifier = generateCodeVerifier();
1023
+ const codeChallenge = await generateCodeChallenge(codeVerifier);
1024
+ storePkceVerifier(codeVerifier);
1025
+ const params = { code_challenge: codeChallenge };
1026
+ if (redirectTo) params.redirect_uri = redirectTo;
1027
+ const isBuiltInProvider = oAuthProvidersSchema.options.includes(
1028
+ providerKey
1029
+ );
1030
+ const oauthPath = isBuiltInProvider ? `/api/auth/oauth/${providerKey}` : `/api/auth/oauth/custom/${providerKey}`;
1031
+ const response = await this.http.get(oauthPath, {
1032
+ params,
1033
+ skipAuthRefresh: true
1034
+ });
1035
+ if (!this.isServerMode() && typeof window !== "undefined" && !skipBrowserRedirect) {
1036
+ window.location.href = response.authUrl;
1037
+ return { data: {}, error: null };
1038
+ }
1039
+ return {
1040
+ data: { url: response.authUrl, provider: providerKey, codeVerifier },
1041
+ error: null
1042
+ };
1043
+ } catch (error) {
1044
+ if (error instanceof InsForgeError) {
1045
+ return { data: {}, error };
1046
+ }
1047
+ return {
1048
+ data: {},
1049
+ error: new InsForgeError(
1050
+ "An unexpected error occurred during OAuth initialization",
1051
+ 500,
1052
+ "UNEXPECTED_ERROR"
1053
+ )
1054
+ };
1055
+ }
1056
+ }
1057
+ /**
1058
+ * Exchange OAuth authorization code for tokens (PKCE flow)
1059
+ * Called automatically on initialization when insforge_code is in URL
1060
+ */
1061
+ async exchangeOAuthCode(code, codeVerifier) {
1062
+ try {
1063
+ const verifier = codeVerifier ?? retrievePkceVerifier();
1064
+ if (!verifier) {
1065
+ return {
1066
+ data: null,
1067
+ error: new InsForgeError(
1068
+ "PKCE code verifier not found. Ensure signInWithOAuth was called in the same browser session.",
1069
+ 400,
1070
+ "PKCE_VERIFIER_MISSING"
1071
+ )
1072
+ };
1073
+ }
1074
+ const request = {
1075
+ code,
1076
+ code_verifier: verifier
1077
+ };
1078
+ const response = await this.http.post(
1079
+ this.isServerMode() ? "/api/auth/oauth/exchange?client_type=mobile" : "/api/auth/oauth/exchange",
1080
+ request,
1081
+ { credentials: "include", skipAuthRefresh: true }
1082
+ );
1083
+ this.saveSessionFromResponse(response);
1084
+ return {
1085
+ data: response,
1086
+ error: null
1087
+ };
1088
+ } catch (error) {
1089
+ return wrapError(
1090
+ error,
1091
+ "An unexpected error occurred during OAuth code exchange"
1092
+ );
1093
+ }
1094
+ }
1095
+ /**
1096
+ * Sign in with an ID token from a native SDK (Google One Tap, etc.)
1097
+ * Use this for native mobile apps or Google One Tap on web.
1098
+ *
1099
+ * @param credentials.provider - The identity provider (currently only 'google' is supported)
1100
+ * @param credentials.token - The ID token from the native SDK
1101
+ */
1102
+ async signInWithIdToken(credentials) {
1103
+ try {
1104
+ const { provider, token } = credentials;
1105
+ const response = await this.http.post(
1106
+ "/api/auth/id-token?client_type=mobile",
1107
+ { provider, token },
1108
+ { credentials: "include", skipAuthRefresh: true }
1109
+ );
1110
+ this.saveSessionFromResponse(response);
1111
+ if (response.refreshToken) {
1112
+ this.http.setRefreshToken(response.refreshToken);
1113
+ }
1114
+ return {
1115
+ data: response,
1116
+ error: null
1117
+ };
1118
+ } catch (error) {
1119
+ return wrapError(
1120
+ error,
1121
+ "An unexpected error occurred during ID token sign in"
1122
+ );
1123
+ }
1124
+ }
1125
+ // ============================================================================
1126
+ // Session Management
1127
+ // ============================================================================
1128
+ /**
1129
+ * Refresh the current auth session.
1130
+ *
1131
+ * Browser mode:
1132
+ * - Uses httpOnly refresh cookie and optional CSRF header.
1133
+ *
1134
+ * Legacy server mode (`isServerMode: true`):
1135
+ * - Uses mobile auth flow and requires `refreshToken` in request body.
1136
+ *
1137
+ * SSR apps should prefer `createRefreshAuthRouter()` / `refreshAuth()` from
1138
+ * `@insforge/sdk/ssr`.
1139
+ */
1140
+ async refreshSession(options) {
1141
+ try {
1142
+ if (this.isServerMode() && !options?.refreshToken) {
1143
+ return {
1144
+ data: null,
1145
+ error: new InsForgeError(
1146
+ "refreshToken is required when refreshing session in server mode",
1147
+ 400,
1148
+ ERROR_CODES.AUTH_UNAUTHORIZED
1149
+ )
1150
+ };
1151
+ }
1152
+ const csrfToken = !this.isServerMode() ? getCsrfToken() : null;
1153
+ const response = await this.http.post(
1154
+ this.isServerMode() ? "/api/auth/refresh?client_type=mobile" : "/api/auth/refresh",
1155
+ this.isServerMode() ? { refresh_token: options?.refreshToken } : void 0,
1156
+ {
1157
+ headers: csrfToken ? { "X-CSRF-Token": csrfToken } : {},
1158
+ credentials: "include",
1159
+ skipAuthRefresh: true
1160
+ }
1161
+ );
1162
+ if (response.accessToken) {
1163
+ this.saveSessionFromResponse(response);
1164
+ }
1165
+ return { data: response, error: null };
1166
+ } catch (error) {
1167
+ return wrapError(
1168
+ error,
1169
+ "An unexpected error occurred during session refresh"
1170
+ );
1171
+ }
1172
+ }
1173
+ /**
1174
+ * Get current user, automatically waits for pending OAuth callback
1175
+ */
1176
+ async getCurrentUser() {
1177
+ await this.authCallbackHandled;
1178
+ try {
1179
+ if (this.isServerMode()) {
1180
+ const accessToken = this.tokenManager.getAccessToken();
1181
+ if (!accessToken) return { data: { user: null }, error: null };
1182
+ this.http.setAuthToken(accessToken);
1183
+ const response = await this.http.get(
1184
+ "/api/auth/sessions/current"
1185
+ );
1186
+ const user = response.user ?? null;
1187
+ return { data: { user }, error: null };
1188
+ }
1189
+ const session = this.tokenManager.getSession();
1190
+ if (session) {
1191
+ this.http.setAuthToken(session.accessToken);
1192
+ return { data: { user: session.user }, error: null };
1193
+ }
1194
+ if (typeof window !== "undefined") {
1195
+ const { data: refreshed, error: refreshError } = await this.refreshSession();
1196
+ if (refreshError) {
1197
+ return { data: { user: null }, error: refreshError };
1198
+ }
1199
+ if (refreshed?.accessToken) {
1200
+ return { data: { user: refreshed.user ?? null }, error: null };
1201
+ }
1202
+ }
1203
+ return { data: { user: null }, error: null };
1204
+ } catch (error) {
1205
+ if (error instanceof InsForgeError) {
1206
+ return { data: { user: null }, error };
1207
+ }
1208
+ return {
1209
+ data: { user: null },
1210
+ error: new InsForgeError(
1211
+ "An unexpected error occurred while getting user",
1212
+ 500,
1213
+ "UNEXPECTED_ERROR"
1214
+ )
1215
+ };
1216
+ }
1217
+ }
1218
+ // ============================================================================
1219
+ // Profile Management
1220
+ // ============================================================================
1221
+ async getProfile(userId) {
1222
+ try {
1223
+ const response = await this.http.get(
1224
+ `/api/auth/profiles/${userId}`
1225
+ );
1226
+ return { data: response, error: null };
1227
+ } catch (error) {
1228
+ return wrapError(
1229
+ error,
1230
+ "An unexpected error occurred while fetching user profile"
1231
+ );
1232
+ }
1233
+ }
1234
+ async setProfile(profile) {
1235
+ try {
1236
+ const response = await this.http.patch(
1237
+ "/api/auth/profiles/current",
1238
+ {
1239
+ profile
1240
+ }
1241
+ );
1242
+ const currentUser = this.tokenManager.getUser();
1243
+ if (!this.isServerMode() && currentUser && response.profile !== void 0) {
1244
+ this.tokenManager.setUser({
1245
+ ...currentUser,
1246
+ profile: response.profile
1247
+ });
1248
+ }
1249
+ return { data: response, error: null };
1250
+ } catch (error) {
1251
+ return wrapError(
1252
+ error,
1253
+ "An unexpected error occurred while updating user profile"
1254
+ );
1255
+ }
1256
+ }
1257
+ // ============================================================================
1258
+ // Email Verification
1259
+ // ============================================================================
1260
+ async resendVerificationEmail(request) {
1261
+ try {
1262
+ const response = await this.http.post("/api/auth/email/send-verification", request, {
1263
+ skipAuthRefresh: true
1264
+ });
1265
+ return { data: response, error: null };
1266
+ } catch (error) {
1267
+ return wrapError(
1268
+ error,
1269
+ "An unexpected error occurred while sending verification email"
1270
+ );
1271
+ }
1272
+ }
1273
+ async verifyEmail(request) {
1274
+ try {
1275
+ const response = await this.http.post(
1276
+ this.isServerMode() ? "/api/auth/email/verify?client_type=mobile" : "/api/auth/email/verify",
1277
+ request,
1278
+ { credentials: "include", skipAuthRefresh: true }
1279
+ );
1280
+ this.saveSessionFromResponse(response);
1281
+ if (response.refreshToken) {
1282
+ this.http.setRefreshToken(response.refreshToken);
1283
+ }
1284
+ return { data: response, error: null };
1285
+ } catch (error) {
1286
+ return wrapError(
1287
+ error,
1288
+ "An unexpected error occurred while verifying email"
1289
+ );
1290
+ }
1291
+ }
1292
+ // ============================================================================
1293
+ // Password Reset
1294
+ // ============================================================================
1295
+ async sendResetPasswordEmail(request) {
1296
+ try {
1297
+ const response = await this.http.post("/api/auth/email/send-reset-password", request, {
1298
+ skipAuthRefresh: true
1299
+ });
1300
+ return { data: response, error: null };
1301
+ } catch (error) {
1302
+ return wrapError(
1303
+ error,
1304
+ "An unexpected error occurred while sending password reset email"
1305
+ );
1306
+ }
1307
+ }
1308
+ async exchangeResetPasswordToken(request) {
1309
+ try {
1310
+ const response = await this.http.post(
1311
+ "/api/auth/email/exchange-reset-password-token",
1312
+ request,
1313
+ { skipAuthRefresh: true }
1314
+ );
1315
+ return { data: response, error: null };
1316
+ } catch (error) {
1317
+ return wrapError(
1318
+ error,
1319
+ "An unexpected error occurred while verifying reset code"
1320
+ );
1321
+ }
1322
+ }
1323
+ async resetPassword(request) {
1324
+ try {
1325
+ const response = await this.http.post(
1326
+ "/api/auth/email/reset-password",
1327
+ request,
1328
+ { skipAuthRefresh: true }
1329
+ );
1330
+ return { data: response, error: null };
1331
+ } catch (error) {
1332
+ return wrapError(
1333
+ error,
1334
+ "An unexpected error occurred while resetting password"
1335
+ );
1336
+ }
1337
+ }
1338
+ // ============================================================================
1339
+ // Configuration
1340
+ // ============================================================================
1341
+ async getPublicAuthConfig() {
1342
+ try {
1343
+ const response = await this.http.get(
1344
+ "/api/auth/public-config",
1345
+ { skipAuthRefresh: true }
1346
+ );
1347
+ return { data: response, error: null };
1348
+ } catch (error) {
1349
+ return wrapError(
1350
+ error,
1351
+ "An unexpected error occurred while fetching auth configuration"
1352
+ );
1353
+ }
1354
+ }
1355
+ };
1356
+
1357
+ // src/modules/database-postgrest.ts
1358
+ import { PostgrestClient } from "@supabase/postgrest-js";
1359
+ function createInsForgePostgrestFetch(httpClient) {
1360
+ return async (input, init) => {
1361
+ const url = typeof input === "string" ? input : input.toString();
1362
+ const urlObj = new URL(url);
1363
+ const pathname = urlObj.pathname.slice(1);
1364
+ const rpcMatch = pathname.match(/^rpc\/(.+)$/);
1365
+ const endpoint = rpcMatch ? `/api/database/rpc/${rpcMatch[1]}` : `/api/database/records/${pathname}`;
1366
+ const insforgeUrl = `${httpClient.baseUrl}${endpoint}${urlObj.search}`;
1367
+ const headers = new Headers(httpClient.getHeaders());
1368
+ new Headers(init?.headers).forEach((value, key) => {
1369
+ headers.set(key, value);
1370
+ });
1371
+ const response = await httpClient.rawFetch(insforgeUrl, {
1372
+ ...init,
1373
+ headers
1374
+ });
1375
+ return response;
1376
+ };
1377
+ }
1378
+ var Database = class {
1379
+ constructor(httpClient) {
1380
+ this.postgrest = new PostgrestClient("http://dummy", {
1381
+ fetch: createInsForgePostgrestFetch(httpClient),
1382
+ headers: {}
1383
+ });
1384
+ }
1385
+ /**
1386
+ * Create a query builder for a table
1387
+ *
1388
+ * @example
1389
+ * // Basic query
1390
+ * const { data, error } = await client.database
1391
+ * .from('posts')
1392
+ * .select('*')
1393
+ * .eq('user_id', userId);
1394
+ *
1395
+ * // With count (Supabase style!)
1396
+ * const { data, error, count } = await client.database
1397
+ * .from('posts')
1398
+ * .select('*', { count: 'exact' })
1399
+ * .range(0, 9);
1400
+ *
1401
+ * // Just get count, no data
1402
+ * const { count } = await client.database
1403
+ * .from('posts')
1404
+ * .select('*', { count: 'exact', head: true });
1405
+ *
1406
+ * // Complex queries with OR
1407
+ * const { data } = await client.database
1408
+ * .from('posts')
1409
+ * .select('*, users!inner(*)')
1410
+ * .or('status.eq.active,status.eq.pending');
1411
+ *
1412
+ * // All features work:
1413
+ * - Nested selects
1414
+ * - Foreign key expansion
1415
+ * - OR/AND/NOT conditions
1416
+ * - Count with head
1417
+ * - Range pagination
1418
+ * - Upserts
1419
+ */
1420
+ from(table) {
1421
+ return this.postgrest.from(table);
1422
+ }
1423
+ /**
1424
+ * Call a PostgreSQL function (RPC)
1425
+ *
1426
+ * @example
1427
+ * // Call a function with parameters
1428
+ * const { data, error } = await client.database
1429
+ * .rpc('get_user_stats', { user_id: 123 });
1430
+ *
1431
+ * // Call a function with no parameters
1432
+ * const { data, error } = await client.database
1433
+ * .rpc('get_all_active_users');
1434
+ *
1435
+ * // With options (head, count, get)
1436
+ * const { data, count } = await client.database
1437
+ * .rpc('search_posts', { query: 'hello' }, { count: 'exact' });
1438
+ */
1439
+ rpc(fn, args, options) {
1440
+ return this.postgrest.rpc(fn, args, options);
1441
+ }
1442
+ };
1443
+
1444
+ // src/modules/storage.ts
1445
+ var StorageBucket = class {
1446
+ constructor(bucketName, http) {
1447
+ this.bucketName = bucketName;
1448
+ this.http = http;
1449
+ }
1450
+ /**
1451
+ * Upload a file with a specific key
1452
+ * Uses the upload strategy from backend (direct or presigned)
1453
+ * @param path - The object key/path
1454
+ * @param file - File or Blob to upload
1455
+ */
1456
+ async upload(path, file) {
1457
+ try {
1458
+ const strategyResponse = await this.http.post(
1459
+ `/api/storage/buckets/${this.bucketName}/upload-strategy`,
1460
+ {
1461
+ filename: path,
1462
+ contentType: file.type || "application/octet-stream",
1463
+ size: file.size
1464
+ }
1465
+ );
1466
+ if (strategyResponse.method === "presigned") {
1467
+ return await this.uploadWithPresignedUrl(strategyResponse, file);
1468
+ }
1469
+ if (strategyResponse.method === "direct") {
1470
+ const formData = new FormData();
1471
+ formData.append("file", file);
1472
+ const response = await this.http.request(
1473
+ "PUT",
1474
+ `/api/storage/buckets/${this.bucketName}/objects/${encodeURIComponent(path)}`,
1475
+ {
1476
+ body: formData,
1477
+ headers: {
1478
+ // Don't set Content-Type, let browser set multipart boundary
1479
+ }
1480
+ }
1481
+ );
1482
+ return { data: response, error: null };
1483
+ }
1484
+ throw new InsForgeError(
1485
+ `Unsupported upload method: ${strategyResponse.method}`,
1486
+ 500,
1487
+ "STORAGE_ERROR"
1488
+ );
1489
+ } catch (error) {
1490
+ return {
1491
+ data: null,
1492
+ error: error instanceof InsForgeError ? error : new InsForgeError(
1493
+ "Upload failed",
1494
+ 500,
1495
+ "STORAGE_ERROR"
1496
+ )
1497
+ };
1498
+ }
1499
+ }
1500
+ /**
1501
+ * Upload a file with auto-generated key
1502
+ * Uses the upload strategy from backend (direct or presigned)
1503
+ * @param file - File or Blob to upload
1504
+ */
1505
+ async uploadAuto(file) {
1506
+ try {
1507
+ const filename = file instanceof File ? file.name : "file";
1508
+ const strategyResponse = await this.http.post(
1509
+ `/api/storage/buckets/${this.bucketName}/upload-strategy`,
1510
+ {
1511
+ filename,
1512
+ contentType: file.type || "application/octet-stream",
1513
+ size: file.size
1514
+ }
1515
+ );
1516
+ if (strategyResponse.method === "presigned") {
1517
+ return await this.uploadWithPresignedUrl(strategyResponse, file);
1518
+ }
1519
+ if (strategyResponse.method === "direct") {
1520
+ const formData = new FormData();
1521
+ formData.append("file", file);
1522
+ const response = await this.http.request(
1523
+ "POST",
1524
+ `/api/storage/buckets/${this.bucketName}/objects`,
1525
+ {
1526
+ body: formData,
1527
+ headers: {
1528
+ // Don't set Content-Type, let browser set multipart boundary
1529
+ }
1530
+ }
1531
+ );
1532
+ return { data: response, error: null };
1533
+ }
1534
+ throw new InsForgeError(
1535
+ `Unsupported upload method: ${strategyResponse.method}`,
1536
+ 500,
1537
+ "STORAGE_ERROR"
1538
+ );
1539
+ } catch (error) {
1540
+ return {
1541
+ data: null,
1542
+ error: error instanceof InsForgeError ? error : new InsForgeError(
1543
+ "Upload failed",
1544
+ 500,
1545
+ "STORAGE_ERROR"
1546
+ )
1547
+ };
1548
+ }
1549
+ }
1550
+ /**
1551
+ * Internal method to handle presigned URL uploads
1552
+ */
1553
+ async uploadWithPresignedUrl(strategy, file) {
1554
+ try {
1555
+ const formData = new FormData();
1556
+ if (strategy.fields) {
1557
+ Object.entries(strategy.fields).forEach(([key, value]) => {
1558
+ formData.append(key, value);
1559
+ });
1560
+ }
1561
+ formData.append("file", file);
1562
+ const uploadResponse = await fetch(strategy.uploadUrl, {
1563
+ method: "POST",
1564
+ body: formData
1565
+ });
1566
+ if (!uploadResponse.ok) {
1567
+ throw new InsForgeError(
1568
+ `Upload to storage failed: ${uploadResponse.statusText}`,
1569
+ uploadResponse.status,
1570
+ "STORAGE_ERROR"
1571
+ );
1572
+ }
1573
+ if (strategy.confirmRequired && strategy.confirmUrl) {
1574
+ const confirmResponse = await this.http.post(
1575
+ strategy.confirmUrl,
1576
+ {
1577
+ size: file.size,
1578
+ contentType: file.type || "application/octet-stream"
1579
+ }
1580
+ );
1581
+ return { data: confirmResponse, error: null };
1582
+ }
1583
+ return {
1584
+ data: {
1585
+ key: strategy.key,
1586
+ bucket: this.bucketName,
1587
+ size: file.size,
1588
+ mimeType: file.type || "application/octet-stream",
1589
+ uploadedAt: (/* @__PURE__ */ new Date()).toISOString(),
1590
+ url: this.getPublicUrl(strategy.key)
1591
+ },
1592
+ error: null
1593
+ };
1594
+ } catch (error) {
1595
+ throw error instanceof InsForgeError ? error : new InsForgeError(
1596
+ "Presigned upload failed",
1597
+ 500,
1598
+ "STORAGE_ERROR"
1599
+ );
1600
+ }
1601
+ }
1602
+ /**
1603
+ * Download a file
1604
+ * Uses the download strategy from backend (direct or presigned)
1605
+ * @param path - The object key/path
1606
+ * Returns the file as a Blob
1607
+ */
1608
+ async download(path) {
1609
+ try {
1610
+ const encodedKey = encodeURIComponent(path);
1611
+ let strategyResponse;
1612
+ try {
1613
+ strategyResponse = await this.http.get(
1614
+ `/api/storage/buckets/${this.bucketName}/download-strategy/objects/${encodedKey}`
1615
+ );
1616
+ } catch (err) {
1617
+ const status = err instanceof InsForgeError ? err.statusCode : void 0;
1618
+ if (status === 404 || status === 405) {
1619
+ strategyResponse = await this.http.post(
1620
+ `/api/storage/buckets/${this.bucketName}/objects/${encodedKey}/download-strategy`,
1621
+ {}
1622
+ );
1623
+ } else {
1624
+ throw err;
1625
+ }
1626
+ }
1627
+ const downloadUrl = strategyResponse.url;
1628
+ const headers = {};
1629
+ if (strategyResponse.method === "direct") {
1630
+ Object.assign(headers, this.http.getHeaders());
1631
+ }
1632
+ const response = await fetch(downloadUrl, {
1633
+ method: "GET",
1634
+ headers
1635
+ });
1636
+ if (!response.ok) {
1637
+ try {
1638
+ const error = await response.json();
1639
+ throw InsForgeError.fromApiError(error);
1640
+ } catch {
1641
+ throw new InsForgeError(
1642
+ `Download failed: ${response.statusText}`,
1643
+ response.status,
1644
+ "STORAGE_ERROR"
1645
+ );
1646
+ }
1647
+ }
1648
+ const blob = await response.blob();
1649
+ return { data: blob, error: null };
1650
+ } catch (error) {
1651
+ return {
1652
+ data: null,
1653
+ error: error instanceof InsForgeError ? error : new InsForgeError(
1654
+ "Download failed",
1655
+ 500,
1656
+ "STORAGE_ERROR"
1657
+ )
1658
+ };
1659
+ }
1660
+ }
1661
+ /**
1662
+ * Get public URL for a file
1663
+ * @param path - The object key/path
1664
+ */
1665
+ getPublicUrl(path) {
1666
+ return `${this.http.baseUrl}/api/storage/buckets/${this.bucketName}/objects/${encodeURIComponent(path)}`;
1667
+ }
1668
+ /**
1669
+ * List objects in the bucket
1670
+ * @param prefix - Filter by key prefix
1671
+ * @param search - Search in file names
1672
+ * @param limit - Maximum number of results (default: 100, max: 1000)
1673
+ * @param offset - Number of results to skip
1674
+ */
1675
+ async list(options) {
1676
+ try {
1677
+ const params = {};
1678
+ if (options?.prefix) params.prefix = options.prefix;
1679
+ if (options?.search) params.search = options.search;
1680
+ if (options?.limit) params.limit = options.limit.toString();
1681
+ if (options?.offset) params.offset = options.offset.toString();
1682
+ const response = await this.http.get(
1683
+ `/api/storage/buckets/${this.bucketName}/objects`,
1684
+ { params }
1685
+ );
1686
+ return { data: response, error: null };
1687
+ } catch (error) {
1688
+ return {
1689
+ data: null,
1690
+ error: error instanceof InsForgeError ? error : new InsForgeError(
1691
+ "List failed",
1692
+ 500,
1693
+ "STORAGE_ERROR"
1694
+ )
1695
+ };
1696
+ }
1697
+ }
1698
+ /**
1699
+ * Delete a file
1700
+ * @param path - The object key/path
1701
+ */
1702
+ async remove(path) {
1703
+ try {
1704
+ const response = await this.http.delete(
1705
+ `/api/storage/buckets/${this.bucketName}/objects/${encodeURIComponent(path)}`
1706
+ );
1707
+ return { data: response, error: null };
1708
+ } catch (error) {
1709
+ return {
1710
+ data: null,
1711
+ error: error instanceof InsForgeError ? error : new InsForgeError(
1712
+ "Delete failed",
1713
+ 500,
1714
+ "STORAGE_ERROR"
1715
+ )
1716
+ };
1717
+ }
1718
+ }
1719
+ };
1720
+ var Storage = class {
1721
+ constructor(http) {
1722
+ this.http = http;
1723
+ }
1724
+ /**
1725
+ * Get a bucket instance for operations
1726
+ * @param bucketName - Name of the bucket
1727
+ */
1728
+ from(bucketName) {
1729
+ return new StorageBucket(bucketName, this.http);
1730
+ }
1731
+ };
1732
+
1733
+ // src/modules/ai.ts
1734
+ var AI = class {
1735
+ constructor(http) {
1736
+ this.http = http;
1737
+ this.chat = new Chat(http);
1738
+ this.images = new Images(http);
1739
+ this.embeddings = new Embeddings(http);
1740
+ }
1741
+ };
1742
+ var Chat = class {
1743
+ constructor(http) {
1744
+ this.completions = new ChatCompletions(http);
1745
+ }
1746
+ };
1747
+ var ChatCompletions = class {
1748
+ constructor(http) {
1749
+ this.http = http;
1750
+ }
1751
+ /**
1752
+ * Create a chat completion - OpenAI-like response format
1753
+ *
1754
+ * @example
1755
+ * ```typescript
1756
+ * // Non-streaming
1757
+ * const completion = await client.ai.chat.completions.create({
1758
+ * model: 'gpt-4',
1759
+ * messages: [{ role: 'user', content: 'Hello!' }]
1760
+ * });
1761
+ * console.log(completion.choices[0].message.content);
1762
+ *
1763
+ * // With images (OpenAI-compatible format)
1764
+ * const response = await client.ai.chat.completions.create({
1765
+ * model: 'gpt-4-vision',
1766
+ * messages: [{
1767
+ * role: 'user',
1768
+ * content: [
1769
+ * { type: 'text', text: 'What is in this image?' },
1770
+ * { type: 'image_url', image_url: { url: 'https://example.com/image.jpg' } }
1771
+ * ]
1772
+ * }]
1773
+ * });
1774
+ *
1775
+ * // With PDF files
1776
+ * const pdfResponse = await client.ai.chat.completions.create({
1777
+ * model: 'anthropic/claude-3.5-sonnet',
1778
+ * messages: [{
1779
+ * role: 'user',
1780
+ * content: [
1781
+ * { type: 'text', text: 'Summarize this document' },
1782
+ * { type: 'file', file: { filename: 'doc.pdf', file_data: 'https://example.com/doc.pdf' } }
1783
+ * ]
1784
+ * }],
1785
+ * fileParser: { enabled: true, pdf: { engine: 'mistral-ocr' } }
1786
+ * });
1787
+ *
1788
+ * // With web search
1789
+ * const searchResponse = await client.ai.chat.completions.create({
1790
+ * model: 'openai/gpt-4',
1791
+ * messages: [{ role: 'user', content: 'What are the latest news about AI?' }],
1792
+ * webSearch: { enabled: true, maxResults: 5 }
1793
+ * });
1794
+ * // Access citations from response.choices[0].message.annotations
1795
+ *
1796
+ * // With thinking/reasoning mode (Anthropic models)
1797
+ * const thinkingResponse = await client.ai.chat.completions.create({
1798
+ * model: 'anthropic/claude-3.5-sonnet',
1799
+ * messages: [{ role: 'user', content: 'Solve this complex math problem...' }],
1800
+ * thinking: true
1801
+ * });
1802
+ *
1803
+ * // Streaming - returns async iterable
1804
+ * const stream = await client.ai.chat.completions.create({
1805
+ * model: 'gpt-4',
1806
+ * messages: [{ role: 'user', content: 'Tell me a story' }],
1807
+ * stream: true
1808
+ * });
1809
+ *
1810
+ * for await (const chunk of stream) {
1811
+ * if (chunk.choices[0]?.delta?.content) {
1812
+ * process.stdout.write(chunk.choices[0].delta.content);
1813
+ * }
1814
+ * }
1815
+ * ```
1816
+ */
1817
+ async create(params) {
1818
+ const backendParams = {
1819
+ model: params.model,
1820
+ messages: params.messages,
1821
+ temperature: params.temperature,
1822
+ maxTokens: params.maxTokens,
1823
+ topP: params.topP,
1824
+ stream: params.stream,
1825
+ // New plugin options
1826
+ webSearch: params.webSearch,
1827
+ fileParser: params.fileParser,
1828
+ thinking: params.thinking,
1829
+ // Tool calling options
1830
+ tools: params.tools,
1831
+ toolChoice: params.toolChoice,
1832
+ parallelToolCalls: params.parallelToolCalls
1833
+ };
1834
+ if (params.stream) {
1835
+ const headers = this.http.getHeaders();
1836
+ headers["Content-Type"] = "application/json";
1837
+ const response2 = await this.http.fetch(
1838
+ `${this.http.baseUrl}/api/ai/chat/completion`,
1839
+ {
1840
+ method: "POST",
1841
+ headers,
1842
+ body: JSON.stringify(backendParams)
1843
+ }
1844
+ );
1845
+ if (!response2.ok) {
1846
+ const error = await response2.json();
1847
+ throw new Error(error.error || "Stream request failed");
1848
+ }
1849
+ return this.parseSSEStream(response2, params.model);
1850
+ }
1851
+ const response = await this.http.post(
1852
+ "/api/ai/chat/completion",
1853
+ backendParams
1854
+ );
1855
+ const content = response.text || "";
1856
+ return {
1857
+ id: `chatcmpl-${Date.now()}`,
1858
+ object: "chat.completion",
1859
+ created: Math.floor(Date.now() / 1e3),
1860
+ model: response.metadata?.model,
1861
+ choices: [
1862
+ {
1863
+ index: 0,
1864
+ message: {
1865
+ role: "assistant",
1866
+ content,
1867
+ // Include tool_calls if present (from tool calling)
1868
+ ...response.tool_calls?.length && { tool_calls: response.tool_calls },
1869
+ // Include annotations if present (from web search or file parsing)
1870
+ ...response.annotations?.length && { annotations: response.annotations }
1871
+ },
1872
+ finish_reason: response.tool_calls?.length ? "tool_calls" : "stop"
1873
+ }
1874
+ ],
1875
+ usage: response.metadata?.usage || {
1876
+ prompt_tokens: 0,
1877
+ completion_tokens: 0,
1878
+ total_tokens: 0
1879
+ }
1880
+ };
1881
+ }
1882
+ /**
1883
+ * Parse SSE stream into async iterable of OpenAI-like chunks
1884
+ */
1885
+ async *parseSSEStream(response, model) {
1886
+ const reader = response.body.getReader();
1887
+ const decoder = new TextDecoder();
1888
+ let buffer = "";
1889
+ try {
1890
+ while (true) {
1891
+ const { done, value } = await reader.read();
1892
+ if (done) break;
1893
+ buffer += decoder.decode(value, { stream: true });
1894
+ const lines = buffer.split("\n");
1895
+ buffer = lines.pop() || "";
1896
+ for (const line of lines) {
1897
+ if (line.startsWith("data: ")) {
1898
+ const dataStr = line.slice(6).trim();
1899
+ if (dataStr) {
1900
+ try {
1901
+ const data = JSON.parse(dataStr);
1902
+ if (data.chunk || data.content) {
1903
+ yield {
1904
+ id: `chatcmpl-${Date.now()}`,
1905
+ object: "chat.completion.chunk",
1906
+ created: Math.floor(Date.now() / 1e3),
1907
+ model,
1908
+ choices: [
1909
+ {
1910
+ index: 0,
1911
+ delta: {
1912
+ content: data.chunk || data.content
1913
+ },
1914
+ finish_reason: null
1915
+ }
1916
+ ]
1917
+ };
1918
+ }
1919
+ if (data.tool_calls?.length) {
1920
+ yield {
1921
+ id: `chatcmpl-${Date.now()}`,
1922
+ object: "chat.completion.chunk",
1923
+ created: Math.floor(Date.now() / 1e3),
1924
+ model,
1925
+ choices: [
1926
+ {
1927
+ index: 0,
1928
+ delta: {
1929
+ tool_calls: data.tool_calls
1930
+ },
1931
+ finish_reason: "tool_calls"
1932
+ }
1933
+ ]
1934
+ };
1935
+ }
1936
+ if (data.done) {
1937
+ reader.releaseLock();
1938
+ return;
1939
+ }
1940
+ } catch (e) {
1941
+ console.warn("Failed to parse SSE data:", dataStr);
1942
+ }
1943
+ }
1944
+ }
1945
+ }
1946
+ }
1947
+ } finally {
1948
+ reader.releaseLock();
1949
+ }
1950
+ }
1951
+ };
1952
+ var Embeddings = class {
1953
+ constructor(http) {
1954
+ this.http = http;
1955
+ }
1956
+ /**
1957
+ * Create embeddings for text input - OpenAI-like response format
1958
+ *
1959
+ * @example
1960
+ * ```typescript
1961
+ * // Single text input
1962
+ * const response = await client.ai.embeddings.create({
1963
+ * model: 'openai/text-embedding-3-small',
1964
+ * input: 'Hello world'
1965
+ * });
1966
+ * console.log(response.data[0].embedding); // number[]
1967
+ *
1968
+ * // Multiple text inputs
1969
+ * const response = await client.ai.embeddings.create({
1970
+ * model: 'openai/text-embedding-3-small',
1971
+ * input: ['Hello world', 'Goodbye world']
1972
+ * });
1973
+ * response.data.forEach((item, i) => {
1974
+ * console.log(`Embedding ${i}:`, item.embedding.slice(0, 5)); // First 5 dimensions
1975
+ * });
1976
+ *
1977
+ * // With custom dimensions (if supported by model)
1978
+ * const response = await client.ai.embeddings.create({
1979
+ * model: 'openai/text-embedding-3-small',
1980
+ * input: 'Hello world',
1981
+ * dimensions: 256
1982
+ * });
1983
+ *
1984
+ * // With base64 encoding format
1985
+ * const response = await client.ai.embeddings.create({
1986
+ * model: 'openai/text-embedding-3-small',
1987
+ * input: 'Hello world',
1988
+ * encoding_format: 'base64'
1989
+ * });
1990
+ * ```
1991
+ */
1992
+ async create(params) {
1993
+ const response = await this.http.post(
1994
+ "/api/ai/embeddings",
1995
+ params
1996
+ );
1997
+ return {
1998
+ object: response.object,
1999
+ data: response.data,
2000
+ model: response.metadata?.model,
2001
+ usage: response.metadata?.usage ? {
2002
+ prompt_tokens: response.metadata.usage.promptTokens || 0,
2003
+ total_tokens: response.metadata.usage.totalTokens || 0
2004
+ } : {
2005
+ prompt_tokens: 0,
2006
+ total_tokens: 0
2007
+ }
2008
+ };
2009
+ }
2010
+ };
2011
+ var Images = class {
2012
+ constructor(http) {
2013
+ this.http = http;
2014
+ }
2015
+ /**
2016
+ * Generate images - OpenAI-like response format
2017
+ *
2018
+ * @example
2019
+ * ```typescript
2020
+ * // Text-to-image
2021
+ * const response = await client.ai.images.generate({
2022
+ * model: 'dall-e-3',
2023
+ * prompt: 'A sunset over mountains',
2024
+ * });
2025
+ * console.log(response.images[0].url);
2026
+ *
2027
+ * // Image-to-image (with input images)
2028
+ * const response = await client.ai.images.generate({
2029
+ * model: 'stable-diffusion-xl',
2030
+ * prompt: 'Transform this into a watercolor painting',
2031
+ * images: [
2032
+ * { url: 'https://example.com/input.jpg' },
2033
+ * // or base64-encoded Data URI:
2034
+ * { url: 'data:image/jpeg;base64,/9j/4AAQ...' }
2035
+ * ]
2036
+ * });
2037
+ * ```
2038
+ */
2039
+ async generate(params) {
2040
+ const response = await this.http.post(
2041
+ "/api/ai/image/generation",
2042
+ params
2043
+ );
2044
+ let data = [];
2045
+ if (response.images && response.images.length > 0) {
2046
+ data = response.images.map((img) => ({
2047
+ b64_json: img.imageUrl.replace(/^data:image\/\w+;base64,/, ""),
2048
+ content: response.text
2049
+ }));
2050
+ } else if (response.text) {
2051
+ data = [{ content: response.text }];
2052
+ }
2053
+ return {
2054
+ created: Math.floor(Date.now() / 1e3),
2055
+ data,
2056
+ ...response.metadata?.usage && {
2057
+ usage: {
2058
+ total_tokens: response.metadata.usage.totalTokens || 0,
2059
+ input_tokens: response.metadata.usage.promptTokens || 0,
2060
+ output_tokens: response.metadata.usage.completionTokens || 0
2061
+ }
2062
+ }
2063
+ };
2064
+ }
2065
+ };
2066
+
2067
+ // src/modules/functions.ts
2068
+ var Functions = class _Functions {
2069
+ constructor(http, functionsUrl) {
2070
+ this.http = http;
2071
+ this.functionsUrl = functionsUrl || _Functions.deriveSubhostingUrl(http.baseUrl);
2072
+ }
2073
+ /**
2074
+ * Derive the subhosting URL from the base URL.
2075
+ * Base URL pattern: https://{appKey}.{region}.insforge.app
2076
+ * Functions URL: https://{appKey}.functions.insforge.app
2077
+ * Only applies to .insforge.app domains.
2078
+ */
2079
+ static deriveSubhostingUrl(baseUrl) {
2080
+ try {
2081
+ const { hostname } = new URL(baseUrl);
2082
+ if (!hostname.endsWith(".insforge.app")) return void 0;
2083
+ const appKey = hostname.split(".")[0];
2084
+ return `https://${appKey}.functions.insforge.app`;
2085
+ } catch {
2086
+ return void 0;
2087
+ }
2088
+ }
2089
+ /**
2090
+ * Build a Request for in-process dispatch. The host is a non-routable
2091
+ * placeholder; the router only reads pathname.
2092
+ */
2093
+ buildInProcessRequest(slug, method, body, callerHeaders) {
2094
+ const url = new URL("/" + slug, "http://insforge.local").toString();
2095
+ const headers = { ...this.http.getHeaders() };
2096
+ const reqBody = serializeBody(method, body, headers);
2097
+ Object.assign(headers, callerHeaders);
2098
+ return new Request(url, {
2099
+ method,
2100
+ headers,
2101
+ body: reqBody
2102
+ });
2103
+ }
2104
+ /**
2105
+ * Invoke an Edge Function.
2106
+ *
2107
+ * Dispatch order:
2108
+ * 1. If `globalThis.__insforge_dispatch__` is present, call it in-process.
2109
+ * This avoids Deno Subhosting's 508 Loop Detected when one bundled
2110
+ * function invokes another inside the same deployment.
2111
+ * 2. Otherwise, try the configured subhosting URL.
2112
+ * 3. On 404 from subhosting, fall back to the proxy path.
2113
+ *
2114
+ * @param slug The function slug to invoke
2115
+ * @param options Request options
2116
+ */
2117
+ async invoke(slug, options = {}) {
2118
+ const { method = "POST", body, headers = {} } = options;
2119
+ const dispatch = globalThis.__insforge_dispatch__;
2120
+ const localFunctionsUrl = _Functions.deriveSubhostingUrl(this.http.baseUrl);
2121
+ if (typeof dispatch === "function" && !!localFunctionsUrl && this.functionsUrl === localFunctionsUrl) {
2122
+ try {
2123
+ const req = this.buildInProcessRequest(slug, method, body, headers);
2124
+ const res = await dispatch(req);
2125
+ const data = await parseResponse(res);
2126
+ return { data, error: null };
2127
+ } catch (error) {
2128
+ if (error instanceof Error && error.name === "AbortError") throw error;
2129
+ return {
2130
+ data: null,
2131
+ error: error instanceof InsForgeError ? error : new InsForgeError(
2132
+ error instanceof Error ? error.message : "Function invocation failed",
2133
+ 500,
2134
+ "FUNCTION_ERROR"
2135
+ )
2136
+ };
2137
+ }
2138
+ }
2139
+ if (this.functionsUrl) {
2140
+ try {
2141
+ const data = await this.http.request(method, `${this.functionsUrl}/${slug}`, {
2142
+ body,
2143
+ headers
2144
+ });
2145
+ return { data, error: null };
2146
+ } catch (error) {
2147
+ if (error instanceof Error && error.name === "AbortError") throw error;
2148
+ if (error instanceof InsForgeError && error.statusCode === 404) {
2149
+ } else {
2150
+ return {
2151
+ data: null,
2152
+ error: error instanceof InsForgeError ? error : new InsForgeError(
2153
+ error instanceof Error ? error.message : "Function invocation failed",
2154
+ 500,
2155
+ "FUNCTION_ERROR"
2156
+ )
2157
+ };
2158
+ }
2159
+ }
2160
+ }
2161
+ try {
2162
+ const path = `/functions/${slug}`;
2163
+ const data = await this.http.request(method, path, { body, headers });
2164
+ return { data, error: null };
2165
+ } catch (error) {
2166
+ if (error instanceof Error && error.name === "AbortError") throw error;
2167
+ return {
2168
+ data: null,
2169
+ error: error instanceof InsForgeError ? error : new InsForgeError(
2170
+ error instanceof Error ? error.message : "Function invocation failed",
2171
+ 500,
2172
+ "FUNCTION_ERROR"
2173
+ )
2174
+ };
2175
+ }
2176
+ }
2177
+ };
2178
+
2179
+ // src/modules/realtime.ts
2180
+ import { io } from "socket.io-client";
2181
+ var CONNECT_TIMEOUT = 1e4;
2182
+ var Realtime = class {
2183
+ constructor(baseUrl, tokenManager, anonKey) {
2184
+ this.socket = null;
2185
+ this.connectPromise = null;
2186
+ this.subscribedChannels = /* @__PURE__ */ new Set();
2187
+ this.eventListeners = /* @__PURE__ */ new Map();
2188
+ this.baseUrl = baseUrl;
2189
+ this.tokenManager = tokenManager;
2190
+ this.anonKey = anonKey;
2191
+ this.tokenManager.onTokenChange = () => this.onTokenChange();
2192
+ }
2193
+ notifyListeners(event, payload) {
2194
+ const listeners = this.eventListeners.get(event);
2195
+ if (!listeners) return;
2196
+ for (const cb of listeners) {
2197
+ try {
2198
+ cb(payload);
2199
+ } catch (err) {
2200
+ console.error(`Error in ${event} callback:`, err);
2201
+ }
2202
+ }
2203
+ }
2204
+ /**
2205
+ * Connect to the realtime server
2206
+ * @returns Promise that resolves when connected
2207
+ */
2208
+ connect() {
2209
+ if (this.socket?.connected) {
2210
+ return Promise.resolve();
2211
+ }
2212
+ if (this.connectPromise) {
2213
+ return this.connectPromise;
2214
+ }
2215
+ this.connectPromise = new Promise((resolve, reject) => {
2216
+ const token = this.tokenManager.getAccessToken() ?? this.anonKey;
2217
+ this.socket = io(this.baseUrl, {
2218
+ transports: ["websocket"],
2219
+ auth: token ? { token } : void 0
2220
+ });
2221
+ let initialConnection = true;
2222
+ let timeoutId = null;
2223
+ const cleanup = () => {
2224
+ if (timeoutId) {
2225
+ clearTimeout(timeoutId);
2226
+ timeoutId = null;
2227
+ }
2228
+ };
2229
+ timeoutId = setTimeout(() => {
2230
+ if (initialConnection) {
2231
+ initialConnection = false;
2232
+ this.connectPromise = null;
2233
+ this.socket?.disconnect();
2234
+ this.socket = null;
2235
+ reject(new Error(`Connection timeout after ${CONNECT_TIMEOUT}ms`));
2236
+ }
2237
+ }, CONNECT_TIMEOUT);
2238
+ this.socket.on("connect", () => {
2239
+ cleanup();
2240
+ for (const channel of this.subscribedChannels) {
2241
+ this.socket.emit("realtime:subscribe", { channel });
2242
+ }
2243
+ this.notifyListeners("connect");
2244
+ if (initialConnection) {
2245
+ initialConnection = false;
2246
+ this.connectPromise = null;
2247
+ resolve();
2248
+ }
2249
+ });
2250
+ this.socket.on("connect_error", (error) => {
2251
+ cleanup();
2252
+ this.notifyListeners("connect_error", error);
2253
+ if (initialConnection) {
2254
+ initialConnection = false;
2255
+ this.connectPromise = null;
2256
+ reject(error);
2257
+ }
2258
+ });
2259
+ this.socket.on("disconnect", (reason) => {
2260
+ this.notifyListeners("disconnect", reason);
2261
+ });
2262
+ this.socket.on("realtime:error", (error) => {
2263
+ this.notifyListeners("error", error);
2264
+ });
2265
+ this.socket.onAny((event, message) => {
2266
+ if (event === "realtime:error") return;
2267
+ this.notifyListeners(event, message);
2268
+ });
2269
+ });
2270
+ return this.connectPromise;
2271
+ }
2272
+ /**
2273
+ * Disconnect from the realtime server
2274
+ */
2275
+ disconnect() {
2276
+ if (this.socket) {
2277
+ this.socket.disconnect();
2278
+ this.socket = null;
2279
+ }
2280
+ this.subscribedChannels.clear();
2281
+ }
2282
+ /**
2283
+ * Handle token changes (e.g., after auth refresh)
2284
+ * Updates socket auth so reconnects use the new token
2285
+ * If connected, triggers reconnect to apply new token immediately
2286
+ */
2287
+ onTokenChange() {
2288
+ const token = this.tokenManager.getAccessToken() ?? this.anonKey;
2289
+ if (this.socket) {
2290
+ this.socket.auth = token ? { token } : {};
2291
+ }
2292
+ if (this.socket && (this.socket.connected || this.connectPromise)) {
2293
+ this.socket.disconnect();
2294
+ this.socket.connect();
2295
+ }
2296
+ }
2297
+ /**
2298
+ * Check if connected to the realtime server
2299
+ */
2300
+ get isConnected() {
2301
+ return this.socket?.connected ?? false;
2302
+ }
2303
+ /**
2304
+ * Get the current connection state
2305
+ */
2306
+ get connectionState() {
2307
+ if (!this.socket) return "disconnected";
2308
+ if (this.socket.connected) return "connected";
2309
+ return "connecting";
2310
+ }
2311
+ /**
2312
+ * Get the socket ID (if connected)
2313
+ */
2314
+ get socketId() {
2315
+ return this.socket?.id;
2316
+ }
2317
+ /**
2318
+ * Subscribe to a channel
2319
+ *
2320
+ * Automatically connects if not already connected.
2321
+ *
2322
+ * @param channel - Channel name (e.g., 'orders:123', 'broadcast')
2323
+ * @returns Promise with the subscription response
2324
+ */
2325
+ async subscribe(channel) {
2326
+ if (this.subscribedChannels.has(channel)) {
2327
+ return { ok: true, channel, presence: { members: [] } };
2328
+ }
2329
+ if (!this.socket?.connected) {
2330
+ try {
2331
+ await this.connect();
2332
+ } catch (error) {
2333
+ const message = error instanceof Error ? error.message : "Connection failed";
2334
+ return { ok: false, channel, error: { code: "CONNECTION_FAILED", message } };
2335
+ }
2336
+ }
2337
+ return new Promise((resolve) => {
2338
+ this.socket.emit("realtime:subscribe", { channel }, (response) => {
2339
+ if (response.ok) {
2340
+ this.subscribedChannels.add(channel);
2341
+ }
2342
+ resolve(response);
2343
+ });
2344
+ });
2345
+ }
2346
+ /**
2347
+ * Unsubscribe from a channel (fire-and-forget)
2348
+ *
2349
+ * @param channel - Channel name to unsubscribe from
2350
+ */
2351
+ unsubscribe(channel) {
2352
+ this.subscribedChannels.delete(channel);
2353
+ if (this.socket?.connected) {
2354
+ this.socket.emit("realtime:unsubscribe", { channel });
2355
+ }
2356
+ }
2357
+ /**
2358
+ * Publish a message to a channel
2359
+ *
2360
+ * @param channel - Channel name
2361
+ * @param event - Event name
2362
+ * @param payload - Message payload
2363
+ */
2364
+ async publish(channel, event, payload) {
2365
+ if (!this.socket?.connected) {
2366
+ throw new Error("Not connected to realtime server. Call connect() first.");
2367
+ }
2368
+ this.socket.emit("realtime:publish", { channel, event, payload });
2369
+ }
2370
+ /**
2371
+ * Listen for events
2372
+ *
2373
+ * Reserved event names:
2374
+ * - 'connect' - Fired when connected to the server
2375
+ * - 'connect_error' - Fired when connection fails (payload: Error)
2376
+ * - 'disconnect' - Fired when disconnected (payload: reason string)
2377
+ * - 'error' - Fired when a realtime error occurs (payload: RealtimeErrorPayload)
2378
+ *
2379
+ * All other events receive a `SocketMessage` payload with metadata.
2380
+ *
2381
+ * @param event - Event name to listen for
2382
+ * @param callback - Callback function when event is received
2383
+ */
2384
+ on(event, callback) {
2385
+ if (!this.eventListeners.has(event)) {
2386
+ this.eventListeners.set(event, /* @__PURE__ */ new Set());
2387
+ }
2388
+ this.eventListeners.get(event).add(callback);
2389
+ }
2390
+ /**
2391
+ * Remove a listener for a specific event
2392
+ *
2393
+ * @param event - Event name
2394
+ * @param callback - The callback function to remove
2395
+ */
2396
+ off(event, callback) {
2397
+ const listeners = this.eventListeners.get(event);
2398
+ if (listeners) {
2399
+ listeners.delete(callback);
2400
+ if (listeners.size === 0) {
2401
+ this.eventListeners.delete(event);
2402
+ }
2403
+ }
2404
+ }
2405
+ /**
2406
+ * Listen for an event only once, then automatically remove the listener
2407
+ *
2408
+ * @param event - Event name to listen for
2409
+ * @param callback - Callback function when event is received
2410
+ */
2411
+ once(event, callback) {
2412
+ const wrapper = (payload) => {
2413
+ this.off(event, wrapper);
2414
+ callback(payload);
2415
+ };
2416
+ this.on(event, wrapper);
2417
+ }
2418
+ /**
2419
+ * Get all currently subscribed channels
2420
+ *
2421
+ * @returns Array of channel names
2422
+ */
2423
+ getSubscribedChannels() {
2424
+ return Array.from(this.subscribedChannels);
2425
+ }
2426
+ };
2427
+
2428
+ // src/modules/email.ts
2429
+ var Emails = class {
2430
+ constructor(http) {
2431
+ this.http = http;
2432
+ }
2433
+ /**
2434
+ * Send a custom HTML email
2435
+ * @param options Email options including recipients, subject, and HTML content
2436
+ */
2437
+ async send(options) {
2438
+ try {
2439
+ const data = await this.http.post(
2440
+ "/api/email/send-raw",
2441
+ options
2442
+ );
2443
+ return { data, error: null };
2444
+ } catch (error) {
2445
+ if (error instanceof Error && error.name === "AbortError") throw error;
2446
+ return {
2447
+ data: null,
2448
+ error: error instanceof InsForgeError ? error : new InsForgeError(
2449
+ error instanceof Error ? error.message : "Email send failed",
2450
+ 500,
2451
+ "EMAIL_ERROR"
2452
+ )
2453
+ };
2454
+ }
2455
+ }
2456
+ };
2457
+
2458
+ // src/modules/payments.ts
2459
+ var Payments = class {
2460
+ constructor(http) {
2461
+ this.http = http;
2462
+ }
2463
+ /**
2464
+ * Create a Stripe Checkout Session through the InsForge backend.
2465
+ *
2466
+ * @example
2467
+ * ```typescript
2468
+ * const { data, error } = await client.payments.createCheckoutSession('test', {
2469
+ * mode: 'payment',
2470
+ * lineItems: [{ stripePriceId: 'price_123', quantity: 1 }],
2471
+ * successUrl: `${window.location.origin}/success`,
2472
+ * cancelUrl: `${window.location.origin}/pricing`
2473
+ * });
2474
+ *
2475
+ * if (!error && data.checkoutSession.url) {
2476
+ * window.location.assign(data.checkoutSession.url);
2477
+ * }
2478
+ * ```
2479
+ */
2480
+ async createCheckoutSession(environment, request) {
2481
+ try {
2482
+ const data = await this.http.post(
2483
+ `/api/payments/${encodeURIComponent(environment)}/checkout-sessions`,
2484
+ request,
2485
+ { idempotent: !!request.idempotencyKey }
2486
+ );
2487
+ return { data, error: null };
2488
+ } catch (error) {
2489
+ return wrapError(
2490
+ error,
2491
+ "Checkout session creation failed"
2492
+ );
2493
+ }
2494
+ }
2495
+ /**
2496
+ * Create a Stripe Billing Portal Session for a mapped billing subject.
2497
+ */
2498
+ async createCustomerPortalSession(environment, request) {
2499
+ try {
2500
+ const data = await this.http.post(
2501
+ `/api/payments/${encodeURIComponent(environment)}/customer-portal-sessions`,
2502
+ request
2503
+ );
2504
+ return { data, error: null };
2505
+ } catch (error) {
2506
+ return wrapError(
2507
+ error,
2508
+ "Customer portal session creation failed"
2509
+ );
2510
+ }
2511
+ }
2512
+ };
2513
+
2514
+ // src/client.ts
2515
+ var InsForgeClient = class {
2516
+ constructor(config = {}) {
2517
+ const logger = new Logger(config.debug);
2518
+ this.tokenManager = new TokenManager();
2519
+ this.http = new HttpClient(config, this.tokenManager, logger);
2520
+ if (config.edgeFunctionToken) {
2521
+ this.http.setAuthToken(config.edgeFunctionToken);
2522
+ this.tokenManager.setAccessToken(config.edgeFunctionToken);
2523
+ }
2524
+ this.auth = new Auth(this.http, this.tokenManager, {
2525
+ isServerMode: config.isServerMode ?? !!config.edgeFunctionToken
2526
+ });
2527
+ this.database = new Database(this.http);
2528
+ this.storage = new Storage(this.http);
2529
+ this.ai = new AI(this.http);
2530
+ this.functions = new Functions(this.http, config.functionsUrl);
2531
+ this.realtime = new Realtime(
2532
+ this.http.baseUrl,
2533
+ this.tokenManager,
2534
+ config.anonKey
2535
+ );
2536
+ this.emails = new Emails(this.http);
2537
+ this.payments = new Payments(this.http);
2538
+ }
2539
+ /**
2540
+ * Get the underlying HTTP client for custom requests
2541
+ *
2542
+ * @example
2543
+ * ```typescript
2544
+ * const httpClient = client.getHttpClient();
2545
+ * const customData = await httpClient.get('/api/custom-endpoint');
2546
+ * ```
2547
+ */
2548
+ getHttpClient() {
2549
+ return this.http;
2550
+ }
2551
+ /**
2552
+ * Set the access token used by every SDK surface. Updates both the HTTP
2553
+ * client (database / storage / functions / AI / emails) and the realtime
2554
+ * token manager (which fires `onTokenChange` to reconnect the WebSocket
2555
+ * with the new bearer). Pass `null` to clear.
2556
+ *
2557
+ * Use this when an external auth provider (Better Auth, Clerk, Auth0,
2558
+ * WorkOS, Kinde, Stytch, …) issues the JWT and you need to keep the
2559
+ * long-lived InsForge client in sync. Without this, you'd have to call
2560
+ * `client.getHttpClient().setAuthToken(token)` AND reach into the private
2561
+ * `client.realtime.tokenManager.setAccessToken(token)` separately —
2562
+ * forgetting the second one silently breaks realtime auth.
2563
+ *
2564
+ * @example
2565
+ * ```typescript
2566
+ * // Refresh a third-party-issued JWT periodically
2567
+ * const { token } = await fetch('/api/insforge-token').then((r) => r.json());
2568
+ * client.setAccessToken(token);
2569
+ *
2570
+ * // Sign-out
2571
+ * client.setAccessToken(null);
2572
+ * ```
2573
+ */
2574
+ setAccessToken(token) {
2575
+ this.http.setAuthToken(token);
2576
+ if (token === null) {
2577
+ this.tokenManager.clearSession();
2578
+ } else {
2579
+ this.tokenManager.setAccessToken(token);
2580
+ }
2581
+ }
2582
+ /**
2583
+ * Future modules will be added here:
2584
+ * - database: Database operations
2585
+ * - storage: File storage operations
2586
+ * - functions: Serverless functions
2587
+ * - tables: Table management
2588
+ * - metadata: Backend metadata
2589
+ */
2590
+ };
2591
+
2592
+ // src/lib/jwt.ts
2593
+ function decodeBase64Url(input) {
2594
+ const normalized = input.replace(/-/g, "+").replace(/_/g, "/");
2595
+ const padded = normalized.padEnd(
2596
+ normalized.length + (4 - normalized.length % 4) % 4,
2597
+ "="
2598
+ );
2599
+ if (typeof atob === "function") {
2600
+ return atob(padded);
2601
+ }
2602
+ return Buffer.from(padded, "base64").toString("utf8");
2603
+ }
2604
+ function getJwtExpiration(token) {
2605
+ if (!token) return null;
2606
+ const [, payload] = token.split(".");
2607
+ if (!payload) return null;
2608
+ try {
2609
+ const parsed = JSON.parse(decodeBase64Url(payload));
2610
+ if (typeof parsed.exp !== "number" || !Number.isFinite(parsed.exp)) {
2611
+ return null;
2612
+ }
2613
+ return new Date(parsed.exp * 1e3);
2614
+ } catch {
2615
+ return null;
2616
+ }
2617
+ }
2618
+ function isJwtExpiredOrExpiring(token, leewaySeconds = 60) {
2619
+ const expires = getJwtExpiration(token);
2620
+ if (!expires) return false;
2621
+ return expires.getTime() <= Date.now() + leewaySeconds * 1e3;
2622
+ }
2623
+
2624
+ // src/ssr/config.ts
2625
+ function env(name) {
2626
+ if (typeof process === "undefined") return void 0;
2627
+ return process.env[name];
2628
+ }
2629
+ function resolveBrowserConfig(config = {}) {
2630
+ return {
2631
+ ...config,
2632
+ baseUrl: config.baseUrl ?? env("NEXT_PUBLIC_INSFORGE_URL"),
2633
+ anonKey: config.anonKey ?? env("NEXT_PUBLIC_INSFORGE_ANON_KEY")
2634
+ };
2635
+ }
2636
+ function resolveServerConfig(config = {}) {
2637
+ return {
2638
+ ...config,
2639
+ baseUrl: config.baseUrl ?? env("INSFORGE_URL") ?? env("NEXT_PUBLIC_INSFORGE_URL"),
2640
+ anonKey: config.anonKey ?? env("INSFORGE_ANON_KEY") ?? env("NEXT_PUBLIC_INSFORGE_ANON_KEY")
2641
+ };
2642
+ }
2643
+
2644
+ // src/ssr/browser-client.ts
2645
+ import { ERROR_CODES as ERROR_CODES2 } from "@insforge/shared-schemas";
2646
+
2647
+ // src/ssr/cookies.ts
2648
+ var DEFAULT_ACCESS_TOKEN_COOKIE = "insforge_access_token";
2649
+ var DEFAULT_REFRESH_TOKEN_COOKIE = "insforge_refresh_token";
2650
+ var EXPIRED_DATE = /* @__PURE__ */ new Date(0);
2651
+ function getAccessTokenCookieName(names) {
2652
+ return names?.accessToken ?? DEFAULT_ACCESS_TOKEN_COOKIE;
2653
+ }
2654
+ function getRefreshTokenCookieName(names) {
2655
+ return names?.refreshToken ?? DEFAULT_REFRESH_TOKEN_COOKIE;
2656
+ }
2657
+ function getCookieValue(cookies, name) {
2658
+ if (!cookies) return null;
2659
+ const value = cookies.get(name);
2660
+ if (typeof value === "string") return value || null;
2661
+ if (value && typeof value.value === "string") return value.value || null;
2662
+ return null;
2663
+ }
2664
+ function getCookieValueFromHeader(cookieHeader, name) {
2665
+ if (!cookieHeader) return null;
2666
+ const parts = cookieHeader.split(";");
2667
+ for (const part of parts) {
2668
+ const [rawName, ...rawValue] = part.trim().split("=");
2669
+ if (rawName !== name) continue;
2670
+ try {
2671
+ return decodeURIComponent(rawValue.join("="));
2672
+ } catch {
2673
+ return rawValue.join("=");
2674
+ }
2675
+ }
2676
+ return null;
2677
+ }
2678
+ function getBrowserCookie(name) {
2679
+ if (typeof document === "undefined") return null;
2680
+ return getCookieValueFromHeader(document.cookie, name);
2681
+ }
2682
+ function defaultCookieOptions() {
2683
+ const secure = typeof process !== "undefined" ? process.env.NODE_ENV === "production" : typeof location !== "undefined" && location.protocol === "https:";
2684
+ return {
2685
+ path: "/",
2686
+ sameSite: "lax",
2687
+ secure
2688
+ };
2689
+ }
2690
+ function accessTokenCookieOptions(token, overrides) {
2691
+ return {
2692
+ ...defaultCookieOptions(),
2693
+ httpOnly: false,
2694
+ expires: getJwtExpiration(token) ?? void 0,
2695
+ ...overrides
2696
+ };
2697
+ }
2698
+ function refreshTokenCookieOptions(token, overrides) {
2699
+ return {
2700
+ ...defaultCookieOptions(),
2701
+ httpOnly: true,
2702
+ expires: getJwtExpiration(token) ?? void 0,
2703
+ ...overrides
2704
+ };
2705
+ }
2706
+ function expiredCookieOptions(overrides) {
2707
+ return {
2708
+ ...defaultCookieOptions(),
2709
+ expires: EXPIRED_DATE,
2710
+ maxAge: 0,
2711
+ ...overrides
2712
+ };
2713
+ }
2714
+ function setCookie(cookies, name, value, options) {
2715
+ if (!cookies?.set) return;
2716
+ cookies.set(name, value, options);
2717
+ }
2718
+ function deleteCookie(cookies, name, options) {
2719
+ if (!cookies) return;
2720
+ if (cookies.delete) {
2721
+ cookies.delete(name, options);
2722
+ return;
2723
+ }
2724
+ cookies.set?.(name, "", expiredCookieOptions(options));
2725
+ }
2726
+ function serializeCookie(name, value, options = {}) {
2727
+ const parts = [`${encodeURIComponent(name)}=${encodeURIComponent(value)}`];
2728
+ if (options.maxAge !== void 0) parts.push(`Max-Age=${options.maxAge}`);
2729
+ if (options.domain) parts.push(`Domain=${options.domain}`);
2730
+ if (options.path) parts.push(`Path=${options.path}`);
2731
+ if (options.expires) parts.push(`Expires=${options.expires.toUTCString()}`);
2732
+ if (options.httpOnly) parts.push("HttpOnly");
2733
+ if (options.secure) parts.push("Secure");
2734
+ if (options.sameSite) {
2735
+ const sameSite = options.sameSite.charAt(0).toUpperCase() + options.sameSite.slice(1);
2736
+ parts.push(`SameSite=${sameSite}`);
2737
+ }
2738
+ return parts.join("; ");
2739
+ }
2740
+ function appendSetCookie(headers, name, value, options) {
2741
+ headers.append("Set-Cookie", serializeCookie(name, value, options));
2742
+ }
2743
+ function setAuthCookies(target, tokens, settings = {}) {
2744
+ const accessName = getAccessTokenCookieName(settings.names);
2745
+ const refreshName = getRefreshTokenCookieName(settings.names);
2746
+ const accessOptions = accessTokenCookieOptions(
2747
+ tokens.accessToken,
2748
+ settings.options?.accessToken
2749
+ );
2750
+ if (target instanceof Headers) {
2751
+ appendSetCookie(target, accessName, tokens.accessToken, accessOptions);
2752
+ if (tokens.refreshToken) {
2753
+ appendSetCookie(
2754
+ target,
2755
+ refreshName,
2756
+ tokens.refreshToken,
2757
+ refreshTokenCookieOptions(
2758
+ tokens.refreshToken,
2759
+ settings.options?.refreshToken
2760
+ )
2761
+ );
2762
+ }
2763
+ return;
2764
+ }
2765
+ setCookie(target, accessName, tokens.accessToken, accessOptions);
2766
+ if (tokens.refreshToken) {
2767
+ setCookie(
2768
+ target,
2769
+ refreshName,
2770
+ tokens.refreshToken,
2771
+ refreshTokenCookieOptions(
2772
+ tokens.refreshToken,
2773
+ settings.options?.refreshToken
2774
+ )
2775
+ );
2776
+ }
2777
+ }
2778
+ function clearAuthCookies(target, settings = {}) {
2779
+ const accessName = getAccessTokenCookieName(settings.names);
2780
+ const refreshName = getRefreshTokenCookieName(settings.names);
2781
+ const accessOptions = expiredCookieOptions(settings.options?.accessToken);
2782
+ const refreshOptions = expiredCookieOptions(settings.options?.refreshToken);
2783
+ if (target instanceof Headers) {
2784
+ appendSetCookie(target, accessName, "", accessOptions);
2785
+ appendSetCookie(target, refreshName, "", refreshOptions);
2786
+ return;
2787
+ }
2788
+ deleteCookie(target, accessName, accessOptions);
2789
+ deleteCookie(target, refreshName, refreshOptions);
2790
+ }
2791
+
2792
+ // src/ssr/browser-client.ts
2793
+ async function parseRefreshResponse(response) {
2794
+ const contentType = response.headers.get("content-type");
2795
+ if (contentType?.includes("json")) {
2796
+ return await response.json();
2797
+ }
2798
+ return await response.text();
2799
+ }
2800
+ function toRefreshError(response, body) {
2801
+ if (body && typeof body === "object") {
2802
+ const errorBody = body;
2803
+ return new InsForgeError(
2804
+ typeof errorBody.message === "string" ? errorBody.message : "Failed to refresh auth session",
2805
+ typeof errorBody.statusCode === "number" ? errorBody.statusCode : response.status,
2806
+ typeof errorBody.error === "string" ? errorBody.error : ERROR_CODES2.UNKNOWN_ERROR
2807
+ );
2808
+ }
2809
+ return new InsForgeError(
2810
+ typeof body === "string" && body ? body : "Failed to refresh auth session",
2811
+ response.status,
2812
+ ERROR_CODES2.UNKNOWN_ERROR
2813
+ );
2814
+ }
2815
+ async function readErrorCode(response) {
2816
+ if (response.status !== 401) return null;
2817
+ try {
2818
+ const body = await response.clone().json();
2819
+ if (!body || typeof body !== "object") return null;
2820
+ const candidate = body.error ?? body.code;
2821
+ return typeof candidate === "string" ? candidate : null;
2822
+ } catch {
2823
+ return null;
2824
+ }
2825
+ }
2826
+ function isRefreshableErrorCode(code) {
2827
+ return code === ERROR_CODES2.AUTH_UNAUTHORIZED || code === ERROR_CODES2.AUTH_TOKEN_EXPIRED || code === "PGRST301";
2828
+ }
2829
+ function withAuthHeader(init, token) {
2830
+ const headers = new Headers(init?.headers);
2831
+ headers.set("Authorization", `Bearer ${token}`);
2832
+ return {
2833
+ ...init,
2834
+ headers
2835
+ };
2836
+ }
2837
+ function createBrowserClient(options = {}) {
2838
+ let accessToken = getBrowserCookie(
2839
+ getAccessTokenCookieName(options.names)
2840
+ );
2841
+ const refreshUrl = options.refreshUrl ?? "/api/auth/refresh";
2842
+ const fetchImpl = options.fetch ?? (globalThis.fetch ? globalThis.fetch.bind(globalThis) : void 0);
2843
+ let client;
2844
+ let sessionChecked = false;
2845
+ let refreshPromise = null;
2846
+ const refreshFromRoute = () => {
2847
+ if (refreshPromise) return refreshPromise;
2848
+ refreshPromise = (async () => {
2849
+ if (!fetchImpl) {
2850
+ throw new Error(
2851
+ "Fetch is not available. Please provide a fetch implementation."
2852
+ );
2853
+ }
2854
+ const response = await fetchImpl(refreshUrl, {
2855
+ method: "POST",
2856
+ credentials: "include",
2857
+ headers: { Accept: "application/json" }
2858
+ });
2859
+ const body = await parseRefreshResponse(response);
2860
+ if (!response.ok) {
2861
+ const error = toRefreshError(response, body);
2862
+ if (response.status === 401 && (error.error === ERROR_CODES2.AUTH_UNAUTHORIZED || error.error === ERROR_CODES2.AUTH_TOKEN_EXPIRED)) {
2863
+ accessToken = null;
2864
+ client?.setAccessToken(null);
2865
+ return null;
2866
+ }
2867
+ throw error;
2868
+ }
2869
+ if (!body || typeof body !== "object") return null;
2870
+ const refreshBody = body;
2871
+ if (!refreshBody.accessToken || !refreshBody.user) return null;
2872
+ accessToken = refreshBody.accessToken;
2873
+ client?.setAccessToken(refreshBody.accessToken);
2874
+ return refreshBody;
2875
+ })().finally(() => {
2876
+ sessionChecked = true;
2877
+ refreshPromise = null;
2878
+ });
2879
+ return refreshPromise;
2880
+ };
2881
+ const shouldSkipRefresh = (input) => {
2882
+ const url = typeof input === "string" ? input : input.toString();
2883
+ return url === refreshUrl || url.endsWith(refreshUrl);
2884
+ };
2885
+ const ssrFetch = async (input, init) => {
2886
+ if (!fetchImpl) {
2887
+ throw new Error(
2888
+ "Fetch is not available. Please provide a fetch implementation."
2889
+ );
2890
+ }
2891
+ if (shouldSkipRefresh(input)) {
2892
+ return fetchImpl(input, init);
2893
+ }
2894
+ let requestInit = init;
2895
+ if (!accessToken && !sessionChecked || isJwtExpiredOrExpiring(accessToken, options.refreshLeewaySeconds)) {
2896
+ const refreshed2 = await refreshFromRoute().catch(() => null);
2897
+ if (refreshed2?.accessToken) {
2898
+ requestInit = withAuthHeader(init, refreshed2.accessToken);
2899
+ }
2900
+ }
2901
+ const response = await fetchImpl(input, requestInit);
2902
+ const errorCode = await readErrorCode(response);
2903
+ if (!isRefreshableErrorCode(errorCode)) {
2904
+ return response;
2905
+ }
2906
+ const refreshed = await refreshFromRoute();
2907
+ if (!refreshed?.accessToken) {
2908
+ client.setAccessToken(null);
2909
+ return response;
2910
+ }
2911
+ return fetchImpl(input, withAuthHeader(init, refreshed.accessToken));
2912
+ };
2913
+ client = new InsForgeClient({
2914
+ ...resolveBrowserConfig(options),
2915
+ fetch: ssrFetch
2916
+ });
2917
+ const setAccessToken = client.setAccessToken.bind(client);
2918
+ client.setAccessToken = (token) => {
2919
+ accessToken = token;
2920
+ setAccessToken(token);
2921
+ };
2922
+ if (accessToken) {
2923
+ client.setAccessToken(accessToken);
2924
+ }
2925
+ if (!accessToken || isJwtExpiredOrExpiring(accessToken, options.refreshLeewaySeconds)) {
2926
+ void refreshFromRoute().catch(() => void 0);
2927
+ }
2928
+ return client;
2929
+ }
2930
+
2931
+ // src/ssr/server-client.ts
2932
+ function createServerClient(options = {}) {
2933
+ const accessToken = options.accessToken ?? getCookieValue(
2934
+ options.cookies,
2935
+ getAccessTokenCookieName(options.names)
2936
+ );
2937
+ return new InsForgeClient({
2938
+ ...resolveServerConfig(options),
2939
+ isServerMode: true,
2940
+ edgeFunctionToken: accessToken ?? void 0
2941
+ });
2942
+ }
2943
+
2944
+ // src/ssr/refresh.ts
2945
+ import { ERROR_CODES as ERROR_CODES3 } from "@insforge/shared-schemas";
2946
+ function jsonResponse(body, init = {}, headers = new Headers(init.headers)) {
2947
+ headers.set("Content-Type", "application/json");
2948
+ return new Response(JSON.stringify(body), {
2949
+ ...init,
2950
+ headers
2951
+ });
2952
+ }
2953
+ function normalizeError(error) {
2954
+ if (error instanceof InsForgeError) return error;
2955
+ return new InsForgeError(
2956
+ error instanceof Error ? error.message : "Failed to refresh auth session",
2957
+ 500,
2958
+ ERROR_CODES3.UNKNOWN_ERROR
2959
+ );
2960
+ }
2961
+ function readRefreshToken(options) {
2962
+ if (options.refreshToken) return options.refreshToken;
2963
+ const refreshCookieName = getRefreshTokenCookieName(options.names);
2964
+ const cookieValue = getCookieValue(options.cookies, refreshCookieName);
2965
+ if (cookieValue) return cookieValue;
2966
+ return getCookieValueFromHeader(
2967
+ options.request?.headers.get("cookie"),
2968
+ refreshCookieName
2969
+ );
2970
+ }
2971
+ async function refreshAuth(options = {}) {
2972
+ const headers = new Headers();
2973
+ const refreshToken = readRefreshToken(options);
2974
+ if (!refreshToken) {
2975
+ clearAuthCookies(headers, options);
2976
+ const error2 = new InsForgeError(
2977
+ "Refresh token cookie is missing",
2978
+ 401,
2979
+ ERROR_CODES3.AUTH_UNAUTHORIZED
2980
+ );
2981
+ return {
2982
+ response: jsonResponse(
2983
+ {
2984
+ error: error2.error,
2985
+ message: error2.message,
2986
+ statusCode: error2.statusCode
2987
+ },
2988
+ { status: error2.statusCode },
2989
+ headers
2990
+ ),
2991
+ data: null,
2992
+ accessToken: null,
2993
+ refreshToken: null,
2994
+ error: error2
2995
+ };
2996
+ }
2997
+ const client = new InsForgeClient({
2998
+ ...resolveServerConfig(options),
2999
+ isServerMode: true
3000
+ });
3001
+ const { data, error } = await client.auth.refreshSession({ refreshToken });
3002
+ if (error || !data?.accessToken) {
3003
+ clearAuthCookies(headers, options);
3004
+ const normalized = normalizeError(error);
3005
+ return {
3006
+ response: jsonResponse(
3007
+ {
3008
+ error: normalized.error,
3009
+ message: normalized.message,
3010
+ statusCode: normalized.statusCode
3011
+ },
3012
+ { status: normalized.statusCode || 500 },
3013
+ headers
3014
+ ),
3015
+ data: null,
3016
+ accessToken: null,
3017
+ refreshToken: null,
3018
+ error: normalized
3019
+ };
3020
+ }
3021
+ const nextRefreshToken = data.refreshToken ?? refreshToken;
3022
+ setAuthCookies(
3023
+ headers,
3024
+ {
3025
+ accessToken: data.accessToken,
3026
+ refreshToken: nextRefreshToken
3027
+ },
3028
+ options
3029
+ );
3030
+ const responseBody = {
3031
+ accessToken: data.accessToken,
3032
+ user: data.user,
3033
+ csrfToken: data.csrfToken
3034
+ };
3035
+ return {
3036
+ response: jsonResponse(responseBody, { status: 200 }, headers),
3037
+ data: responseBody,
3038
+ accessToken: data.accessToken,
3039
+ refreshToken: nextRefreshToken,
3040
+ error: null
3041
+ };
3042
+ }
3043
+ function createRefreshAuthRouter(options = {}) {
3044
+ return {
3045
+ POST: async (request) => (await refreshAuth({ ...options, request })).response
3046
+ };
3047
+ }
3048
+
3049
+ // src/ssr/update-session.ts
3050
+ async function updateSession(options) {
3051
+ const accessCookieName = getAccessTokenCookieName(options.names);
3052
+ const refreshCookieName = getRefreshTokenCookieName(options.names);
3053
+ const accessToken = getCookieValue(
3054
+ options.requestCookies,
3055
+ accessCookieName
3056
+ );
3057
+ if (accessToken && !isJwtExpiredOrExpiring(accessToken, options.refreshLeewaySeconds)) {
3058
+ return {
3059
+ refreshed: false,
3060
+ accessToken,
3061
+ error: null
3062
+ };
3063
+ }
3064
+ const refreshToken = getCookieValue(
3065
+ options.requestCookies,
3066
+ refreshCookieName
3067
+ );
3068
+ if (!refreshToken) {
3069
+ if (accessToken) {
3070
+ clearAuthCookies(options.requestCookies, options);
3071
+ clearAuthCookies(options.responseCookies, options);
3072
+ }
3073
+ return {
3074
+ refreshed: false,
3075
+ accessToken: null,
3076
+ error: null
3077
+ };
3078
+ }
3079
+ const result = await refreshAuth({
3080
+ ...options,
3081
+ refreshToken
3082
+ });
3083
+ if (result.error || !result.accessToken) {
3084
+ clearAuthCookies(options.requestCookies, options);
3085
+ clearAuthCookies(options.responseCookies, options);
3086
+ return {
3087
+ refreshed: false,
3088
+ accessToken: null,
3089
+ error: result.error
3090
+ };
3091
+ }
3092
+ const tokens = {
3093
+ accessToken: result.accessToken,
3094
+ refreshToken: result.refreshToken ?? refreshToken
3095
+ };
3096
+ setAuthCookies(options.requestCookies, tokens, options);
3097
+ setAuthCookies(options.responseCookies, tokens, options);
3098
+ return {
3099
+ refreshed: true,
3100
+ accessToken: result.accessToken,
3101
+ error: null
3102
+ };
3103
+ }
3104
+ export {
3105
+ DEFAULT_ACCESS_TOKEN_COOKIE,
3106
+ DEFAULT_REFRESH_TOKEN_COOKIE,
3107
+ accessTokenCookieOptions,
3108
+ clearAuthCookies,
3109
+ createBrowserClient,
3110
+ createRefreshAuthRouter,
3111
+ createServerClient,
3112
+ getAccessTokenCookieName,
3113
+ getRefreshTokenCookieName,
3114
+ refreshAuth,
3115
+ refreshTokenCookieOptions,
3116
+ setAuthCookies,
3117
+ updateSession
3118
+ };
3119
+ //# sourceMappingURL=ssr.mjs.map