@npora/request 1.3.0 → 1.4.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 +18 -0
- package/dist/index.cjs +359 -24
- package/dist/index.d.cts +56 -1
- package/dist/index.d.ts +56 -1
- package/dist/index.js +359 -24
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -200,6 +200,24 @@ api.interceptors.error.use(error => error)
|
|
|
200
200
|
An optional numeric priority controls execution order. Higher priorities run
|
|
201
201
|
first; equal priorities preserve registration order.
|
|
202
202
|
|
|
203
|
+
## Request testing
|
|
204
|
+
|
|
205
|
+
`MockAdapter` supports method-aware routes, dynamic or one-time responses,
|
|
206
|
+
query/header matching, delay, timeout and network-error simulation, and request
|
|
207
|
+
history:
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
const adapter = new MockAdapter()
|
|
211
|
+
|
|
212
|
+
adapter
|
|
213
|
+
.onGet('/users/1')
|
|
214
|
+
.replyOnce(503, { message: 'busy' })
|
|
215
|
+
.onGet('/users/1')
|
|
216
|
+
.reply(200, { id: 1, name: 'Npora' })
|
|
217
|
+
|
|
218
|
+
const api = createClient({ adapter })
|
|
219
|
+
```
|
|
220
|
+
|
|
203
221
|
## Errors
|
|
204
222
|
|
|
205
223
|
```ts
|
package/dist/index.cjs
CHANGED
|
@@ -469,35 +469,370 @@ function defaultValidateStatus(status) {
|
|
|
469
469
|
var MockAdapter = class {
|
|
470
470
|
constructor(options = {}) {
|
|
471
471
|
this.handlers = /* @__PURE__ */ new Map();
|
|
472
|
+
this.rules = [];
|
|
473
|
+
this.requests = [];
|
|
474
|
+
this.delay = normalizeDelay(options.delay);
|
|
472
475
|
Object.entries(options.handlers ?? {}).forEach(([key, handler]) => {
|
|
473
476
|
this.handlers.set(key, handler);
|
|
474
477
|
});
|
|
475
478
|
}
|
|
479
|
+
get history() {
|
|
480
|
+
return [...this.requests];
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Register a legacy URL-only data handler.
|
|
484
|
+
*/
|
|
476
485
|
on(url, handler) {
|
|
477
486
|
this.handlers.set(url, handler);
|
|
478
487
|
return this;
|
|
479
488
|
}
|
|
489
|
+
onMethod(method, matcher) {
|
|
490
|
+
const normalizedMatcher = normalizeMatcher(matcher);
|
|
491
|
+
const registrar = {
|
|
492
|
+
reply: (once2, replyOrStatus, data, options) => {
|
|
493
|
+
return this.registerReply(
|
|
494
|
+
method,
|
|
495
|
+
normalizedMatcher,
|
|
496
|
+
once2,
|
|
497
|
+
replyOrStatus,
|
|
498
|
+
data,
|
|
499
|
+
options
|
|
500
|
+
);
|
|
501
|
+
},
|
|
502
|
+
error: (once2, code, message) => {
|
|
503
|
+
return this.registerError(
|
|
504
|
+
method,
|
|
505
|
+
normalizedMatcher,
|
|
506
|
+
once2,
|
|
507
|
+
code,
|
|
508
|
+
message
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
return new RouteBuilder(registrar);
|
|
513
|
+
}
|
|
514
|
+
onGet(matcher) {
|
|
515
|
+
return this.onMethod("GET", matcher);
|
|
516
|
+
}
|
|
517
|
+
onPost(matcher) {
|
|
518
|
+
return this.onMethod("POST", matcher);
|
|
519
|
+
}
|
|
520
|
+
onPut(matcher) {
|
|
521
|
+
return this.onMethod("PUT", matcher);
|
|
522
|
+
}
|
|
523
|
+
onPatch(matcher) {
|
|
524
|
+
return this.onMethod("PATCH", matcher);
|
|
525
|
+
}
|
|
526
|
+
onDelete(matcher) {
|
|
527
|
+
return this.onMethod("DELETE", matcher);
|
|
528
|
+
}
|
|
529
|
+
onHead(matcher) {
|
|
530
|
+
return this.onMethod("HEAD", matcher);
|
|
531
|
+
}
|
|
532
|
+
onOptions(matcher) {
|
|
533
|
+
return this.onMethod("OPTIONS", matcher);
|
|
534
|
+
}
|
|
535
|
+
resetHistory() {
|
|
536
|
+
this.requests.length = 0;
|
|
537
|
+
return this;
|
|
538
|
+
}
|
|
539
|
+
resetHandlers() {
|
|
540
|
+
this.handlers.clear();
|
|
541
|
+
this.rules.length = 0;
|
|
542
|
+
return this;
|
|
543
|
+
}
|
|
544
|
+
reset() {
|
|
545
|
+
return this.resetHandlers().resetHistory();
|
|
546
|
+
}
|
|
480
547
|
async request(config) {
|
|
548
|
+
this.requests.push(config);
|
|
549
|
+
const method = config.method ?? "GET";
|
|
550
|
+
const ruleIndex = this.rules.findIndex((rule) => {
|
|
551
|
+
return (rule.method === void 0 || rule.method === method) && matchesRequest(rule.matcher, config);
|
|
552
|
+
});
|
|
553
|
+
if (ruleIndex !== -1) {
|
|
554
|
+
const rule = this.rules[ruleIndex];
|
|
555
|
+
if (rule.once) {
|
|
556
|
+
this.rules.splice(ruleIndex, 1);
|
|
557
|
+
}
|
|
558
|
+
const response = await rule.respond(config);
|
|
559
|
+
return validateMockResponse(response, config);
|
|
560
|
+
}
|
|
481
561
|
const handler = this.handlers.get(config.url);
|
|
482
562
|
if (!handler) {
|
|
483
|
-
throw new Error(
|
|
563
|
+
throw new Error(
|
|
564
|
+
`No mock handler found for ${config.url} (${method})`
|
|
565
|
+
);
|
|
484
566
|
}
|
|
567
|
+
await waitForDelay(this.delay, config);
|
|
485
568
|
const data = await handler(config);
|
|
486
|
-
return {
|
|
487
|
-
data,
|
|
569
|
+
return validateMockResponse(createMockResponse(config, {
|
|
488
570
|
status: 200,
|
|
489
571
|
statusText: "OK",
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
572
|
+
data
|
|
573
|
+
}), config);
|
|
574
|
+
}
|
|
575
|
+
registerReply(method, matcher, once2, replyOrStatus, data, options = {}) {
|
|
576
|
+
const handler = typeof replyOrStatus === "function" ? replyOrStatus : () => ({
|
|
577
|
+
status: replyOrStatus,
|
|
578
|
+
data,
|
|
579
|
+
...options
|
|
580
|
+
});
|
|
581
|
+
this.rules.push({
|
|
582
|
+
method,
|
|
583
|
+
matcher,
|
|
584
|
+
once: once2,
|
|
585
|
+
respond: async (config) => {
|
|
586
|
+
const reply = await handler(config);
|
|
587
|
+
await waitForDelay(
|
|
588
|
+
normalizeDelay(reply.delay ?? this.delay),
|
|
589
|
+
config
|
|
590
|
+
);
|
|
591
|
+
return createMockResponse(config, reply);
|
|
592
|
+
}
|
|
593
|
+
});
|
|
594
|
+
return this;
|
|
595
|
+
}
|
|
596
|
+
registerError(method, matcher, once2, code, message) {
|
|
597
|
+
this.rules.push({
|
|
598
|
+
method,
|
|
599
|
+
matcher,
|
|
600
|
+
once: once2,
|
|
601
|
+
respond: async (config) => {
|
|
602
|
+
await waitForDelay(this.delay, config);
|
|
603
|
+
throw new RequestError(message, {
|
|
604
|
+
code,
|
|
605
|
+
config
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
});
|
|
609
|
+
return this;
|
|
610
|
+
}
|
|
611
|
+
};
|
|
612
|
+
var RouteBuilder = class {
|
|
613
|
+
constructor(registrar) {
|
|
614
|
+
this.registrar = registrar;
|
|
615
|
+
}
|
|
616
|
+
reply(replyOrStatus, data, options = {}) {
|
|
617
|
+
return this.registrar.reply(
|
|
618
|
+
false,
|
|
619
|
+
replyOrStatus,
|
|
620
|
+
data,
|
|
621
|
+
options
|
|
622
|
+
);
|
|
623
|
+
}
|
|
624
|
+
replyOnce(replyOrStatus, data, options = {}) {
|
|
625
|
+
return this.registrar.reply(
|
|
626
|
+
true,
|
|
627
|
+
replyOrStatus,
|
|
628
|
+
data,
|
|
629
|
+
options
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
networkError(message = "Mock network error") {
|
|
633
|
+
return this.registrar.error(
|
|
634
|
+
false,
|
|
635
|
+
"NETWORK_ERROR",
|
|
636
|
+
message
|
|
637
|
+
);
|
|
638
|
+
}
|
|
639
|
+
networkErrorOnce(message = "Mock network error") {
|
|
640
|
+
return this.registrar.error(
|
|
641
|
+
true,
|
|
642
|
+
"NETWORK_ERROR",
|
|
643
|
+
message
|
|
644
|
+
);
|
|
645
|
+
}
|
|
646
|
+
timeout(message = "Mock request timeout") {
|
|
647
|
+
return this.registrar.error(
|
|
648
|
+
false,
|
|
649
|
+
"TIMEOUT_ERROR",
|
|
650
|
+
message
|
|
651
|
+
);
|
|
652
|
+
}
|
|
653
|
+
timeoutOnce(message = "Mock request timeout") {
|
|
654
|
+
return this.registrar.error(
|
|
655
|
+
true,
|
|
656
|
+
"TIMEOUT_ERROR",
|
|
657
|
+
message
|
|
658
|
+
);
|
|
659
|
+
}
|
|
660
|
+
};
|
|
661
|
+
function normalizeMatcher(matcher) {
|
|
662
|
+
if (typeof matcher === "string" || matcher instanceof RegExp) {
|
|
663
|
+
return {
|
|
664
|
+
url: matcher
|
|
498
665
|
};
|
|
499
666
|
}
|
|
667
|
+
return matcher;
|
|
668
|
+
}
|
|
669
|
+
function matchesRequest(matcher, config) {
|
|
670
|
+
return matchesURL(matcher.url, config.url) && matchesQuery(matcher.query, config.query) && matchesHeaders(matcher.headers, config.headers);
|
|
671
|
+
}
|
|
672
|
+
function matchesURL(matcher, url) {
|
|
673
|
+
if (typeof matcher === "string") {
|
|
674
|
+
return matcher === url;
|
|
675
|
+
}
|
|
676
|
+
matcher.lastIndex = 0;
|
|
677
|
+
return matcher.test(url);
|
|
678
|
+
}
|
|
679
|
+
function matchesQuery(expected, actual) {
|
|
680
|
+
if (!expected) {
|
|
681
|
+
return true;
|
|
682
|
+
}
|
|
683
|
+
return normalizeQuery(expected) === normalizeQuery(actual);
|
|
684
|
+
}
|
|
685
|
+
function normalizeQuery(query) {
|
|
686
|
+
if (!query) {
|
|
687
|
+
return "[]";
|
|
688
|
+
}
|
|
689
|
+
const entries = [];
|
|
690
|
+
for (const key of Object.keys(query).sort()) {
|
|
691
|
+
const value = query[key];
|
|
692
|
+
const values = Array.isArray(value) ? value : [value];
|
|
693
|
+
for (const item of values) {
|
|
694
|
+
if (item !== null && item !== void 0) {
|
|
695
|
+
entries.push([key, String(item)]);
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
return JSON.stringify(entries);
|
|
700
|
+
}
|
|
701
|
+
function matchesHeaders(expected, actual) {
|
|
702
|
+
if (!expected) {
|
|
703
|
+
return true;
|
|
704
|
+
}
|
|
705
|
+
const expectedHeaders = new Headers(expected);
|
|
706
|
+
const actualHeaders = new Headers(actual);
|
|
707
|
+
for (const [name, value] of expectedHeaders.entries()) {
|
|
708
|
+
if (actualHeaders.get(name) !== value) {
|
|
709
|
+
return false;
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
return true;
|
|
713
|
+
}
|
|
714
|
+
function createMockResponse(config, reply) {
|
|
715
|
+
const headers = new Headers(reply.headers);
|
|
716
|
+
const rawBody = createRawBody(reply.data, headers, reply.status);
|
|
717
|
+
const statusText = reply.statusText ?? defaultStatusText(reply.status);
|
|
718
|
+
const raw = new Response(rawBody, {
|
|
719
|
+
status: reply.status,
|
|
720
|
+
statusText,
|
|
721
|
+
headers
|
|
722
|
+
});
|
|
723
|
+
return {
|
|
724
|
+
data: reply.data,
|
|
725
|
+
status: reply.status,
|
|
726
|
+
statusText,
|
|
727
|
+
headers: raw.headers,
|
|
728
|
+
config,
|
|
729
|
+
raw
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
function validateMockResponse(response, config) {
|
|
733
|
+
const validateStatus = config.validateStatus ?? defaultValidateStatus2;
|
|
734
|
+
if (!validateStatus(response.status)) {
|
|
735
|
+
throw new RequestError(
|
|
736
|
+
response.statusText || "Mock request failed",
|
|
737
|
+
{
|
|
738
|
+
code: "HTTP_ERROR",
|
|
739
|
+
response
|
|
740
|
+
}
|
|
741
|
+
);
|
|
742
|
+
}
|
|
743
|
+
return response;
|
|
744
|
+
}
|
|
745
|
+
function defaultValidateStatus2(status) {
|
|
746
|
+
return status >= 200 && status < 300;
|
|
747
|
+
}
|
|
748
|
+
function defaultStatusText(status) {
|
|
749
|
+
return STATUS_TEXT[status] ?? "";
|
|
750
|
+
}
|
|
751
|
+
var STATUS_TEXT = {
|
|
752
|
+
200: "OK",
|
|
753
|
+
201: "Created",
|
|
754
|
+
202: "Accepted",
|
|
755
|
+
204: "No Content",
|
|
756
|
+
400: "Bad Request",
|
|
757
|
+
401: "Unauthorized",
|
|
758
|
+
403: "Forbidden",
|
|
759
|
+
404: "Not Found",
|
|
760
|
+
408: "Request Timeout",
|
|
761
|
+
409: "Conflict",
|
|
762
|
+
422: "Unprocessable Content",
|
|
763
|
+
429: "Too Many Requests",
|
|
764
|
+
500: "Internal Server Error",
|
|
765
|
+
502: "Bad Gateway",
|
|
766
|
+
503: "Service Unavailable",
|
|
767
|
+
504: "Gateway Timeout"
|
|
500
768
|
};
|
|
769
|
+
function createRawBody(data, headers, status) {
|
|
770
|
+
if (data === void 0 || data === null || status === 204 || status === 205 || status === 304) {
|
|
771
|
+
return null;
|
|
772
|
+
}
|
|
773
|
+
if (typeof data === "string" || isBodyInit(data)) {
|
|
774
|
+
return data;
|
|
775
|
+
}
|
|
776
|
+
if (!headers.has("content-type")) {
|
|
777
|
+
headers.set("content-type", "application/json");
|
|
778
|
+
}
|
|
779
|
+
return JSON.stringify(data);
|
|
780
|
+
}
|
|
781
|
+
function isBodyInit(value) {
|
|
782
|
+
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);
|
|
783
|
+
}
|
|
784
|
+
function normalizeDelay(delay) {
|
|
785
|
+
if (!Number.isFinite(delay)) {
|
|
786
|
+
return delay && delay > 0 ? MAX_TIMER_DELAY : 0;
|
|
787
|
+
}
|
|
788
|
+
return Math.min(Math.max(0, delay ?? 0), MAX_TIMER_DELAY);
|
|
789
|
+
}
|
|
790
|
+
var MAX_TIMER_DELAY = 2147483647;
|
|
791
|
+
function waitForDelay(delay, config) {
|
|
792
|
+
const signal = config.signal;
|
|
793
|
+
if (signal?.aborted) {
|
|
794
|
+
return Promise.reject(createAbortError(signal.reason, config));
|
|
795
|
+
}
|
|
796
|
+
const timeout = config.timeout && config.timeout > 0 ? config.timeout : void 0;
|
|
797
|
+
const timeoutWins = timeout !== void 0 && timeout <= delay;
|
|
798
|
+
const wait = timeoutWins ? timeout : delay;
|
|
799
|
+
if (wait <= 0) {
|
|
800
|
+
return Promise.resolve();
|
|
801
|
+
}
|
|
802
|
+
return new Promise((resolve, reject) => {
|
|
803
|
+
const timer = setTimeout(() => {
|
|
804
|
+
cleanup();
|
|
805
|
+
if (timeoutWins) {
|
|
806
|
+
reject(
|
|
807
|
+
new RequestError(`Request timeout after ${timeout}ms`, {
|
|
808
|
+
code: "TIMEOUT_ERROR",
|
|
809
|
+
config
|
|
810
|
+
})
|
|
811
|
+
);
|
|
812
|
+
} else {
|
|
813
|
+
resolve();
|
|
814
|
+
}
|
|
815
|
+
}, wait);
|
|
816
|
+
const onAbort = () => {
|
|
817
|
+
clearTimeout(timer);
|
|
818
|
+
cleanup();
|
|
819
|
+
reject(createAbortError(signal?.reason, config));
|
|
820
|
+
};
|
|
821
|
+
const cleanup = () => {
|
|
822
|
+
signal?.removeEventListener("abort", onAbort);
|
|
823
|
+
};
|
|
824
|
+
signal?.addEventListener("abort", onAbort, {
|
|
825
|
+
once: true
|
|
826
|
+
});
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
function createAbortError(reason, config) {
|
|
830
|
+
return new RequestError("Mock request aborted", {
|
|
831
|
+
code: "ABORT_ERROR",
|
|
832
|
+
config,
|
|
833
|
+
cause: reason
|
|
834
|
+
});
|
|
835
|
+
}
|
|
501
836
|
|
|
502
837
|
// src/core/ConfigMerger.ts
|
|
503
838
|
var ConfigMerger = class {
|
|
@@ -821,7 +1156,7 @@ var Pipeline = class {
|
|
|
821
1156
|
function waitForRetry(milliseconds, config) {
|
|
822
1157
|
const signal = config.signal;
|
|
823
1158
|
if (signal?.aborted) {
|
|
824
|
-
return Promise.reject(
|
|
1159
|
+
return Promise.reject(createAbortError2(signal.reason, config));
|
|
825
1160
|
}
|
|
826
1161
|
if (milliseconds <= 0) {
|
|
827
1162
|
return Promise.resolve();
|
|
@@ -834,7 +1169,7 @@ function waitForRetry(milliseconds, config) {
|
|
|
834
1169
|
const onAbort = () => {
|
|
835
1170
|
clearTimeout(timer);
|
|
836
1171
|
cleanup();
|
|
837
|
-
reject(
|
|
1172
|
+
reject(createAbortError2(signal?.reason, config));
|
|
838
1173
|
};
|
|
839
1174
|
const cleanup = () => {
|
|
840
1175
|
signal?.removeEventListener("abort", onAbort);
|
|
@@ -844,7 +1179,7 @@ function waitForRetry(milliseconds, config) {
|
|
|
844
1179
|
});
|
|
845
1180
|
});
|
|
846
1181
|
}
|
|
847
|
-
function
|
|
1182
|
+
function createAbortError2(reason, config) {
|
|
848
1183
|
if (reason instanceof RequestError) {
|
|
849
1184
|
return new RequestError(reason.message, {
|
|
850
1185
|
code: reason.code,
|
|
@@ -1838,7 +2173,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
1838
2173
|
method: config.method ?? "GET",
|
|
1839
2174
|
baseURL: config.baseURL,
|
|
1840
2175
|
url: config.url,
|
|
1841
|
-
query:
|
|
2176
|
+
query: normalizeQuery2(config.query),
|
|
1842
2177
|
responseType: config.responseType ?? "auto",
|
|
1843
2178
|
headers: Object.fromEntries(
|
|
1844
2179
|
varyHeaders.map((name) => [
|
|
@@ -1848,7 +2183,7 @@ function createCacheKey(config, cache, varyHeaders) {
|
|
|
1848
2183
|
)
|
|
1849
2184
|
});
|
|
1850
2185
|
}
|
|
1851
|
-
function
|
|
2186
|
+
function normalizeQuery2(query) {
|
|
1852
2187
|
if (!query) {
|
|
1853
2188
|
return [];
|
|
1854
2189
|
}
|
|
@@ -1950,7 +2285,7 @@ function xhrRequest(config, options = {}) {
|
|
|
1950
2285
|
};
|
|
1951
2286
|
const onSignalAbort = () => {
|
|
1952
2287
|
abortWith(
|
|
1953
|
-
|
|
2288
|
+
createAbortError3(signal?.reason, config)
|
|
1954
2289
|
);
|
|
1955
2290
|
};
|
|
1956
2291
|
xhr.onload = () => {
|
|
@@ -1969,7 +2304,7 @@ function xhrRequest(config, options = {}) {
|
|
|
1969
2304
|
};
|
|
1970
2305
|
xhr.onabort = () => {
|
|
1971
2306
|
rejectOnce(
|
|
1972
|
-
|
|
2307
|
+
createAbortError3(signal?.reason, config)
|
|
1973
2308
|
);
|
|
1974
2309
|
};
|
|
1975
2310
|
xhr.onprogress = createProgressHandler(
|
|
@@ -2059,7 +2394,7 @@ async function processResponse(xhr, config) {
|
|
|
2059
2394
|
config,
|
|
2060
2395
|
raw
|
|
2061
2396
|
};
|
|
2062
|
-
const validateStatus = config.validateStatus ??
|
|
2397
|
+
const validateStatus = config.validateStatus ?? defaultValidateStatus3;
|
|
2063
2398
|
if (!validateStatus(xhr.status)) {
|
|
2064
2399
|
throw new RequestError(
|
|
2065
2400
|
xhr.statusText || "Request failed",
|
|
@@ -2127,7 +2462,7 @@ function parseHeaders(value) {
|
|
|
2127
2462
|
}
|
|
2128
2463
|
return headers;
|
|
2129
2464
|
}
|
|
2130
|
-
function
|
|
2465
|
+
function createAbortError3(reason, config) {
|
|
2131
2466
|
if (reason instanceof RequestError) {
|
|
2132
2467
|
return new RequestError(reason.message, {
|
|
2133
2468
|
code: reason.code,
|
|
@@ -2157,7 +2492,7 @@ function createConfigError(config, cause) {
|
|
|
2157
2492
|
function isNullBodyStatus(status) {
|
|
2158
2493
|
return status === 204 || status === 205 || status === 304;
|
|
2159
2494
|
}
|
|
2160
|
-
function
|
|
2495
|
+
function defaultValidateStatus3(status) {
|
|
2161
2496
|
return status >= 200 && status < 300;
|
|
2162
2497
|
}
|
|
2163
2498
|
|
|
@@ -2630,7 +2965,7 @@ function normalizeRetryOptions(retry, defaults = {}) {
|
|
|
2630
2965
|
return {
|
|
2631
2966
|
retries: normalizeRetries(retry),
|
|
2632
2967
|
methods: normalizeMethods(defaults.methods),
|
|
2633
|
-
delay:
|
|
2968
|
+
delay: normalizeDelay2(defaults.delay),
|
|
2634
2969
|
respectRetryAfter: defaults.respectRetryAfter ?? true,
|
|
2635
2970
|
maxDelay: normalizeMaxDelay(defaults.maxDelay),
|
|
2636
2971
|
jitter: defaults.jitter ?? false,
|
|
@@ -2648,7 +2983,7 @@ function normalizeRetryOptions(retry, defaults = {}) {
|
|
|
2648
2983
|
methods: normalizeMethods(
|
|
2649
2984
|
retry?.methods ?? defaults.methods
|
|
2650
2985
|
),
|
|
2651
|
-
delay:
|
|
2986
|
+
delay: normalizeDelay2(
|
|
2652
2987
|
retry?.delay ?? defaults.delay
|
|
2653
2988
|
),
|
|
2654
2989
|
respectRetryAfter: retry?.respectRetryAfter ?? defaults.respectRetryAfter ?? true,
|
|
@@ -2690,7 +3025,7 @@ function normalizeMaxElapsedTime(maxElapsedTime) {
|
|
|
2690
3025
|
}
|
|
2691
3026
|
return Math.max(0, maxElapsedTime);
|
|
2692
3027
|
}
|
|
2693
|
-
function
|
|
3028
|
+
function normalizeDelay2(delay) {
|
|
2694
3029
|
if (typeof delay === "number") {
|
|
2695
3030
|
const normalizedDelay = Math.max(0, delay);
|
|
2696
3031
|
return () => normalizedDelay;
|
package/dist/index.d.cts
CHANGED
|
@@ -290,14 +290,69 @@ declare class FetchAdapter implements Adapter {
|
|
|
290
290
|
}
|
|
291
291
|
|
|
292
292
|
type MockHandler<T = unknown> = (config: RequestConfig) => T | Promise<T>;
|
|
293
|
+
type MockURLMatcher = string | RegExp;
|
|
294
|
+
interface MockRequestMatcher {
|
|
295
|
+
url: MockURLMatcher;
|
|
296
|
+
query?: QueryParams;
|
|
297
|
+
headers?: HeadersInit;
|
|
298
|
+
}
|
|
299
|
+
interface MockReply<T = unknown> {
|
|
300
|
+
status: number;
|
|
301
|
+
data?: T;
|
|
302
|
+
headers?: HeadersInit;
|
|
303
|
+
statusText?: string;
|
|
304
|
+
delay?: number;
|
|
305
|
+
}
|
|
306
|
+
type MockReplyHandler<T = unknown> = (config: RequestConfig) => MockReply<T> | Promise<MockReply<T>>;
|
|
307
|
+
interface MockResponseOptions {
|
|
308
|
+
headers?: HeadersInit;
|
|
309
|
+
statusText?: string;
|
|
310
|
+
delay?: number;
|
|
311
|
+
}
|
|
312
|
+
interface MockRoute {
|
|
313
|
+
reply<T = unknown>(handler: MockReplyHandler<T>): MockAdapter;
|
|
314
|
+
reply<T = unknown>(status: number, data?: T, options?: MockResponseOptions): MockAdapter;
|
|
315
|
+
replyOnce<T = unknown>(handler: MockReplyHandler<T>): MockAdapter;
|
|
316
|
+
replyOnce<T = unknown>(status: number, data?: T, options?: MockResponseOptions): MockAdapter;
|
|
317
|
+
networkError(message?: string): MockAdapter;
|
|
318
|
+
networkErrorOnce(message?: string): MockAdapter;
|
|
319
|
+
timeout(message?: string): MockAdapter;
|
|
320
|
+
timeoutOnce(message?: string): MockAdapter;
|
|
321
|
+
}
|
|
293
322
|
interface MockAdapterOptions {
|
|
294
323
|
handlers?: Record<string, MockHandler>;
|
|
324
|
+
/**
|
|
325
|
+
* Default delay for mock handlers and routes.
|
|
326
|
+
*
|
|
327
|
+
* @default 0
|
|
328
|
+
*/
|
|
329
|
+
delay?: number;
|
|
295
330
|
}
|
|
296
331
|
declare class MockAdapter implements Adapter {
|
|
297
332
|
private readonly handlers;
|
|
333
|
+
private readonly rules;
|
|
334
|
+
private readonly requests;
|
|
335
|
+
private readonly delay;
|
|
298
336
|
constructor(options?: MockAdapterOptions);
|
|
337
|
+
get history(): readonly RequestConfig[];
|
|
338
|
+
/**
|
|
339
|
+
* Register a legacy URL-only data handler.
|
|
340
|
+
*/
|
|
299
341
|
on<T = unknown>(url: string, handler: MockHandler<T>): this;
|
|
342
|
+
onMethod(method: HttpMethod, matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
343
|
+
onGet(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
344
|
+
onPost(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
345
|
+
onPut(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
346
|
+
onPatch(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
347
|
+
onDelete(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
348
|
+
onHead(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
349
|
+
onOptions(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
350
|
+
resetHistory(): this;
|
|
351
|
+
resetHandlers(): this;
|
|
352
|
+
reset(): this;
|
|
300
353
|
request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
|
|
354
|
+
private registerReply;
|
|
355
|
+
private registerError;
|
|
301
356
|
}
|
|
302
357
|
|
|
303
358
|
type Interceptor<T> = (value: T) => T | Promise<T>;
|
|
@@ -619,4 +674,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
|
|
|
619
674
|
|
|
620
675
|
declare function uploadPlugin(): Plugin;
|
|
621
676
|
|
|
622
|
-
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheEntry, type CacheOptions, type CachePlugin, type CachePluginOptions, type CacheStore, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MemoryCacheStore, MockAdapter, type MockAdapterOptions, type MockHandler, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
|
677
|
+
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheEntry, type CacheOptions, type CachePlugin, type CachePluginOptions, type CacheStore, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MemoryCacheStore, MockAdapter, type MockAdapterOptions, type MockHandler, type MockReply, type MockReplyHandler, type MockRequestMatcher, type MockResponseOptions, type MockRoute, type MockURLMatcher, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
package/dist/index.d.ts
CHANGED
|
@@ -290,14 +290,69 @@ declare class FetchAdapter implements Adapter {
|
|
|
290
290
|
}
|
|
291
291
|
|
|
292
292
|
type MockHandler<T = unknown> = (config: RequestConfig) => T | Promise<T>;
|
|
293
|
+
type MockURLMatcher = string | RegExp;
|
|
294
|
+
interface MockRequestMatcher {
|
|
295
|
+
url: MockURLMatcher;
|
|
296
|
+
query?: QueryParams;
|
|
297
|
+
headers?: HeadersInit;
|
|
298
|
+
}
|
|
299
|
+
interface MockReply<T = unknown> {
|
|
300
|
+
status: number;
|
|
301
|
+
data?: T;
|
|
302
|
+
headers?: HeadersInit;
|
|
303
|
+
statusText?: string;
|
|
304
|
+
delay?: number;
|
|
305
|
+
}
|
|
306
|
+
type MockReplyHandler<T = unknown> = (config: RequestConfig) => MockReply<T> | Promise<MockReply<T>>;
|
|
307
|
+
interface MockResponseOptions {
|
|
308
|
+
headers?: HeadersInit;
|
|
309
|
+
statusText?: string;
|
|
310
|
+
delay?: number;
|
|
311
|
+
}
|
|
312
|
+
interface MockRoute {
|
|
313
|
+
reply<T = unknown>(handler: MockReplyHandler<T>): MockAdapter;
|
|
314
|
+
reply<T = unknown>(status: number, data?: T, options?: MockResponseOptions): MockAdapter;
|
|
315
|
+
replyOnce<T = unknown>(handler: MockReplyHandler<T>): MockAdapter;
|
|
316
|
+
replyOnce<T = unknown>(status: number, data?: T, options?: MockResponseOptions): MockAdapter;
|
|
317
|
+
networkError(message?: string): MockAdapter;
|
|
318
|
+
networkErrorOnce(message?: string): MockAdapter;
|
|
319
|
+
timeout(message?: string): MockAdapter;
|
|
320
|
+
timeoutOnce(message?: string): MockAdapter;
|
|
321
|
+
}
|
|
293
322
|
interface MockAdapterOptions {
|
|
294
323
|
handlers?: Record<string, MockHandler>;
|
|
324
|
+
/**
|
|
325
|
+
* Default delay for mock handlers and routes.
|
|
326
|
+
*
|
|
327
|
+
* @default 0
|
|
328
|
+
*/
|
|
329
|
+
delay?: number;
|
|
295
330
|
}
|
|
296
331
|
declare class MockAdapter implements Adapter {
|
|
297
332
|
private readonly handlers;
|
|
333
|
+
private readonly rules;
|
|
334
|
+
private readonly requests;
|
|
335
|
+
private readonly delay;
|
|
298
336
|
constructor(options?: MockAdapterOptions);
|
|
337
|
+
get history(): readonly RequestConfig[];
|
|
338
|
+
/**
|
|
339
|
+
* Register a legacy URL-only data handler.
|
|
340
|
+
*/
|
|
299
341
|
on<T = unknown>(url: string, handler: MockHandler<T>): this;
|
|
342
|
+
onMethod(method: HttpMethod, matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
343
|
+
onGet(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
344
|
+
onPost(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
345
|
+
onPut(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
346
|
+
onPatch(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
347
|
+
onDelete(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
348
|
+
onHead(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
349
|
+
onOptions(matcher: MockURLMatcher | MockRequestMatcher): MockRoute;
|
|
350
|
+
resetHistory(): this;
|
|
351
|
+
resetHandlers(): this;
|
|
352
|
+
reset(): this;
|
|
300
353
|
request<T = unknown>(config: RequestConfig): Promise<NporaResponse<T>>;
|
|
354
|
+
private registerReply;
|
|
355
|
+
private registerError;
|
|
301
356
|
}
|
|
302
357
|
|
|
303
358
|
type Interceptor<T> = (value: T) => T | Promise<T>;
|
|
@@ -619,4 +674,4 @@ declare function retryPlugin(defaultOptions?: RetryOptions): Plugin;
|
|
|
619
674
|
|
|
620
675
|
declare function uploadPlugin(): Plugin;
|
|
621
676
|
|
|
622
|
-
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheEntry, type CacheOptions, type CachePlugin, type CachePluginOptions, type CacheStore, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MemoryCacheStore, MockAdapter, type MockAdapterOptions, type MockHandler, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
|
677
|
+
export { type Adapter, type AuthOptions, type AuthPluginOptions, type AuthTokenStorage, type CacheEntry, type CacheOptions, type CachePlugin, type CachePluginOptions, type CacheStore, Client, type ClientOptions, type DownloadOptions, type DownloadPluginOptions, type DownloadProgress, type DownloadTransport, type ErrorLogEntry, FetchAdapter, type FetchOptions, type HttpMethod, type Interceptor, InterceptorManager, type InterceptorOptions, type LoggerEntry, type LoggerOptions, MemoryCacheStore, MockAdapter, type MockAdapterOptions, type MockHandler, type MockReply, type MockReplyHandler, type MockRequestMatcher, type MockResponseOptions, type MockRoute, type MockURLMatcher, type NporaResponse, type Plugin, type PluginCleanup, type PluginContext, PluginError, type PluginErrorCode, type PluginErrorOptions, type PluginHookManager, type PluginInterceptorManager, type QueryParams, type QueryValue, type RequestConfig, RequestError, type RequestErrorCode, type RequestErrorOptions, type RequestExtensions, type RequestLogEntry, type RequestLogger, type ResponseLogEntry, type ResponseType, type RetryEvent, type RetryOptions, type UploadData, type UploadOptions, type UploadProgress, authPlugin, cachePlugin, createClient, downloadPlugin, loggerPlugin, retryPlugin, uploadPlugin };
|
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
|
}
|
|
@@ -1911,7 +2246,7 @@ function xhrRequest(config, options = {}) {
|
|
|
1911
2246
|
};
|
|
1912
2247
|
const onSignalAbort = () => {
|
|
1913
2248
|
abortWith(
|
|
1914
|
-
|
|
2249
|
+
createAbortError3(signal?.reason, config)
|
|
1915
2250
|
);
|
|
1916
2251
|
};
|
|
1917
2252
|
xhr.onload = () => {
|
|
@@ -1930,7 +2265,7 @@ function xhrRequest(config, options = {}) {
|
|
|
1930
2265
|
};
|
|
1931
2266
|
xhr.onabort = () => {
|
|
1932
2267
|
rejectOnce(
|
|
1933
|
-
|
|
2268
|
+
createAbortError3(signal?.reason, config)
|
|
1934
2269
|
);
|
|
1935
2270
|
};
|
|
1936
2271
|
xhr.onprogress = createProgressHandler(
|
|
@@ -2020,7 +2355,7 @@ async function processResponse(xhr, config) {
|
|
|
2020
2355
|
config,
|
|
2021
2356
|
raw
|
|
2022
2357
|
};
|
|
2023
|
-
const validateStatus = config.validateStatus ??
|
|
2358
|
+
const validateStatus = config.validateStatus ?? defaultValidateStatus3;
|
|
2024
2359
|
if (!validateStatus(xhr.status)) {
|
|
2025
2360
|
throw new RequestError(
|
|
2026
2361
|
xhr.statusText || "Request failed",
|
|
@@ -2088,7 +2423,7 @@ function parseHeaders(value) {
|
|
|
2088
2423
|
}
|
|
2089
2424
|
return headers;
|
|
2090
2425
|
}
|
|
2091
|
-
function
|
|
2426
|
+
function createAbortError3(reason, config) {
|
|
2092
2427
|
if (reason instanceof RequestError) {
|
|
2093
2428
|
return new RequestError(reason.message, {
|
|
2094
2429
|
code: reason.code,
|
|
@@ -2118,7 +2453,7 @@ function createConfigError(config, cause) {
|
|
|
2118
2453
|
function isNullBodyStatus(status) {
|
|
2119
2454
|
return status === 204 || status === 205 || status === 304;
|
|
2120
2455
|
}
|
|
2121
|
-
function
|
|
2456
|
+
function defaultValidateStatus3(status) {
|
|
2122
2457
|
return status >= 200 && status < 300;
|
|
2123
2458
|
}
|
|
2124
2459
|
|
|
@@ -2591,7 +2926,7 @@ function normalizeRetryOptions(retry, defaults = {}) {
|
|
|
2591
2926
|
return {
|
|
2592
2927
|
retries: normalizeRetries(retry),
|
|
2593
2928
|
methods: normalizeMethods(defaults.methods),
|
|
2594
|
-
delay:
|
|
2929
|
+
delay: normalizeDelay2(defaults.delay),
|
|
2595
2930
|
respectRetryAfter: defaults.respectRetryAfter ?? true,
|
|
2596
2931
|
maxDelay: normalizeMaxDelay(defaults.maxDelay),
|
|
2597
2932
|
jitter: defaults.jitter ?? false,
|
|
@@ -2609,7 +2944,7 @@ function normalizeRetryOptions(retry, defaults = {}) {
|
|
|
2609
2944
|
methods: normalizeMethods(
|
|
2610
2945
|
retry?.methods ?? defaults.methods
|
|
2611
2946
|
),
|
|
2612
|
-
delay:
|
|
2947
|
+
delay: normalizeDelay2(
|
|
2613
2948
|
retry?.delay ?? defaults.delay
|
|
2614
2949
|
),
|
|
2615
2950
|
respectRetryAfter: retry?.respectRetryAfter ?? defaults.respectRetryAfter ?? true,
|
|
@@ -2651,7 +2986,7 @@ function normalizeMaxElapsedTime(maxElapsedTime) {
|
|
|
2651
2986
|
}
|
|
2652
2987
|
return Math.max(0, maxElapsedTime);
|
|
2653
2988
|
}
|
|
2654
|
-
function
|
|
2989
|
+
function normalizeDelay2(delay) {
|
|
2655
2990
|
if (typeof delay === "number") {
|
|
2656
2991
|
const normalizedDelay = Math.max(0, delay);
|
|
2657
2992
|
return () => normalizedDelay;
|