@insforge/sdk 1.2.8-dev.0 → 1.2.8-dev.1
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 +89 -20
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +89 -20
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -434,8 +434,8 @@ var HttpClient = class {
|
|
|
434
434
|
const jitter = base * (0.85 + Math.random() * 0.3);
|
|
435
435
|
return Math.round(jitter);
|
|
436
436
|
}
|
|
437
|
-
shouldRefreshAccessToken(statusCode, errorCode, options = {}) {
|
|
438
|
-
return statusCode === 401 && errorCode === REFRESHABLE_AUTH_ERROR_CODE && !
|
|
437
|
+
shouldRefreshAccessToken(statusCode, errorCode, authToken, options = {}) {
|
|
438
|
+
return statusCode === 401 && errorCode === REFRESHABLE_AUTH_ERROR_CODE && !this.config.isServerMode && !this.config.edgeFunctionToken && !options.skipAuthRefresh && authToken !== null;
|
|
439
439
|
}
|
|
440
440
|
async fetchWithRetry(args) {
|
|
441
441
|
const {
|
|
@@ -554,7 +554,7 @@ var HttpClient = class {
|
|
|
554
554
|
* @returns Parsed response data.
|
|
555
555
|
* @throws {InsForgeError} On timeout, network failure, or HTTP error responses.
|
|
556
556
|
*/
|
|
557
|
-
async handleRequest(method, path, options = {}) {
|
|
557
|
+
async handleRequest(method, path, options = {}, tokenOverride) {
|
|
558
558
|
const {
|
|
559
559
|
params,
|
|
560
560
|
headers = {},
|
|
@@ -570,21 +570,32 @@ var HttpClient = class {
|
|
|
570
570
|
const requestHeaders = {
|
|
571
571
|
...this.defaultHeaders
|
|
572
572
|
};
|
|
573
|
-
const authToken = this.userToken
|
|
573
|
+
const authToken = tokenOverride ?? this.userToken ?? this.anonKey;
|
|
574
574
|
if (authToken) {
|
|
575
575
|
requestHeaders["Authorization"] = `Bearer ${authToken}`;
|
|
576
576
|
}
|
|
577
577
|
const processedBody = serializeBody(method, body, requestHeaders);
|
|
578
|
+
const setRequestHeader = (key, value) => {
|
|
579
|
+
if (key.toLowerCase() === "authorization") {
|
|
580
|
+
delete requestHeaders["Authorization"];
|
|
581
|
+
delete requestHeaders["authorization"];
|
|
582
|
+
requestHeaders["Authorization"] = value;
|
|
583
|
+
return;
|
|
584
|
+
}
|
|
585
|
+
requestHeaders[key] = value;
|
|
586
|
+
};
|
|
578
587
|
if (headers instanceof Headers) {
|
|
579
588
|
headers.forEach((value, key) => {
|
|
580
|
-
|
|
589
|
+
setRequestHeader(key, value);
|
|
581
590
|
});
|
|
582
591
|
} else if (Array.isArray(headers)) {
|
|
583
592
|
headers.forEach(([key, value]) => {
|
|
584
|
-
|
|
593
|
+
setRequestHeader(key, value);
|
|
585
594
|
});
|
|
586
595
|
} else {
|
|
587
|
-
Object.
|
|
596
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
597
|
+
setRequestHeader(key, value);
|
|
598
|
+
});
|
|
588
599
|
}
|
|
589
600
|
this.logger.logRequest(method, url, requestHeaders, processedBody);
|
|
590
601
|
const response = await this.fetchWithRetry({
|
|
@@ -621,19 +632,47 @@ var HttpClient = class {
|
|
|
621
632
|
return data;
|
|
622
633
|
}
|
|
623
634
|
async request(method, path, options = {}) {
|
|
635
|
+
const tokenUsed = this.userToken;
|
|
624
636
|
try {
|
|
625
|
-
return await this.handleRequest(
|
|
637
|
+
return await this.handleRequest(
|
|
638
|
+
method,
|
|
639
|
+
path,
|
|
640
|
+
{ ...options },
|
|
641
|
+
tokenUsed
|
|
642
|
+
);
|
|
626
643
|
} catch (error) {
|
|
627
|
-
if (error instanceof InsForgeError
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
644
|
+
if (!(error instanceof InsForgeError) || !this.shouldRefreshAccessToken(
|
|
645
|
+
error.statusCode,
|
|
646
|
+
error.error,
|
|
647
|
+
tokenUsed,
|
|
648
|
+
options
|
|
649
|
+
)) {
|
|
650
|
+
throw error;
|
|
651
|
+
}
|
|
652
|
+
if (tokenUsed !== this.userToken) {
|
|
653
|
+
if (this.userToken === null) {
|
|
654
|
+
throw error;
|
|
633
655
|
}
|
|
634
|
-
return await this.handleRequest(
|
|
656
|
+
return await this.handleRequest(
|
|
657
|
+
method,
|
|
658
|
+
path,
|
|
659
|
+
{
|
|
660
|
+
...options,
|
|
661
|
+
skipAuthRefresh: true
|
|
662
|
+
},
|
|
663
|
+
this.userToken
|
|
664
|
+
);
|
|
635
665
|
}
|
|
636
|
-
|
|
666
|
+
try {
|
|
667
|
+
await this.refreshAndSaveSession();
|
|
668
|
+
} catch (error2) {
|
|
669
|
+
this.clearAuthSession();
|
|
670
|
+
throw error2;
|
|
671
|
+
}
|
|
672
|
+
return await this.handleRequest(method, path, {
|
|
673
|
+
...options,
|
|
674
|
+
skipAuthRefresh: true
|
|
675
|
+
});
|
|
637
676
|
}
|
|
638
677
|
}
|
|
639
678
|
/**
|
|
@@ -653,7 +692,14 @@ var HttpClient = class {
|
|
|
653
692
|
const method = initMethod ?? request?.method ?? "GET";
|
|
654
693
|
const url = request?.url ?? input.toString();
|
|
655
694
|
const startTime = Date.now();
|
|
656
|
-
const
|
|
695
|
+
const tokenUsed = this.userToken;
|
|
696
|
+
const headers = new Headers({
|
|
697
|
+
...this.defaultHeaders
|
|
698
|
+
});
|
|
699
|
+
const authToken = tokenUsed ?? this.anonKey;
|
|
700
|
+
if (authToken) {
|
|
701
|
+
headers.set("Authorization", `Bearer ${authToken}`);
|
|
702
|
+
}
|
|
657
703
|
request?.headers.forEach((value, key) => {
|
|
658
704
|
headers.set(key, value);
|
|
659
705
|
});
|
|
@@ -664,7 +710,13 @@ var HttpClient = class {
|
|
|
664
710
|
headers.forEach((value, key) => {
|
|
665
711
|
requestHeaders[key] = value;
|
|
666
712
|
});
|
|
667
|
-
const
|
|
713
|
+
const sourceBody = initBody ?? request?.body ?? void 0;
|
|
714
|
+
let body = sourceBody;
|
|
715
|
+
let retryInit = init;
|
|
716
|
+
if (typeof ReadableStream !== "undefined" && sourceBody instanceof ReadableStream) {
|
|
717
|
+
body = await new Response(sourceBody).arrayBuffer();
|
|
718
|
+
retryInit = { ...init ?? {}, body };
|
|
719
|
+
}
|
|
668
720
|
const callerSignal = initSignal ?? request?.signal;
|
|
669
721
|
const maxAttempts = IDEMPOTENT_METHODS.has(method.toUpperCase()) ? this.retryCount : 0;
|
|
670
722
|
this.logger.logRequest(method, url, requestHeaders, body);
|
|
@@ -696,9 +748,26 @@ var HttpClient = class {
|
|
|
696
748
|
} catch {
|
|
697
749
|
}
|
|
698
750
|
}
|
|
699
|
-
if (!this.shouldRefreshAccessToken(
|
|
751
|
+
if (!this.shouldRefreshAccessToken(
|
|
752
|
+
response.status,
|
|
753
|
+
errorCode,
|
|
754
|
+
tokenUsed,
|
|
755
|
+
options
|
|
756
|
+
)) {
|
|
700
757
|
return response;
|
|
701
758
|
}
|
|
759
|
+
if (tokenUsed !== this.userToken) {
|
|
760
|
+
if (this.userToken === null) {
|
|
761
|
+
return response;
|
|
762
|
+
}
|
|
763
|
+
const retryHeaders2 = new Headers(initHeaders);
|
|
764
|
+
retryHeaders2.set("Authorization", `Bearer ${this.userToken}`);
|
|
765
|
+
return await this.rawFetch(
|
|
766
|
+
input,
|
|
767
|
+
{ ...retryInit, headers: retryHeaders2 },
|
|
768
|
+
{ skipAuthRefresh: true }
|
|
769
|
+
);
|
|
770
|
+
}
|
|
702
771
|
let newTokenData;
|
|
703
772
|
try {
|
|
704
773
|
newTokenData = await this.refreshAndSaveSession();
|
|
@@ -710,7 +779,7 @@ var HttpClient = class {
|
|
|
710
779
|
retryHeaders.set("Authorization", `Bearer ${newTokenData.accessToken}`);
|
|
711
780
|
return await this.rawFetch(
|
|
712
781
|
input,
|
|
713
|
-
{ ...
|
|
782
|
+
{ ...retryInit, headers: retryHeaders },
|
|
714
783
|
{ skipAuthRefresh: true }
|
|
715
784
|
);
|
|
716
785
|
}
|