@insforge/sdk 1.2.8-dev.0 → 1.2.8-dev.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +93 -21
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +93 -21
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -284,7 +284,10 @@ var TokenManager = class {
|
|
|
284
284
|
// src/lib/http-client.ts
|
|
285
285
|
var RETRYABLE_STATUS_CODES = /* @__PURE__ */ new Set([500, 502, 503, 504]);
|
|
286
286
|
var IDEMPOTENT_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "PUT", "DELETE", "OPTIONS"]);
|
|
287
|
-
var
|
|
287
|
+
var REFRESHABLE_AUTH_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
288
|
+
"AUTH_UNAUTHORIZED",
|
|
289
|
+
"PGRST301"
|
|
290
|
+
]);
|
|
288
291
|
function serializeBody(method, body, headers) {
|
|
289
292
|
if (body === void 0) return void 0;
|
|
290
293
|
if (method === "GET" || method === "HEAD") return void 0;
|
|
@@ -393,8 +396,8 @@ var HttpClient = class {
|
|
|
393
396
|
const jitter = base * (0.85 + Math.random() * 0.3);
|
|
394
397
|
return Math.round(jitter);
|
|
395
398
|
}
|
|
396
|
-
shouldRefreshAccessToken(statusCode, errorCode, options = {}) {
|
|
397
|
-
return statusCode === 401 && errorCode
|
|
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;
|
|
398
401
|
}
|
|
399
402
|
async fetchWithRetry(args) {
|
|
400
403
|
const {
|
|
@@ -513,7 +516,7 @@ var HttpClient = class {
|
|
|
513
516
|
* @returns Parsed response data.
|
|
514
517
|
* @throws {InsForgeError} On timeout, network failure, or HTTP error responses.
|
|
515
518
|
*/
|
|
516
|
-
async handleRequest(method, path, options = {}) {
|
|
519
|
+
async handleRequest(method, path, options = {}, tokenOverride) {
|
|
517
520
|
const {
|
|
518
521
|
params,
|
|
519
522
|
headers = {},
|
|
@@ -529,21 +532,32 @@ var HttpClient = class {
|
|
|
529
532
|
const requestHeaders = {
|
|
530
533
|
...this.defaultHeaders
|
|
531
534
|
};
|
|
532
|
-
const authToken = this.userToken
|
|
535
|
+
const authToken = tokenOverride ?? this.userToken ?? this.anonKey;
|
|
533
536
|
if (authToken) {
|
|
534
537
|
requestHeaders["Authorization"] = `Bearer ${authToken}`;
|
|
535
538
|
}
|
|
536
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
|
+
};
|
|
537
549
|
if (headers instanceof Headers) {
|
|
538
550
|
headers.forEach((value, key) => {
|
|
539
|
-
|
|
551
|
+
setRequestHeader(key, value);
|
|
540
552
|
});
|
|
541
553
|
} else if (Array.isArray(headers)) {
|
|
542
554
|
headers.forEach(([key, value]) => {
|
|
543
|
-
|
|
555
|
+
setRequestHeader(key, value);
|
|
544
556
|
});
|
|
545
557
|
} else {
|
|
546
|
-
Object.
|
|
558
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
559
|
+
setRequestHeader(key, value);
|
|
560
|
+
});
|
|
547
561
|
}
|
|
548
562
|
this.logger.logRequest(method, url, requestHeaders, processedBody);
|
|
549
563
|
const response = await this.fetchWithRetry({
|
|
@@ -580,19 +594,47 @@ var HttpClient = class {
|
|
|
580
594
|
return data;
|
|
581
595
|
}
|
|
582
596
|
async request(method, path, options = {}) {
|
|
597
|
+
const tokenUsed = this.userToken;
|
|
583
598
|
try {
|
|
584
|
-
return await this.handleRequest(
|
|
599
|
+
return await this.handleRequest(
|
|
600
|
+
method,
|
|
601
|
+
path,
|
|
602
|
+
{ ...options },
|
|
603
|
+
tokenUsed
|
|
604
|
+
);
|
|
585
605
|
} catch (error) {
|
|
586
|
-
if (error instanceof InsForgeError
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
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;
|
|
592
617
|
}
|
|
593
|
-
return await this.handleRequest(
|
|
618
|
+
return await this.handleRequest(
|
|
619
|
+
method,
|
|
620
|
+
path,
|
|
621
|
+
{
|
|
622
|
+
...options,
|
|
623
|
+
skipAuthRefresh: true
|
|
624
|
+
},
|
|
625
|
+
this.userToken
|
|
626
|
+
);
|
|
594
627
|
}
|
|
595
|
-
|
|
628
|
+
try {
|
|
629
|
+
await this.refreshAndSaveSession();
|
|
630
|
+
} catch (error2) {
|
|
631
|
+
this.clearAuthSession();
|
|
632
|
+
throw error2;
|
|
633
|
+
}
|
|
634
|
+
return await this.handleRequest(method, path, {
|
|
635
|
+
...options,
|
|
636
|
+
skipAuthRefresh: true
|
|
637
|
+
});
|
|
596
638
|
}
|
|
597
639
|
}
|
|
598
640
|
/**
|
|
@@ -612,7 +654,14 @@ var HttpClient = class {
|
|
|
612
654
|
const method = initMethod ?? request?.method ?? "GET";
|
|
613
655
|
const url = request?.url ?? input.toString();
|
|
614
656
|
const startTime = Date.now();
|
|
615
|
-
const
|
|
657
|
+
const tokenUsed = this.userToken;
|
|
658
|
+
const headers = new Headers({
|
|
659
|
+
...this.defaultHeaders
|
|
660
|
+
});
|
|
661
|
+
const authToken = tokenUsed ?? this.anonKey;
|
|
662
|
+
if (authToken) {
|
|
663
|
+
headers.set("Authorization", `Bearer ${authToken}`);
|
|
664
|
+
}
|
|
616
665
|
request?.headers.forEach((value, key) => {
|
|
617
666
|
headers.set(key, value);
|
|
618
667
|
});
|
|
@@ -623,7 +672,13 @@ var HttpClient = class {
|
|
|
623
672
|
headers.forEach((value, key) => {
|
|
624
673
|
requestHeaders[key] = value;
|
|
625
674
|
});
|
|
626
|
-
const
|
|
675
|
+
const sourceBody = initBody ?? request?.body ?? void 0;
|
|
676
|
+
let body = sourceBody;
|
|
677
|
+
let retryInit = init;
|
|
678
|
+
if (typeof ReadableStream !== "undefined" && sourceBody instanceof ReadableStream) {
|
|
679
|
+
body = await new Response(sourceBody).arrayBuffer();
|
|
680
|
+
retryInit = { ...init ?? {}, body };
|
|
681
|
+
}
|
|
627
682
|
const callerSignal = initSignal ?? request?.signal;
|
|
628
683
|
const maxAttempts = IDEMPOTENT_METHODS.has(method.toUpperCase()) ? this.retryCount : 0;
|
|
629
684
|
this.logger.logRequest(method, url, requestHeaders, body);
|
|
@@ -655,9 +710,26 @@ var HttpClient = class {
|
|
|
655
710
|
} catch {
|
|
656
711
|
}
|
|
657
712
|
}
|
|
658
|
-
if (!this.shouldRefreshAccessToken(
|
|
713
|
+
if (!this.shouldRefreshAccessToken(
|
|
714
|
+
response.status,
|
|
715
|
+
errorCode,
|
|
716
|
+
tokenUsed,
|
|
717
|
+
options
|
|
718
|
+
)) {
|
|
659
719
|
return response;
|
|
660
720
|
}
|
|
721
|
+
if (tokenUsed !== this.userToken) {
|
|
722
|
+
if (this.userToken === null) {
|
|
723
|
+
return response;
|
|
724
|
+
}
|
|
725
|
+
const retryHeaders2 = new Headers(initHeaders);
|
|
726
|
+
retryHeaders2.set("Authorization", `Bearer ${this.userToken}`);
|
|
727
|
+
return await this.rawFetch(
|
|
728
|
+
input,
|
|
729
|
+
{ ...retryInit, headers: retryHeaders2 },
|
|
730
|
+
{ skipAuthRefresh: true }
|
|
731
|
+
);
|
|
732
|
+
}
|
|
661
733
|
let newTokenData;
|
|
662
734
|
try {
|
|
663
735
|
newTokenData = await this.refreshAndSaveSession();
|
|
@@ -669,7 +741,7 @@ var HttpClient = class {
|
|
|
669
741
|
retryHeaders.set("Authorization", `Bearer ${newTokenData.accessToken}`);
|
|
670
742
|
return await this.rawFetch(
|
|
671
743
|
input,
|
|
672
|
-
{ ...
|
|
744
|
+
{ ...retryInit, headers: retryHeaders },
|
|
673
745
|
{ skipAuthRefresh: true }
|
|
674
746
|
);
|
|
675
747
|
}
|