@ahoo-wang/fetcher 0.3.5 → 0.3.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 +21 -0
- package/README.zh-CN.md +21 -0
- package/dist/index.es.js +18 -16
- package/dist/index.umd.js +1 -1
- package/dist/namedFetcher.d.ts +22 -0
- package/dist/namedFetcher.d.ts.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -127,6 +127,27 @@ try {
|
|
|
127
127
|
}
|
|
128
128
|
```
|
|
129
129
|
|
|
130
|
+
### Default Fetcher Usage
|
|
131
|
+
|
|
132
|
+
The library also exports a pre-configured default fetcher instance that can be used directly:
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { fetcher } from '@ahoo-wang/fetcher';
|
|
136
|
+
|
|
137
|
+
// Use the default fetcher directly
|
|
138
|
+
fetcher
|
|
139
|
+
.get('/users')
|
|
140
|
+
.then(response => response.json())
|
|
141
|
+
.then(data => console.log(data));
|
|
142
|
+
|
|
143
|
+
// The default fetcher is also available through the registrar
|
|
144
|
+
import { fetcherRegistrar } from '@ahoo-wang/fetcher';
|
|
145
|
+
|
|
146
|
+
const defaultFetcher = fetcherRegistrar.default;
|
|
147
|
+
// defaultFetcher is the same instance as fetcher
|
|
148
|
+
console.log(defaultFetcher === fetcher); // true
|
|
149
|
+
```
|
|
150
|
+
|
|
130
151
|
### Interceptor Usage
|
|
131
152
|
|
|
132
153
|
```typescript
|
package/README.zh-CN.md
CHANGED
|
@@ -127,6 +127,27 @@ try {
|
|
|
127
127
|
}
|
|
128
128
|
```
|
|
129
129
|
|
|
130
|
+
### 默认 Fetcher 用法
|
|
131
|
+
|
|
132
|
+
该库还导出了一个预配置的默认 fetcher 实例,可以直接使用:
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
import { fetcher } from '@ahoo-wang/fetcher';
|
|
136
|
+
|
|
137
|
+
// 直接使用默认 fetcher
|
|
138
|
+
fetcher
|
|
139
|
+
.get('/users')
|
|
140
|
+
.then(response => response.json())
|
|
141
|
+
.then(data => console.log(data));
|
|
142
|
+
|
|
143
|
+
// 默认 fetcher 也可以通过注册器获取
|
|
144
|
+
import { fetcherRegistrar } from '@ahoo-wang/fetcher';
|
|
145
|
+
|
|
146
|
+
const defaultFetcher = fetcherRegistrar.default;
|
|
147
|
+
// defaultFetcher 与 fetcher 是同一个实例
|
|
148
|
+
console.log(defaultFetcher === fetcher); // true
|
|
149
|
+
```
|
|
150
|
+
|
|
130
151
|
### 拦截器用法
|
|
131
152
|
|
|
132
153
|
```typescript
|
package/dist/index.es.js
CHANGED
|
@@ -50,15 +50,15 @@ class P {
|
|
|
50
50
|
function O(r, e) {
|
|
51
51
|
return typeof r < "u" ? r : e;
|
|
52
52
|
}
|
|
53
|
-
class
|
|
53
|
+
class f extends Error {
|
|
54
54
|
constructor(e, t) {
|
|
55
55
|
const s = e.request?.method || "GET", i = `Request timeout of ${t}ms exceeded for ${s} ${e.url}`;
|
|
56
|
-
super(i), this.name = "FetchTimeoutError", this.exchange = e, Object.setPrototypeOf(this,
|
|
56
|
+
super(i), this.name = "FetchTimeoutError", this.exchange = e, Object.setPrototypeOf(this, f.prototype);
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
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 || {}), q = /* @__PURE__ */ ((r) => (r.METHOD = "method", r.BODY = "body", r))(q || {});
|
|
60
60
|
const d = "Content-Type";
|
|
61
|
-
var
|
|
61
|
+
var p = /* @__PURE__ */ ((r) => (r.APPLICATION_JSON = "application/json", r.TEXT_EVENT_STREAM = "text/event-stream", r))(p || {});
|
|
62
62
|
class h {
|
|
63
63
|
constructor() {
|
|
64
64
|
this.interceptors = [];
|
|
@@ -133,11 +133,11 @@ class A {
|
|
|
133
133
|
const s = { ...t };
|
|
134
134
|
s.body = JSON.stringify(t.body), s.headers || (s.headers = {});
|
|
135
135
|
const i = s.headers;
|
|
136
|
-
return i[d] || (i[d] =
|
|
136
|
+
return i[d] || (i[d] = p.APPLICATION_JSON), { ...e, request: s };
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
139
|
const y = {
|
|
140
|
-
[d]:
|
|
140
|
+
[d]: p.APPLICATION_JSON
|
|
141
141
|
}, T = {
|
|
142
142
|
baseURL: "",
|
|
143
143
|
headers: y
|
|
@@ -227,11 +227,11 @@ class S {
|
|
|
227
227
|
signal: n.signal
|
|
228
228
|
};
|
|
229
229
|
let a = null;
|
|
230
|
-
const w = new Promise((
|
|
230
|
+
const w = new Promise((U, E) => {
|
|
231
231
|
a = setTimeout(() => {
|
|
232
232
|
a && clearTimeout(a);
|
|
233
|
-
const
|
|
234
|
-
n.abort(
|
|
233
|
+
const m = new f(e, o);
|
|
234
|
+
n.abort(m), E(m);
|
|
235
235
|
}, o);
|
|
236
236
|
});
|
|
237
237
|
try {
|
|
@@ -328,7 +328,7 @@ class S {
|
|
|
328
328
|
return this.methodFetch(c.OPTIONS, e, t);
|
|
329
329
|
}
|
|
330
330
|
}
|
|
331
|
-
const
|
|
331
|
+
const l = "default";
|
|
332
332
|
class R {
|
|
333
333
|
constructor() {
|
|
334
334
|
this.registrar = /* @__PURE__ */ new Map();
|
|
@@ -402,7 +402,7 @@ class R {
|
|
|
402
402
|
* const defaultFetcher = fetcherRegistrar.default;
|
|
403
403
|
*/
|
|
404
404
|
get default() {
|
|
405
|
-
return this.requiredGet(
|
|
405
|
+
return this.requiredGet(l);
|
|
406
406
|
}
|
|
407
407
|
/**
|
|
408
408
|
* Set the default Fetcher instance
|
|
@@ -413,7 +413,7 @@ class R {
|
|
|
413
413
|
* fetcherRegistrar.default = fetcher;
|
|
414
414
|
*/
|
|
415
415
|
set default(e) {
|
|
416
|
-
this.register(
|
|
416
|
+
this.register(l, e);
|
|
417
417
|
}
|
|
418
418
|
/**
|
|
419
419
|
* Get a copy of all registered fetchers
|
|
@@ -430,7 +430,7 @@ class R {
|
|
|
430
430
|
}
|
|
431
431
|
}
|
|
432
432
|
const I = new R();
|
|
433
|
-
class
|
|
433
|
+
class N extends S {
|
|
434
434
|
/**
|
|
435
435
|
* Create a NamedFetcher instance and automatically register it with the global fetcherRegistrar
|
|
436
436
|
*
|
|
@@ -452,21 +452,23 @@ class U extends S {
|
|
|
452
452
|
super(t), this.name = e, I.register(e, this);
|
|
453
453
|
}
|
|
454
454
|
}
|
|
455
|
+
const L = new N(l);
|
|
455
456
|
export {
|
|
456
457
|
d as ContentTypeHeader,
|
|
457
|
-
|
|
458
|
-
|
|
458
|
+
p as ContentTypeValues,
|
|
459
|
+
f as FetchTimeoutError,
|
|
459
460
|
S as Fetcher,
|
|
460
461
|
F as FetcherInterceptors,
|
|
461
462
|
R as FetcherRegistrar,
|
|
462
463
|
c as HttpMethod,
|
|
463
464
|
h as InterceptorManager,
|
|
464
|
-
|
|
465
|
+
N as NamedFetcher,
|
|
465
466
|
q as RequestField,
|
|
466
467
|
P as UrlBuilder,
|
|
467
468
|
g as combineURLs,
|
|
468
|
-
|
|
469
|
+
l as defaultFetcherName,
|
|
469
470
|
T as defaultOptions,
|
|
471
|
+
L as fetcher,
|
|
470
472
|
I as fetcherRegistrar,
|
|
471
473
|
b as isAbsoluteURL,
|
|
472
474
|
O as resolveTimeout
|
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 b(r,e){return h(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class g{constructor(e){this.baseURL=e}build(e,t,s){let o=b(this.baseURL,e),c=this.interpolateUrl(o,t);if(s){const i=new URLSearchParams(s).toString();i&&(c+="?"+i)}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
|
|
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 b(r,e){return h(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class g{constructor(e){this.baseURL=e}build(e,t,s){let o=b(this.baseURL,e),c=this.interpolateUrl(o,t);if(s){const i=new URLSearchParams(s).toString();i&&(c+="?"+i)}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 w(r,e){return typeof r<"u"?r:e}class f 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,f.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 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 F{constructor(){this.request=new p,this.response=new p,this.error=new p}}class U{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]=m.APPLICATION_JSON),{...e,request:s}}}const P={[l]:m.APPLICATION_JSON},T={baseURL:"",headers:P};class O{constructor(e=T){this.headers=P,this.interceptors=new F,this.urlBuilder=new g(e.baseURL),e.headers!==void 0&&(this.headers=e.headers),this.timeout=e.timeout,this.interceptors.request.use(new U)}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 i={fetcher:this,url:c,request:o,response:void 0,error:void 0};try{const d={...i};i=await this.interceptors.request.intercept(d),i.response=await this.timeoutFetch(i);const a={...i};return i=await this.interceptors.response.intercept(a),i}catch(d){if(i.error=d,i=await this.interceptors.error.intercept(i),i.response)return i;throw i.error}}async timeoutFetch(e){const t=e.url,s=e.request,o=s.timeout,c=w(o,this.timeout);if(!c)return fetch(t,s);const i=new AbortController,d={...s,signal:i.signal};let a=null;const N=new Promise((v,L)=>{a=setTimeout(()=>{a&&clearTimeout(a);const A=new f(e,c);i.abort(A),L(A)},c)});try{return await Promise.race([fetch(t,d),N])}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)}}const y="default";class q{constructor(){this.registrar=new Map}register(e,t){this.registrar.set(e,t)}unregister(e){return this.registrar.delete(e)}get(e){return this.registrar.get(e)}requiredGet(e){const t=this.get(e);if(!t)throw new Error(`Fetcher ${e} not found`);return t}get default(){return this.requiredGet(y)}set default(e){this.register(y,e)}get fetchers(){return new Map(this.registrar)}}const R=new q;class S extends O{constructor(e,t=T){super(t),this.name=e,R.register(e,this)}}const I=new S(y);n.ContentTypeHeader=l,n.ContentTypeValues=m,n.FetchTimeoutError=f,n.Fetcher=O,n.FetcherInterceptors=F,n.FetcherRegistrar=q,n.HttpMethod=u,n.InterceptorManager=p,n.NamedFetcher=S,n.RequestField=E,n.UrlBuilder=g,n.combineURLs=b,n.defaultFetcherName=y,n.defaultOptions=T,n.fetcher=I,n.fetcherRegistrar=R,n.isAbsoluteURL=h,n.resolveTimeout=w,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
|
package/dist/namedFetcher.d.ts
CHANGED
|
@@ -44,4 +44,26 @@ export declare class NamedFetcher extends Fetcher implements NamedCapable {
|
|
|
44
44
|
*/
|
|
45
45
|
constructor(name: string, options?: FetcherOptions);
|
|
46
46
|
}
|
|
47
|
+
/**
|
|
48
|
+
* Default named fetcher instance registered with the name 'default'.
|
|
49
|
+
* This provides a convenient way to use a pre-configured fetcher instance
|
|
50
|
+
* without having to create and register one manually.
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* // Use the default fetcher directly
|
|
54
|
+
* import { fetcher } from '@ahoo-wang/fetcher';
|
|
55
|
+
*
|
|
56
|
+
* fetcher.get('/users')
|
|
57
|
+
* .then(response => response.json())
|
|
58
|
+
* .then(data => console.log(data));
|
|
59
|
+
*
|
|
60
|
+
* // Or retrieve it from the registrar
|
|
61
|
+
* import { fetcherRegistrar } from '@ahoo-wang/fetcher';
|
|
62
|
+
*
|
|
63
|
+
* const defaultFetcher = fetcherRegistrar.default;
|
|
64
|
+
* defaultFetcher.get('/users')
|
|
65
|
+
* .then(response => response.json())
|
|
66
|
+
* .then(data => console.log(data));
|
|
67
|
+
*/
|
|
68
|
+
export declare const fetcher: NamedFetcher;
|
|
47
69
|
//# sourceMappingURL=namedFetcher.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"namedFetcher.d.ts","sourceRoot":"","sources":["../src/namedFetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAkB,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGpE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,YAAa,SAAQ,OAAQ,YAAW,YAAY;IAC/D;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;;;;;;;;;;;;;;OAgBG;gBACS,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,cAA+B;CAKnE"}
|
|
1
|
+
{"version":3,"file":"namedFetcher.d.ts","sourceRoot":"","sources":["../src/namedFetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACvC,OAAO,EAAkB,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAGpE;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,YAAa,SAAQ,OAAQ,YAAW,YAAY;IAC/D;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;IAEb;;;;;;;;;;;;;;;;OAgBG;gBACS,IAAI,EAAE,MAAM,EAAE,OAAO,GAAE,cAA+B;CAKnE;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,OAAO,cAAuC,CAAC"}
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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;AAED,eAAO,MAAM,iBAAiB,iBAAiB,CAAC;AAEhD,oBAAY,iBAAiB;IAC3B,gBAAgB,qBAAqB;IACrC,iBAAiB,sBAAsB;CACxC;
|
|
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;AAED,eAAO,MAAM,iBAAiB,iBAAiB,CAAC;AAEhD,oBAAY,iBAAiB;IAC3B,gBAAgB,qBAAqB;IACrC,iBAAiB,sBAAsB;CACxC;AAED,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;CACd"}
|