@ahoo-wang/fetcher 0.0.4 → 0.0.6
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 +0 -1
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +43 -39
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,7 +5,6 @@
|
|
|
5
5
|
[](https://codecov.io/gh/Ahoo-Wang/Fetcher)
|
|
6
6
|
[](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
|
|
7
7
|
[](https://www.npmjs.com/package/@ahoo-wang/fetcher)
|
|
8
|
-
[](https://bundlephobia.com/package/@ahoo-wang/fetcher)
|
|
9
8
|
|
|
10
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.
|
|
11
10
|
|
package/dist/fetcher.d.ts.map
CHANGED
|
@@ -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,EACd,cAAc,EAEd,YAAY,EACb,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAEpD;;GAEG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,cAAc,EACd,cAAc;CAAG;AAMrB;;GAEG;AACH,MAAM,WAAW,cAAe,SAAQ,cAAc,EAAE,WAAW;IACjE,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACjC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACnC;AAED;;;;;;;;;;GAUG;AAC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CAAC;CAMrB"}
|
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
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
function
|
|
1
|
+
function p(r) {
|
|
2
2
|
return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(r);
|
|
3
3
|
}
|
|
4
|
-
function
|
|
5
|
-
return
|
|
4
|
+
function m(r, t) {
|
|
5
|
+
return p(t) ? t : t ? r.replace(/\/?\/$/, "") + "/" + t.replace(/^\/+/, "") : r;
|
|
6
6
|
}
|
|
7
|
-
class
|
|
7
|
+
class d {
|
|
8
8
|
/**
|
|
9
9
|
* 创建UrlBuilder实例
|
|
10
10
|
*
|
|
@@ -23,10 +23,10 @@ class m {
|
|
|
23
23
|
* @throws 当路径参数中缺少必需的占位符时抛出错误
|
|
24
24
|
*/
|
|
25
25
|
build(t, e, s) {
|
|
26
|
-
let
|
|
26
|
+
let i = m(this.baseURL, t), o = this.interpolateUrl(i, e);
|
|
27
27
|
if (s) {
|
|
28
|
-
const
|
|
29
|
-
|
|
28
|
+
const n = new URLSearchParams(s).toString();
|
|
29
|
+
n && (o += "?" + n);
|
|
30
30
|
}
|
|
31
31
|
return o;
|
|
32
32
|
}
|
|
@@ -39,24 +39,24 @@ class m {
|
|
|
39
39
|
* @throws 当路径参数中缺少必需的占位符时抛出错误
|
|
40
40
|
*/
|
|
41
41
|
interpolateUrl(t, e) {
|
|
42
|
-
return e ? t.replace(/{([^}]+)}/g, (s,
|
|
43
|
-
const o = e[
|
|
42
|
+
return e ? t.replace(/{([^}]+)}/g, (s, i) => {
|
|
43
|
+
const o = e[i];
|
|
44
44
|
if (o === void 0)
|
|
45
|
-
throw new Error(`Missing required path parameter: ${
|
|
45
|
+
throw new Error(`Missing required path parameter: ${i}`);
|
|
46
46
|
return String(o);
|
|
47
47
|
}) : t;
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
function
|
|
50
|
+
function T(r, t) {
|
|
51
51
|
return typeof r < "u" ? r : t;
|
|
52
52
|
}
|
|
53
53
|
class a extends Error {
|
|
54
54
|
constructor(t, e, s) {
|
|
55
|
-
const
|
|
56
|
-
super(
|
|
55
|
+
const i = `Request timeout of ${s}ms exceeded for ${e?.method || "GET"} ${t}`;
|
|
56
|
+
super(i), this.name = "FetchTimeoutError", this.url = t, this.request = e, Object.setPrototypeOf(this, a.prototype);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
|
-
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 || {}),
|
|
59
|
+
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 || {}), E = /* @__PURE__ */ ((r) => (r.METHOD = "method", r.BODY = "body", r))(E || {});
|
|
60
60
|
class h {
|
|
61
61
|
constructor() {
|
|
62
62
|
this.interceptors = [];
|
|
@@ -95,22 +95,22 @@ class h {
|
|
|
95
95
|
return e;
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
|
-
class
|
|
98
|
+
class w {
|
|
99
99
|
constructor() {
|
|
100
100
|
this.request = new h(), this.response = new h(), this.error = new h();
|
|
101
101
|
}
|
|
102
102
|
}
|
|
103
|
-
const
|
|
103
|
+
const y = {
|
|
104
104
|
baseURL: ""
|
|
105
105
|
};
|
|
106
|
-
class
|
|
106
|
+
class O {
|
|
107
107
|
/**
|
|
108
108
|
* 创建Fetcher实例
|
|
109
109
|
*
|
|
110
110
|
* @param options - Fetcher配置选项
|
|
111
111
|
*/
|
|
112
|
-
constructor(t =
|
|
113
|
-
this.interceptors = new
|
|
112
|
+
constructor(t = y) {
|
|
113
|
+
this.interceptors = new w(), this.urlBuilder = new d(t.baseURL), this.headers = t.headers, this.timeout = t.timeout;
|
|
114
114
|
}
|
|
115
115
|
/**
|
|
116
116
|
* 发起HTTP请求
|
|
@@ -124,7 +124,7 @@ class P {
|
|
|
124
124
|
...this.headers || {},
|
|
125
125
|
...e.headers || {}
|
|
126
126
|
};
|
|
127
|
-
let
|
|
127
|
+
let i = {
|
|
128
128
|
...e,
|
|
129
129
|
headers: Object.keys(s).length > 0 ? s : void 0
|
|
130
130
|
};
|
|
@@ -133,12 +133,12 @@ class P {
|
|
|
133
133
|
e.pathParams,
|
|
134
134
|
e.queryParams
|
|
135
135
|
);
|
|
136
|
-
|
|
136
|
+
i = await this.interceptors.request.intercept(i);
|
|
137
137
|
try {
|
|
138
|
-
let
|
|
139
|
-
return
|
|
140
|
-
} catch (
|
|
141
|
-
throw await this.interceptors.error.intercept(
|
|
138
|
+
let n = await this.timeoutFetch(o, i);
|
|
139
|
+
return n = await this.interceptors.response.intercept(n), n;
|
|
140
|
+
} catch (n) {
|
|
141
|
+
throw await this.interceptors.error.intercept(n);
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
/**
|
|
@@ -153,23 +153,25 @@ class P {
|
|
|
153
153
|
* @throws FetchTimeoutError 当请求超时时抛出
|
|
154
154
|
*/
|
|
155
155
|
async timeoutFetch(t, e) {
|
|
156
|
-
const s =
|
|
156
|
+
const s = T(e.timeout, this.timeout);
|
|
157
157
|
if (!s)
|
|
158
158
|
return fetch(t, e);
|
|
159
|
-
const
|
|
159
|
+
const i = new AbortController(), o = {
|
|
160
160
|
...e,
|
|
161
|
-
signal:
|
|
161
|
+
signal: i.signal
|
|
162
162
|
};
|
|
163
|
-
let
|
|
164
|
-
const u = new Promise((
|
|
165
|
-
|
|
166
|
-
n
|
|
163
|
+
let n = null;
|
|
164
|
+
const u = new Promise((P, f) => {
|
|
165
|
+
n = setTimeout(() => {
|
|
166
|
+
n && clearTimeout(n);
|
|
167
|
+
const l = new a(t, e, s);
|
|
168
|
+
i.abort(l), f(l);
|
|
167
169
|
}, s);
|
|
168
170
|
});
|
|
169
171
|
try {
|
|
170
172
|
return await Promise.race([fetch(t, o), u]);
|
|
171
173
|
} finally {
|
|
172
|
-
|
|
174
|
+
n && clearTimeout(n);
|
|
173
175
|
}
|
|
174
176
|
}
|
|
175
177
|
/**
|
|
@@ -266,11 +268,13 @@ class P {
|
|
|
266
268
|
}
|
|
267
269
|
export {
|
|
268
270
|
a as FetchTimeoutError,
|
|
269
|
-
|
|
271
|
+
O as Fetcher,
|
|
272
|
+
w as FetcherInterceptors,
|
|
270
273
|
c as HttpMethod,
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
274
|
+
h as InterceptorManager,
|
|
275
|
+
E as RequestField,
|
|
276
|
+
d as UrlBuilder,
|
|
277
|
+
m as combineURLs,
|
|
278
|
+
p as isAbsoluteURL,
|
|
279
|
+
T as resolveTimeout
|
|
276
280
|
};
|
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 o=f(this.baseURL,e),h=this.interpolateUrl(o,t);if(s){const i=new URLSearchParams(s).toString();i&&(h+="?"+i)}return h}interpolateUrl(e,t){return t?e.replace(/{([^}]+)}/g,(s,o)=>{const h=t[o];if(h===void 0)throw new Error(`Missing required path parameter: ${o}`);return String(h)}):e}}function m(r,e){return typeof r<"u"?r:e}class u extends Error{constructor(e,t,s){const o=`Request timeout of ${s}ms exceeded for ${t?.method||"GET"} ${e}`;super(o),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 b={baseURL:""};class w{constructor(e=b){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 o={...t,headers:Object.keys(s).length>0?s:void 0};const h=this.urlBuilder.build(e,t.pathParams,t.queryParams);o=await this.interceptors.request.intercept(o);try{let i=await this.timeoutFetch(h,o);return i=await this.interceptors.response.intercept(i),i}catch(i){throw await this.interceptors.error.intercept(i)}}async timeoutFetch(e,t){const s=m(t.timeout,this.timeout);if(!s)return fetch(e,t);const o=new AbortController,h={...t,signal:o.signal};let i=null;const E=new Promise((g,P)=>{i=setTimeout(()=>{i&&clearTimeout(i);const y=new u(e,t,s);o.abort(y),P(y)},s)});try{return await Promise.race([fetch(e,h),E])}finally{i&&clearTimeout(i)}}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=w,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"})}));
|