@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.js
CHANGED
|
@@ -325,7 +325,10 @@ var TokenManager = class {
|
|
|
325
325
|
// src/lib/http-client.ts
|
|
326
326
|
var RETRYABLE_STATUS_CODES = /* @__PURE__ */ new Set([500, 502, 503, 504]);
|
|
327
327
|
var IDEMPOTENT_METHODS = /* @__PURE__ */ new Set(["GET", "HEAD", "PUT", "DELETE", "OPTIONS"]);
|
|
328
|
-
var
|
|
328
|
+
var REFRESHABLE_AUTH_ERROR_CODES = /* @__PURE__ */ new Set([
|
|
329
|
+
"AUTH_UNAUTHORIZED",
|
|
330
|
+
"PGRST301"
|
|
331
|
+
]);
|
|
329
332
|
function serializeBody(method, body, headers) {
|
|
330
333
|
if (body === void 0) return void 0;
|
|
331
334
|
if (method === "GET" || method === "HEAD") return void 0;
|
|
@@ -434,8 +437,8 @@ var HttpClient = class {
|
|
|
434
437
|
const jitter = base * (0.85 + Math.random() * 0.3);
|
|
435
438
|
return Math.round(jitter);
|
|
436
439
|
}
|
|
437
|
-
shouldRefreshAccessToken(statusCode, errorCode, options = {}) {
|
|
438
|
-
return statusCode === 401 && errorCode
|
|
440
|
+
shouldRefreshAccessToken(statusCode, errorCode, authToken, options = {}) {
|
|
441
|
+
return statusCode === 401 && REFRESHABLE_AUTH_ERROR_CODES.has(errorCode ?? "") && !this.config.isServerMode && !this.config.edgeFunctionToken && !options.skipAuthRefresh && authToken !== null;
|
|
439
442
|
}
|
|
440
443
|
async fetchWithRetry(args) {
|
|
441
444
|
const {
|
|
@@ -554,7 +557,7 @@ var HttpClient = class {
|
|
|
554
557
|
* @returns Parsed response data.
|
|
555
558
|
* @throws {InsForgeError} On timeout, network failure, or HTTP error responses.
|
|
556
559
|
*/
|
|
557
|
-
async handleRequest(method, path, options = {}) {
|
|
560
|
+
async handleRequest(method, path, options = {}, tokenOverride) {
|
|
558
561
|
const {
|
|
559
562
|
params,
|
|
560
563
|
headers = {},
|
|
@@ -570,21 +573,32 @@ var HttpClient = class {
|
|
|
570
573
|
const requestHeaders = {
|
|
571
574
|
...this.defaultHeaders
|
|
572
575
|
};
|
|
573
|
-
const authToken = this.userToken
|
|
576
|
+
const authToken = tokenOverride ?? this.userToken ?? this.anonKey;
|
|
574
577
|
if (authToken) {
|
|
575
578
|
requestHeaders["Authorization"] = `Bearer ${authToken}`;
|
|
576
579
|
}
|
|
577
580
|
const processedBody = serializeBody(method, body, requestHeaders);
|
|
581
|
+
const setRequestHeader = (key, value) => {
|
|
582
|
+
if (key.toLowerCase() === "authorization") {
|
|
583
|
+
delete requestHeaders["Authorization"];
|
|
584
|
+
delete requestHeaders["authorization"];
|
|
585
|
+
requestHeaders["Authorization"] = value;
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
requestHeaders[key] = value;
|
|
589
|
+
};
|
|
578
590
|
if (headers instanceof Headers) {
|
|
579
591
|
headers.forEach((value, key) => {
|
|
580
|
-
|
|
592
|
+
setRequestHeader(key, value);
|
|
581
593
|
});
|
|
582
594
|
} else if (Array.isArray(headers)) {
|
|
583
595
|
headers.forEach(([key, value]) => {
|
|
584
|
-
|
|
596
|
+
setRequestHeader(key, value);
|
|
585
597
|
});
|
|
586
598
|
} else {
|
|
587
|
-
Object.
|
|
599
|
+
Object.entries(headers).forEach(([key, value]) => {
|
|
600
|
+
setRequestHeader(key, value);
|
|
601
|
+
});
|
|
588
602
|
}
|
|
589
603
|
this.logger.logRequest(method, url, requestHeaders, processedBody);
|
|
590
604
|
const response = await this.fetchWithRetry({
|
|
@@ -621,19 +635,47 @@ var HttpClient = class {
|
|
|
621
635
|
return data;
|
|
622
636
|
}
|
|
623
637
|
async request(method, path, options = {}) {
|
|
638
|
+
const tokenUsed = this.userToken;
|
|
624
639
|
try {
|
|
625
|
-
return await this.handleRequest(
|
|
640
|
+
return await this.handleRequest(
|
|
641
|
+
method,
|
|
642
|
+
path,
|
|
643
|
+
{ ...options },
|
|
644
|
+
tokenUsed
|
|
645
|
+
);
|
|
626
646
|
} catch (error) {
|
|
627
|
-
if (error instanceof InsForgeError
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
647
|
+
if (!(error instanceof InsForgeError) || !this.shouldRefreshAccessToken(
|
|
648
|
+
error.statusCode,
|
|
649
|
+
error.error,
|
|
650
|
+
tokenUsed,
|
|
651
|
+
options
|
|
652
|
+
)) {
|
|
653
|
+
throw error;
|
|
654
|
+
}
|
|
655
|
+
if (tokenUsed !== this.userToken) {
|
|
656
|
+
if (this.userToken === null) {
|
|
657
|
+
throw error;
|
|
633
658
|
}
|
|
634
|
-
return await this.handleRequest(
|
|
659
|
+
return await this.handleRequest(
|
|
660
|
+
method,
|
|
661
|
+
path,
|
|
662
|
+
{
|
|
663
|
+
...options,
|
|
664
|
+
skipAuthRefresh: true
|
|
665
|
+
},
|
|
666
|
+
this.userToken
|
|
667
|
+
);
|
|
635
668
|
}
|
|
636
|
-
|
|
669
|
+
try {
|
|
670
|
+
await this.refreshAndSaveSession();
|
|
671
|
+
} catch (error2) {
|
|
672
|
+
this.clearAuthSession();
|
|
673
|
+
throw error2;
|
|
674
|
+
}
|
|
675
|
+
return await this.handleRequest(method, path, {
|
|
676
|
+
...options,
|
|
677
|
+
skipAuthRefresh: true
|
|
678
|
+
});
|
|
637
679
|
}
|
|
638
680
|
}
|
|
639
681
|
/**
|
|
@@ -653,7 +695,14 @@ var HttpClient = class {
|
|
|
653
695
|
const method = initMethod ?? request?.method ?? "GET";
|
|
654
696
|
const url = request?.url ?? input.toString();
|
|
655
697
|
const startTime = Date.now();
|
|
656
|
-
const
|
|
698
|
+
const tokenUsed = this.userToken;
|
|
699
|
+
const headers = new Headers({
|
|
700
|
+
...this.defaultHeaders
|
|
701
|
+
});
|
|
702
|
+
const authToken = tokenUsed ?? this.anonKey;
|
|
703
|
+
if (authToken) {
|
|
704
|
+
headers.set("Authorization", `Bearer ${authToken}`);
|
|
705
|
+
}
|
|
657
706
|
request?.headers.forEach((value, key) => {
|
|
658
707
|
headers.set(key, value);
|
|
659
708
|
});
|
|
@@ -664,7 +713,13 @@ var HttpClient = class {
|
|
|
664
713
|
headers.forEach((value, key) => {
|
|
665
714
|
requestHeaders[key] = value;
|
|
666
715
|
});
|
|
667
|
-
const
|
|
716
|
+
const sourceBody = initBody ?? request?.body ?? void 0;
|
|
717
|
+
let body = sourceBody;
|
|
718
|
+
let retryInit = init;
|
|
719
|
+
if (typeof ReadableStream !== "undefined" && sourceBody instanceof ReadableStream) {
|
|
720
|
+
body = await new Response(sourceBody).arrayBuffer();
|
|
721
|
+
retryInit = { ...init ?? {}, body };
|
|
722
|
+
}
|
|
668
723
|
const callerSignal = initSignal ?? request?.signal;
|
|
669
724
|
const maxAttempts = IDEMPOTENT_METHODS.has(method.toUpperCase()) ? this.retryCount : 0;
|
|
670
725
|
this.logger.logRequest(method, url, requestHeaders, body);
|
|
@@ -696,9 +751,26 @@ var HttpClient = class {
|
|
|
696
751
|
} catch {
|
|
697
752
|
}
|
|
698
753
|
}
|
|
699
|
-
if (!this.shouldRefreshAccessToken(
|
|
754
|
+
if (!this.shouldRefreshAccessToken(
|
|
755
|
+
response.status,
|
|
756
|
+
errorCode,
|
|
757
|
+
tokenUsed,
|
|
758
|
+
options
|
|
759
|
+
)) {
|
|
700
760
|
return response;
|
|
701
761
|
}
|
|
762
|
+
if (tokenUsed !== this.userToken) {
|
|
763
|
+
if (this.userToken === null) {
|
|
764
|
+
return response;
|
|
765
|
+
}
|
|
766
|
+
const retryHeaders2 = new Headers(initHeaders);
|
|
767
|
+
retryHeaders2.set("Authorization", `Bearer ${this.userToken}`);
|
|
768
|
+
return await this.rawFetch(
|
|
769
|
+
input,
|
|
770
|
+
{ ...retryInit, headers: retryHeaders2 },
|
|
771
|
+
{ skipAuthRefresh: true }
|
|
772
|
+
);
|
|
773
|
+
}
|
|
702
774
|
let newTokenData;
|
|
703
775
|
try {
|
|
704
776
|
newTokenData = await this.refreshAndSaveSession();
|
|
@@ -710,7 +782,7 @@ var HttpClient = class {
|
|
|
710
782
|
retryHeaders.set("Authorization", `Bearer ${newTokenData.accessToken}`);
|
|
711
783
|
return await this.rawFetch(
|
|
712
784
|
input,
|
|
713
|
-
{ ...
|
|
785
|
+
{ ...retryInit, headers: retryHeaders },
|
|
714
786
|
{ skipAuthRefresh: true }
|
|
715
787
|
);
|
|
716
788
|
}
|