@ahoo-wang/fetcher 0.2.2 → 0.3.0

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.
@@ -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;AAWD;;GAEG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACjC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAClC,IAAI,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC;CAC9C;AAED;;;;;;;;;;GAUG;AACH,qBAAa,OAAQ,YAAW,cAAc,EAAE,cAAc;IAC5D,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAkB;IAClD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,UAAU,CAAa;IAC/B,YAAY,EAAE,mBAAmB,CAA6B;IAE9D;;;;OAIG;gBACS,OAAO,GAAE,cAA+B;IASpD;;;;;;OAMG;IACG,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,QAAQ,CAAC;IAQzE;;;;;;;;OAQG;IACG,OAAO,CACX,GAAG,EAAE,MAAM,EACX,O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() {
102
102
  this.request = new h(), this.response = new h(), this.error = new h();
103
103
  }
104
104
  }
105
- class S {
105
+ class A {
106
106
  /**
107
107
  * 尝试转换请求体为合法的 fetch API body 类型
108
108
  *
@@ -138,7 +138,7 @@ class S {
138
138
  }
139
139
  const m = {
140
140
  [d]: f.APPLICATION_JSON
141
- }, q = {
141
+ }, S = {
142
142
  baseURL: "",
143
143
  headers: m
144
144
  };
@@ -148,8 +148,8 @@ class I {
148
148
  *
149
149
  * @param options - Fetcher configuration options
150
150
  */
151
- constructor(e = q) {
152
- this.headers = m, this.interceptors = new A(), this.urlBuilder = new w(e.baseURL), e.headers !== void 0 && (this.headers = e.headers), this.timeout = e.timeout, this.interceptors.request.use(new S());
151
+ constructor(e = S) {
152
+ this.headers = m, this.interceptors = new q(), this.urlBuilder = new w(e.baseURL), e.headers !== void 0 && (this.headers = e.headers), this.timeout = e.timeout, this.interceptors.request.use(new A());
153
153
  }
154
154
  /**
155
155
  * Make an HTTP request
@@ -193,7 +193,14 @@ class I {
193
193
  error: void 0
194
194
  };
195
195
  try {
196
- return n = await this.interceptors.request.intercept(n), n.response = await this.timeoutFetch(n), n = await this.interceptors.response.intercept(n), n;
196
+ const u = {
197
+ ...n
198
+ };
199
+ n = await this.interceptors.request.intercept(u), n.response = await this.timeoutFetch(n);
200
+ const a = {
201
+ ...n
202
+ };
203
+ return n = await this.interceptors.response.intercept(a), n;
197
204
  } catch (u) {
198
205
  if (n.error = u, n = await this.interceptors.error.intercept(n), n.response)
199
206
  return n;
@@ -326,7 +333,7 @@ export {
326
333
  f as ContentTypeValues,
327
334
  l as FetchTimeoutError,
328
335
  I as Fetcher,
329
- A as FetcherInterceptors,
336
+ q as FetcherInterceptors,
330
337
  c as HttpMethod,
331
338
  h as InterceptorManager,
332
339
  O as RequestField,
package/dist/index.umd.js CHANGED
@@ -1 +1 @@
1
- (function(i,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(i=typeof globalThis<"u"?globalThis:i||self,a(i.Fetcher={}))})(this,(function(i){"use strict";function a(r){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(r)}function y(r,e){return a(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class T{constructor(e){this.baseURL=e}build(e,t,s){let o=y(this.baseURL,e),c=this.interpolateUrl(o,t);if(s){const n=new URLSearchParams(s).toString();n&&(c+="?"+n)}return c}interpolateUrl(e,t){return t?e.replace(/{([^}]+)}/g,(s,o)=>{const c=t[o];if(c===void 0)throw new Error(`Missing required path parameter: ${o}`);return String(c)}):e}}function b(r,e){return typeof r<"u"?r:e}class d extends Error{constructor(e,t){const s=e.request?.method||"GET",o=`Request timeout of ${t}ms exceeded for ${s} ${e.url}`;super(o),this.name="FetchTimeoutError",this.exchange=e,Object.setPrototypeOf(this,d.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||{}),E=(r=>(r.METHOD="method",r.BODY="body",r))(E||{});const l="Content-Type";var f=(r=>(r.APPLICATION_JSON="application/json",r.TEXT_EVENT_STREAM="text/event-stream",r))(f||{});class m{constructor(){this.interceptors=[]}use(e){const t=this.interceptors.length;return this.interceptors.push(e),t}eject(e){this.interceptors[e]&&(this.interceptors[e]=null)}clear(){this.interceptors=[]}async intercept(e){let t=e;for(let s of this.interceptors)s&&(t=await s.intercept(t));return t}}class P{constructor(){this.request=new m,this.response=new m,this.error=new m}}class F{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 o=s.headers;return o[l]||(o[l]=f.APPLICATION_JSON),{...e,request:s}}}const w={[l]:f.APPLICATION_JSON},S={baseURL:"",headers:w};class A{constructor(e=S){this.headers=w,this.interceptors=new P,this.urlBuilder=new T(e.baseURL),e.headers!==void 0&&(this.headers=e.headers),this.timeout=e.timeout,this.interceptors.request.use(new F)}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||{}},o={...t,headers:Object.keys(s).length>0?s:void 0},c=this.urlBuilder.build(e,t.pathParams,t.queryParams);let n={fetcher:this,url:c,request:o,response:void 0,error:void 0};try{return n=await this.interceptors.request.intercept(n),n.response=await this.timeoutFetch(n),n=await this.interceptors.response.intercept(n),n}catch(p){if(n.error=p,n=await this.interceptors.error.intercept(n),n.response)return n;throw n.error}}async timeoutFetch(e){const t=e.url,s=e.request,o=s.timeout,c=b(o,this.timeout);if(!c)return fetch(t,s);const n=new AbortController,p={...s,signal:n.signal};let h=null;const q=new Promise((I,U)=>{h=setTimeout(()=>{h&&clearTimeout(h);const O=new d(e,c);n.abort(O),U(O)},c)});try{return await Promise.race([fetch(t,p),q])}finally{h&&clearTimeout(h)}}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)}}i.ContentTypeHeader=l,i.ContentTypeValues=f,i.FetchTimeoutError=d,i.Fetcher=A,i.FetcherInterceptors=P,i.HttpMethod=u,i.InterceptorManager=m,i.RequestField=E,i.UrlBuilder=T,i.combineURLs=y,i.isAbsoluteURL=a,i.resolveTimeout=b,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(o,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(o=typeof globalThis<"u"?globalThis:o||self,h(o.Fetcher={}))})(this,(function(o){"use strict";function h(r){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(r)}function y(r,e){return h(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class T{constructor(e){this.baseURL=e}build(e,t,s){let i=y(this.baseURL,e),c=this.interpolateUrl(i,t);if(s){const n=new URLSearchParams(s).toString();n&&(c+="?"+n)}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 b(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||{}),E=(r=>(r.METHOD="method",r.BODY="body",r))(E||{});const f="Content-Type";var m=(r=>(r.APPLICATION_JSON="application/json",r.TEXT_EVENT_STREAM="text/event-stream",r))(m||{});class p{constructor(){this.interceptors=[]}use(e){const t=this.interceptors.length;return this.interceptors.push(e),t}eject(e){this.interceptors[e]&&(this.interceptors[e]=null)}clear(){this.interceptors=[]}async intercept(e){let t=e;for(let s of this.interceptors)s&&(t=await s.intercept(t));return t}}class P{constructor(){this.request=new p,this.response=new p,this.error=new p}}class F{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 w={[f]:m.APPLICATION_JSON},S={baseURL:"",headers:w};class q{constructor(e=S){this.headers=w,this.interceptors=new P,this.urlBuilder=new T(e.baseURL),e.headers!==void 0&&(this.headers=e.headers),this.timeout=e.timeout,this.interceptors.request.use(new F)}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.pathParams,t.queryParams);let n={fetcher:this,url:c,request:i,response:void 0,error:void 0};try{const d={...n};n=await this.interceptors.request.intercept(d),n.response=await this.timeoutFetch(n);const a={...n};return n=await this.interceptors.response.intercept(a),n}catch(d){if(n.error=d,n=await this.interceptors.error.intercept(n),n.response)return n;throw n.error}}async timeoutFetch(e){const t=e.url,s=e.request,i=s.timeout,c=b(i,this.timeout);if(!c)return fetch(t,s);const n=new AbortController,d={...s,signal:n.signal};let a=null;const A=new Promise((g,U)=>{a=setTimeout(()=>{a&&clearTimeout(a);const O=new l(e,c);n.abort(O),U(O)},c)});try{return await Promise.race([fetch(t,d),A])}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)}}o.ContentTypeHeader=f,o.ContentTypeValues=m,o.FetchTimeoutError=l,o.Fetcher=q,o.FetcherInterceptors=P,o.HttpMethod=u,o.InterceptorManager=p,o.RequestField=E,o.UrlBuilder=T,o.combineURLs=y,o.isAbsoluteURL=h,o.resolveTimeout=b,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ahoo-wang/fetcher",
3
- "version": "0.2.2",
3
+ "version": "0.3.0",
4
4
  "description": "Core library providing basic HTTP client functionality for Fetcher",
5
5
  "keywords": [
6
6
  "fetch",