@ahoo-wang/fetcher 0.6.1 → 0.6.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.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './fetcher';
2
2
  export * from './fetcherRegistrar';
3
3
  export * from './interceptor';
4
+ export * from './mergeRequest';
4
5
  export * from './namedFetcher';
5
6
  export * from './orderedCapable';
6
7
  export * from './requestBodyInterceptor';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,WAAW,CAAC;AAC1B,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AACzC,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,WAAW,CAAC;AAC1B,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAC9B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,0BAA0B,CAAC;AACzC,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC"}
package/dist/index.es.js CHANGED
@@ -4,7 +4,7 @@ function w(r) {
4
4
  function I(r, e) {
5
5
  return w(e) ? e : e ? r.replace(/\/?\/$/, "") + "/" + e.replace(/^\/+/, "") : r;
6
6
  }
7
- class P {
7
+ class O {
8
8
  /**
9
9
  * 创建UrlBuilder实例
10
10
  *
@@ -48,7 +48,7 @@ class P {
48
48
  }) : e;
49
49
  }
50
50
  }
51
- function A(r, e) {
51
+ function P(r, e) {
52
52
  return typeof r < "u" ? r : e;
53
53
  }
54
54
  class m extends Error {
@@ -57,8 +57,8 @@ class m extends Error {
57
57
  super(o), this.name = "FetchTimeoutError", this.exchange = e, Object.setPrototypeOf(this, m.prototype);
58
58
  }
59
59
  }
60
- var c = /* @__PURE__ */ ((r) => (r.GET = "GET", r.POST = "POST", r.PUT = "PUT", r.DELETE = "DELETE", r.PATCH = "PATCH", r.HEAD = "HEAD", r.OPTIONS = "OPTIONS", r))(c || {}), F = /* @__PURE__ */ ((r) => (r.METHOD = "method", r.BODY = "body", r))(F || {});
61
- const f = "Content-Type";
60
+ 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 || {}), q = /* @__PURE__ */ ((r) => (r.METHOD = "method", r.BODY = "body", r))(q || {});
61
+ const l = "Content-Type";
62
62
  var p = /* @__PURE__ */ ((r) => (r.APPLICATION_JSON = "application/json", r.TEXT_EVENT_STREAM = "text/event-stream", r))(p || {});
63
63
  function h(r, e) {
64
64
  return e ? r.filter(e).sort((t, s) => t.order - s.order) : [...r].sort((t, s) => t.order - s.order);
@@ -107,12 +107,12 @@ class d {
107
107
  return t;
108
108
  }
109
109
  }
110
- class O {
110
+ class A {
111
111
  constructor() {
112
112
  this.request = new d(), this.response = new d(), this.error = new d();
113
113
  }
114
114
  }
115
- class S {
115
+ class F {
116
116
  constructor() {
117
117
  this.name = "RequestBodyInterceptor", this.order = Number.MIN_SAFE_INTEGER;
118
118
  }
@@ -146,23 +146,23 @@ class S {
146
146
  const s = { ...t };
147
147
  s.body = JSON.stringify(t.body), s.headers || (s.headers = {});
148
148
  const o = s.headers;
149
- return o[f] || (o[f] = p.APPLICATION_JSON), { ...e, request: s };
149
+ return o[l] || (o[l] = p.APPLICATION_JSON), { ...e, request: s };
150
150
  }
151
151
  }
152
- const T = {
153
- [f]: p.APPLICATION_JSON
154
- }, y = {
152
+ const E = {
153
+ [l]: p.APPLICATION_JSON
154
+ }, T = {
155
155
  baseURL: "",
156
- headers: T
156
+ headers: E
157
157
  };
158
- class q {
158
+ class S {
159
159
  /**
160
160
  * Create a Fetcher instance
161
161
  *
162
162
  * @param options - Fetcher configuration options
163
163
  */
164
- constructor(e = y) {
165
- this.headers = T, this.interceptors = new O(), this.urlBuilder = new P(e.baseURL), e.headers !== void 0 && (this.headers = e.headers), this.timeout = e.timeout, this.interceptors.request.use(new S());
164
+ constructor(e = T) {
165
+ this.headers = E, this.interceptors = new A(), this.urlBuilder = new O(e.baseURL), e.headers !== void 0 && (this.headers = e.headers), this.timeout = e.timeout, this.interceptors.request.use(new F());
166
166
  }
167
167
  /**
168
168
  * Make an HTTP request
@@ -206,10 +206,10 @@ class q {
206
206
  ...n
207
207
  };
208
208
  n = await this.interceptors.request.intercept(a), n.response = await this.timeoutFetch(n);
209
- const u = {
209
+ const c = {
210
210
  ...n
211
211
  };
212
- return n = await this.interceptors.response.intercept(u), n;
212
+ return n = await this.interceptors.response.intercept(c), n;
213
213
  } catch (a) {
214
214
  if (n.error = a, n = await this.interceptors.error.intercept(n), n.response)
215
215
  return n;
@@ -228,19 +228,19 @@ class q {
228
228
  * @throws FetchTimeoutError Thrown when the request times out
229
229
  */
230
230
  async timeoutFetch(e) {
231
- const t = e.url, s = e.request, o = s.timeout, i = A(o, this.timeout);
231
+ const t = e.url, s = e.request, o = s.timeout, i = P(o, this.timeout);
232
232
  if (!i)
233
233
  return fetch(t, s);
234
234
  const n = new AbortController(), a = {
235
235
  ...s,
236
236
  signal: n.signal
237
237
  };
238
- let u = null;
238
+ let c = null;
239
239
  const g = new Promise((L, b) => {
240
- u = setTimeout(() => {
241
- u && clearTimeout(u);
242
- const E = new m(e, i);
243
- n.abort(E), b(E);
240
+ c = setTimeout(() => {
241
+ c && clearTimeout(c);
242
+ const y = new m(e, i);
243
+ n.abort(y), b(y);
244
244
  }, i);
245
245
  });
246
246
  try {
@@ -249,7 +249,7 @@ class q {
249
249
  g
250
250
  ]);
251
251
  } finally {
252
- u && clearTimeout(u);
252
+ c && clearTimeout(c);
253
253
  }
254
254
  }
255
255
  /**
@@ -274,7 +274,7 @@ class q {
274
274
  * @returns Promise<Response> HTTP response
275
275
  */
276
276
  async get(e, t = {}) {
277
- return this.methodFetch(c.GET, e, t);
277
+ return this.methodFetch(u.GET, e, t);
278
278
  }
279
279
  /**
280
280
  * Make a POST request
@@ -284,7 +284,7 @@ class q {
284
284
  * @returns Promise<Response> HTTP response
285
285
  */
286
286
  async post(e, t = {}) {
287
- return this.methodFetch(c.POST, e, t);
287
+ return this.methodFetch(u.POST, e, t);
288
288
  }
289
289
  /**
290
290
  * Make a PUT request
@@ -294,7 +294,7 @@ class q {
294
294
  * @returns Promise<Response> HTTP response
295
295
  */
296
296
  async put(e, t = {}) {
297
- return this.methodFetch(c.PUT, e, t);
297
+ return this.methodFetch(u.PUT, e, t);
298
298
  }
299
299
  /**
300
300
  * Make a DELETE request
@@ -304,7 +304,7 @@ class q {
304
304
  * @returns Promise<Response> HTTP response
305
305
  */
306
306
  async delete(e, t = {}) {
307
- return this.methodFetch(c.DELETE, e, t);
307
+ return this.methodFetch(u.DELETE, e, t);
308
308
  }
309
309
  /**
310
310
  * Make a PATCH request
@@ -314,7 +314,7 @@ class q {
314
314
  * @returns Promise<Response> HTTP response
315
315
  */
316
316
  async patch(e, t = {}) {
317
- return this.methodFetch(c.PATCH, e, t);
317
+ return this.methodFetch(u.PATCH, e, t);
318
318
  }
319
319
  /**
320
320
  * Make a HEAD request
@@ -324,7 +324,7 @@ class q {
324
324
  * @returns Promise<Response> HTTP response
325
325
  */
326
326
  async head(e, t = {}) {
327
- return this.methodFetch(c.HEAD, e, t);
327
+ return this.methodFetch(u.HEAD, e, t);
328
328
  }
329
329
  /**
330
330
  * Make an OPTIONS request
@@ -334,10 +334,10 @@ class q {
334
334
  * @returns Promise<Response> HTTP response
335
335
  */
336
336
  async options(e, t = {}) {
337
- return this.methodFetch(c.OPTIONS, e, t);
337
+ return this.methodFetch(u.OPTIONS, e, t);
338
338
  }
339
339
  }
340
- const l = "default";
340
+ const f = "default";
341
341
  class N {
342
342
  constructor() {
343
343
  this.registrar = /* @__PURE__ */ new Map();
@@ -411,7 +411,7 @@ class N {
411
411
  * const defaultFetcher = fetcherRegistrar.default;
412
412
  */
413
413
  get default() {
414
- return this.requiredGet(l);
414
+ return this.requiredGet(f);
415
415
  }
416
416
  /**
417
417
  * Set the default Fetcher instance
@@ -422,7 +422,7 @@ class N {
422
422
  * fetcherRegistrar.default = fetcher;
423
423
  */
424
424
  set default(e) {
425
- this.register(l, e);
425
+ this.register(f, e);
426
426
  }
427
427
  /**
428
428
  * Get a copy of all registered fetchers
@@ -439,7 +439,34 @@ class N {
439
439
  }
440
440
  }
441
441
  const R = new N();
442
- class U extends q {
442
+ function _(r, e) {
443
+ if (Object.keys(r).length === 0)
444
+ return e;
445
+ if (Object.keys(e).length === 0)
446
+ return r;
447
+ const t = {
448
+ ...r.path,
449
+ ...e.path
450
+ }, s = {
451
+ ...r.query,
452
+ ...e.query
453
+ }, o = {
454
+ ...r.headers,
455
+ ...e.headers
456
+ }, i = e.method ?? r.method, n = e.body ?? r.body, a = e.timeout ?? r.timeout, c = e.signal ?? r.signal;
457
+ return {
458
+ ...r,
459
+ ...e,
460
+ method: i,
461
+ path: t,
462
+ query: s,
463
+ headers: o,
464
+ body: n,
465
+ timeout: a,
466
+ signal: c
467
+ };
468
+ }
469
+ class U extends S {
443
470
  /**
444
471
  * Create a NamedFetcher instance and automatically register it with the global fetcherRegistrar
445
472
  *
@@ -457,30 +484,31 @@ class U extends q {
457
484
  * headers: { 'Authorization': 'Bearer token' }
458
485
  * });
459
486
  */
460
- constructor(e, t = y) {
487
+ constructor(e, t = T) {
461
488
  super(t), this.name = e, R.register(e, this);
462
489
  }
463
490
  }
464
- const _ = new U(l);
491
+ const D = new U(f);
465
492
  export {
466
- f as ContentTypeHeader,
493
+ l as ContentTypeHeader,
467
494
  p as ContentTypeValues,
468
- l as DEFAULT_FETCHER_NAME,
469
- y as DEFAULT_OPTIONS,
495
+ f as DEFAULT_FETCHER_NAME,
496
+ T as DEFAULT_OPTIONS,
470
497
  m as FetchTimeoutError,
471
- q as Fetcher,
472
- O as FetcherInterceptors,
498
+ S as Fetcher,
499
+ A as FetcherInterceptors,
473
500
  N as FetcherRegistrar,
474
- c as HttpMethod,
501
+ u as HttpMethod,
475
502
  d as InterceptorManager,
476
503
  U as NamedFetcher,
477
- S as RequestBodyInterceptor,
478
- F as RequestField,
479
- P as UrlBuilder,
504
+ F as RequestBodyInterceptor,
505
+ q as RequestField,
506
+ O as UrlBuilder,
480
507
  I as combineURLs,
481
- _ as fetcher,
508
+ D as fetcher,
482
509
  R as fetcherRegistrar,
483
510
  w as isAbsoluteURL,
484
- A as resolveTimeout,
511
+ _ as mergeRequest,
512
+ P as resolveTimeout,
485
513
  h as toSorted
486
514
  };
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
1
- (function(n,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(n=typeof globalThis<"u"?globalThis:n||self,h(n.Fetcher={}))})(this,(function(n){"use strict";function h(r){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(r)}function g(r,e){return h(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class b{constructor(e){this.baseURL=e}build(e,t,s){const i=g(this.baseURL,e);let c=this.interpolateUrl(i,t);if(s){const o=new URLSearchParams(s).toString();o&&(c+="?"+o)}return c}interpolateUrl(e,t){return t?e.replace(/{([^}]+)}/g,(s,i)=>{const c=t[i];if(c===void 0)throw new Error(`Missing required path parameter: ${i}`);return String(c)}):e}}function F(r,e){return typeof r<"u"?r:e}class f extends Error{constructor(e,t){const s=e.request?.method||"GET",i=`Request timeout of ${t}ms exceeded for ${s} ${e.url}`;super(i),this.name="FetchTimeoutError",this.exchange=e,Object.setPrototypeOf(this,f.prototype)}}var u=(r=>(r.GET="GET",r.POST="POST",r.PUT="PUT",r.DELETE="DELETE",r.PATCH="PATCH",r.HEAD="HEAD",r.OPTIONS="OPTIONS",r))(u||{}),I=(r=>(r.METHOD="method",r.BODY="body",r))(I||{});const l="Content-Type";var m=(r=>(r.APPLICATION_JSON="application/json",r.TEXT_EVENT_STREAM="text/event-stream",r))(m||{});function T(r,e){return e?r.filter(e).sort((t,s)=>t.order-s.order):[...r].sort((t,s)=>t.order-s.order)}class E{constructor(e=[]){this.sortedInterceptors=[],this.sortedInterceptors=T(e)}get name(){return this.constructor.name}get order(){return Number.MIN_SAFE_INTEGER}use(e){return this.sortedInterceptors.some(t=>t.name===e.name)?!1:(this.sortedInterceptors=T([...this.sortedInterceptors,e]),!0)}eject(e){const t=this.sortedInterceptors;return this.sortedInterceptors=T(t,s=>s.name!==e),t.length!==this.sortedInterceptors.length}clear(){this.sortedInterceptors=[]}async intercept(e){let t=e;for(const s of this.sortedInterceptors)t=await s.intercept(t);return t}}class w{constructor(){this.request=new E,this.response=new E,this.error=new E}}class A{constructor(){this.name="RequestBodyInterceptor",this.order=Number.MIN_SAFE_INTEGER}intercept(e){const t=e.request;if(t.body===void 0||t.body===null||typeof t.body!="object"||t.body instanceof ArrayBuffer||ArrayBuffer.isView(t.body)||t.body instanceof Blob||t.body instanceof File||t.body instanceof URLSearchParams||t.body instanceof FormData||t.body instanceof ReadableStream)return e;const s={...t};s.body=JSON.stringify(t.body),s.headers||(s.headers={});const i=s.headers;return i[l]||(i[l]=m.APPLICATION_JSON),{...e,request:s}}}const P={[l]:m.APPLICATION_JSON},p={baseURL:"",headers:P};class O{constructor(e=p){this.headers=P,this.interceptors=new w,this.urlBuilder=new b(e.baseURL),e.headers!==void 0&&(this.headers=e.headers),this.timeout=e.timeout,this.interceptors.request.use(new A)}async fetch(e,t={}){const s=await this.request(e,t);if(!s.response)throw new Error(`Request to ${s.url} failed with no response`);return s.response}async request(e,t={}){const s={...this.headers||{},...t.headers||{}},i={...t,headers:Object.keys(s).length>0?s:void 0},c=this.urlBuilder.build(e,t.path,t.query);let o={fetcher:this,url:c,request:i,response:void 0,error:void 0};try{const d={...o};o=await this.interceptors.request.intercept(d),o.response=await this.timeoutFetch(o);const a={...o};return o=await this.interceptors.response.intercept(a),o}catch(d){if(o.error=d,o=await this.interceptors.error.intercept(o),o.response)return o;throw o.error}}async timeoutFetch(e){const t=e.url,s=e.request,i=s.timeout,c=F(i,this.timeout);if(!c)return fetch(t,s);const o=new AbortController,d={...s,signal:o.signal};let a=null;const L=new Promise((D,_)=>{a=setTimeout(()=>{a&&clearTimeout(a);const q=new f(e,c);o.abort(q),_(q)},c)});try{return await Promise.race([fetch(t,d),L])}finally{a&&clearTimeout(a)}}async methodFetch(e,t,s={}){return this.fetch(t,{...s,method:e})}async get(e,t={}){return this.methodFetch(u.GET,e,t)}async post(e,t={}){return this.methodFetch(u.POST,e,t)}async put(e,t={}){return this.methodFetch(u.PUT,e,t)}async delete(e,t={}){return this.methodFetch(u.DELETE,e,t)}async patch(e,t={}){return this.methodFetch(u.PATCH,e,t)}async head(e,t={}){return this.methodFetch(u.HEAD,e,t)}async options(e,t={}){return this.methodFetch(u.OPTIONS,e,t)}}const y="default";class R{constructor(){this.registrar=new Map}register(e,t){this.registrar.set(e,t)}unregister(e){return this.registrar.delete(e)}get(e){return this.registrar.get(e)}requiredGet(e){const t=this.get(e);if(!t)throw new Error(`Fetcher ${e} not found`);return t}get default(){return this.requiredGet(y)}set default(e){this.register(y,e)}get fetchers(){return new Map(this.registrar)}}const S=new R;class N extends O{constructor(e,t=p){super(t),this.name=e,S.register(e,this)}}const U=new N(y);n.ContentTypeHeader=l,n.ContentTypeValues=m,n.DEFAULT_FETCHER_NAME=y,n.DEFAULT_OPTIONS=p,n.FetchTimeoutError=f,n.Fetcher=O,n.FetcherInterceptors=w,n.FetcherRegistrar=R,n.HttpMethod=u,n.InterceptorManager=E,n.NamedFetcher=N,n.RequestBodyInterceptor=A,n.RequestField=I,n.UrlBuilder=b,n.combineURLs=g,n.fetcher=U,n.fetcherRegistrar=S,n.isAbsoluteURL=h,n.resolveTimeout=F,n.toSorted=T,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(n,d){typeof exports=="object"&&typeof module<"u"?d(exports):typeof define=="function"&&define.amd?define(["exports"],d):(n=typeof globalThis<"u"?globalThis:n||self,d(n.Fetcher={}))})(this,(function(n){"use strict";function d(r){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(r)}function g(r,e){return d(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class b{constructor(e){this.baseURL=e}build(e,t,s){const i=g(this.baseURL,e);let c=this.interpolateUrl(i,t);if(s){const o=new URLSearchParams(s).toString();o&&(c+="?"+o)}return c}interpolateUrl(e,t){return t?e.replace(/{([^}]+)}/g,(s,i)=>{const c=t[i];if(c===void 0)throw new Error(`Missing required path parameter: ${i}`);return String(c)}):e}}function F(r,e){return typeof r<"u"?r:e}class l extends Error{constructor(e,t){const s=e.request?.method||"GET",i=`Request timeout of ${t}ms exceeded for ${s} ${e.url}`;super(i),this.name="FetchTimeoutError",this.exchange=e,Object.setPrototypeOf(this,l.prototype)}}var u=(r=>(r.GET="GET",r.POST="POST",r.PUT="PUT",r.DELETE="DELETE",r.PATCH="PATCH",r.HEAD="HEAD",r.OPTIONS="OPTIONS",r))(u||{}),I=(r=>(r.METHOD="method",r.BODY="body",r))(I||{});const f="Content-Type";var m=(r=>(r.APPLICATION_JSON="application/json",r.TEXT_EVENT_STREAM="text/event-stream",r))(m||{});function y(r,e){return e?r.filter(e).sort((t,s)=>t.order-s.order):[...r].sort((t,s)=>t.order-s.order)}class T{constructor(e=[]){this.sortedInterceptors=[],this.sortedInterceptors=y(e)}get name(){return this.constructor.name}get order(){return Number.MIN_SAFE_INTEGER}use(e){return this.sortedInterceptors.some(t=>t.name===e.name)?!1:(this.sortedInterceptors=y([...this.sortedInterceptors,e]),!0)}eject(e){const t=this.sortedInterceptors;return this.sortedInterceptors=y(t,s=>s.name!==e),t.length!==this.sortedInterceptors.length}clear(){this.sortedInterceptors=[]}async intercept(e){let t=e;for(const s of this.sortedInterceptors)t=await s.intercept(t);return t}}class w{constructor(){this.request=new T,this.response=new T,this.error=new T}}class A{constructor(){this.name="RequestBodyInterceptor",this.order=Number.MIN_SAFE_INTEGER}intercept(e){const t=e.request;if(t.body===void 0||t.body===null||typeof t.body!="object"||t.body instanceof ArrayBuffer||ArrayBuffer.isView(t.body)||t.body instanceof Blob||t.body instanceof File||t.body instanceof URLSearchParams||t.body instanceof FormData||t.body instanceof ReadableStream)return e;const s={...t};s.body=JSON.stringify(t.body),s.headers||(s.headers={});const i=s.headers;return i[f]||(i[f]=m.APPLICATION_JSON),{...e,request:s}}}const O={[f]:m.APPLICATION_JSON},p={baseURL:"",headers:O};class R{constructor(e=p){this.headers=O,this.interceptors=new w,this.urlBuilder=new b(e.baseURL),e.headers!==void 0&&(this.headers=e.headers),this.timeout=e.timeout,this.interceptors.request.use(new A)}async fetch(e,t={}){const s=await this.request(e,t);if(!s.response)throw new Error(`Request to ${s.url} failed with no response`);return s.response}async request(e,t={}){const s={...this.headers||{},...t.headers||{}},i={...t,headers:Object.keys(s).length>0?s:void 0},c=this.urlBuilder.build(e,t.path,t.query);let o={fetcher:this,url:c,request:i,response:void 0,error:void 0};try{const h={...o};o=await this.interceptors.request.intercept(h),o.response=await this.timeoutFetch(o);const a={...o};return o=await this.interceptors.response.intercept(a),o}catch(h){if(o.error=h,o=await this.interceptors.error.intercept(o),o.response)return o;throw o.error}}async timeoutFetch(e){const t=e.url,s=e.request,i=s.timeout,c=F(i,this.timeout);if(!c)return fetch(t,s);const o=new AbortController,h={...s,signal:o.signal};let a=null;const _=new Promise((C,D)=>{a=setTimeout(()=>{a&&clearTimeout(a);const N=new l(e,c);o.abort(N),D(N)},c)});try{return await Promise.race([fetch(t,h),_])}finally{a&&clearTimeout(a)}}async methodFetch(e,t,s={}){return this.fetch(t,{...s,method:e})}async get(e,t={}){return this.methodFetch(u.GET,e,t)}async post(e,t={}){return this.methodFetch(u.POST,e,t)}async put(e,t={}){return this.methodFetch(u.PUT,e,t)}async delete(e,t={}){return this.methodFetch(u.DELETE,e,t)}async patch(e,t={}){return this.methodFetch(u.PATCH,e,t)}async head(e,t={}){return this.methodFetch(u.HEAD,e,t)}async options(e,t={}){return this.methodFetch(u.OPTIONS,e,t)}}const E="default";class q{constructor(){this.registrar=new Map}register(e,t){this.registrar.set(e,t)}unregister(e){return this.registrar.delete(e)}get(e){return this.registrar.get(e)}requiredGet(e){const t=this.get(e);if(!t)throw new Error(`Fetcher ${e} not found`);return t}get default(){return this.requiredGet(E)}set default(e){this.register(E,e)}get fetchers(){return new Map(this.registrar)}}const P=new q;function U(r,e){if(Object.keys(r).length===0)return e;if(Object.keys(e).length===0)return r;const t={...r.path,...e.path},s={...r.query,...e.query},i={...r.headers,...e.headers},c=e.method??r.method,o=e.body??r.body,h=e.timeout??r.timeout,a=e.signal??r.signal;return{...r,...e,method:c,path:t,query:s,headers:i,body:o,timeout:h,signal:a}}class S extends R{constructor(e,t=p){super(t),this.name=e,P.register(e,this)}}const L=new S(E);n.ContentTypeHeader=f,n.ContentTypeValues=m,n.DEFAULT_FETCHER_NAME=E,n.DEFAULT_OPTIONS=p,n.FetchTimeoutError=l,n.Fetcher=R,n.FetcherInterceptors=w,n.FetcherRegistrar=q,n.HttpMethod=u,n.InterceptorManager=T,n.NamedFetcher=S,n.RequestBodyInterceptor=A,n.RequestField=I,n.UrlBuilder=b,n.combineURLs=g,n.fetcher=L,n.fetcherRegistrar=P,n.isAbsoluteURL=d,n.mergeRequest=U,n.resolveTimeout=F,n.toSorted=y,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
@@ -0,0 +1,41 @@
1
+ import { FetcherRequest } from './fetcher';
2
+ /**
3
+ * Merges two FetcherRequest objects into one.
4
+ *
5
+ * This function combines two FetcherRequest objects, with the second object's properties
6
+ * taking precedence over the first object's properties. Special handling is applied
7
+ * to nested objects like path, query, and headers which are merged recursively.
8
+ * For primitive values, the second object's values override the first's.
9
+ *
10
+ * @param first - The first request object (lower priority)
11
+ * @param second - The second request object (higher priority)
12
+ * @returns A new FetcherRequest object with merged properties
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const request1 = {
17
+ * method: 'GET',
18
+ * path: { id: 1 },
19
+ * headers: { 'Content-Type': 'application/json' }
20
+ * };
21
+ *
22
+ * const request2 = {
23
+ * method: 'POST',
24
+ * query: { filter: 'active' },
25
+ * headers: { 'Authorization': 'Bearer token' }
26
+ * };
27
+ *
28
+ * const merged = mergeRequest(request1, request2);
29
+ * // Result: {
30
+ * // method: 'POST',
31
+ * // path: { id: 1 },
32
+ * // query: { filter: 'active' },
33
+ * // headers: {
34
+ * // 'Content-Type': 'application/json',
35
+ * // 'Authorization': 'Bearer token'
36
+ * // }
37
+ * // }
38
+ * ```
39
+ */
40
+ export declare function mergeRequest(first: FetcherRequest, second: FetcherRequest): FetcherRequest;
41
+ //# sourceMappingURL=mergeRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mergeRequest.d.ts","sourceRoot":"","sources":["../src/mergeRequest.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAE3C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,cAAc,EACrB,MAAM,EAAE,cAAc,GACrB,cAAc,CA6ChB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ahoo-wang/fetcher",
3
- "version": "0.6.1",
3
+ "version": "0.6.5",
4
4
  "description": "Ultra-lightweight (1.9kB) HTTP client with built-in path parameters and Axios-like API",
5
5
  "keywords": [
6
6
  "fetch",