@ahoo-wang/fetcher 2.8.2 → 2.8.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,14 +1,14 @@
1
- function L(e) {
1
+ function M(e) {
2
2
  return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(e);
3
3
  }
4
- function B(e, t) {
5
- return L(t) ? t : t ? e.replace(/\/?\/$/, "") + "/" + t.replace(/^\/+/, "") : e;
4
+ function G(e, t) {
5
+ return M(t) ? t : t ? e.replace(/\/?\/$/, "") + "/" + t.replace(/^\/+/, "") : e;
6
6
  }
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;
7
+ var H = /* @__PURE__ */ ((e) => (e[e.UriTemplate = 0] = "UriTemplate", e[e.Express = 1] = "Express", e))(H || {});
8
+ function j(e) {
9
+ return e === 1 ? X : $;
10
10
  }
11
- function x(e, t, r) {
11
+ function C(e, t, r) {
12
12
  return r ? e.replace(t, (s, o) => {
13
13
  const n = r[o];
14
14
  if (n === void 0)
@@ -16,7 +16,7 @@ function x(e, t, r) {
16
16
  return encodeURIComponent(n);
17
17
  }) : e;
18
18
  }
19
- function F(e, t) {
19
+ function U(e, t) {
20
20
  const r = [];
21
21
  let s;
22
22
  for (; (s = t.exec(e)) !== null; )
@@ -48,7 +48,7 @@ const h = class h {
48
48
  * ```
49
49
  */
50
50
  extractPathParams(t) {
51
- return F(
51
+ return U(
52
52
  t,
53
53
  h.PATH_PARAM_REGEX
54
54
  );
@@ -90,7 +90,7 @@ const h = class h {
90
90
  * ```
91
91
  */
92
92
  resolve(t, r) {
93
- return x(
93
+ return C(
94
94
  t,
95
95
  h.PATH_PARAM_REGEX,
96
96
  r
@@ -99,7 +99,7 @@ const h = class h {
99
99
  };
100
100
  h.PATH_PARAM_REGEX = /{([^}]+)}/g;
101
101
  let P = h;
102
- const H = new P(), l = class l {
102
+ const $ = new P(), l = class l {
103
103
  /**
104
104
  * Extracts path parameters from an Express-style URL string.
105
105
  *
@@ -120,7 +120,7 @@ const H = new P(), l = class l {
120
120
  * ```
121
121
  */
122
122
  extractPathParams(t) {
123
- return F(
123
+ return U(
124
124
  t,
125
125
  l.PATH_PARAM_REGEX
126
126
  );
@@ -158,7 +158,7 @@ const H = new P(), l = class l {
158
158
  * ```
159
159
  */
160
160
  resolve(t, r) {
161
- return x(
161
+ return C(
162
162
  t,
163
163
  l.PATH_PARAM_REGEX,
164
164
  r
@@ -167,8 +167,8 @@ const H = new P(), l = class l {
167
167
  };
168
168
  l.PATH_PARAM_REGEX = /:([^/]+)/g;
169
169
  let y = l;
170
- const j = new y();
171
- class $ {
170
+ const X = new y();
171
+ class V {
172
172
  /**
173
173
  * Initializes a new UrlBuilder instance.
174
174
  *
@@ -187,7 +187,7 @@ class $ {
187
187
  * ```
188
188
  */
189
189
  constructor(t, r) {
190
- this.baseURL = t, this.urlTemplateResolver = G(r);
190
+ this.baseURL = t, this.urlTemplateResolver = j(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 = B(this.baseURL, t);
212
- let a = this.urlTemplateResolver.resolve(n, s);
211
+ const s = r?.path, o = r?.query, n = G(this.baseURL, t);
212
+ let u = this.urlTemplateResolver.resolve(n, s);
213
213
  if (o) {
214
- const u = new URLSearchParams(o).toString();
215
- u && (a += "?" + u);
214
+ const a = new URLSearchParams(o).toString();
215
+ a && (u += "?" + a);
216
216
  }
217
- return a;
217
+ return u;
218
218
  }
219
219
  /**
220
220
  * Resolves a complete URL from a FetchRequest.
@@ -253,7 +253,7 @@ class E extends p {
253
253
  super(s, t.error), this.exchange = t, this.name = "ExchangeError", Object.setPrototypeOf(this, E.prototype);
254
254
  }
255
255
  }
256
- class O extends p {
256
+ class _ extends p {
257
257
  /**
258
258
  * Creates a new FetchTimeoutError instance.
259
259
  *
@@ -261,13 +261,13 @@ class O extends p {
261
261
  */
262
262
  constructor(t) {
263
263
  const r = t.method || "GET", s = `Request timeout of ${t.timeout}ms exceeded for ${r} ${t.url}`;
264
- super(s), this.name = "FetchTimeoutError", this.request = t, Object.setPrototypeOf(this, O.prototype);
264
+ super(s), this.name = "FetchTimeoutError", this.request = t, Object.setPrototypeOf(this, _.prototype);
265
265
  }
266
266
  }
267
- function X(e, t) {
267
+ function k(e, t) {
268
268
  return typeof e < "u" ? e : t;
269
269
  }
270
- async function V(e) {
270
+ async function J(e) {
271
271
  const t = e.url, r = e.timeout, s = e;
272
272
  if (e.signal)
273
273
  return await fetch(t, s);
@@ -276,54 +276,54 @@ async function V(e) {
276
276
  const o = e.abortController ?? new AbortController();
277
277
  e.abortController = o, s.signal = o.signal;
278
278
  let n = null;
279
- const a = new Promise((u, T) => {
279
+ const u = new Promise((a, T) => {
280
280
  n = setTimeout(() => {
281
281
  n && clearTimeout(n);
282
- const I = new O(e);
282
+ const I = new _(e);
283
283
  o.abort(I), T(I);
284
284
  }, r);
285
285
  });
286
286
  try {
287
- return await Promise.race([fetch(t, s), a]);
287
+ return await Promise.race([fetch(t, s), u]);
288
288
  } finally {
289
289
  n && clearTimeout(n);
290
290
  }
291
291
  }
292
- const k = (e) => e, J = (e) => e.requiredResponse, Q = (e) => e.requiredResponse.json(), Y = (e) => e.requiredResponse.text(), z = (e) => e.requiredResponse.blob(), K = (e) => e.requiredResponse.arrayBuffer(), W = (e) => e.requiredResponse.bytes(), q = {
292
+ const Q = (e) => e, Y = (e) => e.requiredResponse, z = (e) => e.requiredResponse.json(), K = (e) => e.requiredResponse.text(), W = (e) => e.requiredResponse.blob(), Z = (e) => e.requiredResponse.arrayBuffer(), tt = (e) => e.requiredResponse.bytes(), q = {
293
293
  /**
294
294
  * Returns the original FetchExchange object
295
295
  */
296
- Exchange: k,
296
+ Exchange: Q,
297
297
  /**
298
298
  * Extracts the raw Response object
299
299
  */
300
- Response: J,
300
+ Response: Y,
301
301
  /**
302
302
  * Parses and returns response body as JSON
303
303
  */
304
- Json: Q,
304
+ Json: z,
305
305
  /**
306
306
  * Returns response body as text
307
307
  */
308
- Text: Y,
308
+ Text: K,
309
309
  /**
310
310
  * Returns response body as a Blob
311
311
  */
312
- Blob: z,
312
+ Blob: W,
313
313
  /**
314
314
  * Returns response body as an ArrayBuffer
315
315
  */
316
- ArrayBuffer: K,
316
+ ArrayBuffer: Z,
317
317
  /**
318
318
  * Returns response body as a Uint8Array
319
319
  */
320
- Bytes: W
320
+ Bytes: tt
321
321
  };
322
322
  function S(e, t) {
323
323
  if (!(e === void 0 && t === void 0))
324
324
  return t === void 0 ? e : e === void 0 ? t : { ...e, ...t };
325
325
  }
326
- function Z(e, t) {
326
+ function et(e, t) {
327
327
  if (t ??= /* @__PURE__ */ new Map(), !e)
328
328
  return t;
329
329
  if (e instanceof Map) {
@@ -335,9 +335,9 @@ function Z(e, t) {
335
335
  t.set(r, s);
336
336
  return t;
337
337
  }
338
- class tt {
338
+ class rt {
339
339
  constructor(t) {
340
- this.fetcher = t.fetcher, this.request = t.request, this.resultExtractor = t.resultExtractor ?? q.Exchange, this.attributes = Z(t.attributes), this._response = t.response, this.error = t.error;
340
+ this.fetcher = t.fetcher, this.request = t.request, this.resultExtractor = t.resultExtractor ?? q.Exchange, this.attributes = et(t.attributes), this._response = t.response, this.error = t.error;
341
341
  }
342
342
  /**
343
343
  * Ensures that request headers object exists, creating it if necessary.
@@ -435,10 +435,10 @@ const c = "Content-Type", d = class d {
435
435
  };
436
436
  d.APPLICATION_JSON = "application/json", d.TEXT_EVENT_STREAM = "text/event-stream";
437
437
  let R = d;
438
- const et = "UrlResolveInterceptor", C = Number.MIN_SAFE_INTEGER + 1e3;
439
- class rt {
438
+ const st = "UrlResolveInterceptor", D = Number.MIN_SAFE_INTEGER + 1e3;
439
+ class ot {
440
440
  constructor() {
441
- this.name = et, this.order = C;
441
+ this.name = st, this.order = D;
442
442
  }
443
443
  /**
444
444
  * Resolves the final URL by combining the base URL, path parameters, and query parameters.
@@ -450,10 +450,10 @@ class rt {
450
450
  r.url = t.fetcher.urlBuilder.resolveRequestUrl(r);
451
451
  }
452
452
  }
453
- const st = "RequestBodyInterceptor", ot = C + 1e3;
454
- class nt {
453
+ const nt = "RequestBodyInterceptor", it = D + 1e3;
454
+ class ut {
455
455
  constructor() {
456
- this.name = st, this.order = ot;
456
+ this.name = nt, this.order = it;
457
457
  }
458
458
  /**
459
459
  * Checks if the provided body is of a supported complex type that doesn't require JSON serialization.
@@ -526,10 +526,10 @@ class nt {
526
526
  this.isSupportedComplexBodyType(r.body) || (t.request.body = JSON.stringify(r.body), s[c] || (s[c] = R.APPLICATION_JSON));
527
527
  }
528
528
  }
529
- const it = "FetchInterceptor", at = Number.MAX_SAFE_INTEGER - 1e3;
530
- class ut {
529
+ const at = "FetchInterceptor", ct = Number.MAX_SAFE_INTEGER - 1e3;
530
+ class ht {
531
531
  constructor() {
532
- this.name = it, this.order = at;
532
+ this.name = at, this.order = ct;
533
533
  }
534
534
  /**
535
535
  * Intercept and process HTTP requests.
@@ -557,11 +557,15 @@ class ut {
557
557
  * console.log(exchange.response); // HTTP response object
558
558
  */
559
559
  async intercept(t) {
560
- t.response = await V(t.request);
560
+ t.response = await J(t.request);
561
561
  }
562
562
  }
563
+ const N = 0;
564
+ function x(e, t) {
565
+ return (e.order ?? N) - (t.order ?? N);
566
+ }
563
567
  function f(e, t) {
564
- return t ? e.filter(t).sort((r, s) => r.order - s.order) : [...e].sort((r, s) => r.order - s.order);
568
+ return t ? e.filter(t).sort(x) : [...e].sort(x);
565
569
  }
566
570
  class m {
567
571
  /**
@@ -664,14 +668,14 @@ class b extends E {
664
668
  ), this.name = "HttpStatusValidationError", Object.setPrototypeOf(this, b.prototype);
665
669
  }
666
670
  }
667
- const ct = (e) => e >= 200 && e < 300, ht = "ValidateStatusInterceptor", lt = Number.MAX_SAFE_INTEGER - 1e3;
668
- class Et {
671
+ const lt = (e) => e >= 200 && e < 300, Et = "ValidateStatusInterceptor", Rt = Number.MAX_SAFE_INTEGER - 1e3;
672
+ class dt {
669
673
  /**
670
674
  * Creates a new ValidateStatusInterceptor instance.
671
675
  *
672
676
  * @param validateStatus - Function that determines if a status code is valid
673
677
  */
674
- constructor(t = ct) {
678
+ constructor(t = lt) {
675
679
  this.validateStatus = t;
676
680
  }
677
681
  /**
@@ -680,7 +684,7 @@ class Et {
680
684
  * @returns The name of this interceptor
681
685
  */
682
686
  get name() {
683
- return ht;
687
+ return Et;
684
688
  }
685
689
  /**
686
690
  * Gets the order of this interceptor.
@@ -688,7 +692,7 @@ class Et {
688
692
  * @returns VALIDATE_STATUS_INTERCEPTOR_ORDER, indicating this interceptor should execute early
689
693
  */
690
694
  get order() {
691
- return lt;
695
+ return Rt;
692
696
  }
693
697
  /**
694
698
  * Validates the response status code.
@@ -711,14 +715,14 @@ class Et {
711
715
  throw new b(t);
712
716
  }
713
717
  }
714
- class Rt {
718
+ class pt {
715
719
  constructor() {
716
720
  this.request = new m([
717
- new rt(),
718
- new nt(),
719
- new ut()
721
+ new ot(),
722
+ new ut(),
723
+ new ht()
720
724
  ]), this.response = new m([
721
- new Et()
725
+ new dt()
722
726
  ]), this.error = new m();
723
727
  }
724
728
  /**
@@ -817,7 +821,7 @@ class Rt {
817
821
  }
818
822
  }
819
823
  }
820
- function Tt(e, t) {
824
+ function mt(e, t) {
821
825
  if (Object.keys(e).length === 0)
822
826
  return t;
823
827
  if (Object.keys(t).length === 0)
@@ -828,7 +832,7 @@ function Tt(e, t) {
828
832
  }, s = {
829
833
  ...e.headers,
830
834
  ...t.headers
831
- }, o = t.method ?? e.method, n = t.body ?? e.body, a = t.timeout ?? e.timeout, u = t.signal ?? e.signal, T = t.abortController ?? e.abortController;
835
+ }, o = t.method ?? e.method, n = t.body ?? e.body, u = t.timeout ?? e.timeout, a = t.signal ?? e.signal, T = t.abortController ?? e.abortController;
832
836
  return {
833
837
  ...e,
834
838
  ...t,
@@ -836,8 +840,8 @@ function Tt(e, t) {
836
840
  urlParams: r,
837
841
  headers: s,
838
842
  body: n,
839
- timeout: a,
840
- signal: u,
843
+ timeout: u,
844
+ signal: a,
841
845
  abortController: T
842
846
  };
843
847
  }
@@ -849,15 +853,15 @@ function A(e, t) {
849
853
  }
850
854
  const w = {
851
855
  [c]: R.APPLICATION_JSON
852
- }, U = {
856
+ }, L = {
853
857
  baseURL: "",
854
858
  headers: w
855
859
  }, v = {
856
860
  resultExtractor: q.Exchange
857
- }, N = {
861
+ }, F = {
858
862
  resultExtractor: q.Response
859
863
  };
860
- class D {
864
+ class B {
861
865
  /**
862
866
  * Initializes a new Fetcher instance with optional configuration.
863
867
  *
@@ -871,8 +875,8 @@ class D {
871
875
  * @param options.urlTemplateStyle - Style for URL template parameter interpolation.
872
876
  * @param options.interceptors - Interceptor manager for processing requests and responses.
873
877
  */
874
- constructor(t = U) {
875
- this.headers = w, this.urlBuilder = new $(t.baseURL, t.urlTemplateStyle), this.headers = t.headers ?? w, this.timeout = t.timeout, this.interceptors = t.interceptors ?? new Rt();
878
+ constructor(t = L) {
879
+ this.headers = w, this.urlBuilder = new V(t.baseURL, t.urlTemplateStyle), this.headers = t.headers ?? w, this.timeout = t.timeout, this.interceptors = t.interceptors ?? new pt();
876
880
  }
877
881
  /**
878
882
  * Processes an HTTP request through the Fetcher's internal workflow.
@@ -891,17 +895,17 @@ class D {
891
895
  }, o = {
892
896
  ...t,
893
897
  headers: s,
894
- timeout: X(t.timeout, this.timeout)
895
- }, { resultExtractor: n, attributes: a } = A(
898
+ timeout: k(t.timeout, this.timeout)
899
+ }, { resultExtractor: n, attributes: u } = A(
896
900
  v,
897
901
  r
898
- ), u = new tt({
902
+ ), a = new rt({
899
903
  fetcher: this,
900
904
  request: o,
901
905
  resultExtractor: n,
902
- attributes: a
906
+ attributes: u
903
907
  });
904
- return await this.interceptors.exchange(u);
908
+ return await this.interceptors.exchange(a);
905
909
  }
906
910
  /**
907
911
  * Processes an HTTP request through the Fetcher's internal workflow.
@@ -949,7 +953,7 @@ class D {
949
953
  };
950
954
  return await this.request(
951
955
  o,
952
- A(N, s)
956
+ A(F, s)
953
957
  );
954
958
  }
955
959
  /**
@@ -978,7 +982,7 @@ class D {
978
982
  };
979
983
  return await this.request(
980
984
  n,
981
- A(N, o)
985
+ A(F, o)
982
986
  );
983
987
  }
984
988
  /**
@@ -1137,7 +1141,7 @@ class D {
1137
1141
  }
1138
1142
  }
1139
1143
  const g = "default";
1140
- class dt {
1144
+ class Tt {
1141
1145
  constructor() {
1142
1146
  this.registrar = /* @__PURE__ */ new Map();
1143
1147
  }
@@ -1237,11 +1241,11 @@ class dt {
1237
1241
  return new Map(this.registrar);
1238
1242
  }
1239
1243
  }
1240
- const _ = new dt();
1241
- function ft(e, t) {
1242
- return e ? e instanceof D ? e : _.requiredGet(e) : t ?? _.default;
1244
+ const O = new Tt();
1245
+ function At(e, t) {
1246
+ return e ? e instanceof B ? e : O.requiredGet(e) : t ?? O.default;
1243
1247
  }
1244
- class pt extends D {
1248
+ class ft extends B {
1245
1249
  /**
1246
1250
  * Create a NamedFetcher instance and automatically register it with the global fetcherRegistrar
1247
1251
  *
@@ -1259,69 +1263,70 @@ class pt extends D {
1259
1263
  * headers: { 'Authorization': 'Bearer token' }
1260
1264
  * });
1261
1265
  */
1262
- constructor(t, r = U) {
1263
- super(r), this.name = t, _.register(t, this);
1266
+ constructor(t, r = L) {
1267
+ super(r), this.name = t, O.register(t, this);
1264
1268
  }
1265
1269
  }
1266
- const mt = new pt(g);
1270
+ const Pt = new ft(g);
1267
1271
  export {
1268
- K as ArrayBufferResultExtractor,
1269
- z as BlobResultExtractor,
1270
- W as BytesResultExtractor,
1272
+ Z as ArrayBufferResultExtractor,
1273
+ W as BlobResultExtractor,
1274
+ tt as BytesResultExtractor,
1271
1275
  c as CONTENT_TYPE_HEADER,
1272
1276
  R as ContentTypeValues,
1273
1277
  g as DEFAULT_FETCHER_NAME,
1274
- N as DEFAULT_FETCH_OPTIONS,
1275
- U as DEFAULT_OPTIONS,
1278
+ F as DEFAULT_FETCH_OPTIONS,
1279
+ L as DEFAULT_OPTIONS,
1276
1280
  v as DEFAULT_REQUEST_OPTIONS,
1277
1281
  E as ExchangeError,
1278
- k as ExchangeResultExtractor,
1282
+ Q as ExchangeResultExtractor,
1279
1283
  y as ExpressUrlTemplateResolver,
1280
- it as FETCH_INTERCEPTOR_NAME,
1281
- at as FETCH_INTERCEPTOR_ORDER,
1282
- tt as FetchExchange,
1283
- ut as FetchInterceptor,
1284
- O as FetchTimeoutError,
1285
- D as Fetcher,
1284
+ at as FETCH_INTERCEPTOR_NAME,
1285
+ ct as FETCH_INTERCEPTOR_ORDER,
1286
+ rt as FetchExchange,
1287
+ ht as FetchInterceptor,
1288
+ _ as FetchTimeoutError,
1289
+ B as Fetcher,
1286
1290
  p as FetcherError,
1287
- dt as FetcherRegistrar,
1291
+ Tt as FetcherRegistrar,
1288
1292
  i as HttpMethod,
1289
1293
  b as HttpStatusValidationError,
1290
- Rt as InterceptorManager,
1294
+ pt as InterceptorManager,
1291
1295
  m as InterceptorRegistry,
1292
- Q as JsonResultExtractor,
1293
- pt as NamedFetcher,
1294
- st as REQUEST_BODY_INTERCEPTOR_NAME,
1295
- ot as REQUEST_BODY_INTERCEPTOR_ORDER,
1296
- nt as RequestBodyInterceptor,
1297
- J as ResponseResultExtractor,
1296
+ z as JsonResultExtractor,
1297
+ ft as NamedFetcher,
1298
+ nt as REQUEST_BODY_INTERCEPTOR_NAME,
1299
+ it as REQUEST_BODY_INTERCEPTOR_ORDER,
1300
+ ut as RequestBodyInterceptor,
1301
+ Y as ResponseResultExtractor,
1298
1302
  q as ResultExtractors,
1299
- Y as TextResultExtractor,
1300
- et as URL_RESOLVE_INTERCEPTOR_NAME,
1301
- C as URL_RESOLVE_INTERCEPTOR_ORDER,
1303
+ K as TextResultExtractor,
1304
+ st as URL_RESOLVE_INTERCEPTOR_NAME,
1305
+ D as URL_RESOLVE_INTERCEPTOR_ORDER,
1302
1306
  P as UriTemplateResolver,
1303
- $ as UrlBuilder,
1304
- rt as UrlResolveInterceptor,
1305
- M as UrlTemplateStyle,
1306
- ht as VALIDATE_STATUS_INTERCEPTOR_NAME,
1307
- lt as VALIDATE_STATUS_INTERCEPTOR_ORDER,
1308
- Et as ValidateStatusInterceptor,
1309
- B as combineURLs,
1310
- j as expressUrlTemplateResolver,
1311
- mt as fetcher,
1312
- _ as fetcherRegistrar,
1313
- ft as getFetcher,
1314
- G as getUrlTemplateResolver,
1315
- L as isAbsoluteURL,
1316
- Z as mergeRecordToMap,
1307
+ V as UrlBuilder,
1308
+ ot as UrlResolveInterceptor,
1309
+ H as UrlTemplateStyle,
1310
+ Et as VALIDATE_STATUS_INTERCEPTOR_NAME,
1311
+ Rt as VALIDATE_STATUS_INTERCEPTOR_ORDER,
1312
+ dt as ValidateStatusInterceptor,
1313
+ G as combineURLs,
1314
+ X as expressUrlTemplateResolver,
1315
+ Pt as fetcher,
1316
+ O as fetcherRegistrar,
1317
+ At as getFetcher,
1318
+ j as getUrlTemplateResolver,
1319
+ M as isAbsoluteURL,
1320
+ et as mergeRecordToMap,
1317
1321
  S as mergeRecords,
1318
- Tt as mergeRequest,
1322
+ mt as mergeRequest,
1319
1323
  A as mergeRequestOptions,
1320
- X as resolveTimeout,
1321
- V as timeoutFetch,
1324
+ k as resolveTimeout,
1325
+ x as sortOrder,
1326
+ J as timeoutFetch,
1322
1327
  f as toSorted,
1323
- H as uriTemplateResolver,
1324
- F as urlTemplateRegexExtract,
1325
- x as urlTemplateRegexResolve
1328
+ $ as uriTemplateResolver,
1329
+ U as urlTemplateRegexExtract,
1330
+ C as urlTemplateRegexResolve
1326
1331
  };
1327
1332
  //# sourceMappingURL=index.es.js.map