@ahoo-wang/fetcher 0.0.8 → 0.0.9
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.map +1 -1
- package/dist/index.es.js +29 -29
- package/dist/index.umd.js +1 -1
- package/package.json +2 -2
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;AAEpD;;GAEG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,cAAc,EACd,cAAc;CAAG;
|
|
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;AAEpD;;GAEG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,cAAc,EACd,cAAc;CAAG;AAWrB;;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;AACH,qBAAa,OAAQ,YAAW,cAAc,EAAE,cAAc;IAC5D,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,C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AQ,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 m(r) {
|
|
2
2
|
return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(r);
|
|
3
3
|
}
|
|
4
|
-
function
|
|
5
|
-
return
|
|
4
|
+
function T(r, e) {
|
|
5
|
+
return m(e) ? e : e ? r.replace(/\/?\/$/, "") + "/" + e.replace(/^\/+/, "") : r;
|
|
6
6
|
}
|
|
7
|
-
class
|
|
7
|
+
class E {
|
|
8
8
|
/**
|
|
9
9
|
* 创建UrlBuilder实例
|
|
10
10
|
*
|
|
@@ -23,7 +23,7 @@ class T {
|
|
|
23
23
|
* @throws 当路径参数中缺少必需的占位符时抛出错误
|
|
24
24
|
*/
|
|
25
25
|
build(e, t, s) {
|
|
26
|
-
let i =
|
|
26
|
+
let i = T(this.baseURL, e), o = this.interpolateUrl(i, t);
|
|
27
27
|
if (s) {
|
|
28
28
|
const n = new URLSearchParams(s).toString();
|
|
29
29
|
n && (o += "?" + n);
|
|
@@ -47,7 +47,7 @@ class T {
|
|
|
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
53
|
class h extends Error {
|
|
@@ -56,9 +56,9 @@ class h extends Error {
|
|
|
56
56
|
super(i), this.name = "FetchTimeoutError", this.url = e, this.request = t, Object.setPrototypeOf(this, h.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
|
|
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 || {}), y = /* @__PURE__ */ ((r) => (r.METHOD = "method", r.BODY = "body", r))(y || {});
|
|
60
|
+
const O = "Content-Type";
|
|
61
|
+
var d = /* @__PURE__ */ ((r) => (r.APPLICATION_JSON = "application/json", r.TEXT_EVENT_STREAM = "text/event-stream", r))(d || {});
|
|
62
62
|
class a {
|
|
63
63
|
constructor() {
|
|
64
64
|
this.interceptors = [];
|
|
@@ -97,25 +97,25 @@ class a {
|
|
|
97
97
|
return t;
|
|
98
98
|
}
|
|
99
99
|
}
|
|
100
|
-
class
|
|
100
|
+
class w {
|
|
101
101
|
constructor() {
|
|
102
102
|
this.request = new a(), this.response = new a(), this.error = new a();
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
-
const
|
|
105
|
+
const f = {
|
|
106
|
+
[O]: d.APPLICATION_JSON
|
|
107
|
+
}, b = {
|
|
106
108
|
baseURL: "",
|
|
107
|
-
headers:
|
|
108
|
-
ContentTypeHeader: p.APPLICATION_JSON
|
|
109
|
-
}
|
|
109
|
+
headers: f
|
|
110
110
|
};
|
|
111
|
-
class
|
|
111
|
+
class A {
|
|
112
112
|
/**
|
|
113
113
|
* 创建Fetcher实例
|
|
114
114
|
*
|
|
115
115
|
* @param options - Fetcher配置选项
|
|
116
116
|
*/
|
|
117
|
-
constructor(e =
|
|
118
|
-
this.interceptors = new
|
|
117
|
+
constructor(e = b) {
|
|
118
|
+
this.headers = f, this.interceptors = new w(), this.urlBuilder = new E(e.baseURL), e.headers !== void 0 && (this.headers = e.headers), this.timeout = e.timeout;
|
|
119
119
|
}
|
|
120
120
|
/**
|
|
121
121
|
* 发起HTTP请求
|
|
@@ -158,7 +158,7 @@ class g {
|
|
|
158
158
|
* @throws FetchTimeoutError 当请求超时时抛出
|
|
159
159
|
*/
|
|
160
160
|
async timeoutFetch(e, t) {
|
|
161
|
-
const s =
|
|
161
|
+
const s = P(t.timeout, this.timeout);
|
|
162
162
|
if (!s)
|
|
163
163
|
return fetch(e, t);
|
|
164
164
|
const i = new AbortController(), o = {
|
|
@@ -166,11 +166,11 @@ class g {
|
|
|
166
166
|
signal: i.signal
|
|
167
167
|
};
|
|
168
168
|
let n = null;
|
|
169
|
-
const u = new Promise((
|
|
169
|
+
const u = new Promise((g, p) => {
|
|
170
170
|
n = setTimeout(() => {
|
|
171
171
|
n && clearTimeout(n);
|
|
172
172
|
const l = new h(e, t, s);
|
|
173
|
-
i.abort(l),
|
|
173
|
+
i.abort(l), p(l);
|
|
174
174
|
}, s);
|
|
175
175
|
});
|
|
176
176
|
try {
|
|
@@ -272,16 +272,16 @@ class g {
|
|
|
272
272
|
}
|
|
273
273
|
}
|
|
274
274
|
export {
|
|
275
|
-
|
|
276
|
-
|
|
275
|
+
O as ContentTypeHeader,
|
|
276
|
+
d as ContentTypeValues,
|
|
277
277
|
h as FetchTimeoutError,
|
|
278
|
-
|
|
279
|
-
|
|
278
|
+
A as Fetcher,
|
|
279
|
+
w as FetcherInterceptors,
|
|
280
280
|
c as HttpMethod,
|
|
281
281
|
a as InterceptorManager,
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
282
|
+
y as RequestField,
|
|
283
|
+
E as UrlBuilder,
|
|
284
|
+
T as combineURLs,
|
|
285
|
+
m as isAbsoluteURL,
|
|
286
|
+
P as resolveTimeout
|
|
287
287
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(n,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(n=typeof globalThis<"u"?globalThis:n||self,h(n.Fetcher={}))})(this,(function(n){"use strict";function h(r){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(r)}function f(r,e){return h(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class m{constructor(e){this.baseURL=e}build(e,t,s){let o=f(this.baseURL,e),a=this.interpolateUrl(o,t);if(s){const i=new URLSearchParams(s).toString();i&&(a+="?"+i)}return a}interpolateUrl(e,t){return t?e.replace(/{([^}]+)}/g,(s,o)=>{const a=t[o];if(a===void 0)throw new Error(`Missing required path parameter: ${o}`);return String(a)}):e}}function p(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||{}),T=(r=>(r.METHOD="method",r.BODY="body",r))(T||{});const
|
|
1
|
+
(function(n,h){typeof exports=="object"&&typeof module<"u"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(n=typeof globalThis<"u"?globalThis:n||self,h(n.Fetcher={}))})(this,(function(n){"use strict";function h(r){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(r)}function f(r,e){return h(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class m{constructor(e){this.baseURL=e}build(e,t,s){let o=f(this.baseURL,e),a=this.interpolateUrl(o,t);if(s){const i=new URLSearchParams(s).toString();i&&(a+="?"+i)}return a}interpolateUrl(e,t){return t?e.replace(/{([^}]+)}/g,(s,o)=>{const a=t[o];if(a===void 0)throw new Error(`Missing required path parameter: ${o}`);return String(a)}):e}}function p(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||{}),T=(r=>(r.METHOD="method",r.BODY="body",r))(T||{});const E="Content-Type";var d=(r=>(r.APPLICATION_JSON="application/json",r.TEXT_EVENT_STREAM="text/event-stream",r))(d||{});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 y{constructor(){this.request=new l,this.response=new l,this.error=new l}}const P={[E]:d.APPLICATION_JSON},w={baseURL:"",headers:P};class g{constructor(e=w){this.headers=P,this.interceptors=new y,this.urlBuilder=new m(e.baseURL),e.headers!==void 0&&(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 a=this.urlBuilder.build(e,t.pathParams,t.queryParams);o=await this.interceptors.request.intercept(o);try{let i=await this.timeoutFetch(a,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=p(t.timeout,this.timeout);if(!s)return fetch(e,t);const o=new AbortController,a={...t,signal:o.signal};let i=null;const O=new Promise((U,S)=>{i=setTimeout(()=>{i&&clearTimeout(i);const b=new u(e,t,s);o.abort(b),S(b)},s)});try{return await Promise.race([fetch(e,a),O])}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.ContentTypeHeader=E,n.ContentTypeValues=d,n.FetchTimeoutError=u,n.Fetcher=g,n.FetcherInterceptors=y,n.HttpMethod=c,n.InterceptorManager=l,n.RequestField=T,n.UrlBuilder=m,n.combineURLs=f,n.isAbsoluteURL=h,n.resolveTimeout=p,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/fetcher",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.9",
|
|
4
4
|
"description": "Core library providing basic HTTP client functionality for Fetcher",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
],
|
|
11
11
|
"author": "Ahoo-Wang",
|
|
12
12
|
"license": "Apache-2.0",
|
|
13
|
-
"homepage": "https://github.com/Ahoo-Wang/fetcher/tree/master/packages/
|
|
13
|
+
"homepage": "https://github.com/Ahoo-Wang/fetcher/tree/master/packages/fetcher",
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
16
16
|
"url": "https://github.com/Ahoo-Wang/fetcher.git",
|