@ahoo-wang/fetcher 0.0.3 → 0.0.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/README.md +6 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +2 -0
- package/dist/index.umd.js +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @ahoo-wang/fetcher
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher)
|
|
4
|
+
[](https://github.com/Ahoo-Wang/fetcher/actions)
|
|
5
|
+
[](https://codecov.io/gh/Ahoo-Wang/Fetcher)
|
|
6
|
+
[](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
|
|
7
|
+
[](https://www.npmjs.com/package/@ahoo-wang/fetcher)
|
|
8
|
+
|
|
3
9
|
A modern HTTP client library based on the Fetch API, designed to simplify and optimize interactions with backend RESTful APIs. It provides an Axios-like API with support for path parameters, query parameters, timeout settings, and request/response interceptors.
|
|
4
10
|
|
|
5
11
|
## Features
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC;AAC1B,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,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC"}
|
package/dist/index.es.js
CHANGED
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
1
|
+
(function(n,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(n=typeof globalThis<"u"?globalThis:n||self,a(n.Fetcher={}))})(this,(function(n){"use strict";function a(r){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(r)}function f(r,e){return a(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class d{constructor(e){this.baseURL=e}build(e,t,s){let i=f(this.baseURL,e),h=this.interpolateUrl(i,t);if(s){const o=new URLSearchParams(s).toString();o&&(h+="?"+o)}return h}interpolateUrl(e,t){return t?e.replace(/{([^}]+)}/g,(s,i)=>{const h=t[i];if(h===void 0)throw new Error(`Missing required path parameter: ${i}`);return String(h)}):e}}function m(r,e){return typeof r<"u"?r:e}class u extends Error{constructor(e,t,s){const i=`Request timeout of ${s}ms exceeded for ${t?.method||"GET"} ${e}`;super(i),this.name="FetchTimeoutError",this.url=e,this.request=t,Object.setPrototypeOf(this,u.prototype)}}var c=(r=>(r.GET="GET",r.POST="POST",r.PUT="PUT",r.DELETE="DELETE",r.PATCH="PATCH",r.HEAD="HEAD",r.OPTIONS="OPTIONS",r))(c||{}),p=(r=>(r.METHOD="method",r.BODY="body",r))(p||{});class l{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 T{constructor(){this.request=new l,this.response=new l,this.error=new l}}const y={baseURL:""};class b{constructor(e=y){this.interceptors=new T,this.urlBuilder=new d(e.baseURL),this.headers=e.headers,this.timeout=e.timeout}async fetch(e,t={}){const s={...this.headers||{},...t.headers||{}};let i={...t,headers:Object.keys(s).length>0?s:void 0};const h=this.urlBuilder.build(e,t.pathParams,t.queryParams);i=await this.interceptors.request.intercept(i);try{let o=await this.timeoutFetch(h,i);return o=await this.interceptors.response.intercept(o),o}catch(o){throw await this.interceptors.error.intercept(o)}}async timeoutFetch(e,t){const s=m(t.timeout,this.timeout);if(!s)return fetch(e,t);const i=new AbortController,h={...t,signal:i.signal};let o=null;const E=new Promise((P,w)=>{o=setTimeout(()=>{i.abort(),w(new u(e,t,s))},s)});try{return await Promise.race([fetch(e,h),E])}finally{o&&clearTimeout(o)}}async get(e,t={}){return this.fetch(e,{...t,method:c.GET})}async post(e,t={}){return this.fetch(e,{...t,method:c.POST})}async put(e,t={}){return this.fetch(e,{...t,method:c.PUT})}async delete(e,t={}){return this.fetch(e,{...t,method:c.DELETE})}async patch(e,t={}){return this.fetch(e,{...t,method:c.PATCH})}async head(e,t={}){return this.fetch(e,{...t,method:c.HEAD})}async options(e,t={}){return this.fetch(e,{...t,method:c.OPTIONS})}}n.FetchTimeoutError=u,n.Fetcher=b,n.FetcherInterceptors=T,n.HttpMethod=c,n.InterceptorManager=l,n.RequestField=p,n.UrlBuilder=d,n.combineURLs=f,n.isAbsoluteURL=a,n.resolveTimeout=m,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CAClC;AAED;;GAEG;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,oBAAY,YAAY;IACtB,MAAM,WAAW;IACjB,IAAI,SAAS;CACd"}
|