@ahoo-wang/fetcher 1.3.2 → 1.3.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.
@@ -105,6 +105,28 @@ export interface FetchRequestInit extends TimeoutCapable, RequestHeadersCapable,
105
105
  * ```
106
106
  */
107
107
  body?: BodyInit | Record<string, any> | string | null;
108
+ /**
109
+ * AbortController for this request.
110
+ * Used to cancel the request if needed.
111
+ *
112
+ * In timeout scenarios, if this property is provided, it will be used instead of creating a new AbortController.
113
+ * This allows the caller to provide a custom AbortController for more advanced cancellation scenarios.
114
+ *
115
+ * @example
116
+ * ```typescript
117
+ * // Provide a custom AbortController
118
+ * const controller = new AbortController();
119
+ * const request: FetchRequest = {
120
+ * url: 'https://api.example.com/data',
121
+ * method: 'GET',
122
+ * abortController: controller
123
+ * };
124
+ *
125
+ * // Later, cancel the request
126
+ * controller.abort();
127
+ * ```
128
+ */
129
+ abortController?: AbortController;
108
130
  }
109
131
  /**
110
132
  * Fetcher request interface
@@ -1 +1 @@
1
- {"version":3,"file":"fetchRequest.d.ts","sourceRoot":"","sources":["../src/fetchRequest.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,KAAK,cAAc,EAAE,MAAM,WAAW,CAAC;AAChD,OAAO,EAAE,KAAK,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C;;;;;GAKG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;CACjB;AAED;;;;;GAKG;AACH,oBAAY,UAAU;IACpB,GAAG,QAAQ;IACX,IAAI,SAAS;IACb,GAAG,QAAQ;IACX,MAAM,WAAW;IACjB,KAAK,UAAU;IACf,IAAI,SAAS;IACb,OAAO,YAAY;CACpB;AAED,eAAO,MAAM,mBAAmB,iBAAiB,CAAC;AAElD,qBAAa,iBAAiB;IAC5B,MAAM,CAAC,QAAQ,CAAC,gBAAgB,sBAAsB;IACtD,MAAM,CAAC,QAAQ,CAAC,iB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mBAAmB,iBAAiB,CAAC;AAElD,qBAAa,iBAAiB;IAC5B,MAAM,CAAC,QAAQ,CAAC,gBAAgB,sBAAsB;IACtD,MAAM,CAAC,QAAQ,CAAC,iBAAiB,uBAAuB;CACzD;AAED;;;;;GAKG;AACH,MAAM,WAAW,cAAc;IAC7B,CAAC,mBAAmB,CAAC,CAAC,EAAE,MAAM,CAAC;IAC/B,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAAC;CACnC;AAED;;;;GAIG;AACH,MAAM,WAAW,qBAAqB;IACpC;;OAEG;IACH,OAAO,CAAC,EAAE,cAAc,CAAC;CAC1B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,MAAM,WAAW,gBACf,SAAQ,cAAc,EACpB,qBAAqB,EACrB,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC,SAAS,CAAC,EAAE,SAAS,CAAC;IAEtB;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,IAAI,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM,GAAG,IAAI,CAAC;IAEtD;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,eAAe,CAAC,EAAE,eAAe,CAAC;CACnC;AAED;;;;;GAKG;AACH,MAAM,WAAW,YAAa,SAAQ,gBAAgB;IACpD;;OAEG;IACH,GAAG,EAAE,MAAM,CAAC;CACb"}
package/dist/index.es.js CHANGED
@@ -4,26 +4,26 @@ function S(r) {
4
4
  function F(r, e) {
5
5
  return S(e) ? e : e ? r.replace(/\/?\/$/, "") + "/" + e.replace(/^\/+/, "") : r;
6
6
  }
7
- var U = /* @__PURE__ */ ((r) => (r[r.UriTemplate = 0] = "UriTemplate", r[r.Express = 1] = "Express", r))(U || {});
8
- function D(r) {
9
- return r === 1 ? C : L;
7
+ var C = /* @__PURE__ */ ((r) => (r[r.UriTemplate = 0] = "UriTemplate", r[r.Express = 1] = "Express", r))(C || {});
8
+ function U(r) {
9
+ return r === 1 ? L : D;
10
10
  }
11
11
  function _(r, e, t) {
12
12
  return t ? r.replace(e, (s, n) => {
13
- const i = t[n];
14
- if (i === void 0)
13
+ const o = t[n];
14
+ if (o === void 0)
15
15
  throw new Error(`Missing required path parameter: ${n}`);
16
- return encodeURIComponent(i);
16
+ return encodeURIComponent(o);
17
17
  }) : r;
18
18
  }
19
- function g(r, e) {
19
+ function b(r, e) {
20
20
  const t = [];
21
21
  let s;
22
22
  for (; (s = e.exec(r)) !== null; )
23
23
  t.push(s[1]);
24
24
  return t;
25
25
  }
26
- const a = class a {
26
+ const c = class c {
27
27
  /**
28
28
  * Extracts path parameters from a URL string.
29
29
  *
@@ -48,9 +48,9 @@ const a = class a {
48
48
  * ```
49
49
  */
50
50
  extractPathParams(e) {
51
- return g(
51
+ return b(
52
52
  e,
53
- a.PATH_PARAM_REGEX
53
+ c.PATH_PARAM_REGEX
54
54
  );
55
55
  }
56
56
  /**
@@ -92,14 +92,14 @@ const a = class a {
92
92
  resolve(e, t) {
93
93
  return _(
94
94
  e,
95
- a.PATH_PARAM_REGEX,
95
+ c.PATH_PARAM_REGEX,
96
96
  t
97
97
  );
98
98
  }
99
99
  };
100
- a.PATH_PARAM_REGEX = /{([^}]+)}/g;
101
- let T = a;
102
- const L = new T(), h = class h {
100
+ c.PATH_PARAM_REGEX = /{([^}]+)}/g;
101
+ let T = c;
102
+ const D = new T(), h = class h {
103
103
  /**
104
104
  * Extracts path parameters from an Express-style URL string.
105
105
  *
@@ -120,7 +120,7 @@ const L = new T(), h = class h {
120
120
  * ```
121
121
  */
122
122
  extractPathParams(e) {
123
- return g(
123
+ return b(
124
124
  e,
125
125
  h.PATH_PARAM_REGEX
126
126
  );
@@ -167,7 +167,7 @@ const L = new T(), h = class h {
167
167
  };
168
168
  h.PATH_PARAM_REGEX = /:([^/]+)/g;
169
169
  let f = h;
170
- const C = new f();
170
+ const L = new f();
171
171
  class v {
172
172
  /**
173
173
  * Initializes a new UrlBuilder instance.
@@ -187,7 +187,7 @@ class v {
187
187
  * ```
188
188
  */
189
189
  constructor(e, t) {
190
- this.baseURL = e, this.urlTemplateResolver = D(t);
190
+ this.baseURL = e, this.urlTemplateResolver = U(t);
191
191
  }
192
192
  /**
193
193
  * Builds a complete URL, including path parameter replacement and query parameter addition.
@@ -208,13 +208,13 @@ class v {
208
208
  * ```
209
209
  */
210
210
  build(e, t) {
211
- const s = t?.path, n = t?.query, i = F(this.baseURL, e);
212
- let o = this.urlTemplateResolver.resolve(i, s);
211
+ const s = t?.path, n = t?.query, o = F(this.baseURL, e);
212
+ let u = this.urlTemplateResolver.resolve(o, s);
213
213
  if (n) {
214
- const c = new URLSearchParams(n).toString();
215
- c && (o += "?" + c);
214
+ const a = new URLSearchParams(n).toString();
215
+ a && (u += "?" + a);
216
216
  }
217
- return o;
217
+ return u;
218
218
  }
219
219
  /**
220
220
  * Resolves a complete URL from a FetchRequest.
@@ -257,22 +257,22 @@ function G(r, e) {
257
257
  }
258
258
  async function M(r) {
259
259
  const e = r.url, t = r.timeout, s = r;
260
- if (!t)
260
+ if (r.signal)
261
261
  return fetch(e, s);
262
- const n = new AbortController(), i = {
263
- ...s,
264
- signal: n.signal
265
- };
262
+ if (!t)
263
+ return r.abortController && (s.signal = r.abortController.signal), fetch(e, s);
264
+ const n = r.abortController ?? new AbortController();
265
+ s.signal = n.signal;
266
266
  let o = null;
267
- const c = new Promise((se, N) => {
267
+ const u = new Promise((a, N) => {
268
268
  o = setTimeout(() => {
269
269
  o && clearTimeout(o);
270
- const q = new y(r);
271
- n.abort(q), N(q);
270
+ const g = new y(r);
271
+ n.abort(g), N(g);
272
272
  }, t);
273
273
  });
274
274
  try {
275
- return await Promise.race([fetch(e, i), c]);
275
+ return await Promise.race([fetch(e, s), u]);
276
276
  } finally {
277
277
  o && clearTimeout(o);
278
278
  }
@@ -292,8 +292,8 @@ class B {
292
292
  t.url = e.fetcher.urlBuilder.resolveRequestUrl(t);
293
293
  }
294
294
  }
295
- var u = /* @__PURE__ */ ((r) => (r.GET = "GET", r.POST = "POST", r.PUT = "PUT", r.DELETE = "DELETE", r.PATCH = "PATCH", r.HEAD = "HEAD", r.OPTIONS = "OPTIONS", r))(u || {});
296
- const ne = "Content-Type", E = class E {
295
+ var i = /* @__PURE__ */ ((r) => (r.GET = "GET", r.POST = "POST", r.PUT = "PUT", r.DELETE = "DELETE", r.PATCH = "PATCH", r.HEAD = "HEAD", r.OPTIONS = "OPTIONS", r))(i || {});
296
+ const se = "Content-Type", E = class E {
297
297
  };
298
298
  E.APPLICATION_JSON = "application/json", E.TEXT_EVENT_STREAM = "text/event-stream";
299
299
  let d = E;
@@ -721,7 +721,7 @@ class W {
721
721
  }
722
722
  const P = {
723
723
  "Content-Type": d.APPLICATION_JSON
724
- }, b = {
724
+ }, q = {
725
725
  baseURL: "",
726
726
  headers: P
727
727
  };
@@ -734,7 +734,7 @@ class Z {
734
734
  *
735
735
  * @param options - Configuration options for the Fetcher instance
736
736
  */
737
- constructor(e = b) {
737
+ constructor(e = q) {
738
738
  this.headers = P, this.urlBuilder = new v(e.baseURL, e.urlTemplateStyle), this.headers = e.headers ?? P, this.timeout = e.timeout, this.interceptors = e.interceptors ?? new K();
739
739
  }
740
740
  /**
@@ -802,7 +802,7 @@ class Z {
802
802
  * @returns Promise that resolves to the HTTP response
803
803
  */
804
804
  async get(e, t = {}) {
805
- return this.methodFetch(u.GET, e, t);
805
+ return this.methodFetch(i.GET, e, t);
806
806
  }
807
807
  /**
808
808
  * Makes a POST HTTP request.
@@ -814,7 +814,7 @@ class Z {
814
814
  * @returns Promise that resolves to the HTTP response
815
815
  */
816
816
  async post(e, t = {}) {
817
- return this.methodFetch(u.POST, e, t);
817
+ return this.methodFetch(i.POST, e, t);
818
818
  }
819
819
  /**
820
820
  * Makes a PUT HTTP request.
@@ -826,7 +826,7 @@ class Z {
826
826
  * @returns Promise that resolves to the HTTP response
827
827
  */
828
828
  async put(e, t = {}) {
829
- return this.methodFetch(u.PUT, e, t);
829
+ return this.methodFetch(i.PUT, e, t);
830
830
  }
831
831
  /**
832
832
  * Makes a DELETE HTTP request.
@@ -838,7 +838,7 @@ class Z {
838
838
  * @returns Promise that resolves to the HTTP response
839
839
  */
840
840
  async delete(e, t = {}) {
841
- return this.methodFetch(u.DELETE, e, t);
841
+ return this.methodFetch(i.DELETE, e, t);
842
842
  }
843
843
  /**
844
844
  * Makes a PATCH HTTP request.
@@ -850,7 +850,7 @@ class Z {
850
850
  * @returns Promise that resolves to the HTTP response
851
851
  */
852
852
  async patch(e, t = {}) {
853
- return this.methodFetch(u.PATCH, e, t);
853
+ return this.methodFetch(i.PATCH, e, t);
854
854
  }
855
855
  /**
856
856
  * Makes a HEAD HTTP request.
@@ -863,7 +863,7 @@ class Z {
863
863
  * @returns Promise that resolves to the HTTP response
864
864
  */
865
865
  async head(e, t = {}) {
866
- return this.methodFetch(u.HEAD, e, t);
866
+ return this.methodFetch(i.HEAD, e, t);
867
867
  }
868
868
  /**
869
869
  * Makes an OPTIONS HTTP request.
@@ -876,7 +876,7 @@ class Z {
876
876
  * @returns Promise that resolves to the HTTP response
877
877
  */
878
878
  async options(e, t = {}) {
879
- return this.methodFetch(u.OPTIONS, e, t);
879
+ return this.methodFetch(i.OPTIONS, e, t);
880
880
  }
881
881
  }
882
882
  const A = "default";
@@ -985,7 +985,7 @@ function O(r, e) {
985
985
  if (!(r === void 0 && e === void 0))
986
986
  return e === void 0 ? r : r === void 0 ? e : { ...r, ...e };
987
987
  }
988
- function oe(r, e) {
988
+ function ne(r, e) {
989
989
  if (Object.keys(r).length === 0)
990
990
  return e;
991
991
  if (Object.keys(e).length === 0)
@@ -996,16 +996,16 @@ function oe(r, e) {
996
996
  }, s = {
997
997
  ...r.headers,
998
998
  ...e.headers
999
- }, n = e.method ?? r.method, i = e.body ?? r.body, o = e.timeout ?? r.timeout, c = e.signal ?? r.signal;
999
+ }, n = e.method ?? r.method, o = e.body ?? r.body, u = e.timeout ?? r.timeout, a = e.signal ?? r.signal;
1000
1000
  return {
1001
1001
  ...r,
1002
1002
  ...e,
1003
1003
  method: n,
1004
1004
  urlParams: t,
1005
1005
  headers: s,
1006
- body: i,
1007
- timeout: o,
1008
- signal: c
1006
+ body: o,
1007
+ timeout: u,
1008
+ signal: a
1009
1009
  };
1010
1010
  }
1011
1011
  class re extends Z {
@@ -1026,16 +1026,16 @@ class re extends Z {
1026
1026
  * headers: { 'Authorization': 'Bearer token' }
1027
1027
  * });
1028
1028
  */
1029
- constructor(e, t = b) {
1029
+ constructor(e, t = q) {
1030
1030
  super(t), this.name = e, te.register(e, this);
1031
1031
  }
1032
1032
  }
1033
- const ie = new re(A);
1033
+ const oe = new re(A);
1034
1034
  export {
1035
- ne as CONTENT_TYPE_HEADER,
1035
+ se as CONTENT_TYPE_HEADER,
1036
1036
  d as ContentTypeValues,
1037
1037
  A as DEFAULT_FETCHER_NAME,
1038
- b as DEFAULT_OPTIONS,
1038
+ q as DEFAULT_OPTIONS,
1039
1039
  m as ExchangeError,
1040
1040
  f as ExpressUrlTemplateResolver,
1041
1041
  x as FETCH_INTERCEPTOR_NAME,
@@ -1046,7 +1046,7 @@ export {
1046
1046
  Z as Fetcher,
1047
1047
  l as FetcherError,
1048
1048
  ee as FetcherRegistrar,
1049
- u as HttpMethod,
1049
+ i as HttpMethod,
1050
1050
  I as HttpStatusValidationError,
1051
1051
  K as InterceptorManager,
1052
1052
  R as InterceptorRegistry,
@@ -1059,23 +1059,23 @@ export {
1059
1059
  T as UriTemplateResolver,
1060
1060
  v as UrlBuilder,
1061
1061
  B as UrlResolveInterceptor,
1062
- U as UrlTemplateStyle,
1062
+ C as UrlTemplateStyle,
1063
1063
  Y as VALIDATE_STATUS_INTERCEPTOR_NAME,
1064
1064
  z as VALIDATE_STATUS_INTERCEPTOR_ORDER,
1065
1065
  Q as ValidateStatusInterceptor,
1066
1066
  F as combineURLs,
1067
- C as expressUrlTemplateResolver,
1068
- ie as fetcher,
1067
+ L as expressUrlTemplateResolver,
1068
+ oe as fetcher,
1069
1069
  te as fetcherRegistrar,
1070
- D as getUrlTemplateResolver,
1070
+ U as getUrlTemplateResolver,
1071
1071
  S as isAbsoluteURL,
1072
1072
  O as mergeRecords,
1073
- oe as mergeRequest,
1073
+ ne as mergeRequest,
1074
1074
  G as resolveTimeout,
1075
1075
  M as timeoutFetch,
1076
1076
  p as toSorted,
1077
- L as uriTemplateResolver,
1078
- g as urlTemplateRegexExtract,
1077
+ D as uriTemplateResolver,
1078
+ b as urlTemplateRegexExtract,
1079
1079
  _ as urlTemplateRegexResolve
1080
1080
  };
1081
1081
  //# sourceMappingURL=index.es.js.map