@npora/request 1.2.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/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(`No mock handler found for ${config.url}`);
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
- headers: new Headers(),
452
- config,
453
- raw: new Response(JSON.stringify(data), {
454
- status: 200,
455
- headers: {
456
- "content-type": "application/json"
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 {
@@ -733,6 +1068,12 @@ var Pipeline = class {
733
1068
  }
734
1069
  } finally {
735
1070
  context.endTime = Date.now();
1071
+ if (this.hooks.hasSettledHooks) {
1072
+ try {
1073
+ await this.hooks.runSettled(context);
1074
+ } catch {
1075
+ }
1076
+ }
736
1077
  }
737
1078
  }
738
1079
  async processResponse(context) {
@@ -776,7 +1117,7 @@ var Pipeline = class {
776
1117
  function waitForRetry(milliseconds, config) {
777
1118
  const signal = config.signal;
778
1119
  if (signal?.aborted) {
779
- return Promise.reject(createAbortError(signal.reason, config));
1120
+ return Promise.reject(createAbortError2(signal.reason, config));
780
1121
  }
781
1122
  if (milliseconds <= 0) {
782
1123
  return Promise.resolve();
@@ -789,7 +1130,7 @@ function waitForRetry(milliseconds, config) {
789
1130
  const onAbort = () => {
790
1131
  clearTimeout(timer);
791
1132
  cleanup();
792
- reject(createAbortError(signal?.reason, config));
1133
+ reject(createAbortError2(signal?.reason, config));
793
1134
  };
794
1135
  const cleanup = () => {
795
1136
  signal?.removeEventListener("abort", onAbort);
@@ -799,7 +1140,7 @@ function waitForRetry(milliseconds, config) {
799
1140
  });
800
1141
  });
801
1142
  }
802
- function createAbortError(reason, config) {
1143
+ function createAbortError2(reason, config) {
803
1144
  if (reason instanceof RequestError) {
804
1145
  return new RequestError(reason.message, {
805
1146
  code: reason.code,
@@ -873,6 +1214,7 @@ var PluginHooks = class {
873
1214
  this.requestHooks = new HookRegistry();
874
1215
  this.responseHooks = new HookRegistry();
875
1216
  this.errorHooks = new HookRegistry();
1217
+ this.settledHooks = new HookRegistry();
876
1218
  this.retryHooks = new HookRegistry();
877
1219
  }
878
1220
  get hasRequestHooks() {
@@ -887,6 +1229,9 @@ var PluginHooks = class {
887
1229
  get hasRetryHooks() {
888
1230
  return this.retryHooks.active;
889
1231
  }
1232
+ get hasSettledHooks() {
1233
+ return this.settledHooks.active;
1234
+ }
890
1235
  onRequest(hook, options = {}) {
891
1236
  return this.requestHooks.register(hook, options);
892
1237
  }
@@ -899,6 +1244,9 @@ var PluginHooks = class {
899
1244
  onRetry(hook, options = {}) {
900
1245
  return this.retryHooks.register(hook, options);
901
1246
  }
1247
+ onSettled(hook, options = {}) {
1248
+ return this.settledHooks.register(hook, options);
1249
+ }
902
1250
  async runRequest(context) {
903
1251
  for (const hook of this.requestHooks.values()) {
904
1252
  await hook(context);
@@ -914,6 +1262,14 @@ var PluginHooks = class {
914
1262
  await hook(context);
915
1263
  }
916
1264
  }
1265
+ async runSettled(context) {
1266
+ for (const hook of this.settledHooks.values()) {
1267
+ try {
1268
+ await hook(context);
1269
+ } catch {
1270
+ }
1271
+ }
1272
+ }
917
1273
  async resolveRetry(context, attempt) {
918
1274
  for (const hook of this.retryHooks.values()) {
919
1275
  const decision = await hook(context, attempt);
@@ -1063,6 +1419,12 @@ function createScopedHooks(hooks, pluginPriority, cleanups) {
1063
1419
  cleanups
1064
1420
  );
1065
1421
  },
1422
+ onSettled(hook, options) {
1423
+ return trackHook(
1424
+ hooks.onSettled(hook, withPriority(options, pluginPriority)),
1425
+ cleanups
1426
+ );
1427
+ },
1066
1428
  onRetry(hook, options) {
1067
1429
  return trackHook(
1068
1430
  hooks.onRetry(hook, withPriority(options, pluginPriority)),
@@ -1466,6 +1828,23 @@ function defaultShouldRefresh(error) {
1466
1828
  }
1467
1829
 
1468
1830
  // src/plugins/cachePlugin.ts
1831
+ var MemoryCacheStore = class {
1832
+ constructor() {
1833
+ this.entries = /* @__PURE__ */ new Map();
1834
+ }
1835
+ get(key) {
1836
+ return this.entries.get(key);
1837
+ }
1838
+ set(key, entry) {
1839
+ this.entries.set(key, entry);
1840
+ }
1841
+ delete(key) {
1842
+ this.entries.delete(key);
1843
+ }
1844
+ clear() {
1845
+ this.entries.clear();
1846
+ }
1847
+ };
1469
1848
  var DEFAULT_CACHE_METHODS = [
1470
1849
  "GET",
1471
1850
  "HEAD"
@@ -1477,8 +1856,11 @@ var DEFAULT_VARY_HEADERS = [
1477
1856
  "accept-language"
1478
1857
  ];
1479
1858
  function cachePlugin(options = {}) {
1480
- const cacheStore = /* @__PURE__ */ new Map();
1859
+ const store = options.store ?? new MemoryCacheStore();
1481
1860
  const cacheHits = /* @__PURE__ */ new WeakSet();
1861
+ const leaders = /* @__PURE__ */ new WeakMap();
1862
+ const completedRecords = /* @__PURE__ */ new WeakMap();
1863
+ const inFlight = /* @__PURE__ */ new Map();
1482
1864
  const methods = new Set(
1483
1865
  options.methods ?? DEFAULT_CACHE_METHODS
1484
1866
  );
@@ -1486,10 +1868,12 @@ function cachePlugin(options = {}) {
1486
1868
  const plugin = {
1487
1869
  name: "cache",
1488
1870
  clear() {
1489
- cacheStore.clear();
1871
+ return store.clear();
1490
1872
  },
1491
1873
  install(context) {
1492
- context.hooks.onRequest((requestContext) => {
1874
+ const ownedLeaders = /* @__PURE__ */ new Set();
1875
+ let active = true;
1876
+ context.hooks.onRequest(async (requestContext) => {
1493
1877
  const cache = resolveExtensionConfig(
1494
1878
  requestContext.config,
1495
1879
  "cache"
@@ -1502,25 +1886,48 @@ function cachePlugin(options = {}) {
1502
1886
  cache,
1503
1887
  varyHeaders
1504
1888
  );
1505
- const record = cacheStore.get(key);
1506
- if (!record) {
1889
+ const record = await readStore(store, key);
1890
+ if (!active) {
1507
1891
  return;
1508
1892
  }
1509
- if (Date.now() > record.expiresAt) {
1510
- cacheStore.delete(key);
1893
+ if (record) {
1894
+ if (!Number.isFinite(record.expiresAt) || Date.now() > record.expiresAt) {
1895
+ await deleteStore(store, key);
1896
+ } else {
1897
+ const cachedResponse = restoreCacheEntry(
1898
+ record,
1899
+ requestContext.config
1900
+ );
1901
+ if (cachedResponse) {
1902
+ requestContext.response = cachedResponse;
1903
+ cacheHits.add(requestContext);
1904
+ return;
1905
+ }
1906
+ await deleteStore(store, key);
1907
+ }
1908
+ }
1909
+ if (!(cache.dedupe ?? options.dedupe ?? true)) {
1511
1910
  return;
1512
1911
  }
1513
- requestContext.response = {
1514
- data: cloneCacheValue(record.data),
1515
- status: record.status,
1516
- statusText: record.statusText,
1517
- headers: new Headers(record.headers),
1518
- config: requestContext.config,
1519
- raw: cloneRawResponse(record)
1520
- };
1521
- cacheHits.add(requestContext);
1912
+ const pending = inFlight.get(key);
1913
+ if (pending) {
1914
+ const sharedRecord = await waitForSharedRecord(
1915
+ pending.promise,
1916
+ requestContext.config
1917
+ );
1918
+ requestContext.response = createCachedResponse(
1919
+ sharedRecord,
1920
+ requestContext.config
1921
+ );
1922
+ cacheHits.add(requestContext);
1923
+ return;
1924
+ }
1925
+ const created = createInFlightRequest(requestContext);
1926
+ inFlight.set(key, created);
1927
+ leaders.set(requestContext, key);
1928
+ ownedLeaders.add(requestContext);
1522
1929
  });
1523
- context.hooks.onResponse((requestContext) => {
1930
+ context.hooks.onResponse(async (requestContext) => {
1524
1931
  if (cacheHits.has(requestContext)) {
1525
1932
  return;
1526
1933
  }
@@ -1537,23 +1944,184 @@ function cachePlugin(options = {}) {
1537
1944
  varyHeaders
1538
1945
  );
1539
1946
  const ttl = cache.ttl ?? 3e4;
1947
+ const record = createCacheEntry(
1948
+ requestContext.response,
1949
+ Date.now() + Math.max(0, ttl)
1950
+ );
1951
+ completedRecords.set(requestContext, record);
1540
1952
  if (ttl <= 0) {
1541
- cacheStore.delete(key);
1953
+ await deleteStore(store, key);
1542
1954
  return;
1543
1955
  }
1544
- cacheStore.set(key, {
1545
- data: cloneCacheValue(requestContext.response.data),
1546
- expiresAt: Date.now() + ttl,
1547
- status: requestContext.response.status,
1548
- statusText: requestContext.response.statusText,
1549
- headers: new Headers(requestContext.response.headers),
1550
- raw: cloneResponse(requestContext.response.raw)
1551
- });
1956
+ await writeStore(store, key, record);
1957
+ });
1958
+ context.hooks.onSettled((requestContext) => {
1959
+ const key = leaders.get(requestContext);
1960
+ if (!key) {
1961
+ return;
1962
+ }
1963
+ leaders.delete(requestContext);
1964
+ ownedLeaders.delete(requestContext);
1965
+ const pending = inFlight.get(key);
1966
+ if (!pending || pending.owner !== requestContext) {
1967
+ return;
1968
+ }
1969
+ inFlight.delete(key);
1970
+ const record = completedRecords.get(requestContext);
1971
+ completedRecords.delete(requestContext);
1972
+ if (record && requestContext.response && !requestContext.error) {
1973
+ pending.resolve(record);
1974
+ return;
1975
+ }
1976
+ pending.reject(
1977
+ requestContext.error ?? new RequestError("Shared request failed", {
1978
+ code: "NETWORK_ERROR",
1979
+ config: requestContext.config
1980
+ })
1981
+ );
1552
1982
  });
1983
+ return () => {
1984
+ active = false;
1985
+ for (const owner of ownedLeaders) {
1986
+ const key = leaders.get(owner);
1987
+ leaders.delete(owner);
1988
+ if (!key) {
1989
+ continue;
1990
+ }
1991
+ const pending = inFlight.get(key);
1992
+ if (!pending || pending.owner !== owner) {
1993
+ continue;
1994
+ }
1995
+ inFlight.delete(key);
1996
+ pending.reject(
1997
+ new RequestError("Cache plugin removed during shared request", {
1998
+ code: "ABORT_ERROR"
1999
+ })
2000
+ );
2001
+ }
2002
+ ownedLeaders.clear();
2003
+ };
1553
2004
  }
1554
2005
  };
1555
2006
  return plugin;
1556
2007
  }
2008
+ function createInFlightRequest(owner) {
2009
+ let resolve;
2010
+ let reject;
2011
+ const promise = new Promise((resolvePromise, rejectPromise) => {
2012
+ resolve = resolvePromise;
2013
+ reject = rejectPromise;
2014
+ });
2015
+ void promise.catch(() => {
2016
+ });
2017
+ return {
2018
+ owner,
2019
+ promise,
2020
+ resolve,
2021
+ reject
2022
+ };
2023
+ }
2024
+ async function readStore(store, key) {
2025
+ try {
2026
+ return await store.get(key);
2027
+ } catch {
2028
+ return void 0;
2029
+ }
2030
+ }
2031
+ async function writeStore(store, key, entry) {
2032
+ try {
2033
+ await store.set(key, entry);
2034
+ } catch {
2035
+ }
2036
+ }
2037
+ async function deleteStore(store, key) {
2038
+ try {
2039
+ await store.delete(key);
2040
+ } catch {
2041
+ }
2042
+ }
2043
+ async function waitForSharedRecord(promise, config) {
2044
+ const signal = config.signal;
2045
+ if (!signal) {
2046
+ try {
2047
+ return await promise;
2048
+ } catch (error) {
2049
+ throw cloneSharedError(error, config);
2050
+ }
2051
+ }
2052
+ if (signal.aborted) {
2053
+ throw createSharedAbortError(signal.reason, config);
2054
+ }
2055
+ return new Promise((resolve, reject) => {
2056
+ const cleanup = () => {
2057
+ signal.removeEventListener("abort", onAbort);
2058
+ };
2059
+ const onAbort = () => {
2060
+ cleanup();
2061
+ reject(createSharedAbortError(signal.reason, config));
2062
+ };
2063
+ signal.addEventListener("abort", onAbort, {
2064
+ once: true
2065
+ });
2066
+ promise.then(
2067
+ (entry) => {
2068
+ cleanup();
2069
+ resolve(entry);
2070
+ },
2071
+ (error) => {
2072
+ cleanup();
2073
+ reject(cloneSharedError(error, config));
2074
+ }
2075
+ );
2076
+ });
2077
+ }
2078
+ function createSharedAbortError(reason, config) {
2079
+ return new RequestError("Request aborted while waiting for shared response", {
2080
+ code: "ABORT_ERROR",
2081
+ config,
2082
+ cause: reason
2083
+ });
2084
+ }
2085
+ function cloneSharedError(error, config) {
2086
+ if (!(error instanceof RequestError)) {
2087
+ return error;
2088
+ }
2089
+ return new RequestError(error.message, {
2090
+ code: error.code,
2091
+ status: error.status,
2092
+ data: error.data,
2093
+ response: error.response,
2094
+ config,
2095
+ cause: error
2096
+ });
2097
+ }
2098
+ function createCacheEntry(response, expiresAt) {
2099
+ return {
2100
+ data: cloneCacheValue(response.data),
2101
+ expiresAt,
2102
+ status: response.status,
2103
+ statusText: response.statusText,
2104
+ headers: [...response.headers.entries()],
2105
+ raw: cloneResponse(response.raw)
2106
+ };
2107
+ }
2108
+ function createCachedResponse(record, config) {
2109
+ return {
2110
+ data: cloneCacheValue(record.data),
2111
+ status: record.status,
2112
+ statusText: record.statusText,
2113
+ headers: new Headers(record.headers),
2114
+ config,
2115
+ raw: cloneRawResponse(record)
2116
+ };
2117
+ }
2118
+ function restoreCacheEntry(record, config) {
2119
+ try {
2120
+ return createCachedResponse(record, config);
2121
+ } catch {
2122
+ return void 0;
2123
+ }
2124
+ }
1557
2125
  function isCacheableRequest(config, methods) {
1558
2126
  return methods.has(config.method ?? "GET") && config.responseType !== "stream";
1559
2127
  }
@@ -1566,7 +2134,7 @@ function createCacheKey(config, cache, varyHeaders) {
1566
2134
  method: config.method ?? "GET",
1567
2135
  baseURL: config.baseURL,
1568
2136
  url: config.url,
1569
- query: normalizeQuery(config.query),
2137
+ query: normalizeQuery2(config.query),
1570
2138
  responseType: config.responseType ?? "auto",
1571
2139
  headers: Object.fromEntries(
1572
2140
  varyHeaders.map((name) => [
@@ -1576,7 +2144,7 @@ function createCacheKey(config, cache, varyHeaders) {
1576
2144
  )
1577
2145
  });
1578
2146
  }
1579
- function normalizeQuery(query) {
2147
+ function normalizeQuery2(query) {
1580
2148
  if (!query) {
1581
2149
  return [];
1582
2150
  }
@@ -1678,7 +2246,7 @@ function xhrRequest(config, options = {}) {
1678
2246
  };
1679
2247
  const onSignalAbort = () => {
1680
2248
  abortWith(
1681
- createAbortError2(signal?.reason, config)
2249
+ createAbortError3(signal?.reason, config)
1682
2250
  );
1683
2251
  };
1684
2252
  xhr.onload = () => {
@@ -1697,7 +2265,7 @@ function xhrRequest(config, options = {}) {
1697
2265
  };
1698
2266
  xhr.onabort = () => {
1699
2267
  rejectOnce(
1700
- createAbortError2(signal?.reason, config)
2268
+ createAbortError3(signal?.reason, config)
1701
2269
  );
1702
2270
  };
1703
2271
  xhr.onprogress = createProgressHandler(
@@ -1787,7 +2355,7 @@ async function processResponse(xhr, config) {
1787
2355
  config,
1788
2356
  raw
1789
2357
  };
1790
- const validateStatus = config.validateStatus ?? defaultValidateStatus2;
2358
+ const validateStatus = config.validateStatus ?? defaultValidateStatus3;
1791
2359
  if (!validateStatus(xhr.status)) {
1792
2360
  throw new RequestError(
1793
2361
  xhr.statusText || "Request failed",
@@ -1855,7 +2423,7 @@ function parseHeaders(value) {
1855
2423
  }
1856
2424
  return headers;
1857
2425
  }
1858
- function createAbortError2(reason, config) {
2426
+ function createAbortError3(reason, config) {
1859
2427
  if (reason instanceof RequestError) {
1860
2428
  return new RequestError(reason.message, {
1861
2429
  code: reason.code,
@@ -1885,7 +2453,7 @@ function createConfigError(config, cause) {
1885
2453
  function isNullBodyStatus(status) {
1886
2454
  return status === 204 || status === 205 || status === 304;
1887
2455
  }
1888
- function defaultValidateStatus2(status) {
2456
+ function defaultValidateStatus3(status) {
1889
2457
  return status >= 200 && status < 300;
1890
2458
  }
1891
2459
 
@@ -2358,7 +2926,7 @@ function normalizeRetryOptions(retry, defaults = {}) {
2358
2926
  return {
2359
2927
  retries: normalizeRetries(retry),
2360
2928
  methods: normalizeMethods(defaults.methods),
2361
- delay: normalizeDelay(defaults.delay),
2929
+ delay: normalizeDelay2(defaults.delay),
2362
2930
  respectRetryAfter: defaults.respectRetryAfter ?? true,
2363
2931
  maxDelay: normalizeMaxDelay(defaults.maxDelay),
2364
2932
  jitter: defaults.jitter ?? false,
@@ -2376,7 +2944,7 @@ function normalizeRetryOptions(retry, defaults = {}) {
2376
2944
  methods: normalizeMethods(
2377
2945
  retry?.methods ?? defaults.methods
2378
2946
  ),
2379
- delay: normalizeDelay(
2947
+ delay: normalizeDelay2(
2380
2948
  retry?.delay ?? defaults.delay
2381
2949
  ),
2382
2950
  respectRetryAfter: retry?.respectRetryAfter ?? defaults.respectRetryAfter ?? true,
@@ -2418,7 +2986,7 @@ function normalizeMaxElapsedTime(maxElapsedTime) {
2418
2986
  }
2419
2987
  return Math.max(0, maxElapsedTime);
2420
2988
  }
2421
- function normalizeDelay(delay) {
2989
+ function normalizeDelay2(delay) {
2422
2990
  if (typeof delay === "number") {
2423
2991
  const normalizedDelay = Math.max(0, delay);
2424
2992
  return () => normalizedDelay;
@@ -2551,6 +3119,7 @@ export {
2551
3119
  Client,
2552
3120
  FetchAdapter,
2553
3121
  InterceptorManager,
3122
+ MemoryCacheStore,
2554
3123
  MockAdapter,
2555
3124
  PluginError,
2556
3125
  RequestError,