@ahoo-wang/fetcher 0.1.5 → 0.1.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/dist/fetcher.d.ts +2 -1
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/index.es.js +54 -47
- package/dist/index.umd.js +1 -1
- package/package.json +1 -1
package/dist/fetcher.d.ts
CHANGED
|
@@ -9,9 +9,10 @@ export interface FetcherOptions extends BaseURLCapable, HeadersCapable, TimeoutC
|
|
|
9
9
|
/**
|
|
10
10
|
* Fetcher请求选项接口
|
|
11
11
|
*/
|
|
12
|
-
export interface FetcherRequest extends TimeoutCapable, RequestInit {
|
|
12
|
+
export interface FetcherRequest extends TimeoutCapable, Omit<RequestInit, 'body'> {
|
|
13
13
|
pathParams?: Record<string, any>;
|
|
14
14
|
queryParams?: Record<string, any>;
|
|
15
|
+
body?: BodyInit | Record<string, any> | null;
|
|
15
16
|
}
|
|
16
17
|
/**
|
|
17
18
|
* HTTP请求客户端类,支持URL构建、超时控制等功能
|
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,EAGd,cAAc,EAEd,YAAY,EACb,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAGpD;;GAEG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,cAAc,EACd,cAAc;CAAG;AAWrB;;GAEG;AACH,MAAM,WAAW,
|
|
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,Y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B;;;;;;OAMG;IACG,GAAG,CACP,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAOpB;;;;;;OAMG;IACG,MAAM,CACV,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAOpB;;;;;;OAMG;IACG,KAAK,CACT,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAOpB;;;;;;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;IAOpB;;;;;;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;CAMrB"}
|
package/dist/index.es.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
function
|
|
1
|
+
function T(r) {
|
|
2
2
|
return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(r);
|
|
3
3
|
}
|
|
4
|
-
function
|
|
5
|
-
return
|
|
4
|
+
function b(r, e) {
|
|
5
|
+
return T(e) ? e : e ? r.replace(/\/?\/$/, "") + "/" + e.replace(/^\/+/, "") : r;
|
|
6
6
|
}
|
|
7
|
-
class
|
|
7
|
+
class E {
|
|
8
8
|
/**
|
|
9
9
|
* 创建UrlBuilder实例
|
|
10
10
|
*
|
|
@@ -23,10 +23,10 @@ class b {
|
|
|
23
23
|
* @throws 当路径参数中缺少必需的占位符时抛出错误
|
|
24
24
|
*/
|
|
25
25
|
build(e, t, n) {
|
|
26
|
-
let s =
|
|
26
|
+
let s = b(this.baseURL, e), o = this.interpolateUrl(s, t);
|
|
27
27
|
if (n) {
|
|
28
|
-
const
|
|
29
|
-
|
|
28
|
+
const a = new URLSearchParams(n).toString();
|
|
29
|
+
a && (o += "?" + a);
|
|
30
30
|
}
|
|
31
31
|
return o;
|
|
32
32
|
}
|
|
@@ -47,19 +47,19 @@ class b {
|
|
|
47
47
|
}) : e;
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
-
function
|
|
50
|
+
function P(r, e) {
|
|
51
51
|
return typeof r < "u" ? r : e;
|
|
52
52
|
}
|
|
53
|
-
class
|
|
53
|
+
class f extends Error {
|
|
54
54
|
constructor(e, t, n) {
|
|
55
55
|
const s = `Request timeout of ${n}ms exceeded for ${t?.method || "GET"} ${e}`;
|
|
56
|
-
super(s), this.name = "FetchTimeoutError", this.url = e, this.request = t, Object.setPrototypeOf(this,
|
|
56
|
+
super(s), this.name = "FetchTimeoutError", this.url = e, this.request = t, Object.setPrototypeOf(this, f.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 || {}),
|
|
60
|
-
const
|
|
61
|
-
var
|
|
62
|
-
class
|
|
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 || {}), O = /* @__PURE__ */ ((r) => (r.METHOD = "method", r.BODY = "body", r))(O || {});
|
|
60
|
+
const d = "Content-Type";
|
|
61
|
+
var u = /* @__PURE__ */ ((r) => (r.APPLICATION_JSON = "application/json", r.TEXT_EVENT_STREAM = "text/event-stream", r))(u || {});
|
|
62
|
+
class h {
|
|
63
63
|
constructor() {
|
|
64
64
|
this.interceptors = [];
|
|
65
65
|
}
|
|
@@ -97,12 +97,12 @@ class a {
|
|
|
97
97
|
return t;
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
-
class
|
|
100
|
+
class w {
|
|
101
101
|
constructor() {
|
|
102
|
-
this.request = new
|
|
102
|
+
this.request = new h(), this.response = new h(), this.error = new h();
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
class
|
|
105
|
+
class A {
|
|
106
106
|
/**
|
|
107
107
|
* 尝试转换请求体为合法的 fetch API body 类型
|
|
108
108
|
*
|
|
@@ -132,23 +132,23 @@ class w {
|
|
|
132
132
|
const t = { ...e };
|
|
133
133
|
t.body = JSON.stringify(e.body), t.headers || (t.headers = {});
|
|
134
134
|
const n = t.headers;
|
|
135
|
-
return n[
|
|
135
|
+
return n[d] || (n[d] = u.APPLICATION_JSON), t;
|
|
136
136
|
}
|
|
137
137
|
}
|
|
138
|
-
const
|
|
139
|
-
[
|
|
140
|
-
},
|
|
138
|
+
const p = {
|
|
139
|
+
[d]: u.APPLICATION_JSON
|
|
140
|
+
}, S = {
|
|
141
141
|
baseURL: "",
|
|
142
|
-
headers:
|
|
142
|
+
headers: p
|
|
143
143
|
};
|
|
144
|
-
class
|
|
144
|
+
class I {
|
|
145
145
|
/**
|
|
146
146
|
* 创建Fetcher实例
|
|
147
147
|
*
|
|
148
148
|
* @param options - Fetcher配置选项
|
|
149
149
|
*/
|
|
150
|
-
constructor(e =
|
|
151
|
-
this.headers =
|
|
150
|
+
constructor(e = S) {
|
|
151
|
+
this.headers = p, this.interceptors = new w(), this.urlBuilder = new E(e.baseURL), e.headers !== void 0 && (this.headers = e.headers), this.timeout = e.timeout, this.interceptors.request.use(new A());
|
|
152
152
|
}
|
|
153
153
|
/**
|
|
154
154
|
* 发起HTTP请求
|
|
@@ -172,8 +172,11 @@ class g {
|
|
|
172
172
|
e,
|
|
173
173
|
t.pathParams,
|
|
174
174
|
t.queryParams
|
|
175
|
-
)
|
|
176
|
-
|
|
175
|
+
), a = {
|
|
176
|
+
...s,
|
|
177
|
+
body: s.body
|
|
178
|
+
};
|
|
179
|
+
let i = await this.timeoutFetch(o, a);
|
|
177
180
|
return i = await this.interceptors.response.intercept(i), i;
|
|
178
181
|
} catch (o) {
|
|
179
182
|
throw await this.interceptors.error.intercept(o);
|
|
@@ -191,23 +194,27 @@ class g {
|
|
|
191
194
|
* @throws FetchTimeoutError 当请求超时时抛出
|
|
192
195
|
*/
|
|
193
196
|
async timeoutFetch(e, t) {
|
|
194
|
-
const n =
|
|
195
|
-
if (!
|
|
197
|
+
const n = t.timeout, s = P(n, this.timeout);
|
|
198
|
+
if (!s)
|
|
196
199
|
return fetch(e, t);
|
|
197
|
-
const
|
|
200
|
+
const o = new AbortController(), a = {
|
|
198
201
|
...t,
|
|
199
|
-
signal:
|
|
202
|
+
signal: o.signal
|
|
200
203
|
};
|
|
201
204
|
let i = null;
|
|
202
|
-
const
|
|
205
|
+
const m = new Promise((g, y) => {
|
|
203
206
|
i = setTimeout(() => {
|
|
204
207
|
i && clearTimeout(i);
|
|
205
|
-
const
|
|
206
|
-
|
|
207
|
-
|
|
208
|
+
const l = new f(
|
|
209
|
+
e,
|
|
210
|
+
t,
|
|
211
|
+
s
|
|
212
|
+
);
|
|
213
|
+
o.abort(l), y(l);
|
|
214
|
+
}, s);
|
|
208
215
|
});
|
|
209
216
|
try {
|
|
210
|
-
return await Promise.race([fetch(e,
|
|
217
|
+
return await Promise.race([fetch(e, a), m]);
|
|
211
218
|
} finally {
|
|
212
219
|
i && clearTimeout(i);
|
|
213
220
|
}
|
|
@@ -305,16 +312,16 @@ class g {
|
|
|
305
312
|
}
|
|
306
313
|
}
|
|
307
314
|
export {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
315
|
+
d as ContentTypeHeader,
|
|
316
|
+
u as ContentTypeValues,
|
|
317
|
+
f as FetchTimeoutError,
|
|
318
|
+
I as Fetcher,
|
|
319
|
+
w as FetcherInterceptors,
|
|
313
320
|
c as HttpMethod,
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
321
|
+
h as InterceptorManager,
|
|
322
|
+
O as RequestField,
|
|
323
|
+
E as UrlBuilder,
|
|
324
|
+
b as combineURLs,
|
|
325
|
+
T as isAbsoluteURL,
|
|
326
|
+
P as resolveTimeout
|
|
320
327
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(
|
|
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 y(r,e){return d(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class p{constructor(e){this.baseURL=e}build(e,t,i){let o=y(this.baseURL,e),s=this.interpolateUrl(o,t);if(i){const h=new URLSearchParams(i).toString();h&&(s+="?"+h)}return s}interpolateUrl(e,t){return t?e.replace(/{([^}]+)}/g,(i,o)=>{const s=t[o];if(s===void 0)throw new Error(`Missing required path parameter: ${o}`);return String(s)}):e}}function T(r,e){return typeof r<"u"?r:e}class u extends Error{constructor(e,t,i){const o=`Request timeout of ${i}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||{}),b=(r=>(r.METHOD="method",r.BODY="body",r))(b||{});const f="Content-Type";var l=(r=>(r.APPLICATION_JSON="application/json",r.TEXT_EVENT_STREAM="text/event-stream",r))(l||{});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 i of this.interceptors)i&&(t=await i.intercept(t));return t}}class E{constructor(){this.request=new m,this.response=new m,this.error=new m}}class w{intercept(e){if(e.body===void 0||e.body===null||typeof e.body!="object"||e.body instanceof ArrayBuffer||ArrayBuffer.isView(e.body)||e.body instanceof Blob||e.body instanceof File||e.body instanceof URLSearchParams||e.body instanceof FormData||e.body instanceof ReadableStream)return e;const t={...e};t.body=JSON.stringify(e.body),t.headers||(t.headers={});const i=t.headers;return i[f]||(i[f]=l.APPLICATION_JSON),t}}const P={[f]:l.APPLICATION_JSON},S={baseURL:"",headers:P};class A{constructor(e=S){this.headers=P,this.interceptors=new E,this.urlBuilder=new p(e.baseURL),e.headers!==void 0&&(this.headers=e.headers),this.timeout=e.timeout,this.interceptors.request.use(new w)}async fetch(e,t={}){const i={...this.headers||{},...t.headers||{}};let o={...t,headers:Object.keys(i).length>0?i:void 0};try{o=await this.interceptors.request.intercept(o);const s=this.urlBuilder.build(e,t.pathParams,t.queryParams),h={...o,body:o.body};let a=await this.timeoutFetch(s,h);return a=await this.interceptors.response.intercept(a),a}catch(s){throw await this.interceptors.error.intercept(s)}}async timeoutFetch(e,t){const i=t.timeout,o=T(i,this.timeout);if(!o)return fetch(e,t);const s=new AbortController,h={...t,signal:s.signal};let a=null;const g=new Promise((I,U)=>{a=setTimeout(()=>{a&&clearTimeout(a);const O=new u(e,t,o);s.abort(O),U(O)},o)});try{return await Promise.race([fetch(e,h),g])}finally{a&&clearTimeout(a)}}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.ContentTypeHeader=f,n.ContentTypeValues=l,n.FetchTimeoutError=u,n.Fetcher=A,n.FetcherInterceptors=E,n.HttpMethod=c,n.InterceptorManager=m,n.RequestField=b,n.UrlBuilder=p,n.combineURLs=y,n.isAbsoluteURL=d,n.resolveTimeout=T,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
|