@ahoo-wang/fetcher 0.3.1 → 0.3.3
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/fetcherRegistrar.d.ts +122 -0
- package/dist/fetcherRegistrar.d.ts.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +139 -35
- package/dist/index.umd.js +1 -1
- package/dist/interceptor.d.ts.map +1 -1
- package/dist/requestBodyInterceptor.d.ts.map +1 -1
- package/dist/timeout.d.ts.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/urlBuilder.d.ts.map +1 -1
- package/dist/urls.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/fetcher.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"
|
|
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,aAAa,EAAE,MAAM,eAAe,CAAC;AAGnE;;GAEG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,cAAc,EACd,cAAc;CACjB;AAWD;;GAEG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,IAAI,CAAC,WAAW,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IACjC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAClC,IAAI,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAC;CAC9C;AAED;;;;;;;;;;GAUG;AACH,qBAAa,OAAQ,YAAW,cAAc,EAAE,cAAc;IAC5D,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAkB;IAClD,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,UAAU,CAAa;IAC/B,YAAY,EAAE,mBAAmB,CAA6B;IAE9D;;;;OAIG;gBACS,OAAO,GAAE,cAA+B;IASpD;;;;;;OAMG;IACG,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,cAAmB,GAAG,OAAO,CAAC,QAAQ,CAAC;IAQzE;;;;;;;;OAQG;IACG,OAAO,CACX,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,cAAmB,GAC3B,OAAO,CAAC,aAAa,CAAC;IAgDzB;;;;;;;;;;OAUG;YACW,YAAY;IA2C1B;;;;;;;OAOG;YACW,WAAW;IAWzB;;;;;;OAMG;IACG,GAAG,CACP,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,GAAG,YAAY,CAAC,IAAI,CAAM,GAC1E,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;OAMG;IACG,IAAI,CACR,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;OAMG;IACG,GAAG,CACP,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;OAMG;IACG,MAAM,CACV,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;OAMG;IACG,KAAK,CACT,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,cAAc,EAAE,YAAY,CAAC,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;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;IAIpB;;;;;;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;CAGrB"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { Fetcher } from './fetcher';
|
|
2
|
+
/**
|
|
3
|
+
* FetcherRegistrar is a registry for managing multiple Fetcher instances.
|
|
4
|
+
* It allows registering, retrieving, and unregistering Fetcher instances by name.
|
|
5
|
+
* This is useful for applications that need to manage multiple HTTP clients
|
|
6
|
+
* with different configurations.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* // Register a fetcher
|
|
10
|
+
* const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
11
|
+
* fetcherRegistrar.register('api', fetcher);
|
|
12
|
+
*
|
|
13
|
+
* // Retrieve a fetcher
|
|
14
|
+
* const apiFetcher = fetcherRegistrar.get('api');
|
|
15
|
+
*
|
|
16
|
+
* // Use the default fetcher
|
|
17
|
+
* const defaultFetcher = fetcherRegistrar.default;
|
|
18
|
+
*
|
|
19
|
+
* // Unregister a fetcher
|
|
20
|
+
* fetcherRegistrar.unregister('api');
|
|
21
|
+
*/
|
|
22
|
+
export declare class FetcherRegistrar {
|
|
23
|
+
/**
|
|
24
|
+
* Internal map for storing registered fetchers
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
27
|
+
private registrar;
|
|
28
|
+
/**
|
|
29
|
+
* Register a Fetcher instance with a given name
|
|
30
|
+
*
|
|
31
|
+
* @param name - The name to register the fetcher under
|
|
32
|
+
* @param fetcher - The Fetcher instance to register
|
|
33
|
+
* @example
|
|
34
|
+
* const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
35
|
+
* fetcherRegistrar.register('api', fetcher);
|
|
36
|
+
*/
|
|
37
|
+
register(name: string, fetcher: Fetcher): void;
|
|
38
|
+
/**
|
|
39
|
+
* Unregister a Fetcher instance by name
|
|
40
|
+
*
|
|
41
|
+
* @param name - The name of the fetcher to unregister
|
|
42
|
+
* @returns boolean - True if the fetcher was successfully unregistered, false otherwise
|
|
43
|
+
* @example
|
|
44
|
+
* const success = fetcherRegistrar.unregister('api');
|
|
45
|
+
* if (success) {
|
|
46
|
+
* console.log('Fetcher unregistered successfully');
|
|
47
|
+
* }
|
|
48
|
+
*/
|
|
49
|
+
unregister(name: string): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Get a Fetcher instance by name
|
|
52
|
+
*
|
|
53
|
+
* @param name - The name of the fetcher to retrieve
|
|
54
|
+
* @returns Fetcher | undefined - The Fetcher instance if found, undefined otherwise
|
|
55
|
+
* @example
|
|
56
|
+
* const fetcher = fetcherRegistrar.get('api');
|
|
57
|
+
* if (fetcher) {
|
|
58
|
+
* // Use the fetcher
|
|
59
|
+
* }
|
|
60
|
+
*/
|
|
61
|
+
get(name: string): Fetcher | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Get a Fetcher instance by name, throwing an error if not found
|
|
64
|
+
*
|
|
65
|
+
* @param name - The name of the fetcher to retrieve
|
|
66
|
+
* @returns Fetcher - The Fetcher instance
|
|
67
|
+
* @throws Error - If no fetcher is registered with the given name
|
|
68
|
+
* @example
|
|
69
|
+
* try {
|
|
70
|
+
* const fetcher = fetcherRegistrar.requiredGet('api');
|
|
71
|
+
* // Use the fetcher
|
|
72
|
+
* } catch (error) {
|
|
73
|
+
* console.error('Fetcher not found:', error.message);
|
|
74
|
+
* }
|
|
75
|
+
*/
|
|
76
|
+
requiredGet(name: string): Fetcher;
|
|
77
|
+
/**
|
|
78
|
+
* Get the default Fetcher instance
|
|
79
|
+
*
|
|
80
|
+
* @returns Fetcher - The default Fetcher instance
|
|
81
|
+
* @throws Error - If no default fetcher is registered
|
|
82
|
+
* @example
|
|
83
|
+
* const defaultFetcher = fetcherRegistrar.default;
|
|
84
|
+
*/
|
|
85
|
+
get default(): Fetcher;
|
|
86
|
+
/**
|
|
87
|
+
* Set the default Fetcher instance
|
|
88
|
+
*
|
|
89
|
+
* @param fetcher - The Fetcher instance to set as default
|
|
90
|
+
* @example
|
|
91
|
+
* const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
92
|
+
* fetcherRegistrar.default = fetcher;
|
|
93
|
+
*/
|
|
94
|
+
set default(fetcher: Fetcher);
|
|
95
|
+
/**
|
|
96
|
+
* Get a copy of all registered fetchers
|
|
97
|
+
*
|
|
98
|
+
* @returns Map<string, Fetcher> - A copy of the internal registrar map
|
|
99
|
+
* @example
|
|
100
|
+
* const allFetchers = fetcherRegistrar.fetchers;
|
|
101
|
+
* for (const [name, fetcher] of allFetchers) {
|
|
102
|
+
* console.log(`Fetcher ${name}:`, fetcher);
|
|
103
|
+
* }
|
|
104
|
+
*/
|
|
105
|
+
get fetchers(): Map<string, Fetcher>;
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Global instance of FetcherRegistrar
|
|
109
|
+
* This is the default registrar used throughout the application
|
|
110
|
+
*
|
|
111
|
+
* @example
|
|
112
|
+
* import { fetcherRegistrar } from '@ahoo-wang/fetcher';
|
|
113
|
+
*
|
|
114
|
+
* // Register a fetcher
|
|
115
|
+
* const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
116
|
+
* fetcherRegistrar.register('api', fetcher);
|
|
117
|
+
*
|
|
118
|
+
* // Retrieve a fetcher
|
|
119
|
+
* const apiFetcher = fetcherRegistrar.get('api');
|
|
120
|
+
*/
|
|
121
|
+
export declare const fetcherRegistrar: FetcherRegistrar;
|
|
122
|
+
//# sourceMappingURL=fetcherRegistrar.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetcherRegistrar.d.ts","sourceRoot":"","sources":["../src/fetcherRegistrar.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAOpC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,gBAAgB;IAC3B;;;OAGG;IACH,OAAO,CAAC,SAAS,CAAmC;IAEpD;;;;;;;;OAQG;IACH,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI;IAI9C;;;;;;;;;;OAUG;IACH,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAIjC;;;;;;;;;;OAUG;IACH,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG,SAAS;IAItC;;;;;;;;;;;;;OAaG;IACH,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO;IAQlC;;;;;;;OAOG;IACH,IAAI,OAAO,IAAI,OAAO,CAErB;IAED;;;;;;;OAOG;IACH,IAAI,OAAO,CAAC,OAAO,EAAE,OAAO,EAE3B;IAED;;;;;;;;;OASG;IACH,IAAI,QAAQ,IAAI,GAAG,CAAC,MAAM,EAAE,OAAO,CAAC,CAEnC;CACF;AAED;;;;;;;;;;;;;GAaG;AACH,eAAO,MAAM,gBAAgB,kBAAyB,CAAC"}
|
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":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,WAAW,CAAC;AAC1B,cAAc,oBAAoB,CAAC;AACnC,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 E(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 E(e) ? e : e ? r.replace(/\/?\/$/, "") + "/" + e.replace(/^\/+/, "") : r;
|
|
6
6
|
}
|
|
7
|
-
class
|
|
7
|
+
class g {
|
|
8
8
|
/**
|
|
9
9
|
* 创建UrlBuilder实例
|
|
10
10
|
*
|
|
@@ -23,12 +23,12 @@ class w {
|
|
|
23
23
|
* @throws 当路径参数中缺少必需的占位符时抛出错误
|
|
24
24
|
*/
|
|
25
25
|
build(e, t, s) {
|
|
26
|
-
let
|
|
26
|
+
let i = b(this.baseURL, e), o = this.interpolateUrl(i, t);
|
|
27
27
|
if (s) {
|
|
28
28
|
const n = new URLSearchParams(s).toString();
|
|
29
|
-
n && (
|
|
29
|
+
n && (o += "?" + n);
|
|
30
30
|
}
|
|
31
|
-
return
|
|
31
|
+
return o;
|
|
32
32
|
}
|
|
33
33
|
/**
|
|
34
34
|
* 替换url中的占位符参数
|
|
@@ -39,11 +39,11 @@ class w {
|
|
|
39
39
|
* @throws 当路径参数中缺少必需的占位符时抛出错误
|
|
40
40
|
*/
|
|
41
41
|
interpolateUrl(e, t) {
|
|
42
|
-
return t ? e.replace(/{([^}]+)}/g, (s,
|
|
43
|
-
const
|
|
44
|
-
if (
|
|
45
|
-
throw new Error(`Missing required path parameter: ${
|
|
46
|
-
return String(
|
|
42
|
+
return t ? e.replace(/{([^}]+)}/g, (s, i) => {
|
|
43
|
+
const o = t[i];
|
|
44
|
+
if (o === void 0)
|
|
45
|
+
throw new Error(`Missing required path parameter: ${i}`);
|
|
46
|
+
return String(o);
|
|
47
47
|
}) : e;
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -52,8 +52,8 @@ function P(r, e) {
|
|
|
52
52
|
}
|
|
53
53
|
class l extends Error {
|
|
54
54
|
constructor(e, t) {
|
|
55
|
-
const s = e.request?.method || "GET",
|
|
56
|
-
super(
|
|
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, l.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 || {}), O = /* @__PURE__ */ ((r) => (r.METHOD = "method", r.BODY = "body", r))(O || {});
|
|
@@ -132,15 +132,15 @@ class A {
|
|
|
132
132
|
return e;
|
|
133
133
|
const s = { ...t };
|
|
134
134
|
s.body = JSON.stringify(t.body), s.headers || (s.headers = {});
|
|
135
|
-
const
|
|
136
|
-
return
|
|
135
|
+
const i = s.headers;
|
|
136
|
+
return i[d] || (i[d] = f.APPLICATION_JSON), { ...e, request: s };
|
|
137
137
|
}
|
|
138
138
|
}
|
|
139
|
-
const
|
|
139
|
+
const y = {
|
|
140
140
|
[d]: f.APPLICATION_JSON
|
|
141
|
-
},
|
|
141
|
+
}, F = {
|
|
142
142
|
baseURL: "",
|
|
143
|
-
headers:
|
|
143
|
+
headers: y
|
|
144
144
|
};
|
|
145
145
|
class I {
|
|
146
146
|
/**
|
|
@@ -148,8 +148,8 @@ class I {
|
|
|
148
148
|
*
|
|
149
149
|
* @param options - Fetcher configuration options
|
|
150
150
|
*/
|
|
151
|
-
constructor(e =
|
|
152
|
-
this.headers =
|
|
151
|
+
constructor(e = F) {
|
|
152
|
+
this.headers = y, this.interceptors = new q(), this.urlBuilder = new g(e.baseURL), e.headers !== void 0 && (this.headers = e.headers), this.timeout = e.timeout, this.interceptors.request.use(new A());
|
|
153
153
|
}
|
|
154
154
|
/**
|
|
155
155
|
* Make an HTTP request
|
|
@@ -177,18 +177,18 @@ class I {
|
|
|
177
177
|
const s = {
|
|
178
178
|
...this.headers || {},
|
|
179
179
|
...t.headers || {}
|
|
180
|
-
},
|
|
180
|
+
}, i = {
|
|
181
181
|
...t,
|
|
182
182
|
headers: Object.keys(s).length > 0 ? s : void 0
|
|
183
|
-
},
|
|
183
|
+
}, o = this.urlBuilder.build(
|
|
184
184
|
e,
|
|
185
185
|
t.pathParams,
|
|
186
186
|
t.queryParams
|
|
187
187
|
);
|
|
188
188
|
let n = {
|
|
189
189
|
fetcher: this,
|
|
190
|
-
url:
|
|
191
|
-
request:
|
|
190
|
+
url: o,
|
|
191
|
+
request: i,
|
|
192
192
|
response: void 0,
|
|
193
193
|
error: void 0
|
|
194
194
|
};
|
|
@@ -219,25 +219,25 @@ class I {
|
|
|
219
219
|
* @throws FetchTimeoutError Thrown when the request times out
|
|
220
220
|
*/
|
|
221
221
|
async timeoutFetch(e) {
|
|
222
|
-
const t = e.url, s = e.request,
|
|
223
|
-
if (!
|
|
222
|
+
const t = e.url, s = e.request, i = s.timeout, o = P(i, this.timeout);
|
|
223
|
+
if (!o)
|
|
224
224
|
return fetch(t, s);
|
|
225
225
|
const n = new AbortController(), u = {
|
|
226
226
|
...s,
|
|
227
227
|
signal: n.signal
|
|
228
228
|
};
|
|
229
229
|
let a = null;
|
|
230
|
-
const
|
|
230
|
+
const T = new Promise((R, w) => {
|
|
231
231
|
a = setTimeout(() => {
|
|
232
232
|
a && clearTimeout(a);
|
|
233
|
-
const p = new l(e,
|
|
234
|
-
n.abort(p),
|
|
235
|
-
},
|
|
233
|
+
const p = new l(e, o);
|
|
234
|
+
n.abort(p), w(p);
|
|
235
|
+
}, o);
|
|
236
236
|
});
|
|
237
237
|
try {
|
|
238
238
|
return await Promise.race([
|
|
239
239
|
fetch(t, u),
|
|
240
|
-
|
|
240
|
+
T
|
|
241
241
|
]);
|
|
242
242
|
} finally {
|
|
243
243
|
a && clearTimeout(a);
|
|
@@ -328,17 +328,121 @@ class I {
|
|
|
328
328
|
return this.methodFetch(c.OPTIONS, e, t);
|
|
329
329
|
}
|
|
330
330
|
}
|
|
331
|
+
const m = "default";
|
|
332
|
+
class S {
|
|
333
|
+
constructor() {
|
|
334
|
+
this.registrar = /* @__PURE__ */ new Map();
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Register a Fetcher instance with a given name
|
|
338
|
+
*
|
|
339
|
+
* @param name - The name to register the fetcher under
|
|
340
|
+
* @param fetcher - The Fetcher instance to register
|
|
341
|
+
* @example
|
|
342
|
+
* const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
343
|
+
* fetcherRegistrar.register('api', fetcher);
|
|
344
|
+
*/
|
|
345
|
+
register(e, t) {
|
|
346
|
+
this.registrar.set(e, t);
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Unregister a Fetcher instance by name
|
|
350
|
+
*
|
|
351
|
+
* @param name - The name of the fetcher to unregister
|
|
352
|
+
* @returns boolean - True if the fetcher was successfully unregistered, false otherwise
|
|
353
|
+
* @example
|
|
354
|
+
* const success = fetcherRegistrar.unregister('api');
|
|
355
|
+
* if (success) {
|
|
356
|
+
* console.log('Fetcher unregistered successfully');
|
|
357
|
+
* }
|
|
358
|
+
*/
|
|
359
|
+
unregister(e) {
|
|
360
|
+
return this.registrar.delete(e);
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* Get a Fetcher instance by name
|
|
364
|
+
*
|
|
365
|
+
* @param name - The name of the fetcher to retrieve
|
|
366
|
+
* @returns Fetcher | undefined - The Fetcher instance if found, undefined otherwise
|
|
367
|
+
* @example
|
|
368
|
+
* const fetcher = fetcherRegistrar.get('api');
|
|
369
|
+
* if (fetcher) {
|
|
370
|
+
* // Use the fetcher
|
|
371
|
+
* }
|
|
372
|
+
*/
|
|
373
|
+
get(e) {
|
|
374
|
+
return this.registrar.get(e);
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Get a Fetcher instance by name, throwing an error if not found
|
|
378
|
+
*
|
|
379
|
+
* @param name - The name of the fetcher to retrieve
|
|
380
|
+
* @returns Fetcher - The Fetcher instance
|
|
381
|
+
* @throws Error - If no fetcher is registered with the given name
|
|
382
|
+
* @example
|
|
383
|
+
* try {
|
|
384
|
+
* const fetcher = fetcherRegistrar.requiredGet('api');
|
|
385
|
+
* // Use the fetcher
|
|
386
|
+
* } catch (error) {
|
|
387
|
+
* console.error('Fetcher not found:', error.message);
|
|
388
|
+
* }
|
|
389
|
+
*/
|
|
390
|
+
requiredGet(e) {
|
|
391
|
+
const t = this.get(e);
|
|
392
|
+
if (!t)
|
|
393
|
+
throw new Error(`Fetcher ${e} not found`);
|
|
394
|
+
return t;
|
|
395
|
+
}
|
|
396
|
+
/**
|
|
397
|
+
* Get the default Fetcher instance
|
|
398
|
+
*
|
|
399
|
+
* @returns Fetcher - The default Fetcher instance
|
|
400
|
+
* @throws Error - If no default fetcher is registered
|
|
401
|
+
* @example
|
|
402
|
+
* const defaultFetcher = fetcherRegistrar.default;
|
|
403
|
+
*/
|
|
404
|
+
get default() {
|
|
405
|
+
return this.requiredGet(m);
|
|
406
|
+
}
|
|
407
|
+
/**
|
|
408
|
+
* Set the default Fetcher instance
|
|
409
|
+
*
|
|
410
|
+
* @param fetcher - The Fetcher instance to set as default
|
|
411
|
+
* @example
|
|
412
|
+
* const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
413
|
+
* fetcherRegistrar.default = fetcher;
|
|
414
|
+
*/
|
|
415
|
+
set default(e) {
|
|
416
|
+
this.register(m, e);
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Get a copy of all registered fetchers
|
|
420
|
+
*
|
|
421
|
+
* @returns Map<string, Fetcher> - A copy of the internal registrar map
|
|
422
|
+
* @example
|
|
423
|
+
* const allFetchers = fetcherRegistrar.fetchers;
|
|
424
|
+
* for (const [name, fetcher] of allFetchers) {
|
|
425
|
+
* console.log(`Fetcher ${name}:`, fetcher);
|
|
426
|
+
* }
|
|
427
|
+
*/
|
|
428
|
+
get fetchers() {
|
|
429
|
+
return new Map(this.registrar);
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
const U = new S();
|
|
331
433
|
export {
|
|
332
434
|
d as ContentTypeHeader,
|
|
333
435
|
f as ContentTypeValues,
|
|
334
436
|
l as FetchTimeoutError,
|
|
335
437
|
I as Fetcher,
|
|
336
438
|
q as FetcherInterceptors,
|
|
439
|
+
S as FetcherRegistrar,
|
|
337
440
|
c as HttpMethod,
|
|
338
441
|
h as InterceptorManager,
|
|
339
442
|
O as RequestField,
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
443
|
+
g as UrlBuilder,
|
|
444
|
+
b as combineURLs,
|
|
445
|
+
U as fetcherRegistrar,
|
|
446
|
+
E as isAbsoluteURL,
|
|
343
447
|
P as resolveTimeout
|
|
344
448
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(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 y(r,e){return h(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class T{constructor(e){this.baseURL=e}build(e,t,s){let o=y(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 b(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||{}),g=(r=>(r.METHOD="method",r.BODY="body",r))(g||{});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 E{constructor(){this.request=new p,this.response=new p,this.error=new p}}class q{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 w={[l]:m.APPLICATION_JSON},R={baseURL:"",headers:w};class S{constructor(e=R){this.headers=w,this.interceptors=new E,this.urlBuilder=new T(e.baseURL),e.headers!==void 0&&(this.headers=e.headers),this.timeout=e.timeout,this.interceptors.request.use(new q)}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=b(o,this.timeout);if(!c)return fetch(t,s);const i=new AbortController,d={...s,signal:i.signal};let a=null;const U=new Promise((L,I)=>{a=setTimeout(()=>{a&&clearTimeout(a);const O=new f(e,c);i.abort(O),I(O)},c)});try{return await Promise.race([fetch(t,d),U])}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 P="default";class F{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(P)}set default(e){this.register(P,e)}get fetchers(){return new Map(this.registrar)}}const A=new F;n.ContentTypeHeader=l,n.ContentTypeValues=m,n.FetchTimeoutError=f,n.Fetcher=S,n.FetcherInterceptors=E,n.FetcherRegistrar=F,n.HttpMethod=u,n.InterceptorManager=p,n.RequestField=g,n.UrlBuilder=T,n.combineURLs=y,n.fetcherRegistrar=A,n.isAbsoluteURL=h,n.resolveTimeout=b,Object.defineProperty(n,Symbol.toStringTag,{value:"Module"})}));
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interceptor.d.ts","sourceRoot":"","sources":["../src/interceptor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"interceptor.d.ts","sourceRoot":"","sources":["../src/interceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEpD,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,OAAO,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,cAAc,CAAC;IACxB,QAAQ,EAAE,QAAQ,GAAG,SAAS,CAAC;IAC/B,KAAK,EAAE,KAAK,GAAG,GAAG,GAAG,SAAS,CAAC;CAChC;AAED;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B;;;;OAIG;IACH,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC,CAAC;CAC5E;AAED;;GAEG;AACH,qBAAa,kBAAmB,YAAW,WAAW;IACpD,OAAO,CAAC,YAAY,CAAiC;IAErD;;;;OAIG;IACH,GAAG,CAAC,WAAW,EAAE,WAAW,GAAG,MAAM;IAMrC;;;OAGG;IACH,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI;IAM1B;;OAEG;IACH,KAAK,IAAI,IAAI;IAIb;;;;OAIG;IACG,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;CAUjE;AAED;;GAEG;AACH,qBAAa,mBAAmB;IAC9B;;OAEG;IACH,OAAO,EAAE,kBAAkB,CAA4B;IAEvD;;OAEG;IACH,QAAQ,EAAE,kBAAkB,CAA4B;IAExD;;OAEG;IACH,KAAK,EAAE,kBAAkB,CAA4B;CACtD"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"requestBodyInterceptor.d.ts","sourceRoot":"","sources":["../src/requestBodyInterceptor.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"requestBodyInterceptor.d.ts","sourceRoot":"","sources":["../src/requestBodyInterceptor.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG3D;;GAEG;AACH,qBAAa,sBAAuB,YAAW,WAAW;IACxD;;;;;;;;;;;;;;;;;;;;;OAqBG;IACH,SAAS,CAAC,QAAQ,EAAE,aAAa,GAAG,aAAa;CA2ClD"}
|
package/dist/timeout.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../src/timeout.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../src/timeout.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAE9C;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAC5B,cAAc,CAAC,EAAE,MAAM,EACvB,cAAc,CAAC,EAAE,MAAM,GACtB,MAAM,GAAG,SAAS,CAKpB;AAED;;;GAGG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;IAC1C,QAAQ,EAAE,aAAa,CAAC;gBAEZ,QAAQ,EAAE,aAAa,EAAE,OAAO,EAAE,MAAM;CAUrD"}
|
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;AAED,eAAO,MAAM,iBAAiB,iBAAiB,CAAC;AAEhD,oBAAY,iBAAiB;IAC3B,gBAAgB,qBAAqB;IACrC,iBAAiB,sBAAsB;CACxC"}
|
package/dist/urlBuilder.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"urlBuilder.d.ts","sourceRoot":"","sources":["../src/urlBuilder.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"urlBuilder.d.ts","sourceRoot":"","sources":["../src/urlBuilder.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAEzC;;;;;;;GAOG;AACH,qBAAa,UAAW,YAAW,cAAc;IAC/C,OAAO,EAAE,MAAM,CAAC;IAEhB;;;;OAIG;gBACS,OAAO,EAAE,MAAM;IAI3B;;;;;;;;OAQG;IACH,KAAK,CACH,GAAG,EAAE,MAAM,EACX,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAChC,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAChC,MAAM;IAYT;;;;;;;OAOG;IACH,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,MAAM;CAWtE"}
|
package/dist/urls.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"urls.d.ts","sourceRoot":"","sources":["../src/urls.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"urls.d.ts","sourceRoot":"","sources":["../src/urls.ts"],"names":[],"mappings":"AAaA;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,WAExC;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,UAQ/D"}
|