@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.mjs
CHANGED
|
@@ -393,8 +393,8 @@ var HttpClient = class {
|
|
|
393
393
|
const jitter = base * (0.85 + Math.random() * 0.3);
|
|
394
394
|
return Math.round(jitter);
|
|
395
395
|
}
|
|
396
|
-
shouldRefreshAccessToken(statusCode, errorCode, options = {}) {
|
|
397
|
-
return statusCode === 401 && errorCode === REFRESHABLE_AUTH_ERROR_CODE && !
|
|
396
|
+
shouldRefreshAccessToken(statusCode, errorCode, authToken, options = {}) {
|
|
397
|
+
return statusCode === 401 && errorCode === REFRESHABLE_AUTH_ERROR_CODE && !this.config.isServerMode && !this.config.edgeFunctionToken && !options.skipAuthRefresh && authToken !== null;
|
|
398
398
|
}
|
|
399
399
|
async fetchWithRetry(args) {
|
|
400
400
|
const {
|
|
@@ -513,7 +513,7 @@ var HttpClient = class {
|
|
|
513
513
|
* @returns Parsed response data.
|
|
514
514
|
* @throws {InsForgeError} On timeout, network failure, or HTTP error responses.
|
|
515
515
|
*/
|
|
516
|
-
async handleRequest(method, path, options = {}) {
|
|
516
|
+
async handleRequest(method, path, options = {}, tokenOverride) {
|
|
517
517
|
const {
|
|
518
518
|
params,
|
|
519
519
|
headers = {},
|
|
@@ -529,21 +529,32 @@ var HttpClient = class {
|
|
|
529
529
|
const requestHeaders = {
|
|
530
530
|
...this.defaultHeaders
|
|
531
531
|
};
|
|
532
|
-
const authToken = this.userToken
|
|
532
|
+
const authToken = tokenOverride ?? this.userToken ?? this.anonKey;
|
|
533
533
|
if (authToken) {
|
|
534
534
|
requestHeaders["Authorization"] = `Bearer ${authToken}`;
|
|
535
535
|
}
|
|
536
536
|
const processedBody = serializeBody(method, body, requestHeaders);
|
|
537
|
+
const setRequestHeader = (key, value) => {
|
|
538
|
+
if (key.toLowerCase() === "authorization") {
|
|
539
|
+
delete requestHeaders["Authorization"];
|
|
540
|
+
delete requestHeaders["authorization"];
|
|
541
|
+
requestHeaders["Authorization"] = value;
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
requestHeaders[key] = value;
|
|
545
|
+
};
|
|
537
546
|
if (headers instanceof Headers) {
|
|
538
547
|
headers.forEach((value, key) => {
|
|
539
|
-
|
|
548
|
+
setRequestHeader(key, value);
|
|
540
549
|
});
|
|
541
550
|
} else if (Array.isArray(headers)) {
|
|
542
551
|
headers.forEach(([key, value]) => {
|
|
543
|
-
|
|
552
|
+
setRequestHeader(key, value);
|
|
544
553
|
});
|
|
545
554
|
} else {
|
|
546
|
-
Object.
|
|
555
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
556
|
+
setRequestHeader(key, value);
|
|
557
|
+
});
|
|
547
558
|
}
|
|
548
559
|
this.logger.logRequest(method, url, requestHeaders, processedBody);
|
|
549
560
|
const response = await this.fetchWithRetry({
|
|
@@ -580,19 +591,47 @@ var HttpClient = class {
|
|
|
580
591
|
return data;
|
|
581
592
|
}
|
|
582
593
|
async request(method, path, options = {}) {
|
|
594
|
+
const tokenUsed = this.userToken;
|
|
583
595
|
try {
|
|
584
|
-
return await this.handleRequest(
|
|
596
|
+
return await this.handleRequest(
|
|
597
|
+
method,
|
|
598
|
+
path,
|
|
599
|
+
{ ...options },
|
|
600
|
+
tokenUsed
|
|
601
|
+
);
|
|
585
602
|
} catch (error) {
|
|
586
|
-
if (error instanceof InsForgeError
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
603
|
+
if (!(error instanceof InsForgeError) || !this.shouldRefreshAccessToken(
|
|
604
|
+
error.statusCode,
|
|
605
|
+
error.error,
|
|
606
|
+
tokenUsed,
|
|
607
|
+
options
|
|
608
|
+
)) {
|
|
609
|
+
throw error;
|
|
610
|
+
}
|
|
611
|
+
if (tokenUsed !== this.userToken) {
|
|
612
|
+
if (this.userToken === null) {
|
|
613
|
+
throw error;
|
|
592
614
|
}
|
|
593
|
-
return await this.handleRequest(
|
|
615
|
+
return await this.handleRequest(
|
|
616
|
+
method,
|
|
617
|
+
path,
|
|
618
|
+
{
|
|
619
|
+
...options,
|
|
620
|
+
skipAuthRefresh: true
|
|
621
|
+
},
|
|
622
|
+
this.userToken
|
|
623
|
+
);
|
|
594
624
|
}
|
|
595
|
-
|
|
625
|
+
try {
|
|
626
|
+
await this.refreshAndSaveSession();
|
|
627
|
+
} catch (error2) {
|
|
628
|
+
this.clearAuthSession();
|
|
629
|
+
throw error2;
|
|
630
|
+
}
|
|
631
|
+
return await this.handleRequest(method, path, {
|
|
632
|
+
...options,
|
|
633
|
+
skipAuthRefresh: true
|
|
634
|
+
});
|
|
596
635
|
}
|
|
597
636
|
}
|
|
598
637
|
/**
|
|
@@ -612,7 +651,14 @@ var HttpClient = class {
|
|
|
612
651
|
const method = initMethod ?? request?.method ?? "GET";
|
|
613
652
|
const url = request?.url ?? input.toString();
|
|
614
653
|
const startTime = Date.now();
|
|
615
|
-
const
|
|
654
|
+
const tokenUsed = this.userToken;
|
|
655
|
+
const headers = new Headers({
|
|
656
|
+
...this.defaultHeaders
|
|
657
|
+
});
|
|
658
|
+
const authToken = tokenUsed ?? this.anonKey;
|
|
659
|
+
if (authToken) {
|
|
660
|
+
headers.set("Authorization", `Bearer ${authToken}`);
|
|
661
|
+
}
|
|
616
662
|
request?.headers.forEach((value, key) => {
|
|
617
663
|
headers.set(key, value);
|
|
618
664
|
});
|
|
@@ -623,7 +669,13 @@ var HttpClient = class {
|
|
|
623
669
|
headers.forEach((value, key) => {
|
|
624
670
|
requestHeaders[key] = value;
|
|
625
671
|
});
|
|
626
|
-
const
|
|
672
|
+
const sourceBody = initBody ?? request?.body ?? void 0;
|
|
673
|
+
let body = sourceBody;
|
|
674
|
+
let retryInit = init;
|
|
675
|
+
if (typeof ReadableStream !== "undefined" && sourceBody instanceof ReadableStream) {
|
|
676
|
+
body = await new Response(sourceBody).arrayBuffer();
|
|
677
|
+
retryInit = { ...init ?? {}, body };
|
|
678
|
+
}
|
|
627
679
|
const callerSignal = initSignal ?? request?.signal;
|
|
628
680
|
const maxAttempts = IDEMPOTENT_METHODS.has(method.toUpperCase()) ? this.retryCount : 0;
|
|
629
681
|
this.logger.logRequest(method, url, requestHeaders, body);
|
|
@@ -655,9 +707,26 @@ var HttpClient = class {
|
|
|
655
707
|
} catch {
|
|
656
708
|
}
|
|
657
709
|
}
|
|
658
|
-
if (!this.shouldRefreshAccessToken(
|
|
710
|
+
if (!this.shouldRefreshAccessToken(
|
|
711
|
+
response.status,
|
|
712
|
+
errorCode,
|
|
713
|
+
tokenUsed,
|
|
714
|
+
options
|
|
715
|
+
)) {
|
|
659
716
|
return response;
|
|
660
717
|
}
|
|
718
|
+
if (tokenUsed !== this.userToken) {
|
|
719
|
+
if (this.userToken === null) {
|
|
720
|
+
return response;
|
|
721
|
+
}
|
|
722
|
+
const retryHeaders2 = new Headers(initHeaders);
|
|
723
|
+
retryHeaders2.set("Authorization", `Bearer ${this.userToken}`);
|
|
724
|
+
return await this.rawFetch(
|
|
725
|
+
input,
|
|
726
|
+
{ ...retryInit, headers: retryHeaders2 },
|
|
727
|
+
{ skipAuthRefresh: true }
|
|
728
|
+
);
|
|
729
|
+
}
|
|
661
730
|
let newTokenData;
|
|
662
731
|
try {
|
|
663
732
|
newTokenData = await this.refreshAndSaveSession();
|
|
@@ -669,7 +738,7 @@ var HttpClient = class {
|
|
|
669
738
|
retryHeaders.set("Authorization", `Bearer ${newTokenData.accessToken}`);
|
|
670
739
|
return await this.rawFetch(
|
|
671
740
|
input,
|
|
672
|
-
{ ...
|
|
741
|
+
{ ...retryInit, headers: retryHeaders },
|
|
673
742
|
{ skipAuthRefresh: true }
|
|
674
743
|
);
|
|
675
744
|
}
|