@npora/request 1.3.0 → 1.5.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/README.md +39 -0
- package/dist/index.cjs +588 -24
- package/dist/index.d.cts +126 -2
- package/dist/index.d.ts +126 -2
- package/dist/index.js +587 -24
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -29,6 +29,7 @@ __export(index_exports, {
|
|
|
29
29
|
RequestError: () => RequestError,
|
|
30
30
|
authPlugin: () => authPlugin,
|
|
31
31
|
cachePlugin: () => cachePlugin,
|
|
32
|
+
circuitBreakerPlugin: () => circuitBreakerPlugin,
|
|
32
33
|
createClient: () => createClient,
|
|
33
34
|
downloadPlugin: () => downloadPlugin,
|
|
34
35
|
loggerPlugin: () => loggerPlugin,
|
|
@@ -469,35 +470,370 @@ function defaultValidateStatus(status) {
|
|
|
469
470
|
var MockAdapter = class {
|
|
470
471
|
constructor(options = {}) {
|
|
471
472
|
this.handlers = /* @__PURE__ */ new Map();
|
|
473
|
+
this.rules = [];
|
|
474
|
+
this.requests = [];
|
|
475
|
+
this.delay = normalizeDelay(options.delay);
|
|
472
476
|
Object.entries(options.handlers ?? {}).forEach(([key, handler]) => {
|
|
473
477
|
this.handlers.set(key, handler);
|
|
474
478
|
});
|
|
475
479
|
}
|
|
480
|
+
get history() {
|
|
481
|
+
return [...this.requests];
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* Register a legacy URL-only data handler.
|
|
485
|
+
*/
|
|
476
486
|
on(url, handler) {
|
|
477
487
|
this.handlers.set(url, handler);
|
|
478
488
|
return this;
|
|
479
489
|
}
|
|
490
|
+
onMethod(method, matcher) {
|
|
491
|
+
const normalizedMatcher = normalizeMatcher(matcher);
|
|
492
|
+
const registrar = {
|
|
493
|
+
reply: (once2, replyOrStatus, data, options) => {
|
|
494
|
+
return this.registerReply(
|
|
495
|
+
method,
|
|
496
|
+
normalizedMatcher,
|
|
497
|
+
once2,
|
|
498
|
+
replyOrStatus,
|
|
499
|
+
data,
|
|
500
|
+
options
|
|
501
|
+
);
|
|
502
|
+
},
|
|
503
|
+
error: (once2, code, message) => {
|
|
504
|
+
return this.registerError(
|
|
505
|
+
method,
|
|
506
|
+
normalizedMatcher,
|
|
507
|
+
once2,
|
|
508
|
+
code,
|
|
509
|
+
message
|
|
510
|
+
);
|
|
511
|
+
}
|
|
512
|
+
};
|
|
513
|
+
return new RouteBuilder(registrar);
|
|
514
|
+
}
|
|
515
|
+
onGet(matcher) {
|
|
516
|
+
return this.onMethod("GET", matcher);
|
|
517
|
+
}
|
|
518
|
+
onPost(matcher) {
|
|
519
|
+
return this.onMethod("POST", matcher);
|
|
520
|
+
}
|
|
521
|
+
onPut(matcher) {
|
|
522
|
+
return this.onMethod("PUT", matcher);
|
|
523
|
+
}
|
|
524
|
+
onPatch(matcher) {
|
|
525
|
+
return this.onMethod("PATCH", matcher);
|
|
526
|
+
}
|
|
527
|
+
onDelete(matcher) {
|
|
528
|
+
return this.onMethod("DELETE", matcher);
|
|
529
|
+
}
|
|
530
|
+
onHead(matcher) {
|
|
531
|
+
return this.onMethod("HEAD", matcher);
|
|
532
|
+
}
|
|
533
|
+
onOptions(matcher) {
|
|
534
|
+
return this.onMethod("OPTIONS", matcher);
|
|
535
|
+
}
|
|
536
|
+
resetHistory() {
|
|
537
|
+
this.requests.length = 0;
|
|
538
|
+
return this;
|
|
539
|
+
}
|
|
540
|
+
resetHandlers() {
|
|
541
|
+
this.handlers.clear();
|
|
542
|
+
this.rules.length = 0;
|
|
543
|
+
return this;
|
|
544
|
+
}
|
|
545
|
+
reset() {
|
|
546
|
+
return this.resetHandlers().resetHistory();
|
|
547
|
+
}
|
|
480
548
|
async request(config) {
|
|
549
|
+
this.requests.push(config);
|
|
550
|
+
const method = config.method ?? "GET";
|
|
551
|
+
const ruleIndex = this.rules.findIndex((rule) => {
|
|
552
|
+
return (rule.method === void 0 || rule.method === method) && matchesRequest(rule.matcher, config);
|
|
553
|
+
});
|
|
554
|
+
if (ruleIndex !== -1) {
|
|
555
|
+
const rule = this.rules[ruleIndex];
|
|
556
|
+
if (rule.once) {
|
|
557
|
+
this.rules.splice(ruleIndex, 1);
|
|
558
|
+
}
|
|
559
|
+
const response = await rule.respond(config);
|
|
560
|
+
return validateMockResponse(response, config);
|
|
561
|
+
}
|
|
481
562
|
const handler = this.handlers.get(config.url);
|
|
482
563
|
if (!handler) {
|
|
483
|
-
throw new Error(
|
|
564
|
+
throw new Error(
|
|
565
|
+
`No mock handler found for ${config.url} (${method})`
|
|
566
|
+
);
|
|
484
567
|
}
|
|
568
|
+
await waitForDelay(this.delay, config);
|
|
485
569
|
const data = await handler(config);
|
|
486
|
-
return {
|
|
487
|
-
data,
|
|
570
|
+
return validateMockResponse(createMockResponse(config, {
|
|
488
571
|
status: 200,
|
|
489
572
|
statusText: "OK",
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
573
|
+
data
|
|
574
|
+
}), config);
|
|
575
|
+
}
|
|
576
|
+
registerReply(method, matcher, once2, replyOrStatus, data, options = {}) {
|
|
577
|
+
const handler = typeof replyOrStatus === "function" ? replyOrStatus : () => ({
|
|
578
|
+
status: replyOrStatus,
|
|
579
|
+
data,
|
|
580
|
+
...options
|
|
581
|
+
});
|
|
582
|
+
this.rules.push({
|
|
583
|
+
method,
|
|
584
|
+
matcher,
|
|
585
|
+
once: once2,
|
|
586
|
+
respond: async (config) => {
|
|
587
|
+
const reply = await handler(config);
|
|
588
|
+
await waitForDelay(
|
|
589
|
+
normalizeDelay(reply.delay ?? this.delay),
|
|
590
|
+
config
|
|
591
|
+
);
|
|
592
|
+
return createMockResponse(config, reply);
|
|
593
|
+
}
|
|
594
|
+
});
|
|
595
|
+
return this;
|
|
596
|
+
}
|
|
597
|
+
registerError(method, matcher, once2, code, message) {
|
|
598
|
+
this.rules.push({
|
|
599
|
+
method,
|
|
600
|
+
matcher,
|
|
601
|
+
once: once2,
|
|
602
|
+
respond: async (config) => {
|
|
603
|
+
await waitForDelay(this.delay, config);
|
|
604
|
+
throw new RequestError(message, {
|
|
605
|
+
code,
|
|
606
|
+
config
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
});
|
|
610
|
+
return this;
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
var RouteBuilder = class {
|
|
614
|
+
constructor(registrar) {
|
|
615
|
+
this.registrar = registrar;
|
|
616
|
+
}
|
|
617
|
+
reply(replyOrStatus, data, options = {}) {
|
|
618
|
+
return this.registrar.reply(
|
|
619
|
+
false,
|
|
620
|
+
replyOrStatus,
|
|
621
|
+
data,
|
|
622
|
+
options
|
|
623
|
+
);
|
|
624
|
+
}
|
|
625
|
+
replyOnce(replyOrStatus, data, options = {}) {
|
|
626
|
+
return this.registrar.reply(
|
|
627
|
+
true,
|
|
628
|
+
replyOrStatus,
|
|
629
|
+
data,
|
|
630
|
+
options
|
|
631
|
+
);
|
|
632
|
+
}
|
|
633
|
+
networkError(message = "Mock network error") {
|
|
634
|
+
return this.registrar.error(
|
|
635
|
+
false,
|
|
636
|
+
"NETWORK_ERROR",
|
|
637
|
+
message
|
|
638
|
+
);
|
|
639
|
+
}
|
|
640
|
+
networkErrorOnce(message = "Mock network error") {
|
|
641
|
+
return this.registrar.error(
|
|
642
|
+
true,
|
|
643
|
+
"NETWORK_ERROR",
|
|
644
|
+
message
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
timeout(message = "Mock request timeout") {
|
|
648
|
+
return this.registrar.error(
|
|
649
|
+
false,
|
|
650
|
+
"TIMEOUT_ERROR",
|
|
651
|
+
message
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
timeoutOnce(message = "Mock request timeout") {
|
|
655
|
+
return this.registrar.error(
|
|
656
|
+
true,
|
|
657
|
+
"TIMEOUT_ERROR",
|
|
658
|
+
message
|
|
659
|
+
);
|
|
660
|
+
}
|
|
661
|
+
};
|
|
662
|
+
function normalizeMatcher(matcher) {
|
|
663
|
+
if (typeof matcher === "string" || matcher instanceof RegExp) {
|
|
664
|
+
return {
|
|
665
|
+
url: matcher
|
|
498
666
|
};
|
|
499
667
|
}
|
|
668
|
+
return matcher;
|
|
669
|
+
}
|
|
670
|
+
function matchesRequest(matcher, config) {
|
|
671
|
+
return matchesURL(matcher.url, config.url) && matchesQuery(matcher.query, config.query) && matchesHeaders(matcher.headers, config.headers);
|
|
672
|
+
}
|
|
673
|
+
function matchesURL(matcher, url) {
|
|
674
|
+
if (typeof matcher === "string") {
|
|
675
|
+
return matcher === url;
|
|
676
|
+
}
|
|
677
|
+
matcher.lastIndex = 0;
|
|
678
|
+
return matcher.test(url);
|
|
679
|
+
}
|
|
680
|
+
function matchesQuery(expected, actual) {
|
|
681
|
+
if (!expected) {
|
|
682
|
+
return true;
|
|
683
|
+
}
|
|
684
|
+
return normalizeQuery(expected) === normalizeQuery(actual);
|
|
685
|
+
}
|
|
686
|
+
function normalizeQuery(query) {
|
|
687
|
+
if (!query) {
|
|
688
|
+
return "[]";
|
|
689
|
+
}
|
|
690
|
+
const entries = [];
|
|
691
|
+
for (const key of Object.keys(query).sort()) {
|
|
692
|
+
const value = query[key];
|
|
693
|
+
const values = Array.isArray(value) ? value : [value];
|
|
694
|
+
for (const item of values) {
|
|
695
|
+
if (item !== null && item !== void 0) {
|
|
696
|
+
entries.push([key, String(item)]);
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
return JSON.stringify(entries);
|
|
701
|
+
}
|
|
702
|
+
function matchesHeaders(expected, actual) {
|
|
703
|
+
if (!expected) {
|
|
704
|
+
return true;
|
|
705
|
+
}
|
|
706
|
+
const expectedHeaders = new Headers(expected);
|
|
707
|
+
const actualHeaders = new Headers(actual);
|
|
708
|
+
for (const [name, value] of expectedHeaders.entries()) {
|
|
709
|
+
if (actualHeaders.get(name) !== value) {
|
|
710
|
+
return false;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
return true;
|
|
714
|
+
}
|
|
715
|
+
function createMockResponse(config, reply) {
|
|
716
|
+
const headers = new Headers(reply.headers);
|
|
717
|
+
const rawBody = createRawBody(reply.data, headers, reply.status);
|
|
718
|
+
const statusText = reply.statusText ?? defaultStatusText(reply.status);
|
|
719
|
+
const raw = new Response(rawBody, {
|
|
720
|
+
status: reply.status,
|
|
721
|
+
statusText,
|
|
722
|
+
headers
|
|
723
|
+
});
|
|
724
|
+
return {
|
|
725
|
+
data: reply.data,
|
|
726
|
+
status: reply.status,
|
|
727
|
+
statusText,
|
|
728
|
+
headers: raw.headers,
|
|
729
|
+
config,
|
|
730
|
+
raw
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
function validateMockResponse(response, config) {
|
|
734
|
+
const validateStatus = config.validateStatus ?? defaultValidateStatus2;
|
|
735
|
+
if (!validateStatus(response.status)) {
|
|
736
|
+
throw new RequestError(
|
|
737
|
+
response.statusText || "Mock request failed",
|
|
738
|
+
{
|
|
739
|
+
code: "HTTP_ERROR",
|
|
740
|
+
response
|
|
741
|
+
}
|
|
742
|
+
);
|
|
743
|
+
}
|
|
744
|
+
return response;
|
|
745
|
+
}
|
|
746
|
+
function defaultValidateStatus2(status) {
|
|
747
|
+
return status >= 200 && status < 300;
|
|
748
|
+
}
|
|
749
|
+
function defaultStatusText(status) {
|
|
750
|
+
return STATUS_TEXT[status] ?? "";
|
|
751
|
+
}
|
|
752
|
+
var STATUS_TEXT = {
|
|
753
|
+
200: "OK",
|
|
754
|
+
201: "Created",
|
|
755
|
+
202: "Accepted",
|
|
756
|
+
204: "No Content",
|
|
757
|
+
400: "Bad Request",
|
|
758
|
+
401: "Unauthorized",
|
|
759
|
+
403: "Forbidden",
|
|
760
|
+
404: "Not Found",
|
|
761
|
+
408: "Request Timeout",
|
|
762
|
+
409: "Conflict",
|
|
763
|
+
422: "Unprocessable Content",
|
|
764
|
+
429: "Too Many Requests",
|
|
765
|
+
500: "Internal Server Error",
|
|
766
|
+
502: "Bad Gateway",
|
|
767
|
+
503: "Service Unavailable",
|
|
768
|
+
504: "Gateway Timeout"
|
|
500
769
|
};
|
|
770
|
+
function createRawBody(data, headers, status) {
|
|
771
|
+
if (data === void 0 || data === null || status === 204 || status === 205 || status === 304) {
|
|
772
|
+
return null;
|
|
773
|
+
}
|
|
774
|
+
if (typeof data === "string" || isBodyInit(data)) {
|
|
775
|
+
return data;
|
|
776
|
+
}
|
|
777
|
+
if (!headers.has("content-type")) {
|
|
778
|
+
headers.set("content-type", "application/json");
|
|
779
|
+
}
|
|
780
|
+
return JSON.stringify(data);
|
|
781
|
+
}
|
|
782
|
+
function isBodyInit(value) {
|
|
783
|
+
return typeof Blob !== "undefined" && value instanceof Blob || typeof FormData !== "undefined" && value instanceof FormData || typeof URLSearchParams !== "undefined" && value instanceof URLSearchParams || typeof ReadableStream !== "undefined" && value instanceof ReadableStream || value instanceof ArrayBuffer || ArrayBuffer.isView(value);
|
|
784
|
+
}
|
|
785
|
+
function normalizeDelay(delay) {
|
|
786
|
+
if (!Number.isFinite(delay)) {
|
|
787
|
+
return delay && delay > 0 ? MAX_TIMER_DELAY : 0;
|
|
788
|
+
}
|
|
789
|
+
return Math.min(Math.max(0, delay ?? 0), MAX_TIMER_DELAY);
|
|
790
|
+
}
|
|
791
|
+
var MAX_TIMER_DELAY = 2147483647;
|
|
792
|
+
function waitForDelay(delay, config) {
|
|
793
|
+
const signal = config.signal;
|
|
794
|
+
if (signal?.aborted) {
|
|
795
|
+
return Promise.reject(createAbortError(signal.reason, config));
|
|
796
|
+
}
|
|
797
|
+
const timeout = config.timeout && config.timeout > 0 ? config.timeout : void 0;
|
|
798
|
+
const timeoutWins = timeout !== void 0 && timeout <= delay;
|
|
799
|
+
const wait = timeoutWins ? timeout : delay;
|
|
800
|
+
if (wait <= 0) {
|
|
801
|
+
return Promise.resolve();
|
|
802
|
+
}
|
|
803
|
+
return new Promise((resolve, reject) => {
|
|
804
|
+
const timer = setTimeout(() => {
|
|
805
|
+
cleanup();
|
|
806
|
+
if (timeoutWins) {
|
|
807
|
+
reject(
|
|
808
|
+
new RequestError(`Request timeout after ${timeout}ms`, {
|
|
809
|
+
code: "TIMEOUT_ERROR",
|
|
810
|
+
config
|
|
811
|
+
})
|
|
812
|
+
);
|
|
813
|
+
} else {
|
|
814
|
+
resolve();
|
|
815
|
+
}
|
|
816
|
+
}, wait);
|
|
817
|
+
const onAbort = () => {
|
|
818
|
+
clearTimeout(timer);
|
|
819
|
+
cleanup();
|
|
820
|
+
reject(createAbortError(signal?.reason, config));
|
|
821
|
+
};
|
|
822
|
+
const cleanup = () => {
|
|
823
|
+
signal?.removeEventListener("abort", onAbort);
|
|
824
|
+
};
|
|
825
|
+
signal?.addEventListener("abort", onAbort, {
|
|
826
|
+
once: true
|
|
827
|
+
});
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
function createAbortError(reason, config) {
|
|
831
|
+
return new RequestError("Mock request aborted", {
|
|
832
|
+
code: "ABORT_ERROR",
|
|
833
|
+
config,
|
|
834
|
+
cause: reason
|
|
835
|
+
});
|
|
836
|
+
}
|
|
501
837
|
|
|
502
838
|
// src/core/ConfigMerger.ts
|
|
503
839
|
var ConfigMerger = class {
|
|
@@ -821,7 +1157,7 @@ var Pipeline = class {
|
|
|
821
1157
|
function waitForRetry(milliseconds, config) {
|
|
822
1158
|
const signal = config.signal;
|
|
823
1159
|
if (signal?.aborted) {
|
|
824
|
-
return Promise.reject(
|
|
1160
|
+
return Promise.reject(createAbortError2(signal.reason, config));
|
|
825
1161
|
}
|
|
826
1162
|
if (milliseconds <= 0) {
|
|
827
1163
|
return Promise.resolve();
|
|
@@ -834,7 +1170,7 @@ function waitForRetry(milliseconds, config) {
|
|
|
834
1170
|
const onAbort = () => {
|
|
835
1171
|
clearTimeout(timer);
|
|
836
1172
|
cleanup();
|
|
837
|
-
reject(
|
|
1173
|
+
reject(createAbortError2(signal?.reason, config));
|
|
838
1174
|
};
|
|
839
1175
|
const cleanup = () => {
|
|
840
1176
|
signal?.removeEventListener("abort", onAbort);
|
|
@@ -844,7 +1180,7 @@ function waitForRetry(milliseconds, config) {
|
|
|
844
1180
|
});
|
|
845
1181
|
});
|
|
846
1182
|
}
|
|
847
|
-
function
|
|
1183
|
+
function createAbortError2(reason, config) {
|
|
848
1184
|
if (reason instanceof RequestError) {
|
|
849
1185
|
return new RequestError(reason.message, {
|
|
850
1186
|
code: reason.code,
|
|
@@ -1838,7 +2174,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
1838
2174
|
method: config.method ?? "GET",
|
|
1839
2175
|
baseURL: config.baseURL,
|
|
1840
2176
|
url: config.url,
|
|
1841
|
-
query:
|
|
2177
|
+
query: normalizeQuery2(config.query),
|
|
1842
2178
|
responseType: config.responseType ?? "auto",
|
|
1843
2179
|
headers: Object.fromEntries(
|
|
1844
2180
|
varyHeaders.map((name) => [
|
|
@@ -1848,7 +2184,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
1848
2184
|
)
|
|
1849
2185
|
});
|
|
1850
2186
|
}
|
|
1851
|
-
function
|
|
2187
|
+
function normalizeQuery2(query) {
|
|
1852
2188
|
if (!query) {
|
|
1853
2189
|
return [];
|
|
1854
2190
|
}
|
|
@@ -1901,6 +2237,233 @@ function cloneCacheValue(value) {
|
|
|
1901
2237
|
}
|
|
1902
2238
|
}
|
|
1903
2239
|
|
|
2240
|
+
// src/plugins/circuitBreakerPlugin.ts
|
|
2241
|
+
function circuitBreakerPlugin(options = {}) {
|
|
2242
|
+
const normalized = normalizeOptions(options);
|
|
2243
|
+
const circuits = /* @__PURE__ */ new Map();
|
|
2244
|
+
const plugin = {
|
|
2245
|
+
name: "circuit-breaker",
|
|
2246
|
+
getState(key) {
|
|
2247
|
+
return circuits.get(key)?.state ?? "closed";
|
|
2248
|
+
},
|
|
2249
|
+
reset(key) {
|
|
2250
|
+
if (key === void 0) {
|
|
2251
|
+
circuits.clear();
|
|
2252
|
+
return;
|
|
2253
|
+
}
|
|
2254
|
+
circuits.delete(key);
|
|
2255
|
+
},
|
|
2256
|
+
install(context) {
|
|
2257
|
+
const admissions = /* @__PURE__ */ new WeakMap();
|
|
2258
|
+
let active = true;
|
|
2259
|
+
context.hooks.onRequest((requestContext) => {
|
|
2260
|
+
const requestOptions = resolveExtensionConfig(
|
|
2261
|
+
requestContext.config,
|
|
2262
|
+
"circuitBreaker"
|
|
2263
|
+
);
|
|
2264
|
+
if (requestOptions?.enabled === false) {
|
|
2265
|
+
return;
|
|
2266
|
+
}
|
|
2267
|
+
const key = normalizeKey(
|
|
2268
|
+
requestOptions?.key ?? normalized.createKey(requestContext.config)
|
|
2269
|
+
);
|
|
2270
|
+
const record = getCircuit(circuits, key);
|
|
2271
|
+
if (record.state === "open" && Date.now() - record.openedAt >= normalized.resetTimeout) {
|
|
2272
|
+
transition(record, key, "half-open", normalized);
|
|
2273
|
+
}
|
|
2274
|
+
if (record.state === "open") {
|
|
2275
|
+
throw createOpenError(key, requestContext.config);
|
|
2276
|
+
}
|
|
2277
|
+
const probe = record.state === "half-open";
|
|
2278
|
+
if (probe && record.probes >= normalized.halfOpenMaxRequests) {
|
|
2279
|
+
throw createOpenError(key, requestContext.config);
|
|
2280
|
+
}
|
|
2281
|
+
if (probe) {
|
|
2282
|
+
record.probes += 1;
|
|
2283
|
+
}
|
|
2284
|
+
admissions.set(requestContext, {
|
|
2285
|
+
key,
|
|
2286
|
+
generation: record.generation,
|
|
2287
|
+
probe
|
|
2288
|
+
});
|
|
2289
|
+
});
|
|
2290
|
+
context.hooks.onSettled(async (requestContext) => {
|
|
2291
|
+
const admission = admissions.get(requestContext);
|
|
2292
|
+
if (!admission) {
|
|
2293
|
+
return;
|
|
2294
|
+
}
|
|
2295
|
+
admissions.delete(requestContext);
|
|
2296
|
+
if (!active) {
|
|
2297
|
+
return;
|
|
2298
|
+
}
|
|
2299
|
+
const record = circuits.get(admission.key);
|
|
2300
|
+
if (!record || record.generation !== admission.generation) {
|
|
2301
|
+
return;
|
|
2302
|
+
}
|
|
2303
|
+
if (admission.probe) {
|
|
2304
|
+
record.probes = Math.max(0, record.probes - 1);
|
|
2305
|
+
}
|
|
2306
|
+
if (!requestContext.error && requestContext.response) {
|
|
2307
|
+
record.failures = 0;
|
|
2308
|
+
if (admission.probe && record.state === "half-open") {
|
|
2309
|
+
record.successes += 1;
|
|
2310
|
+
if (record.successes >= normalized.successThreshold) {
|
|
2311
|
+
transition(record, admission.key, "closed", normalized);
|
|
2312
|
+
}
|
|
2313
|
+
}
|
|
2314
|
+
return;
|
|
2315
|
+
}
|
|
2316
|
+
const counted = await normalized.shouldCountFailure(
|
|
2317
|
+
requestContext.error,
|
|
2318
|
+
requestContext.config
|
|
2319
|
+
);
|
|
2320
|
+
if (!active || circuits.get(admission.key) !== record || record.generation !== admission.generation || !counted) {
|
|
2321
|
+
return;
|
|
2322
|
+
}
|
|
2323
|
+
if (admission.probe && record.state === "half-open") {
|
|
2324
|
+
transition(record, admission.key, "open", normalized);
|
|
2325
|
+
return;
|
|
2326
|
+
}
|
|
2327
|
+
if (record.state === "closed") {
|
|
2328
|
+
record.failures += 1;
|
|
2329
|
+
if (record.failures >= normalized.failureThreshold) {
|
|
2330
|
+
transition(record, admission.key, "open", normalized);
|
|
2331
|
+
}
|
|
2332
|
+
}
|
|
2333
|
+
});
|
|
2334
|
+
return () => {
|
|
2335
|
+
active = false;
|
|
2336
|
+
circuits.clear();
|
|
2337
|
+
};
|
|
2338
|
+
}
|
|
2339
|
+
};
|
|
2340
|
+
return plugin;
|
|
2341
|
+
}
|
|
2342
|
+
function normalizeOptions(options) {
|
|
2343
|
+
return {
|
|
2344
|
+
failureThreshold: normalizePositiveInteger(
|
|
2345
|
+
options.failureThreshold,
|
|
2346
|
+
5
|
|
2347
|
+
),
|
|
2348
|
+
successThreshold: normalizePositiveInteger(
|
|
2349
|
+
options.successThreshold,
|
|
2350
|
+
1
|
|
2351
|
+
),
|
|
2352
|
+
resetTimeout: normalizeDuration(options.resetTimeout, 3e4),
|
|
2353
|
+
halfOpenMaxRequests: normalizePositiveInteger(
|
|
2354
|
+
options.halfOpenMaxRequests,
|
|
2355
|
+
1
|
|
2356
|
+
),
|
|
2357
|
+
createKey: options.createKey ?? createDefaultKey,
|
|
2358
|
+
shouldCountFailure: options.shouldCountFailure ?? defaultShouldCountFailure,
|
|
2359
|
+
onStateChange: options.onStateChange
|
|
2360
|
+
};
|
|
2361
|
+
}
|
|
2362
|
+
function normalizePositiveInteger(value, fallback) {
|
|
2363
|
+
if (value === void 0) {
|
|
2364
|
+
return fallback;
|
|
2365
|
+
}
|
|
2366
|
+
if (!Number.isFinite(value)) {
|
|
2367
|
+
return fallback;
|
|
2368
|
+
}
|
|
2369
|
+
return Math.max(1, Math.floor(value));
|
|
2370
|
+
}
|
|
2371
|
+
function normalizeDuration(value, fallback) {
|
|
2372
|
+
if (value === void 0) {
|
|
2373
|
+
return fallback;
|
|
2374
|
+
}
|
|
2375
|
+
if (!Number.isFinite(value)) {
|
|
2376
|
+
return value > 0 ? Number.MAX_SAFE_INTEGER : 0;
|
|
2377
|
+
}
|
|
2378
|
+
return Math.max(0, value);
|
|
2379
|
+
}
|
|
2380
|
+
function normalizeKey(key) {
|
|
2381
|
+
return key || "default";
|
|
2382
|
+
}
|
|
2383
|
+
function createDefaultKey(config) {
|
|
2384
|
+
try {
|
|
2385
|
+
return new URL(config.url).origin;
|
|
2386
|
+
} catch {
|
|
2387
|
+
}
|
|
2388
|
+
try {
|
|
2389
|
+
if (config.baseURL) {
|
|
2390
|
+
return new URL(config.url, config.baseURL).origin;
|
|
2391
|
+
}
|
|
2392
|
+
} catch {
|
|
2393
|
+
}
|
|
2394
|
+
return "default";
|
|
2395
|
+
}
|
|
2396
|
+
function getCircuit(circuits, key) {
|
|
2397
|
+
let record = circuits.get(key);
|
|
2398
|
+
if (!record) {
|
|
2399
|
+
record = {
|
|
2400
|
+
state: "closed",
|
|
2401
|
+
failures: 0,
|
|
2402
|
+
successes: 0,
|
|
2403
|
+
openedAt: 0,
|
|
2404
|
+
probes: 0,
|
|
2405
|
+
generation: 0
|
|
2406
|
+
};
|
|
2407
|
+
circuits.set(key, record);
|
|
2408
|
+
}
|
|
2409
|
+
return record;
|
|
2410
|
+
}
|
|
2411
|
+
function transition(record, key, state, options) {
|
|
2412
|
+
if (record.state === state) {
|
|
2413
|
+
if (state === "open") {
|
|
2414
|
+
record.openedAt = Date.now();
|
|
2415
|
+
record.successes = 0;
|
|
2416
|
+
record.probes = 0;
|
|
2417
|
+
record.generation += 1;
|
|
2418
|
+
}
|
|
2419
|
+
return;
|
|
2420
|
+
}
|
|
2421
|
+
const previousState = record.state;
|
|
2422
|
+
record.state = state;
|
|
2423
|
+
record.successes = 0;
|
|
2424
|
+
record.probes = 0;
|
|
2425
|
+
record.generation += 1;
|
|
2426
|
+
if (state === "open") {
|
|
2427
|
+
record.openedAt = Date.now();
|
|
2428
|
+
} else if (state === "closed") {
|
|
2429
|
+
record.failures = 0;
|
|
2430
|
+
record.openedAt = 0;
|
|
2431
|
+
}
|
|
2432
|
+
notifyStateChange(options.onStateChange, {
|
|
2433
|
+
key,
|
|
2434
|
+
previousState,
|
|
2435
|
+
state,
|
|
2436
|
+
timestamp: Date.now(),
|
|
2437
|
+
failures: record.failures
|
|
2438
|
+
});
|
|
2439
|
+
}
|
|
2440
|
+
function notifyStateChange(observer, event) {
|
|
2441
|
+
if (!observer) {
|
|
2442
|
+
return;
|
|
2443
|
+
}
|
|
2444
|
+
try {
|
|
2445
|
+
void Promise.resolve(observer(event)).catch(ignoreObserverError);
|
|
2446
|
+
} catch {
|
|
2447
|
+
}
|
|
2448
|
+
}
|
|
2449
|
+
function ignoreObserverError() {
|
|
2450
|
+
}
|
|
2451
|
+
function createOpenError(key, config) {
|
|
2452
|
+
return new RequestError(`Circuit is open for ${key}`, {
|
|
2453
|
+
code: "CIRCUIT_OPEN",
|
|
2454
|
+
config
|
|
2455
|
+
});
|
|
2456
|
+
}
|
|
2457
|
+
function defaultShouldCountFailure(error) {
|
|
2458
|
+
if (!(error instanceof RequestError)) {
|
|
2459
|
+
return true;
|
|
2460
|
+
}
|
|
2461
|
+
if (error.code === "NETWORK_ERROR" || error.code === "TIMEOUT_ERROR") {
|
|
2462
|
+
return true;
|
|
2463
|
+
}
|
|
2464
|
+
return error.code === "HTTP_ERROR" && (error.status === 429 || error.status !== void 0 && error.status >= 500);
|
|
2465
|
+
}
|
|
2466
|
+
|
|
1904
2467
|
// src/plugins/xhrTransport.ts
|
|
1905
2468
|
function xhrRequest(config, options = {}) {
|
|
1906
2469
|
let request;
|
|
@@ -1950,7 +2513,7 @@ function xhrRequest(config, options = {}) {
|
|
|
1950
2513
|
};
|
|
1951
2514
|
const onSignalAbort = () => {
|
|
1952
2515
|
abortWith(
|
|
1953
|
-
|
|
2516
|
+
createAbortError3(signal?.reason, config)
|
|
1954
2517
|
);
|
|
1955
2518
|
};
|
|
1956
2519
|
xhr.onload = () => {
|
|
@@ -1969,7 +2532,7 @@ function xhrRequest(config, options = {}) {
|
|
|
1969
2532
|
};
|
|
1970
2533
|
xhr.onabort = () => {
|
|
1971
2534
|
rejectOnce(
|
|
1972
|
-
|
|
2535
|
+
createAbortError3(signal?.reason, config)
|
|
1973
2536
|
);
|
|
1974
2537
|
};
|
|
1975
2538
|
xhr.onprogress = createProgressHandler(
|
|
@@ -2059,7 +2622,7 @@ async function processResponse(xhr, config) {
|
|
|
2059
2622
|
config,
|
|
2060
2623
|
raw
|
|
2061
2624
|
};
|
|
2062
|
-
const validateStatus = config.validateStatus ??
|
|
2625
|
+
const validateStatus = config.validateStatus ?? defaultValidateStatus3;
|
|
2063
2626
|
if (!validateStatus(xhr.status)) {
|
|
2064
2627
|
throw new RequestError(
|
|
2065
2628
|
xhr.statusText || "Request failed",
|
|
@@ -2127,7 +2690,7 @@ function parseHeaders(value) {
|
|
|
2127
2690
|
}
|
|
2128
2691
|
return headers;
|
|
2129
2692
|
}
|
|
2130
|
-
function
|
|
2693
|
+
function createAbortError3(reason, config) {
|
|
2131
2694
|
if (reason instanceof RequestError) {
|
|
2132
2695
|
return new RequestError(reason.message, {
|
|
2133
2696
|
code: reason.code,
|
|
@@ -2157,7 +2720,7 @@ function createConfigError(config, cause) {
|
|
|
2157
2720
|
function isNullBodyStatus(status) {
|
|
2158
2721
|
return status === 204 || status === 205 || status === 304;
|
|
2159
2722
|
}
|
|
2160
|
-
function
|
|
2723
|
+
function defaultValidateStatus3(status) {
|
|
2161
2724
|
return status >= 200 && status < 300;
|
|
2162
2725
|
}
|
|
2163
2726
|
|
|
@@ -2630,7 +3193,7 @@ function normalizeRetryOptions(retry, defaults = {}) {
|
|
|
2630
3193
|
return {
|
|
2631
3194
|
retries: normalizeRetries(retry),
|
|
2632
3195
|
methods: normalizeMethods(defaults.methods),
|
|
2633
|
-
delay:
|
|
3196
|
+
delay: normalizeDelay2(defaults.delay),
|
|
2634
3197
|
respectRetryAfter: defaults.respectRetryAfter ?? true,
|
|
2635
3198
|
maxDelay: normalizeMaxDelay(defaults.maxDelay),
|
|
2636
3199
|
jitter: defaults.jitter ?? false,
|
|
@@ -2648,7 +3211,7 @@ function normalizeRetryOptions(retry, defaults = {}) {
|
|
|
2648
3211
|
methods: normalizeMethods(
|
|
2649
3212
|
retry?.methods ?? defaults.methods
|
|
2650
3213
|
),
|
|
2651
|
-
delay:
|
|
3214
|
+
delay: normalizeDelay2(
|
|
2652
3215
|
retry?.delay ?? defaults.delay
|
|
2653
3216
|
),
|
|
2654
3217
|
respectRetryAfter: retry?.respectRetryAfter ?? defaults.respectRetryAfter ?? true,
|
|
@@ -2690,7 +3253,7 @@ function normalizeMaxElapsedTime(maxElapsedTime) {
|
|
|
2690
3253
|
}
|
|
2691
3254
|
return Math.max(0, maxElapsedTime);
|
|
2692
3255
|
}
|
|
2693
|
-
function
|
|
3256
|
+
function normalizeDelay2(delay) {
|
|
2694
3257
|
if (typeof delay === "number") {
|
|
2695
3258
|
const normalizedDelay = Math.max(0, delay);
|
|
2696
3259
|
return () => normalizedDelay;
|
|
@@ -2830,6 +3393,7 @@ function uploadPlugin() {
|
|
|
2830
3393
|
RequestError,
|
|
2831
3394
|
authPlugin,
|
|
2832
3395
|
cachePlugin,
|
|
3396
|
+
circuitBreakerPlugin,
|
|
2833
3397
|
createClient,
|
|
2834
3398
|
downloadPlugin,
|
|
2835
3399
|
loggerPlugin,
|