@ahoo-wang/fetcher 0.6.5 → 0.6.8

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/fetcher.d.ts CHANGED
@@ -5,6 +5,7 @@ import { FetcherInterceptors, FetchExchange } from './interceptor';
5
5
  * Fetcher configuration options interface
6
6
  */
7
7
  export interface FetcherOptions extends BaseURLCapable, HeadersCapable, TimeoutCapable {
8
+ interceptors?: FetcherInterceptors;
8
9
  }
9
10
  export declare const DEFAULT_OPTIONS: FetcherOptions;
10
11
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AAcA,OAAO,EAAqC,cAAc,EAAE,MAAM,WAAW,CAAC;AAC9E,OAAO,EACL,cAAc,EAGd,cAAc,EAEd,YAAY,EACb,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,mBAAmB,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAGnE;;GAEG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,cAAc,EACd,cAAc;CACjB;AAMD,eAAO,MAAM,eAAe,EAAE,cAG7B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,cAAc,EACd,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,SAAS,CAAC;IACvC;;;;;;;;;;;;;;OAcG;IACH,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAE3B;;;;;;;;;;;;;;;;;;OAkBG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAE5B;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACH,IAAI,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC;CAC9C;AAED;;;;;;;;;;GAU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AC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;OAMG;IACG,KAAK,CACT,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;OAMG;IACG,IAAI,CACR,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,GAAG,YAAY,CAAC,IAAI,CAAM,GAC1E,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;OAMG;IACG,OAAO,CACX,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,GAAG,YAAY,CAAC,IAAI,CAAM,GAC1E,OAAO,CAAC,QAAQ,CAAC;CAGrB"}
package/dist/index.es.js CHANGED
@@ -51,18 +51,55 @@ class O {
51
51
  function P(r, e) {
52
52
  return typeof r < "u" ? r : e;
53
53
  }
54
- class m extends Error {
54
+ class p extends Error {
55
55
  constructor(e, t) {
56
56
  const s = e.request?.method || "GET", o = `Request timeout of ${t}ms exceeded for ${s} ${e.url}`;
57
- super(o), this.name = "FetchTimeoutError", this.exchange = e, Object.setPrototypeOf(this, m.prototype);
57
+ super(o), this.name = "FetchTimeoutError", this.exchange = e, Object.setPrototypeOf(this, p.prototype);
58
58
  }
59
59
  }
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 || {});
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 || {}), A = /* @__PURE__ */ ((r) => (r.METHOD = "method", r.BODY = "body", r))(A || {});
61
61
  const l = "Content-Type";
62
- var p = /* @__PURE__ */ ((r) => (r.APPLICATION_JSON = "application/json", r.TEXT_EVENT_STREAM = "text/event-stream", r))(p || {});
62
+ var y = /* @__PURE__ */ ((r) => (r.APPLICATION_JSON = "application/json", r.TEXT_EVENT_STREAM = "text/event-stream", r))(y || {});
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);
65
65
  }
66
+ class q {
67
+ constructor() {
68
+ this.name = "RequestBodyInterceptor", this.order = Number.MIN_SAFE_INTEGER;
69
+ }
70
+ /**
71
+ * 尝试转换请求体为合法的 fetch API body 类型
72
+ *
73
+ * 根据 Fetch API 规范,body 可以是多种类型,但对于普通对象,需要转换为 JSON 字符串
74
+ * https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body
75
+ *
76
+ * 支持的类型:
77
+ * - a string
78
+ * - ArrayBuffer
79
+ * - TypedArray
80
+ * - DataView
81
+ * - Blob
82
+ * - File
83
+ * - URLSearchParams
84
+ * - FormData
85
+ * - ReadableStream
86
+ *
87
+ * 对于不支持的 object 类型(如普通对象),将自动转换为 JSON 字符串
88
+ *
89
+ * @param exchange
90
+ * @returns 转换后的请求
91
+ */
92
+ intercept(e) {
93
+ const t = e.request;
94
+ if (t.body === void 0 || t.body === null || typeof t.body != "object" || t.body instanceof ArrayBuffer || ArrayBuffer.isView(t.body) || // 包括 TypedArray 和 DataView
95
+ t.body instanceof Blob || t.body instanceof File || t.body instanceof URLSearchParams || t.body instanceof FormData || t.body instanceof ReadableStream)
96
+ return e;
97
+ const s = { ...t };
98
+ s.body = JSON.stringify(t.body), s.headers || (s.headers = {});
99
+ const o = s.headers;
100
+ return o[l] || (o[l] = y.APPLICATION_JSON), { ...e, request: s };
101
+ }
102
+ }
66
103
  class d {
67
104
  constructor(e = []) {
68
105
  this.sortedInterceptors = [], this.sortedInterceptors = h(e);
@@ -107,53 +144,16 @@ class d {
107
144
  return t;
108
145
  }
109
146
  }
110
- class A {
111
- constructor() {
112
- this.request = new d(), this.response = new d(), this.error = new d();
113
- }
114
- }
115
147
  class F {
116
148
  constructor() {
117
- this.name = "RequestBodyInterceptor", this.order = Number.MIN_SAFE_INTEGER;
118
- }
119
- /**
120
- * 尝试转换请求体为合法的 fetch API body 类型
121
- *
122
- * 根据 Fetch API 规范,body 可以是多种类型,但对于普通对象,需要转换为 JSON 字符串
123
- * https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body
124
- *
125
- * 支持的类型:
126
- * - a string
127
- * - ArrayBuffer
128
- * - TypedArray
129
- * - DataView
130
- * - Blob
131
- * - File
132
- * - URLSearchParams
133
- * - FormData
134
- * - ReadableStream
135
- *
136
- * 对于不支持的 object 类型(如普通对象),将自动转换为 JSON 字符串
137
- *
138
- * @param exchange
139
- * @returns 转换后的请求
140
- */
141
- intercept(e) {
142
- const t = e.request;
143
- if (t.body === void 0 || t.body === null || typeof t.body != "object" || t.body instanceof ArrayBuffer || ArrayBuffer.isView(t.body) || // 包括 TypedArray 和 DataView
144
- t.body instanceof Blob || t.body instanceof File || t.body instanceof URLSearchParams || t.body instanceof FormData || t.body instanceof ReadableStream)
145
- return e;
146
- const s = { ...t };
147
- s.body = JSON.stringify(t.body), s.headers || (s.headers = {});
148
- const o = s.headers;
149
- return o[l] || (o[l] = p.APPLICATION_JSON), { ...e, request: s };
149
+ this.request = new d([new q()]), this.response = new d(), this.error = new d();
150
150
  }
151
151
  }
152
- const E = {
153
- [l]: p.APPLICATION_JSON
152
+ const m = {
153
+ [l]: y.APPLICATION_JSON
154
154
  }, T = {
155
155
  baseURL: "",
156
- headers: E
156
+ headers: m
157
157
  };
158
158
  class S {
159
159
  /**
@@ -162,7 +162,7 @@ class S {
162
162
  * @param options - Fetcher configuration options
163
163
  */
164
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());
165
+ this.headers = m, this.urlBuilder = new O(e.baseURL), this.headers = e.headers ?? m, this.timeout = e.timeout, this.interceptors = e.interceptors ?? new F();
166
166
  }
167
167
  /**
168
168
  * Make an HTTP request
@@ -239,8 +239,8 @@ class S {
239
239
  const g = new Promise((L, b) => {
240
240
  c = setTimeout(() => {
241
241
  c && clearTimeout(c);
242
- const y = new m(e, i);
243
- n.abort(y), b(y);
242
+ const E = new p(e, i);
243
+ n.abort(E), b(E);
244
244
  }, i);
245
245
  });
246
246
  try {
@@ -491,18 +491,18 @@ class U extends S {
491
491
  const D = new U(f);
492
492
  export {
493
493
  l as ContentTypeHeader,
494
- p as ContentTypeValues,
494
+ y as ContentTypeValues,
495
495
  f as DEFAULT_FETCHER_NAME,
496
496
  T as DEFAULT_OPTIONS,
497
- m as FetchTimeoutError,
497
+ p as FetchTimeoutError,
498
498
  S as Fetcher,
499
- A as FetcherInterceptors,
499
+ F as FetcherInterceptors,
500
500
  N as FetcherRegistrar,
501
501
  u as HttpMethod,
502
502
  d as InterceptorManager,
503
503
  U as NamedFetcher,
504
- F as RequestBodyInterceptor,
505
- q as RequestField,
504
+ q as RequestBodyInterceptor,
505
+ A as RequestField,
506
506
  O as UrlBuilder,
507
507
  I as combineURLs,
508
508
  D as fetcher,
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
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"})}));
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 b(r,e){return d(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class F{constructor(e){this.baseURL=e}build(e,t,s){const i=b(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 I(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||{}),w=(r=>(r.METHOD="method",r.BODY="body",r))(w||{});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 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}}}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 O{constructor(){this.request=new T([new A]),this.response=new T,this.error=new T}}const p={[f]:m.APPLICATION_JSON},g={baseURL:"",headers:p};class R{constructor(e=g){this.headers=p,this.urlBuilder=new F(e.baseURL),this.headers=e.headers??p,this.timeout=e.timeout,this.interceptors=e.interceptors??new O}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=I(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 P{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 q=new P;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=g){super(t),this.name=e,q.register(e,this)}}const L=new S(E);n.ContentTypeHeader=f,n.ContentTypeValues=m,n.DEFAULT_FETCHER_NAME=E,n.DEFAULT_OPTIONS=g,n.FetchTimeoutError=l,n.Fetcher=R,n.FetcherInterceptors=O,n.FetcherRegistrar=P,n.HttpMethod=u,n.InterceptorManager=T,n.NamedFetcher=S,n.RequestBodyInterceptor=A,n.RequestField=w,n.UrlBuilder=F,n.combineURLs=b,n.fetcher=L,n.fetcherRegistrar=q,n.isAbsoluteURL=d,n.mergeRequest=U,n.resolveTimeout=I,n.toSorted=y,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
@@ -1 +1 @@
1
- {"version":3,"file":"interceptor.d.ts","sourceRoot":"","sources":["../src/interceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAE,cAAc,EAAY,MAAM,kBAAkB,CAAC;AAE5D,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,cAAc,CAAC;IACxB,QAAQ,EAAE,QAAQ,GAAG,SAAS,CAAC;IAC/B,KAAK,EAAE,KAAK,GAAG,GAAG,GAAG,SAAS,CAAC;CAChC;AAED;;;GAGG;AACH,MAAM,WAAW,WAAY,SAAQ,YAAY,EAAE,cAAc;IAC/D;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;;OAIG;IACH,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;CAC5E;AAED;;GAEG;AACH,qBAAa,kBAAmB,YAAW,WAAW;IACpD,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,IAAI,KAAK,IAAI,MAAM,CAElB;IAED,OAAO,CAAC,kBAAkB,CAAqB;gBAEnC,YAAY,GAAE,WAAW,EAAO;IAI5C;;;;OAIG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;gBAEnC,YAAY,GAAE,WAAW,EAAO;IAI5C;;;;OAIG;IACH,GAAG,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO;IAQtC;;;OAGG;IACH,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAM5B;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;;;OAIG;IACG,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;CAQjE;AAED;;GAEG;AACH,qBAAa,mBAAmB;IAC9B;;OAEG;IACH,OAAO,EAAE,kBAAkB,CAA0D;IAErF;;OAEG;IACH,QAAQ,EAAE,kBAAkB,CAA4B;IAExD;;OAEG;IACH,KAAK,EAAE,kBAAkB,CAA4B;CACtD"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ahoo-wang/fetcher",
3
- "version": "0.6.5",
3
+ "version": "0.6.8",
4
4
  "description": "Ultra-lightweight (1.9kB) HTTP client with built-in path parameters and Axios-like API",
5
5
  "keywords": [
6
6
  "fetch",