@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.js
CHANGED
|
@@ -430,35 +430,370 @@ function defaultValidateStatus(status) {
|
|
|
430
430
|
var MockAdapter = class {
|
|
431
431
|
constructor(options = {}) {
|
|
432
432
|
this.handlers = /* @__PURE__ */ new Map();
|
|
433
|
+
this.rules = [];
|
|
434
|
+
this.requests = [];
|
|
435
|
+
this.delay = normalizeDelay(options.delay);
|
|
433
436
|
Object.entries(options.handlers ?? {}).forEach(([key, handler]) => {
|
|
434
437
|
this.handlers.set(key, handler);
|
|
435
438
|
});
|
|
436
439
|
}
|
|
440
|
+
get history() {
|
|
441
|
+
return [...this.requests];
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Register a legacy URL-only data handler.
|
|
445
|
+
*/
|
|
437
446
|
on(url, handler) {
|
|
438
447
|
this.handlers.set(url, handler);
|
|
439
448
|
return this;
|
|
440
449
|
}
|
|
450
|
+
onMethod(method, matcher) {
|
|
451
|
+
const normalizedMatcher = normalizeMatcher(matcher);
|
|
452
|
+
const registrar = {
|
|
453
|
+
reply: (once2, replyOrStatus, data, options) => {
|
|
454
|
+
return this.registerReply(
|
|
455
|
+
method,
|
|
456
|
+
normalizedMatcher,
|
|
457
|
+
once2,
|
|
458
|
+
replyOrStatus,
|
|
459
|
+
data,
|
|
460
|
+
options
|
|
461
|
+
);
|
|
462
|
+
},
|
|
463
|
+
error: (once2, code, message) => {
|
|
464
|
+
return this.registerError(
|
|
465
|
+
method,
|
|
466
|
+
normalizedMatcher,
|
|
467
|
+
once2,
|
|
468
|
+
code,
|
|
469
|
+
message
|
|
470
|
+
);
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
return new RouteBuilder(registrar);
|
|
474
|
+
}
|
|
475
|
+
onGet(matcher) {
|
|
476
|
+
return this.onMethod("GET", matcher);
|
|
477
|
+
}
|
|
478
|
+
onPost(matcher) {
|
|
479
|
+
return this.onMethod("POST", matcher);
|
|
480
|
+
}
|
|
481
|
+
onPut(matcher) {
|
|
482
|
+
return this.onMethod("PUT", matcher);
|
|
483
|
+
}
|
|
484
|
+
onPatch(matcher) {
|
|
485
|
+
return this.onMethod("PATCH", matcher);
|
|
486
|
+
}
|
|
487
|
+
onDelete(matcher) {
|
|
488
|
+
return this.onMethod("DELETE", matcher);
|
|
489
|
+
}
|
|
490
|
+
onHead(matcher) {
|
|
491
|
+
return this.onMethod("HEAD", matcher);
|
|
492
|
+
}
|
|
493
|
+
onOptions(matcher) {
|
|
494
|
+
return this.onMethod("OPTIONS", matcher);
|
|
495
|
+
}
|
|
496
|
+
resetHistory() {
|
|
497
|
+
this.requests.length = 0;
|
|
498
|
+
return this;
|
|
499
|
+
}
|
|
500
|
+
resetHandlers() {
|
|
501
|
+
this.handlers.clear();
|
|
502
|
+
this.rules.length = 0;
|
|
503
|
+
return this;
|
|
504
|
+
}
|
|
505
|
+
reset() {
|
|
506
|
+
return this.resetHandlers().resetHistory();
|
|
507
|
+
}
|
|
441
508
|
async request(config) {
|
|
509
|
+
this.requests.push(config);
|
|
510
|
+
const method = config.method ?? "GET";
|
|
511
|
+
const ruleIndex = this.rules.findIndex((rule) => {
|
|
512
|
+
return (rule.method === void 0 || rule.method === method) && matchesRequest(rule.matcher, config);
|
|
513
|
+
});
|
|
514
|
+
if (ruleIndex !== -1) {
|
|
515
|
+
const rule = this.rules[ruleIndex];
|
|
516
|
+
if (rule.once) {
|
|
517
|
+
this.rules.splice(ruleIndex, 1);
|
|
518
|
+
}
|
|
519
|
+
const response = await rule.respond(config);
|
|
520
|
+
return validateMockResponse(response, config);
|
|
521
|
+
}
|
|
442
522
|
const handler = this.handlers.get(config.url);
|
|
443
523
|
if (!handler) {
|
|
444
|
-
throw new Error(
|
|
524
|
+
throw new Error(
|
|
525
|
+
`No mock handler found for ${config.url} (${method})`
|
|
526
|
+
);
|
|
445
527
|
}
|
|
528
|
+
await waitForDelay(this.delay, config);
|
|
446
529
|
const data = await handler(config);
|
|
447
|
-
return {
|
|
448
|
-
data,
|
|
530
|
+
return validateMockResponse(createMockResponse(config, {
|
|
449
531
|
status: 200,
|
|
450
532
|
statusText: "OK",
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
533
|
+
data
|
|
534
|
+
}), config);
|
|
535
|
+
}
|
|
536
|
+
registerReply(method, matcher, once2, replyOrStatus, data, options = {}) {
|
|
537
|
+
const handler = typeof replyOrStatus === "function" ? replyOrStatus : () => ({
|
|
538
|
+
status: replyOrStatus,
|
|
539
|
+
data,
|
|
540
|
+
...options
|
|
541
|
+
});
|
|
542
|
+
this.rules.push({
|
|
543
|
+
method,
|
|
544
|
+
matcher,
|
|
545
|
+
once: once2,
|
|
546
|
+
respond: async (config) => {
|
|
547
|
+
const reply = await handler(config);
|
|
548
|
+
await waitForDelay(
|
|
549
|
+
normalizeDelay(reply.delay ?? this.delay),
|
|
550
|
+
config
|
|
551
|
+
);
|
|
552
|
+
return createMockResponse(config, reply);
|
|
553
|
+
}
|
|
554
|
+
});
|
|
555
|
+
return this;
|
|
556
|
+
}
|
|
557
|
+
registerError(method, matcher, once2, code, message) {
|
|
558
|
+
this.rules.push({
|
|
559
|
+
method,
|
|
560
|
+
matcher,
|
|
561
|
+
once: once2,
|
|
562
|
+
respond: async (config) => {
|
|
563
|
+
await waitForDelay(this.delay, config);
|
|
564
|
+
throw new RequestError(message, {
|
|
565
|
+
code,
|
|
566
|
+
config
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
});
|
|
570
|
+
return this;
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
var RouteBuilder = class {
|
|
574
|
+
constructor(registrar) {
|
|
575
|
+
this.registrar = registrar;
|
|
576
|
+
}
|
|
577
|
+
reply(replyOrStatus, data, options = {}) {
|
|
578
|
+
return this.registrar.reply(
|
|
579
|
+
false,
|
|
580
|
+
replyOrStatus,
|
|
581
|
+
data,
|
|
582
|
+
options
|
|
583
|
+
);
|
|
584
|
+
}
|
|
585
|
+
replyOnce(replyOrStatus, data, options = {}) {
|
|
586
|
+
return this.registrar.reply(
|
|
587
|
+
true,
|
|
588
|
+
replyOrStatus,
|
|
589
|
+
data,
|
|
590
|
+
options
|
|
591
|
+
);
|
|
592
|
+
}
|
|
593
|
+
networkError(message = "Mock network error") {
|
|
594
|
+
return this.registrar.error(
|
|
595
|
+
false,
|
|
596
|
+
"NETWORK_ERROR",
|
|
597
|
+
message
|
|
598
|
+
);
|
|
599
|
+
}
|
|
600
|
+
networkErrorOnce(message = "Mock network error") {
|
|
601
|
+
return this.registrar.error(
|
|
602
|
+
true,
|
|
603
|
+
"NETWORK_ERROR",
|
|
604
|
+
message
|
|
605
|
+
);
|
|
606
|
+
}
|
|
607
|
+
timeout(message = "Mock request timeout") {
|
|
608
|
+
return this.registrar.error(
|
|
609
|
+
false,
|
|
610
|
+
"TIMEOUT_ERROR",
|
|
611
|
+
message
|
|
612
|
+
);
|
|
613
|
+
}
|
|
614
|
+
timeoutOnce(message = "Mock request timeout") {
|
|
615
|
+
return this.registrar.error(
|
|
616
|
+
true,
|
|
617
|
+
"TIMEOUT_ERROR",
|
|
618
|
+
message
|
|
619
|
+
);
|
|
620
|
+
}
|
|
621
|
+
};
|
|
622
|
+
function normalizeMatcher(matcher) {
|
|
623
|
+
if (typeof matcher === "string" || matcher instanceof RegExp) {
|
|
624
|
+
return {
|
|
625
|
+
url: matcher
|
|
459
626
|
};
|
|
460
627
|
}
|
|
628
|
+
return matcher;
|
|
629
|
+
}
|
|
630
|
+
function matchesRequest(matcher, config) {
|
|
631
|
+
return matchesURL(matcher.url, config.url) && matchesQuery(matcher.query, config.query) && matchesHeaders(matcher.headers, config.headers);
|
|
632
|
+
}
|
|
633
|
+
function matchesURL(matcher, url) {
|
|
634
|
+
if (typeof matcher === "string") {
|
|
635
|
+
return matcher === url;
|
|
636
|
+
}
|
|
637
|
+
matcher.lastIndex = 0;
|
|
638
|
+
return matcher.test(url);
|
|
639
|
+
}
|
|
640
|
+
function matchesQuery(expected, actual) {
|
|
641
|
+
if (!expected) {
|
|
642
|
+
return true;
|
|
643
|
+
}
|
|
644
|
+
return normalizeQuery(expected) === normalizeQuery(actual);
|
|
645
|
+
}
|
|
646
|
+
function normalizeQuery(query) {
|
|
647
|
+
if (!query) {
|
|
648
|
+
return "[]";
|
|
649
|
+
}
|
|
650
|
+
const entries = [];
|
|
651
|
+
for (const key of Object.keys(query).sort()) {
|
|
652
|
+
const value = query[key];
|
|
653
|
+
const values = Array.isArray(value) ? value : [value];
|
|
654
|
+
for (const item of values) {
|
|
655
|
+
if (item !== null && item !== void 0) {
|
|
656
|
+
entries.push([key, String(item)]);
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
return JSON.stringify(entries);
|
|
661
|
+
}
|
|
662
|
+
function matchesHeaders(expected, actual) {
|
|
663
|
+
if (!expected) {
|
|
664
|
+
return true;
|
|
665
|
+
}
|
|
666
|
+
const expectedHeaders = new Headers(expected);
|
|
667
|
+
const actualHeaders = new Headers(actual);
|
|
668
|
+
for (const [name, value] of expectedHeaders.entries()) {
|
|
669
|
+
if (actualHeaders.get(name) !== value) {
|
|
670
|
+
return false;
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
return true;
|
|
674
|
+
}
|
|
675
|
+
function createMockResponse(config, reply) {
|
|
676
|
+
const headers = new Headers(reply.headers);
|
|
677
|
+
const rawBody = createRawBody(reply.data, headers, reply.status);
|
|
678
|
+
const statusText = reply.statusText ?? defaultStatusText(reply.status);
|
|
679
|
+
const raw = new Response(rawBody, {
|
|
680
|
+
status: reply.status,
|
|
681
|
+
statusText,
|
|
682
|
+
headers
|
|
683
|
+
});
|
|
684
|
+
return {
|
|
685
|
+
data: reply.data,
|
|
686
|
+
status: reply.status,
|
|
687
|
+
statusText,
|
|
688
|
+
headers: raw.headers,
|
|
689
|
+
config,
|
|
690
|
+
raw
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
function validateMockResponse(response, config) {
|
|
694
|
+
const validateStatus = config.validateStatus ?? defaultValidateStatus2;
|
|
695
|
+
if (!validateStatus(response.status)) {
|
|
696
|
+
throw new RequestError(
|
|
697
|
+
response.statusText || "Mock request failed",
|
|
698
|
+
{
|
|
699
|
+
code: "HTTP_ERROR",
|
|
700
|
+
response
|
|
701
|
+
}
|
|
702
|
+
);
|
|
703
|
+
}
|
|
704
|
+
return response;
|
|
705
|
+
}
|
|
706
|
+
function defaultValidateStatus2(status) {
|
|
707
|
+
return status >= 200 && status < 300;
|
|
708
|
+
}
|
|
709
|
+
function defaultStatusText(status) {
|
|
710
|
+
return STATUS_TEXT[status] ?? "";
|
|
711
|
+
}
|
|
712
|
+
var STATUS_TEXT = {
|
|
713
|
+
200: "OK",
|
|
714
|
+
201: "Created",
|
|
715
|
+
202: "Accepted",
|
|
716
|
+
204: "No Content",
|
|
717
|
+
400: "Bad Request",
|
|
718
|
+
401: "Unauthorized",
|
|
719
|
+
403: "Forbidden",
|
|
720
|
+
404: "Not Found",
|
|
721
|
+
408: "Request Timeout",
|
|
722
|
+
409: "Conflict",
|
|
723
|
+
422: "Unprocessable Content",
|
|
724
|
+
429: "Too Many Requests",
|
|
725
|
+
500: "Internal Server Error",
|
|
726
|
+
502: "Bad Gateway",
|
|
727
|
+
503: "Service Unavailable",
|
|
728
|
+
504: "Gateway Timeout"
|
|
461
729
|
};
|
|
730
|
+
function createRawBody(data, headers, status) {
|
|
731
|
+
if (data === void 0 || data === null || status === 204 || status === 205 || status === 304) {
|
|
732
|
+
return null;
|
|
733
|
+
}
|
|
734
|
+
if (typeof data === "string" || isBodyInit(data)) {
|
|
735
|
+
return data;
|
|
736
|
+
}
|
|
737
|
+
if (!headers.has("content-type")) {
|
|
738
|
+
headers.set("content-type", "application/json");
|
|
739
|
+
}
|
|
740
|
+
return JSON.stringify(data);
|
|
741
|
+
}
|
|
742
|
+
function isBodyInit(value) {
|
|
743
|
+
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);
|
|
744
|
+
}
|
|
745
|
+
function normalizeDelay(delay) {
|
|
746
|
+
if (!Number.isFinite(delay)) {
|
|
747
|
+
return delay && delay > 0 ? MAX_TIMER_DELAY : 0;
|
|
748
|
+
}
|
|
749
|
+
return Math.min(Math.max(0, delay ?? 0), MAX_TIMER_DELAY);
|
|
750
|
+
}
|
|
751
|
+
var MAX_TIMER_DELAY = 2147483647;
|
|
752
|
+
function waitForDelay(delay, config) {
|
|
753
|
+
const signal = config.signal;
|
|
754
|
+
if (signal?.aborted) {
|
|
755
|
+
return Promise.reject(createAbortError(signal.reason, config));
|
|
756
|
+
}
|
|
757
|
+
const timeout = config.timeout && config.timeout > 0 ? config.timeout : void 0;
|
|
758
|
+
const timeoutWins = timeout !== void 0 && timeout <= delay;
|
|
759
|
+
const wait = timeoutWins ? timeout : delay;
|
|
760
|
+
if (wait <= 0) {
|
|
761
|
+
return Promise.resolve();
|
|
762
|
+
}
|
|
763
|
+
return new Promise((resolve, reject) => {
|
|
764
|
+
const timer = setTimeout(() => {
|
|
765
|
+
cleanup();
|
|
766
|
+
if (timeoutWins) {
|
|
767
|
+
reject(
|
|
768
|
+
new RequestError(`Request timeout after ${timeout}ms`, {
|
|
769
|
+
code: "TIMEOUT_ERROR",
|
|
770
|
+
config
|
|
771
|
+
})
|
|
772
|
+
);
|
|
773
|
+
} else {
|
|
774
|
+
resolve();
|
|
775
|
+
}
|
|
776
|
+
}, wait);
|
|
777
|
+
const onAbort = () => {
|
|
778
|
+
clearTimeout(timer);
|
|
779
|
+
cleanup();
|
|
780
|
+
reject(createAbortError(signal?.reason, config));
|
|
781
|
+
};
|
|
782
|
+
const cleanup = () => {
|
|
783
|
+
signal?.removeEventListener("abort", onAbort);
|
|
784
|
+
};
|
|
785
|
+
signal?.addEventListener("abort", onAbort, {
|
|
786
|
+
once: true
|
|
787
|
+
});
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
function createAbortError(reason, config) {
|
|
791
|
+
return new RequestError("Mock request aborted", {
|
|
792
|
+
code: "ABORT_ERROR",
|
|
793
|
+
config,
|
|
794
|
+
cause: reason
|
|
795
|
+
});
|
|
796
|
+
}
|
|
462
797
|
|
|
463
798
|
// src/core/ConfigMerger.ts
|
|
464
799
|
var ConfigMerger = class {
|
|
@@ -782,7 +1117,7 @@ var Pipeline = class {
|
|
|
782
1117
|
function waitForRetry(milliseconds, config) {
|
|
783
1118
|
const signal = config.signal;
|
|
784
1119
|
if (signal?.aborted) {
|
|
785
|
-
return Promise.reject(
|
|
1120
|
+
return Promise.reject(createAbortError2(signal.reason, config));
|
|
786
1121
|
}
|
|
787
1122
|
if (milliseconds <= 0) {
|
|
788
1123
|
return Promise.resolve();
|
|
@@ -795,7 +1130,7 @@ function waitForRetry(milliseconds, config) {
|
|
|
795
1130
|
const onAbort = () => {
|
|
796
1131
|
clearTimeout(timer);
|
|
797
1132
|
cleanup();
|
|
798
|
-
reject(
|
|
1133
|
+
reject(createAbortError2(signal?.reason, config));
|
|
799
1134
|
};
|
|
800
1135
|
const cleanup = () => {
|
|
801
1136
|
signal?.removeEventListener("abort", onAbort);
|
|
@@ -805,7 +1140,7 @@ function waitForRetry(milliseconds, config) {
|
|
|
805
1140
|
});
|
|
806
1141
|
});
|
|
807
1142
|
}
|
|
808
|
-
function
|
|
1143
|
+
function createAbortError2(reason, config) {
|
|
809
1144
|
if (reason instanceof RequestError) {
|
|
810
1145
|
return new RequestError(reason.message, {
|
|
811
1146
|
code: reason.code,
|
|
@@ -1799,7 +2134,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
1799
2134
|
method: config.method ?? "GET",
|
|
1800
2135
|
baseURL: config.baseURL,
|
|
1801
2136
|
url: config.url,
|
|
1802
|
-
query:
|
|
2137
|
+
query: normalizeQuery2(config.query),
|
|
1803
2138
|
responseType: config.responseType ?? "auto",
|
|
1804
2139
|
headers: Object.fromEntries(
|
|
1805
2140
|
varyHeaders.map((name) => [
|
|
@@ -1809,7 +2144,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
1809
2144
|
)
|
|
1810
2145
|
});
|
|
1811
2146
|
}
|
|
1812
|
-
function
|
|
2147
|
+
function normalizeQuery2(query) {
|
|
1813
2148
|
if (!query) {
|
|
1814
2149
|
return [];
|
|
1815
2150
|
}
|
|
@@ -1862,6 +2197,233 @@ function cloneCacheValue(value) {
|
|
|
1862
2197
|
}
|
|
1863
2198
|
}
|
|
1864
2199
|
|
|
2200
|
+
// src/plugins/circuitBreakerPlugin.ts
|
|
2201
|
+
function circuitBreakerPlugin(options = {}) {
|
|
2202
|
+
const normalized = normalizeOptions(options);
|
|
2203
|
+
const circuits = /* @__PURE__ */ new Map();
|
|
2204
|
+
const plugin = {
|
|
2205
|
+
name: "circuit-breaker",
|
|
2206
|
+
getState(key) {
|
|
2207
|
+
return circuits.get(key)?.state ?? "closed";
|
|
2208
|
+
},
|
|
2209
|
+
reset(key) {
|
|
2210
|
+
if (key === void 0) {
|
|
2211
|
+
circuits.clear();
|
|
2212
|
+
return;
|
|
2213
|
+
}
|
|
2214
|
+
circuits.delete(key);
|
|
2215
|
+
},
|
|
2216
|
+
install(context) {
|
|
2217
|
+
const admissions = /* @__PURE__ */ new WeakMap();
|
|
2218
|
+
let active = true;
|
|
2219
|
+
context.hooks.onRequest((requestContext) => {
|
|
2220
|
+
const requestOptions = resolveExtensionConfig(
|
|
2221
|
+
requestContext.config,
|
|
2222
|
+
"circuitBreaker"
|
|
2223
|
+
);
|
|
2224
|
+
if (requestOptions?.enabled === false) {
|
|
2225
|
+
return;
|
|
2226
|
+
}
|
|
2227
|
+
const key = normalizeKey(
|
|
2228
|
+
requestOptions?.key ?? normalized.createKey(requestContext.config)
|
|
2229
|
+
);
|
|
2230
|
+
const record = getCircuit(circuits, key);
|
|
2231
|
+
if (record.state === "open" && Date.now() - record.openedAt >= normalized.resetTimeout) {
|
|
2232
|
+
transition(record, key, "half-open", normalized);
|
|
2233
|
+
}
|
|
2234
|
+
if (record.state === "open") {
|
|
2235
|
+
throw createOpenError(key, requestContext.config);
|
|
2236
|
+
}
|
|
2237
|
+
const probe = record.state === "half-open";
|
|
2238
|
+
if (probe && record.probes >= normalized.halfOpenMaxRequests) {
|
|
2239
|
+
throw createOpenError(key, requestContext.config);
|
|
2240
|
+
}
|
|
2241
|
+
if (probe) {
|
|
2242
|
+
record.probes += 1;
|
|
2243
|
+
}
|
|
2244
|
+
admissions.set(requestContext, {
|
|
2245
|
+
key,
|
|
2246
|
+
generation: record.generation,
|
|
2247
|
+
probe
|
|
2248
|
+
});
|
|
2249
|
+
});
|
|
2250
|
+
context.hooks.onSettled(async (requestContext) => {
|
|
2251
|
+
const admission = admissions.get(requestContext);
|
|
2252
|
+
if (!admission) {
|
|
2253
|
+
return;
|
|
2254
|
+
}
|
|
2255
|
+
admissions.delete(requestContext);
|
|
2256
|
+
if (!active) {
|
|
2257
|
+
return;
|
|
2258
|
+
}
|
|
2259
|
+
const record = circuits.get(admission.key);
|
|
2260
|
+
if (!record || record.generation !== admission.generation) {
|
|
2261
|
+
return;
|
|
2262
|
+
}
|
|
2263
|
+
if (admission.probe) {
|
|
2264
|
+
record.probes = Math.max(0, record.probes - 1);
|
|
2265
|
+
}
|
|
2266
|
+
if (!requestContext.error && requestContext.response) {
|
|
2267
|
+
record.failures = 0;
|
|
2268
|
+
if (admission.probe && record.state === "half-open") {
|
|
2269
|
+
record.successes += 1;
|
|
2270
|
+
if (record.successes >= normalized.successThreshold) {
|
|
2271
|
+
transition(record, admission.key, "closed", normalized);
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
return;
|
|
2275
|
+
}
|
|
2276
|
+
const counted = await normalized.shouldCountFailure(
|
|
2277
|
+
requestContext.error,
|
|
2278
|
+
requestContext.config
|
|
2279
|
+
);
|
|
2280
|
+
if (!active || circuits.get(admission.key) !== record || record.generation !== admission.generation || !counted) {
|
|
2281
|
+
return;
|
|
2282
|
+
}
|
|
2283
|
+
if (admission.probe && record.state === "half-open") {
|
|
2284
|
+
transition(record, admission.key, "open", normalized);
|
|
2285
|
+
return;
|
|
2286
|
+
}
|
|
2287
|
+
if (record.state === "closed") {
|
|
2288
|
+
record.failures += 1;
|
|
2289
|
+
if (record.failures >= normalized.failureThreshold) {
|
|
2290
|
+
transition(record, admission.key, "open", normalized);
|
|
2291
|
+
}
|
|
2292
|
+
}
|
|
2293
|
+
});
|
|
2294
|
+
return () => {
|
|
2295
|
+
active = false;
|
|
2296
|
+
circuits.clear();
|
|
2297
|
+
};
|
|
2298
|
+
}
|
|
2299
|
+
};
|
|
2300
|
+
return plugin;
|
|
2301
|
+
}
|
|
2302
|
+
function normalizeOptions(options) {
|
|
2303
|
+
return {
|
|
2304
|
+
failureThreshold: normalizePositiveInteger(
|
|
2305
|
+
options.failureThreshold,
|
|
2306
|
+
5
|
|
2307
|
+
),
|
|
2308
|
+
successThreshold: normalizePositiveInteger(
|
|
2309
|
+
options.successThreshold,
|
|
2310
|
+
1
|
|
2311
|
+
),
|
|
2312
|
+
resetTimeout: normalizeDuration(options.resetTimeout, 3e4),
|
|
2313
|
+
halfOpenMaxRequests: normalizePositiveInteger(
|
|
2314
|
+
options.halfOpenMaxRequests,
|
|
2315
|
+
1
|
|
2316
|
+
),
|
|
2317
|
+
createKey: options.createKey ?? createDefaultKey,
|
|
2318
|
+
shouldCountFailure: options.shouldCountFailure ?? defaultShouldCountFailure,
|
|
2319
|
+
onStateChange: options.onStateChange
|
|
2320
|
+
};
|
|
2321
|
+
}
|
|
2322
|
+
function normalizePositiveInteger(value, fallback) {
|
|
2323
|
+
if (value === void 0) {
|
|
2324
|
+
return fallback;
|
|
2325
|
+
}
|
|
2326
|
+
if (!Number.isFinite(value)) {
|
|
2327
|
+
return fallback;
|
|
2328
|
+
}
|
|
2329
|
+
return Math.max(1, Math.floor(value));
|
|
2330
|
+
}
|
|
2331
|
+
function normalizeDuration(value, fallback) {
|
|
2332
|
+
if (value === void 0) {
|
|
2333
|
+
return fallback;
|
|
2334
|
+
}
|
|
2335
|
+
if (!Number.isFinite(value)) {
|
|
2336
|
+
return value > 0 ? Number.MAX_SAFE_INTEGER : 0;
|
|
2337
|
+
}
|
|
2338
|
+
return Math.max(0, value);
|
|
2339
|
+
}
|
|
2340
|
+
function normalizeKey(key) {
|
|
2341
|
+
return key || "default";
|
|
2342
|
+
}
|
|
2343
|
+
function createDefaultKey(config) {
|
|
2344
|
+
try {
|
|
2345
|
+
return new URL(config.url).origin;
|
|
2346
|
+
} catch {
|
|
2347
|
+
}
|
|
2348
|
+
try {
|
|
2349
|
+
if (config.baseURL) {
|
|
2350
|
+
return new URL(config.url, config.baseURL).origin;
|
|
2351
|
+
}
|
|
2352
|
+
} catch {
|
|
2353
|
+
}
|
|
2354
|
+
return "default";
|
|
2355
|
+
}
|
|
2356
|
+
function getCircuit(circuits, key) {
|
|
2357
|
+
let record = circuits.get(key);
|
|
2358
|
+
if (!record) {
|
|
2359
|
+
record = {
|
|
2360
|
+
state: "closed",
|
|
2361
|
+
failures: 0,
|
|
2362
|
+
successes: 0,
|
|
2363
|
+
openedAt: 0,
|
|
2364
|
+
probes: 0,
|
|
2365
|
+
generation: 0
|
|
2366
|
+
};
|
|
2367
|
+
circuits.set(key, record);
|
|
2368
|
+
}
|
|
2369
|
+
return record;
|
|
2370
|
+
}
|
|
2371
|
+
function transition(record, key, state, options) {
|
|
2372
|
+
if (record.state === state) {
|
|
2373
|
+
if (state === "open") {
|
|
2374
|
+
record.openedAt = Date.now();
|
|
2375
|
+
record.successes = 0;
|
|
2376
|
+
record.probes = 0;
|
|
2377
|
+
record.generation += 1;
|
|
2378
|
+
}
|
|
2379
|
+
return;
|
|
2380
|
+
}
|
|
2381
|
+
const previousState = record.state;
|
|
2382
|
+
record.state = state;
|
|
2383
|
+
record.successes = 0;
|
|
2384
|
+
record.probes = 0;
|
|
2385
|
+
record.generation += 1;
|
|
2386
|
+
if (state === "open") {
|
|
2387
|
+
record.openedAt = Date.now();
|
|
2388
|
+
} else if (state === "closed") {
|
|
2389
|
+
record.failures = 0;
|
|
2390
|
+
record.openedAt = 0;
|
|
2391
|
+
}
|
|
2392
|
+
notifyStateChange(options.onStateChange, {
|
|
2393
|
+
key,
|
|
2394
|
+
previousState,
|
|
2395
|
+
state,
|
|
2396
|
+
timestamp: Date.now(),
|
|
2397
|
+
failures: record.failures
|
|
2398
|
+
});
|
|
2399
|
+
}
|
|
2400
|
+
function notifyStateChange(observer, event) {
|
|
2401
|
+
if (!observer) {
|
|
2402
|
+
return;
|
|
2403
|
+
}
|
|
2404
|
+
try {
|
|
2405
|
+
void Promise.resolve(observer(event)).catch(ignoreObserverError);
|
|
2406
|
+
} catch {
|
|
2407
|
+
}
|
|
2408
|
+
}
|
|
2409
|
+
function ignoreObserverError() {
|
|
2410
|
+
}
|
|
2411
|
+
function createOpenError(key, config) {
|
|
2412
|
+
return new RequestError(`Circuit is open for ${key}`, {
|
|
2413
|
+
code: "CIRCUIT_OPEN",
|
|
2414
|
+
config
|
|
2415
|
+
});
|
|
2416
|
+
}
|
|
2417
|
+
function defaultShouldCountFailure(error) {
|
|
2418
|
+
if (!(error instanceof RequestError)) {
|
|
2419
|
+
return true;
|
|
2420
|
+
}
|
|
2421
|
+
if (error.code === "NETWORK_ERROR" || error.code === "TIMEOUT_ERROR") {
|
|
2422
|
+
return true;
|
|
2423
|
+
}
|
|
2424
|
+
return error.code === "HTTP_ERROR" && (error.status === 429 || error.status !== void 0 && error.status >= 500);
|
|
2425
|
+
}
|
|
2426
|
+
|
|
1865
2427
|
// src/plugins/xhrTransport.ts
|
|
1866
2428
|
function xhrRequest(config, options = {}) {
|
|
1867
2429
|
let request;
|
|
@@ -1911,7 +2473,7 @@ function xhrRequest(config, options = {}) {
|
|
|
1911
2473
|
};
|
|
1912
2474
|
const onSignalAbort = () => {
|
|
1913
2475
|
abortWith(
|
|
1914
|
-
|
|
2476
|
+
createAbortError3(signal?.reason, config)
|
|
1915
2477
|
);
|
|
1916
2478
|
};
|
|
1917
2479
|
xhr.onload = () => {
|
|
@@ -1930,7 +2492,7 @@ function xhrRequest(config, options = {}) {
|
|
|
1930
2492
|
};
|
|
1931
2493
|
xhr.onabort = () => {
|
|
1932
2494
|
rejectOnce(
|
|
1933
|
-
|
|
2495
|
+
createAbortError3(signal?.reason, config)
|
|
1934
2496
|
);
|
|
1935
2497
|
};
|
|
1936
2498
|
xhr.onprogress = createProgressHandler(
|
|
@@ -2020,7 +2582,7 @@ async function processResponse(xhr, config) {
|
|
|
2020
2582
|
config,
|
|
2021
2583
|
raw
|
|
2022
2584
|
};
|
|
2023
|
-
const validateStatus = config.validateStatus ??
|
|
2585
|
+
const validateStatus = config.validateStatus ?? defaultValidateStatus3;
|
|
2024
2586
|
if (!validateStatus(xhr.status)) {
|
|
2025
2587
|
throw new RequestError(
|
|
2026
2588
|
xhr.statusText || "Request failed",
|
|
@@ -2088,7 +2650,7 @@ function parseHeaders(value) {
|
|
|
2088
2650
|
}
|
|
2089
2651
|
return headers;
|
|
2090
2652
|
}
|
|
2091
|
-
function
|
|
2653
|
+
function createAbortError3(reason, config) {
|
|
2092
2654
|
if (reason instanceof RequestError) {
|
|
2093
2655
|
return new RequestError(reason.message, {
|
|
2094
2656
|
code: reason.code,
|
|
@@ -2118,7 +2680,7 @@ function createConfigError(config, cause) {
|
|
|
2118
2680
|
function isNullBodyStatus(status) {
|
|
2119
2681
|
return status === 204 || status === 205 || status === 304;
|
|
2120
2682
|
}
|
|
2121
|
-
function
|
|
2683
|
+
function defaultValidateStatus3(status) {
|
|
2122
2684
|
return status >= 200 && status < 300;
|
|
2123
2685
|
}
|
|
2124
2686
|
|
|
@@ -2591,7 +3153,7 @@ function normalizeRetryOptions(retry, defaults = {}) {
|
|
|
2591
3153
|
return {
|
|
2592
3154
|
retries: normalizeRetries(retry),
|
|
2593
3155
|
methods: normalizeMethods(defaults.methods),
|
|
2594
|
-
delay:
|
|
3156
|
+
delay: normalizeDelay2(defaults.delay),
|
|
2595
3157
|
respectRetryAfter: defaults.respectRetryAfter ?? true,
|
|
2596
3158
|
maxDelay: normalizeMaxDelay(defaults.maxDelay),
|
|
2597
3159
|
jitter: defaults.jitter ?? false,
|
|
@@ -2609,7 +3171,7 @@ function normalizeRetryOptions(retry, defaults = {}) {
|
|
|
2609
3171
|
methods: normalizeMethods(
|
|
2610
3172
|
retry?.methods ?? defaults.methods
|
|
2611
3173
|
),
|
|
2612
|
-
delay:
|
|
3174
|
+
delay: normalizeDelay2(
|
|
2613
3175
|
retry?.delay ?? defaults.delay
|
|
2614
3176
|
),
|
|
2615
3177
|
respectRetryAfter: retry?.respectRetryAfter ?? defaults.respectRetryAfter ?? true,
|
|
@@ -2651,7 +3213,7 @@ function normalizeMaxElapsedTime(maxElapsedTime) {
|
|
|
2651
3213
|
}
|
|
2652
3214
|
return Math.max(0, maxElapsedTime);
|
|
2653
3215
|
}
|
|
2654
|
-
function
|
|
3216
|
+
function normalizeDelay2(delay) {
|
|
2655
3217
|
if (typeof delay === "number") {
|
|
2656
3218
|
const normalizedDelay = Math.max(0, delay);
|
|
2657
3219
|
return () => normalizedDelay;
|
|
@@ -2790,6 +3352,7 @@ export {
|
|
|
2790
3352
|
RequestError,
|
|
2791
3353
|
authPlugin,
|
|
2792
3354
|
cachePlugin,
|
|
3355
|
+
circuitBreakerPlugin,
|
|
2793
3356
|
createClient,
|
|
2794
3357
|
downloadPlugin,
|
|
2795
3358
|
loggerPlugin,
|