@ahoo-wang/fetcher 2.5.2 → 2.5.5

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.es.js CHANGED
@@ -1,12 +1,12 @@
1
1
  function L(e) {
2
2
  return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(e);
3
3
  }
4
- function M(e, t) {
4
+ function B(e, t) {
5
5
  return L(t) ? t : t ? e.replace(/\/?\/$/, "") + "/" + t.replace(/^\/+/, "") : e;
6
6
  }
7
- var G = /* @__PURE__ */ ((e) => (e[e.UriTemplate = 0] = "UriTemplate", e[e.Express = 1] = "Express", e))(G || {});
8
- function H(e) {
9
- return e === 1 ? j : B;
7
+ var M = /* @__PURE__ */ ((e) => (e[e.UriTemplate = 0] = "UriTemplate", e[e.Express = 1] = "Express", e))(M || {});
8
+ function G(e) {
9
+ return e === 1 ? j : H;
10
10
  }
11
11
  function x(e, t, r) {
12
12
  return r ? e.replace(t, (s, o) => {
@@ -99,7 +99,7 @@ const h = class h {
99
99
  };
100
100
  h.PATH_PARAM_REGEX = /{([^}]+)}/g;
101
101
  let P = h;
102
- const B = new P(), l = class l {
102
+ const H = new P(), l = class l {
103
103
  /**
104
104
  * Extracts path parameters from an Express-style URL string.
105
105
  *
@@ -166,8 +166,8 @@ const B = new P(), l = class l {
166
166
  }
167
167
  };
168
168
  l.PATH_PARAM_REGEX = /:([^/]+)/g;
169
- let g = l;
170
- const j = new g();
169
+ let y = l;
170
+ const j = new y();
171
171
  class $ {
172
172
  /**
173
173
  * Initializes a new UrlBuilder instance.
@@ -187,7 +187,7 @@ class $ {
187
187
  * ```
188
188
  */
189
189
  constructor(t, r) {
190
- this.baseURL = t, this.urlTemplateResolver = H(r);
190
+ this.baseURL = t, this.urlTemplateResolver = G(r);
191
191
  }
192
192
  /**
193
193
  * Builds a complete URL, including path parameter replacement and query parameter addition.
@@ -208,13 +208,13 @@ class $ {
208
208
  * ```
209
209
  */
210
210
  build(t, r) {
211
- const s = r?.path, o = r?.query, n = M(this.baseURL, t);
212
- let u = this.urlTemplateResolver.resolve(n, s);
211
+ const s = r?.path, o = r?.query, n = B(this.baseURL, t);
212
+ let i = this.urlTemplateResolver.resolve(n, s);
213
213
  if (o) {
214
- const c = new URLSearchParams(o).toString();
215
- c && (u += "?" + c);
214
+ const a = new URLSearchParams(o).toString();
215
+ a && (i += "?" + a);
216
216
  }
217
- return u;
217
+ return i;
218
218
  }
219
219
  /**
220
220
  * Resolves a complete URL from a FetchRequest.
@@ -241,7 +241,7 @@ class E extends Error {
241
241
  super(s), this.cause = r, this.name = "FetcherError", r?.stack && (this.stack = r.stack), Object.setPrototypeOf(this, E.prototype);
242
242
  }
243
243
  }
244
- class I extends E {
244
+ class q extends E {
245
245
  /**
246
246
  * Creates a new FetchTimeoutError instance.
247
247
  *
@@ -249,7 +249,7 @@ class I extends E {
249
249
  */
250
250
  constructor(t) {
251
251
  const r = t.method || "GET", s = `Request timeout of ${t.timeout}ms exceeded for ${r} ${t.url}`;
252
- super(s), this.name = "FetchTimeoutError", this.request = t, Object.setPrototypeOf(this, I.prototype);
252
+ super(s), this.name = "FetchTimeoutError", this.request = t, Object.setPrototypeOf(this, q.prototype);
253
253
  }
254
254
  }
255
255
  function X(e, t) {
@@ -264,15 +264,15 @@ async function V(e) {
264
264
  const o = e.abortController ?? new AbortController();
265
265
  e.abortController = o, s.signal = o.signal;
266
266
  let n = null;
267
- const u = new Promise((c, T) => {
267
+ const i = new Promise((a, T) => {
268
268
  n = setTimeout(() => {
269
269
  n && clearTimeout(n);
270
- const w = new I(e);
270
+ const w = new q(e);
271
271
  o.abort(w), T(w);
272
272
  }, r);
273
273
  });
274
274
  try {
275
- return await Promise.race([fetch(t, s), u]);
275
+ return await Promise.race([fetch(t, s), i]);
276
276
  } finally {
277
277
  n && clearTimeout(n);
278
278
  }
@@ -292,11 +292,11 @@ class J {
292
292
  r.url = t.fetcher.urlBuilder.resolveRequestUrl(r);
293
293
  }
294
294
  }
295
- var i = /* @__PURE__ */ ((e) => (e.GET = "GET", e.POST = "POST", e.PUT = "PUT", e.DELETE = "DELETE", e.PATCH = "PATCH", e.HEAD = "HEAD", e.OPTIONS = "OPTIONS", e.TRACE = "TRACE", e))(i || {});
296
- const a = "Content-Type", R = class R {
295
+ var u = /* @__PURE__ */ ((e) => (e.GET = "GET", e.POST = "POST", e.PUT = "PUT", e.DELETE = "DELETE", e.PATCH = "PATCH", e.HEAD = "HEAD", e.OPTIONS = "OPTIONS", e.TRACE = "TRACE", e))(u || {});
296
+ const c = "Content-Type", d = class d {
297
297
  };
298
- R.APPLICATION_JSON = "application/json", R.TEXT_EVENT_STREAM = "text/event-stream";
299
- let d = R;
298
+ d.APPLICATION_JSON = "application/json", d.TEXT_EVENT_STREAM = "text/event-stream";
299
+ let R = d;
300
300
  const Q = "RequestBodyInterceptor", Y = C + 1e3;
301
301
  class z {
302
302
  constructor() {
@@ -367,10 +367,10 @@ class z {
367
367
  return;
368
368
  const s = t.ensureRequestHeaders();
369
369
  if (this.isAutoAppendContentType(r.body)) {
370
- s[a] && delete s[a];
370
+ s[c] && delete s[c];
371
371
  return;
372
372
  }
373
- this.isSupportedComplexBodyType(r.body) || (t.request.body = JSON.stringify(r.body), s[a] || (s[a] = d.APPLICATION_JSON));
373
+ this.isSupportedComplexBodyType(r.body) || (t.request.body = JSON.stringify(r.body), s[c] || (s[c] = R.APPLICATION_JSON));
374
374
  }
375
375
  }
376
376
  const K = "FetchInterceptor", W = Number.MAX_SAFE_INTEGER - 1e3;
@@ -407,10 +407,10 @@ class Z {
407
407
  t.response = await V(t.request);
408
408
  }
409
409
  }
410
- function m(e, t) {
410
+ function f(e, t) {
411
411
  return t ? e.filter(t).sort((r, s) => r.order - s.order) : [...e].sort((r, s) => r.order - s.order);
412
412
  }
413
- class f {
413
+ class m {
414
414
  /**
415
415
  * Initializes a new InterceptorRegistry instance.
416
416
  *
@@ -421,7 +421,7 @@ class f {
421
421
  * upon construction.
422
422
  */
423
423
  constructor(t = []) {
424
- this.sortedInterceptors = [], this.sortedInterceptors = m(t);
424
+ this.sortedInterceptors = [], this.sortedInterceptors = f(t);
425
425
  }
426
426
  /**
427
427
  * Gets the name of this interceptor registry.
@@ -459,7 +459,7 @@ class f {
459
459
  * After adding, interceptors are automatically sorted by their order property.
460
460
  */
461
461
  use(t) {
462
- return this.sortedInterceptors.some((r) => r.name === t.name) ? !1 : (this.sortedInterceptors = m([
462
+ return this.sortedInterceptors.some((r) => r.name === t.name) ? !1 : (this.sortedInterceptors = f([
463
463
  ...this.sortedInterceptors,
464
464
  t
465
465
  ]), !0);
@@ -473,7 +473,7 @@ class f {
473
473
  */
474
474
  eject(t) {
475
475
  const r = this.sortedInterceptors;
476
- return this.sortedInterceptors = m(
476
+ return this.sortedInterceptors = f(
477
477
  r,
478
478
  (s) => s.name !== t
479
479
  ), r.length !== this.sortedInterceptors.length;
@@ -503,11 +503,11 @@ class f {
503
503
  await r.intercept(t);
504
504
  }
505
505
  }
506
- class q extends E {
506
+ class b extends E {
507
507
  constructor(t) {
508
508
  super(
509
509
  `Request failed with status code ${t.response?.status} for ${t.request.url}`
510
- ), this.exchange = t, this.name = "HttpStatusValidationError", Object.setPrototypeOf(this, q.prototype);
510
+ ), this.exchange = t, this.name = "HttpStatusValidationError", Object.setPrototypeOf(this, b.prototype);
511
511
  }
512
512
  }
513
513
  const tt = (e) => e >= 200 && e < 300, et = "ValidateStatusInterceptor", rt = Number.MAX_SAFE_INTEGER - 1e3;
@@ -554,7 +554,7 @@ class st {
554
554
  return;
555
555
  const r = t.response.status;
556
556
  if (!this.validateStatus(r))
557
- throw new q(t);
557
+ throw new b(t);
558
558
  }
559
559
  }
560
560
  class p extends E {
@@ -571,13 +571,13 @@ class p extends E {
571
571
  }
572
572
  class ot {
573
573
  constructor() {
574
- this.request = new f([
574
+ this.request = new m([
575
575
  new J(),
576
576
  new z(),
577
577
  new Z()
578
- ]), this.response = new f([
578
+ ]), this.response = new m([
579
579
  new st()
580
- ]), this.error = new f();
580
+ ]), this.error = new m();
581
581
  }
582
582
  /**
583
583
  * Processes a FetchExchange through the interceptor pipeline.
@@ -675,17 +675,41 @@ class ot {
675
675
  }
676
676
  }
677
677
  }
678
- const nt = (e) => e, it = (e) => e.requiredResponse, ut = (e) => e.requiredResponse.json(), ct = (e) => e.requiredResponse.text(), b = {
678
+ const nt = (e) => e, ut = (e) => e.requiredResponse, it = (e) => e.requiredResponse.json(), at = (e) => e.requiredResponse.text(), ct = (e) => e.requiredResponse.blob(), ht = (e) => e.requiredResponse.arrayBuffer(), lt = (e) => e.requiredResponse.bytes(), I = {
679
+ /**
680
+ * Returns the original FetchExchange object
681
+ */
679
682
  Exchange: nt,
680
- Response: it,
681
- Json: ut,
682
- Text: ct
683
+ /**
684
+ * Extracts the raw Response object
685
+ */
686
+ Response: ut,
687
+ /**
688
+ * Parses and returns response body as JSON
689
+ */
690
+ Json: it,
691
+ /**
692
+ * Returns response body as text
693
+ */
694
+ Text: at,
695
+ /**
696
+ * Returns response body as a Blob
697
+ */
698
+ Blob: ct,
699
+ /**
700
+ * Returns response body as an ArrayBuffer
701
+ */
702
+ ArrayBuffer: ht,
703
+ /**
704
+ * Returns response body as a Uint8Array
705
+ */
706
+ Bytes: lt
683
707
  };
684
708
  function S(e, t) {
685
709
  if (!(e === void 0 && t === void 0))
686
710
  return t === void 0 ? e : e === void 0 ? t : { ...e, ...t };
687
711
  }
688
- function at(e, t) {
712
+ function Et(e, t) {
689
713
  if (t ??= /* @__PURE__ */ new Map(), !e)
690
714
  return t;
691
715
  if (e instanceof Map) {
@@ -697,9 +721,9 @@ function at(e, t) {
697
721
  t.set(r, s);
698
722
  return t;
699
723
  }
700
- class ht {
724
+ class Rt {
701
725
  constructor(t) {
702
- this.fetcher = t.fetcher, this.request = t.request, this.resultExtractor = t.resultExtractor ?? b.Exchange, this.attributes = at(t.attributes), this._response = t.response, this.error = t.error;
726
+ this.fetcher = t.fetcher, this.request = t.request, this.resultExtractor = t.resultExtractor ?? I.Exchange, this.attributes = Et(t.attributes), this._response = t.response, this.error = t.error;
703
727
  }
704
728
  /**
705
729
  * Ensures that request headers object exists, creating it if necessary.
@@ -792,7 +816,7 @@ class ht {
792
816
  return this.cachedExtractedResult !== void 0 ? this.cachedExtractedResult : (this.cachedExtractedResult = this.resultExtractor(this), this.cachedExtractedResult);
793
817
  }
794
818
  }
795
- function dt(e, t) {
819
+ function Tt(e, t) {
796
820
  if (Object.keys(e).length === 0)
797
821
  return t;
798
822
  if (Object.keys(t).length === 0)
@@ -803,7 +827,7 @@ function dt(e, t) {
803
827
  }, s = {
804
828
  ...e.headers,
805
829
  ...t.headers
806
- }, o = t.method ?? e.method, n = t.body ?? e.body, u = t.timeout ?? e.timeout, c = t.signal ?? e.signal, T = t.abortController ?? e.abortController;
830
+ }, o = t.method ?? e.method, n = t.body ?? e.body, i = t.timeout ?? e.timeout, a = t.signal ?? e.signal, T = t.abortController ?? e.abortController;
807
831
  return {
808
832
  ...e,
809
833
  ...t,
@@ -811,8 +835,8 @@ function dt(e, t) {
811
835
  urlParams: r,
812
836
  headers: s,
813
837
  body: n,
814
- timeout: u,
815
- signal: c,
838
+ timeout: i,
839
+ signal: a,
816
840
  abortController: T
817
841
  };
818
842
  }
@@ -822,15 +846,15 @@ function A(e, t) {
822
846
  attributes: t?.attributes ?? e?.attributes
823
847
  };
824
848
  }
825
- const y = {
826
- [a]: d.APPLICATION_JSON
849
+ const g = {
850
+ [c]: R.APPLICATION_JSON
827
851
  }, U = {
828
852
  baseURL: "",
829
- headers: y
853
+ headers: g
830
854
  }, v = {
831
- resultExtractor: b.Exchange
855
+ resultExtractor: I.Exchange
832
856
  }, N = {
833
- resultExtractor: b.Response
857
+ resultExtractor: I.Response
834
858
  };
835
859
  class D {
836
860
  /**
@@ -847,7 +871,7 @@ class D {
847
871
  * @param options.interceptors - Interceptor manager for processing requests and responses.
848
872
  */
849
873
  constructor(t = U) {
850
- this.headers = y, this.urlBuilder = new $(t.baseURL, t.urlTemplateStyle), this.headers = t.headers ?? y, this.timeout = t.timeout, this.interceptors = t.interceptors ?? new ot();
874
+ this.headers = g, this.urlBuilder = new $(t.baseURL, t.urlTemplateStyle), this.headers = t.headers ?? g, this.timeout = t.timeout, this.interceptors = t.interceptors ?? new ot();
851
875
  }
852
876
  /**
853
877
  * Processes an HTTP request through the Fetcher's internal workflow.
@@ -867,16 +891,16 @@ class D {
867
891
  ...t,
868
892
  headers: s,
869
893
  timeout: X(t.timeout, this.timeout)
870
- }, { resultExtractor: n, attributes: u } = A(
894
+ }, { resultExtractor: n, attributes: i } = A(
871
895
  v,
872
896
  r
873
- ), c = new ht({
897
+ ), a = new Rt({
874
898
  fetcher: this,
875
899
  request: o,
876
900
  resultExtractor: n,
877
- attributes: u
901
+ attributes: i
878
902
  });
879
- return this.interceptors.exchange(c);
903
+ return this.interceptors.exchange(a);
880
904
  }
881
905
  /**
882
906
  * Processes an HTTP request through the Fetcher's internal workflow.
@@ -974,7 +998,7 @@ class D {
974
998
  * @returns Promise that resolves to the HTTP response
975
999
  */
976
1000
  async get(t, r = {}, s) {
977
- return this.methodFetch(i.GET, t, r, s);
1001
+ return this.methodFetch(u.GET, t, r, s);
978
1002
  }
979
1003
  /**
980
1004
  * Makes a PUT HTTP request.
@@ -993,7 +1017,7 @@ class D {
993
1017
  * @returns Promise that resolves to the HTTP response
994
1018
  */
995
1019
  async put(t, r = {}, s) {
996
- return this.methodFetch(i.PUT, t, r, s);
1020
+ return this.methodFetch(u.PUT, t, r, s);
997
1021
  }
998
1022
  /**
999
1023
  * Makes a POST HTTP request.
@@ -1012,7 +1036,7 @@ class D {
1012
1036
  * @returns Promise that resolves to the HTTP response
1013
1037
  */
1014
1038
  async post(t, r = {}, s) {
1015
- return this.methodFetch(i.POST, t, r, s);
1039
+ return this.methodFetch(u.POST, t, r, s);
1016
1040
  }
1017
1041
  /**
1018
1042
  * Makes a PATCH HTTP request.
@@ -1031,7 +1055,7 @@ class D {
1031
1055
  * @returns Promise that resolves to the HTTP response
1032
1056
  */
1033
1057
  async patch(t, r = {}, s) {
1034
- return this.methodFetch(i.PATCH, t, r, s);
1058
+ return this.methodFetch(u.PATCH, t, r, s);
1035
1059
  }
1036
1060
  /**
1037
1061
  * Makes a DELETE HTTP request.
@@ -1050,7 +1074,7 @@ class D {
1050
1074
  * @returns Promise that resolves to the HTTP response
1051
1075
  */
1052
1076
  async delete(t, r = {}, s) {
1053
- return this.methodFetch(i.DELETE, t, r, s);
1077
+ return this.methodFetch(u.DELETE, t, r, s);
1054
1078
  }
1055
1079
  /**
1056
1080
  * Makes a HEAD HTTP request.
@@ -1070,7 +1094,7 @@ class D {
1070
1094
  * @returns Promise that resolves to the HTTP response
1071
1095
  */
1072
1096
  async head(t, r = {}, s) {
1073
- return this.methodFetch(i.HEAD, t, r, s);
1097
+ return this.methodFetch(u.HEAD, t, r, s);
1074
1098
  }
1075
1099
  /**
1076
1100
  * Makes an OPTIONS HTTP request.
@@ -1090,7 +1114,7 @@ class D {
1090
1114
  * @returns Promise that resolves to the HTTP response
1091
1115
  */
1092
1116
  async options(t, r = {}, s) {
1093
- return this.methodFetch(i.OPTIONS, t, r, s);
1117
+ return this.methodFetch(u.OPTIONS, t, r, s);
1094
1118
  }
1095
1119
  /**
1096
1120
  * Sends an HTTP TRACE request to the specified URL and returns the response.
@@ -1108,11 +1132,11 @@ class D {
1108
1132
  * The response contains status, headers, and body data from the TRACE request.
1109
1133
  */
1110
1134
  async trace(t, r = {}, s) {
1111
- return this.methodFetch(i.TRACE, t, r, s);
1135
+ return this.methodFetch(u.TRACE, t, r, s);
1112
1136
  }
1113
1137
  }
1114
1138
  const _ = "default";
1115
- class lt {
1139
+ class dt {
1116
1140
  constructor() {
1117
1141
  this.registrar = /* @__PURE__ */ new Map();
1118
1142
  }
@@ -1212,11 +1236,11 @@ class lt {
1212
1236
  return new Map(this.registrar);
1213
1237
  }
1214
1238
  }
1215
- const O = new lt();
1216
- function Rt(e, t) {
1239
+ const O = new dt();
1240
+ function ft(e, t) {
1217
1241
  return e ? e instanceof D ? e : O.requiredGet(e) : t ?? O.default;
1218
1242
  }
1219
- class Et extends D {
1243
+ class pt extends D {
1220
1244
  /**
1221
1245
  * Create a NamedFetcher instance and automatically register it with the global fetcherRegistrar
1222
1246
  *
@@ -1238,61 +1262,64 @@ class Et extends D {
1238
1262
  super(r), this.name = t, O.register(t, this);
1239
1263
  }
1240
1264
  }
1241
- const pt = new Et(_);
1265
+ const mt = new pt(_);
1242
1266
  export {
1243
- a as CONTENT_TYPE_HEADER,
1244
- d as ContentTypeValues,
1267
+ ht as ArrayBufferResultExtractor,
1268
+ ct as BlobResultExtractor,
1269
+ lt as BytesResultExtractor,
1270
+ c as CONTENT_TYPE_HEADER,
1271
+ R as ContentTypeValues,
1245
1272
  _ as DEFAULT_FETCHER_NAME,
1246
1273
  N as DEFAULT_FETCH_OPTIONS,
1247
1274
  U as DEFAULT_OPTIONS,
1248
1275
  v as DEFAULT_REQUEST_OPTIONS,
1249
1276
  p as ExchangeError,
1250
1277
  nt as ExchangeResultExtractor,
1251
- g as ExpressUrlTemplateResolver,
1278
+ y as ExpressUrlTemplateResolver,
1252
1279
  K as FETCH_INTERCEPTOR_NAME,
1253
1280
  W as FETCH_INTERCEPTOR_ORDER,
1254
- ht as FetchExchange,
1281
+ Rt as FetchExchange,
1255
1282
  Z as FetchInterceptor,
1256
- I as FetchTimeoutError,
1283
+ q as FetchTimeoutError,
1257
1284
  D as Fetcher,
1258
1285
  E as FetcherError,
1259
- lt as FetcherRegistrar,
1260
- i as HttpMethod,
1261
- q as HttpStatusValidationError,
1286
+ dt as FetcherRegistrar,
1287
+ u as HttpMethod,
1288
+ b as HttpStatusValidationError,
1262
1289
  ot as InterceptorManager,
1263
- f as InterceptorRegistry,
1264
- ut as JsonResultExtractor,
1265
- Et as NamedFetcher,
1290
+ m as InterceptorRegistry,
1291
+ it as JsonResultExtractor,
1292
+ pt as NamedFetcher,
1266
1293
  Q as REQUEST_BODY_INTERCEPTOR_NAME,
1267
1294
  Y as REQUEST_BODY_INTERCEPTOR_ORDER,
1268
1295
  z as RequestBodyInterceptor,
1269
- it as ResponseResultExtractor,
1270
- b as ResultExtractors,
1271
- ct as TextResultExtractor,
1296
+ ut as ResponseResultExtractor,
1297
+ I as ResultExtractors,
1298
+ at as TextResultExtractor,
1272
1299
  k as URL_RESOLVE_INTERCEPTOR_NAME,
1273
1300
  C as URL_RESOLVE_INTERCEPTOR_ORDER,
1274
1301
  P as UriTemplateResolver,
1275
1302
  $ as UrlBuilder,
1276
1303
  J as UrlResolveInterceptor,
1277
- G as UrlTemplateStyle,
1304
+ M as UrlTemplateStyle,
1278
1305
  et as VALIDATE_STATUS_INTERCEPTOR_NAME,
1279
1306
  rt as VALIDATE_STATUS_INTERCEPTOR_ORDER,
1280
1307
  st as ValidateStatusInterceptor,
1281
- M as combineURLs,
1308
+ B as combineURLs,
1282
1309
  j as expressUrlTemplateResolver,
1283
- pt as fetcher,
1310
+ mt as fetcher,
1284
1311
  O as fetcherRegistrar,
1285
- Rt as getFetcher,
1286
- H as getUrlTemplateResolver,
1312
+ ft as getFetcher,
1313
+ G as getUrlTemplateResolver,
1287
1314
  L as isAbsoluteURL,
1288
- at as mergeRecordToMap,
1315
+ Et as mergeRecordToMap,
1289
1316
  S as mergeRecords,
1290
- dt as mergeRequest,
1317
+ Tt as mergeRequest,
1291
1318
  A as mergeRequestOptions,
1292
1319
  X as resolveTimeout,
1293
1320
  V as timeoutFetch,
1294
- m as toSorted,
1295
- B as uriTemplateResolver,
1321
+ f as toSorted,
1322
+ H as uriTemplateResolver,
1296
1323
  F as urlTemplateRegexExtract,
1297
1324
  x as urlTemplateRegexResolve
1298
1325
  };