@ahoo-wang/fetcher 0.9.5 → 0.9.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/fetchExchange.d.ts +11 -0
- package/dist/fetchExchange.d.ts.map +1 -1
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/fetcherError.d.ts +0 -30
- package/dist/fetcherError.d.ts.map +1 -1
- package/dist/index.es.js +242 -243
- package/dist/index.umd.js +1 -1
- package/dist/interceptorManager.d.ts +2 -1
- package/dist/interceptorManager.d.ts.map +1 -1
- package/package.json +3 -2
package/dist/fetchExchange.d.ts
CHANGED
|
@@ -86,5 +86,16 @@ export declare class FetchExchange {
|
|
|
86
86
|
* @returns true if a response is present, false otherwise
|
|
87
87
|
*/
|
|
88
88
|
hasResponse(): boolean;
|
|
89
|
+
/**
|
|
90
|
+
* Gets the required response object, throwing an error if no response is available.
|
|
91
|
+
*
|
|
92
|
+
* This getter ensures that a response object is available, and throws an ExchangeError
|
|
93
|
+
* with details about the request if no response was received. This is useful for
|
|
94
|
+
* guaranteeing that downstream code always has a valid Response object to work with.
|
|
95
|
+
*
|
|
96
|
+
* @throws {ExchangeError} If no response is available for the current exchange
|
|
97
|
+
* @returns The Response object for this exchange
|
|
98
|
+
*/
|
|
99
|
+
get requiredResponse(): Response;
|
|
89
100
|
}
|
|
90
101
|
//# sourceMappingURL=fetchExchange.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchExchange.d.ts","sourceRoot":"","sources":["../src/fetchExchange.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"fetchExchange.d.ts","sourceRoot":"","sources":["../src/fetchExchange.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAG9C;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAwCG;AACH,qBAAa,aAAa;IACxB;;OAEG;IACH,OAAO,EAAE,OAAO,CAAC;IAEjB;;OAEG;IACH,OAAO,EAAE,YAAY,CAAC;IAEtB;;OAEG;IACH,QAAQ,EAAE,QAAQ,GAAG,SAAS,CAAC;IAE/B;;OAEG;IACH,KAAK,EAAE,KAAK,GAAG,GAAG,GAAG,SAAS,CAAC;IAE/B;;;;;;;;;;;;;OAaG;IACH,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAM;gBAGnC,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,YAAY,EACrB,QAAQ,CAAC,EAAE,QAAQ,EACnB,KAAK,CAAC,EAAE,KAAK,GAAG,GAAG;IAQrB;;;;OAIG;IACH,QAAQ,IAAI,OAAO;IAInB;;;;OAIG;IACH,WAAW,IAAI,OAAO;IAItB;;;;;;;;;OASG;IACH,IAAI,gBAAgB,IAAI,QAAQ,CAQ/B;CACF"}
|
package/dist/fetcher.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAkB,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EACL,cAAc,EAEd,YAAY,EACZ,gBAAgB,EAEhB,cAAc,EACd,qBAAqB,EACtB,MAAM,gBAAgB,CAAC;
|
|
1
|
+
{"version":3,"file":"fetcher.d.ts","sourceRoot":"","sources":["../src/fetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,UAAU,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAC7D,OAAO,EAAkB,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3D,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EACL,cAAc,EAEd,YAAY,EACZ,gBAAgB,EAEhB,cAAc,EACd,qBAAqB,EACtB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,cACf,SAAQ,cAAc,EACpB,qBAAqB,EACrB,cAAc;IAChB,YAAY,CAAC,EAAE,kBAAkB,CAAC;CACnC;AAMD,eAAO,MAAM,eAAe,EAAE,cAG7B,CAAC;AAEF;;;;;;;;;;;;;;;;;;GAkBG;AACH,qBAAa,OACX,YAAW,iBAAiB,EAAE,qBAAqB,EAAE,cAAc;IACnE,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;IAChC,QAAQ,CAAC,OAAO,CAAC,EAAE,cAAc,CAAmB;IACpD,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,YAAY,EAAE,kBAAkB,CAAC;IAE1C;;;;;;;OAOG;gBACS,OAAO,GAAE,cAAgC;IAOrD;;;;;;;;;;OAUG;IACG,KAAK,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,GAAE,gBAAqB,GAAG,OAAO,CAAC,QAAQ,CAAC;IAO3E;;;;;;;;;;OAUG;IACG,OAAO,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IAa5D;;;;;;;;;;OAUG;YACW,WAAW;IAWzB;;;;;;;;;OASG;IACG,GAAG,CACP,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,GAAG,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;;;OAQG;IACG,IAAI,CACR,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAM,GAC7C,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;;;OAQG;IACG,GAAG,CACP,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAM,GAC7C,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;;;OAQG;IACG,MAAM,CACV,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAM,GAC7C,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;;;OAQG;IACG,KAAK,CACT,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,CAAM,GAC7C,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;;;;OASG;IACG,IAAI,CACR,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,GAAG,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;IAIpB;;;;;;;;;OASG;IACG,OAAO,CACX,GAAG,EAAE,MAAM,EACX,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,QAAQ,GAAG,MAAM,CAAM,GACtD,OAAO,CAAC,QAAQ,CAAC;CAGrB"}
|
package/dist/fetcherError.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { FetchExchange } from './fetchExchange';
|
|
2
1
|
/**
|
|
3
2
|
* Base error class for all Fetcher-related errors.
|
|
4
3
|
*
|
|
@@ -30,33 +29,4 @@ export declare class FetcherError extends Error {
|
|
|
30
29
|
*/
|
|
31
30
|
constructor(errorMsg?: string, cause?: (Error | any) | undefined);
|
|
32
31
|
}
|
|
33
|
-
/**
|
|
34
|
-
* Custom error class for Fetcher request failures.
|
|
35
|
-
*
|
|
36
|
-
* This error is thrown when a fetch request fails and no response is generated.
|
|
37
|
-
* It wraps the FetchExchange object to provide comprehensive information about the failed request.
|
|
38
|
-
*
|
|
39
|
-
* @example
|
|
40
|
-
* ```typescript
|
|
41
|
-
* try {
|
|
42
|
-
* await fetcher.get('/api/users');
|
|
43
|
-
* } catch (error) {
|
|
44
|
-
* if (error instanceof FetchError) {
|
|
45
|
-
* console.log('Failed URL:', error.exchange.request.url);
|
|
46
|
-
* console.log('Request headers:', error.exchange.request.headers);
|
|
47
|
-
* console.log('Interceptor attributes:', error.exchange.attributes);
|
|
48
|
-
* }
|
|
49
|
-
* }
|
|
50
|
-
* ```
|
|
51
|
-
*/
|
|
52
|
-
export declare class FetchError extends FetcherError {
|
|
53
|
-
readonly exchange: FetchExchange;
|
|
54
|
-
/**
|
|
55
|
-
* Creates a new FetchError instance.
|
|
56
|
-
*
|
|
57
|
-
* @param exchange - The FetchExchange object containing request/response/error information.
|
|
58
|
-
* @param errorMsg - Optional custom error message. If not provided, will use the exchange's error message.
|
|
59
|
-
*/
|
|
60
|
-
constructor(exchange: FetchExchange, errorMsg?: string);
|
|
61
|
-
}
|
|
62
32
|
//# sourceMappingURL=fetcherError.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetcherError.d.ts","sourceRoot":"","sources":["../src/fetcherError.ts"],"names":[],"mappings":"AAaA
|
|
1
|
+
{"version":3,"file":"fetcherError.d.ts","sourceRoot":"","sources":["../src/fetcherError.ts"],"names":[],"mappings":"AAaA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,qBAAa,YAAa,SAAQ,KAAK;aASnB,KAAK,CAAC,GAAE,KAAK,GAAG,GAAG;IARrC;;;;;OAKG;gBAED,QAAQ,CAAC,EAAE,MAAM,EACD,KAAK,CAAC,GAAE,KAAK,GAAG,GAAG,aAAA;CAetC"}
|
package/dist/index.es.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
function
|
|
1
|
+
function g(r) {
|
|
2
2
|
return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(r);
|
|
3
3
|
}
|
|
4
|
-
function
|
|
5
|
-
return
|
|
4
|
+
function P(r, e) {
|
|
5
|
+
return g(e) ? e : e ? r.replace(/\/?\/$/, "") + "/" + e.replace(/^\/+/, "") : r;
|
|
6
6
|
}
|
|
7
|
-
class
|
|
7
|
+
class b {
|
|
8
8
|
/**
|
|
9
9
|
* Initializes a new UrlBuilder instance.
|
|
10
10
|
*
|
|
@@ -15,8 +15,8 @@ class w {
|
|
|
15
15
|
* const urlBuilder = new UrlBuilder('https://api.example.com');
|
|
16
16
|
* ```
|
|
17
17
|
*/
|
|
18
|
-
constructor(
|
|
19
|
-
this.baseURL =
|
|
18
|
+
constructor(e) {
|
|
19
|
+
this.baseURL = e;
|
|
20
20
|
}
|
|
21
21
|
/**
|
|
22
22
|
* Builds a complete URL, including path parameter replacement and query parameter addition.
|
|
@@ -36,8 +36,8 @@ class w {
|
|
|
36
36
|
* // Result: https://api.example.com/users/123/posts/456?filter=active&limit=10
|
|
37
37
|
* ```
|
|
38
38
|
*/
|
|
39
|
-
build(
|
|
40
|
-
const s =
|
|
39
|
+
build(e, t) {
|
|
40
|
+
const s = t?.path, o = t?.query, i = P(this.baseURL, e);
|
|
41
41
|
let n = this.interpolateUrl(i, s);
|
|
42
42
|
if (o) {
|
|
43
43
|
const u = new URLSearchParams(o).toString();
|
|
@@ -54,8 +54,8 @@ class w {
|
|
|
54
54
|
* @param request - The FetchRequest containing URL and URL parameters
|
|
55
55
|
* @returns Complete resolved URL string
|
|
56
56
|
*/
|
|
57
|
-
resolveRequestUrl(
|
|
58
|
-
return this.build(
|
|
57
|
+
resolveRequestUrl(e) {
|
|
58
|
+
return this.build(e.url, e.urlParams);
|
|
59
59
|
}
|
|
60
60
|
/**
|
|
61
61
|
* Replaces placeholders in the URL with path parameters.
|
|
@@ -84,13 +84,13 @@ class w {
|
|
|
84
84
|
* }
|
|
85
85
|
* ```
|
|
86
86
|
*/
|
|
87
|
-
interpolateUrl(
|
|
88
|
-
return
|
|
89
|
-
const i =
|
|
87
|
+
interpolateUrl(e, t) {
|
|
88
|
+
return t ? e.replace(/{([^}]+)}/g, (s, o) => {
|
|
89
|
+
const i = t[o];
|
|
90
90
|
if (i === void 0)
|
|
91
91
|
throw new Error(`Missing required path parameter: ${o}`);
|
|
92
92
|
return String(i);
|
|
93
|
-
}) :
|
|
93
|
+
}) : e;
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
class a extends Error {
|
|
@@ -100,21 +100,9 @@ class a extends Error {
|
|
|
100
100
|
* @param errorMsg - Optional error message. If not provided, will use the cause's message or a default message.
|
|
101
101
|
* @param cause - Optional underlying error that caused this error.
|
|
102
102
|
*/
|
|
103
|
-
constructor(
|
|
104
|
-
const s =
|
|
105
|
-
super(s), this.cause =
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
class m extends a {
|
|
109
|
-
/**
|
|
110
|
-
* Creates a new FetchError instance.
|
|
111
|
-
*
|
|
112
|
-
* @param exchange - The FetchExchange object containing request/response/error information.
|
|
113
|
-
* @param errorMsg - Optional custom error message. If not provided, will use the exchange's error message.
|
|
114
|
-
*/
|
|
115
|
-
constructor(t, e) {
|
|
116
|
-
const s = e || t.error?.message || `Request to ${t.request.url} failed with no response`;
|
|
117
|
-
super(s, t.error), this.exchange = t, this.name = "FetchError", Object.setPrototypeOf(this, m.prototype);
|
|
103
|
+
constructor(e, t) {
|
|
104
|
+
const s = e || t?.message || "An error occurred in the fetcher";
|
|
105
|
+
super(s), this.cause = t, this.name = "FetcherError", t?.stack && (this.stack = t.stack), Object.setPrototypeOf(this, a.prototype);
|
|
118
106
|
}
|
|
119
107
|
}
|
|
120
108
|
class T extends a {
|
|
@@ -123,83 +111,58 @@ class T extends a {
|
|
|
123
111
|
*
|
|
124
112
|
* @param request - The request options that timed out
|
|
125
113
|
*/
|
|
126
|
-
constructor(
|
|
127
|
-
const
|
|
128
|
-
super(s), this.name = "FetchTimeoutError", this.request =
|
|
114
|
+
constructor(e) {
|
|
115
|
+
const t = e.method || "GET", s = `Request timeout of ${e.timeout}ms exceeded for ${t} ${e.url}`;
|
|
116
|
+
super(s), this.name = "FetchTimeoutError", this.request = e, Object.setPrototypeOf(this, T.prototype);
|
|
129
117
|
}
|
|
130
118
|
}
|
|
131
|
-
function S(r,
|
|
132
|
-
return typeof r < "u" ? r :
|
|
119
|
+
function S(r, e) {
|
|
120
|
+
return typeof r < "u" ? r : e;
|
|
133
121
|
}
|
|
134
122
|
async function _(r) {
|
|
135
|
-
const
|
|
136
|
-
if (!
|
|
137
|
-
return fetch(
|
|
123
|
+
const e = r.url, t = r.timeout, s = r;
|
|
124
|
+
if (!t)
|
|
125
|
+
return fetch(e, s);
|
|
138
126
|
const o = new AbortController(), i = {
|
|
139
127
|
...s,
|
|
140
128
|
signal: o.signal
|
|
141
129
|
};
|
|
142
130
|
let n = null;
|
|
143
|
-
const u = new Promise((
|
|
131
|
+
const u = new Promise((X, A) => {
|
|
144
132
|
n = setTimeout(() => {
|
|
145
133
|
n && clearTimeout(n);
|
|
146
|
-
const
|
|
147
|
-
o.abort(
|
|
148
|
-
},
|
|
134
|
+
const y = new T(r);
|
|
135
|
+
o.abort(y), A(y);
|
|
136
|
+
}, t);
|
|
149
137
|
});
|
|
150
138
|
try {
|
|
151
|
-
return await Promise.race([fetch(
|
|
139
|
+
return await Promise.race([fetch(e, i), u]);
|
|
152
140
|
} finally {
|
|
153
141
|
n && clearTimeout(n);
|
|
154
142
|
}
|
|
155
143
|
}
|
|
144
|
+
const w = "UrlResolveInterceptor", I = Number.MIN_SAFE_INTEGER + 1e3;
|
|
156
145
|
class N {
|
|
157
|
-
constructor(t, e, s, o) {
|
|
158
|
-
this.attributes = {}, this.fetcher = t, this.request = e, this.response = s, this.error = o;
|
|
159
|
-
}
|
|
160
|
-
/**
|
|
161
|
-
* Checks if the exchange has an error.
|
|
162
|
-
*
|
|
163
|
-
* @returns true if an error is present, false otherwise
|
|
164
|
-
*/
|
|
165
|
-
hasError() {
|
|
166
|
-
return !!this.error;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Checks if the exchange has a response.
|
|
170
|
-
*
|
|
171
|
-
* @returns true if a response is present, false otherwise
|
|
172
|
-
*/
|
|
173
|
-
hasResponse() {
|
|
174
|
-
return !!this.response;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
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 || {});
|
|
178
|
-
const Q = "Content-Type";
|
|
179
|
-
var f = /* @__PURE__ */ ((r) => (r.APPLICATION_JSON = "application/json", r.TEXT_EVENT_STREAM = "text/event-stream", r))(f || {});
|
|
180
|
-
function l(r, t) {
|
|
181
|
-
if (!(r === void 0 && t === void 0))
|
|
182
|
-
return t === void 0 ? r : r === void 0 ? t : { ...r, ...t };
|
|
183
|
-
}
|
|
184
|
-
const F = "UrlResolveInterceptor", O = Number.MIN_SAFE_INTEGER + 1e3;
|
|
185
|
-
class q {
|
|
186
146
|
constructor() {
|
|
187
|
-
this.name =
|
|
147
|
+
this.name = w, this.order = I;
|
|
188
148
|
}
|
|
189
149
|
/**
|
|
190
150
|
* Resolves the final URL by combining the base URL, path parameters, and query parameters.
|
|
191
151
|
*
|
|
192
152
|
* @param exchange - The fetch exchange containing the request information
|
|
193
153
|
*/
|
|
194
|
-
intercept(
|
|
195
|
-
const
|
|
196
|
-
|
|
154
|
+
intercept(e) {
|
|
155
|
+
const t = e.request;
|
|
156
|
+
t.url = e.fetcher.urlBuilder.resolveRequestUrl(t);
|
|
197
157
|
}
|
|
198
158
|
}
|
|
199
|
-
|
|
200
|
-
|
|
159
|
+
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 || {});
|
|
160
|
+
const z = "Content-Type";
|
|
161
|
+
var R = /* @__PURE__ */ ((r) => (r.APPLICATION_JSON = "application/json", r.TEXT_EVENT_STREAM = "text/event-stream", r))(R || {});
|
|
162
|
+
const F = "RequestBodyInterceptor", q = I + 1e3;
|
|
163
|
+
class U {
|
|
201
164
|
constructor() {
|
|
202
|
-
this.name =
|
|
165
|
+
this.name = F, this.order = q;
|
|
203
166
|
}
|
|
204
167
|
/**
|
|
205
168
|
* Attempts to convert request body to a valid fetch API body type.
|
|
@@ -238,21 +201,21 @@ class L {
|
|
|
238
201
|
* // exchange.request.body will be '{"name":"John","age":30}'
|
|
239
202
|
* // exchange.request.headers will include 'Content-Type: application/json'
|
|
240
203
|
*/
|
|
241
|
-
intercept(
|
|
242
|
-
const
|
|
243
|
-
if (
|
|
244
|
-
|
|
204
|
+
intercept(e) {
|
|
205
|
+
const t = e.request;
|
|
206
|
+
if (t.body === void 0 || t.body === null || typeof t.body != "object" || t.body instanceof ArrayBuffer || ArrayBuffer.isView(t.body) || // Includes TypedArray and DataView
|
|
207
|
+
t.body instanceof Blob || t.body instanceof File || t.body instanceof URLSearchParams || t.body instanceof FormData || t.body instanceof ReadableStream)
|
|
245
208
|
return;
|
|
246
|
-
const s = { ...
|
|
247
|
-
s.body = JSON.stringify(
|
|
209
|
+
const s = { ...t };
|
|
210
|
+
s.body = JSON.stringify(t.body), s.headers || (s.headers = {});
|
|
248
211
|
const o = s.headers;
|
|
249
|
-
o["Content-Type"] || (o["Content-Type"] =
|
|
212
|
+
o["Content-Type"] || (o["Content-Type"] = R.APPLICATION_JSON), e.request = s;
|
|
250
213
|
}
|
|
251
214
|
}
|
|
252
|
-
const
|
|
253
|
-
class
|
|
215
|
+
const C = "FetchInterceptor", L = Number.MAX_SAFE_INTEGER - 1e3;
|
|
216
|
+
class D {
|
|
254
217
|
constructor() {
|
|
255
|
-
this.name =
|
|
218
|
+
this.name = C, this.order = L;
|
|
256
219
|
}
|
|
257
220
|
/**
|
|
258
221
|
* Intercept and process HTTP requests.
|
|
@@ -279,14 +242,14 @@ class M {
|
|
|
279
242
|
* await fetchInterceptor.intercept(exchange);
|
|
280
243
|
* console.log(exchange.response); // HTTP response object
|
|
281
244
|
*/
|
|
282
|
-
async intercept(
|
|
283
|
-
|
|
245
|
+
async intercept(e) {
|
|
246
|
+
e.response = await _(e.request);
|
|
284
247
|
}
|
|
285
248
|
}
|
|
286
|
-
function
|
|
287
|
-
return
|
|
249
|
+
function d(r, e) {
|
|
250
|
+
return e ? r.filter(e).sort((t, s) => t.order - s.order) : [...r].sort((t, s) => t.order - s.order);
|
|
288
251
|
}
|
|
289
|
-
class
|
|
252
|
+
class l {
|
|
290
253
|
/**
|
|
291
254
|
* Initializes a new InterceptorRegistry instance.
|
|
292
255
|
*
|
|
@@ -296,8 +259,8 @@ class d {
|
|
|
296
259
|
* The provided interceptors will be sorted by their order property immediately
|
|
297
260
|
* upon construction.
|
|
298
261
|
*/
|
|
299
|
-
constructor(
|
|
300
|
-
this.sortedInterceptors = [], this.sortedInterceptors =
|
|
262
|
+
constructor(e = []) {
|
|
263
|
+
this.sortedInterceptors = [], this.sortedInterceptors = d(e);
|
|
301
264
|
}
|
|
302
265
|
/**
|
|
303
266
|
* Gets the name of this interceptor registry.
|
|
@@ -334,10 +297,10 @@ class d {
|
|
|
334
297
|
*
|
|
335
298
|
* After adding, interceptors are automatically sorted by their order property.
|
|
336
299
|
*/
|
|
337
|
-
use(
|
|
338
|
-
return this.sortedInterceptors.some((
|
|
300
|
+
use(e) {
|
|
301
|
+
return this.sortedInterceptors.some((t) => t.name === e.name) ? !1 : (this.sortedInterceptors = d([
|
|
339
302
|
...this.sortedInterceptors,
|
|
340
|
-
|
|
303
|
+
e
|
|
341
304
|
]), !0);
|
|
342
305
|
}
|
|
343
306
|
/**
|
|
@@ -347,12 +310,12 @@ class d {
|
|
|
347
310
|
* @returns True if an interceptor was removed, false if no interceptor with the
|
|
348
311
|
* given name was found
|
|
349
312
|
*/
|
|
350
|
-
eject(
|
|
351
|
-
const
|
|
352
|
-
return this.sortedInterceptors =
|
|
353
|
-
|
|
354
|
-
(s) => s.name !==
|
|
355
|
-
),
|
|
313
|
+
eject(e) {
|
|
314
|
+
const t = this.sortedInterceptors;
|
|
315
|
+
return this.sortedInterceptors = d(
|
|
316
|
+
t,
|
|
317
|
+
(s) => s.name !== e
|
|
318
|
+
), t.length !== this.sortedInterceptors.length;
|
|
356
319
|
}
|
|
357
320
|
/**
|
|
358
321
|
* Removes all interceptors from this registry.
|
|
@@ -374,27 +337,27 @@ class d {
|
|
|
374
337
|
* If any interceptor throws an error, the execution chain is broken and the error
|
|
375
338
|
* is propagated to the caller.
|
|
376
339
|
*/
|
|
377
|
-
async intercept(
|
|
378
|
-
for (const
|
|
379
|
-
await
|
|
340
|
+
async intercept(e) {
|
|
341
|
+
for (const t of this.sortedInterceptors)
|
|
342
|
+
await t.intercept(e);
|
|
380
343
|
}
|
|
381
344
|
}
|
|
382
|
-
class
|
|
383
|
-
constructor(
|
|
345
|
+
class f extends a {
|
|
346
|
+
constructor(e) {
|
|
384
347
|
super(
|
|
385
|
-
`Request failed with status code ${
|
|
386
|
-
), this.exchange =
|
|
348
|
+
`Request failed with status code ${e.response?.status} for ${e.request.url}`
|
|
349
|
+
), this.exchange = e, this.name = "HttpStatusValidationError", Object.setPrototypeOf(this, f.prototype);
|
|
387
350
|
}
|
|
388
351
|
}
|
|
389
|
-
const
|
|
390
|
-
class
|
|
352
|
+
const v = (r) => r >= 200 && r < 300, B = "ValidateStatusInterceptor", G = Number.MAX_SAFE_INTEGER - 1e3;
|
|
353
|
+
class M {
|
|
391
354
|
/**
|
|
392
355
|
* Creates a new ValidateStatusInterceptor instance.
|
|
393
356
|
*
|
|
394
357
|
* @param validateStatus - Function that determines if a status code is valid
|
|
395
358
|
*/
|
|
396
|
-
constructor(
|
|
397
|
-
this.validateStatus =
|
|
359
|
+
constructor(e = v) {
|
|
360
|
+
this.validateStatus = e;
|
|
398
361
|
}
|
|
399
362
|
/**
|
|
400
363
|
* Gets the name of this interceptor.
|
|
@@ -402,7 +365,7 @@ class G {
|
|
|
402
365
|
* @returns The name of this interceptor
|
|
403
366
|
*/
|
|
404
367
|
get name() {
|
|
405
|
-
return
|
|
368
|
+
return B;
|
|
406
369
|
}
|
|
407
370
|
/**
|
|
408
371
|
* Gets the order of this interceptor.
|
|
@@ -410,7 +373,7 @@ class G {
|
|
|
410
373
|
* @returns VALIDATE_STATUS_INTERCEPTOR_ORDER, indicating this interceptor should execute early
|
|
411
374
|
*/
|
|
412
375
|
get order() {
|
|
413
|
-
return
|
|
376
|
+
return G;
|
|
414
377
|
}
|
|
415
378
|
/**
|
|
416
379
|
* Validates the response status code.
|
|
@@ -425,34 +388,35 @@ class G {
|
|
|
425
388
|
* preventing other response handlers from attempting to process them. Valid responses
|
|
426
389
|
* proceed through the rest of the response interceptor chain normally.
|
|
427
390
|
*/
|
|
428
|
-
intercept(
|
|
429
|
-
if (!
|
|
391
|
+
intercept(e) {
|
|
392
|
+
if (!e.response)
|
|
430
393
|
return;
|
|
431
|
-
const
|
|
432
|
-
if (!this.validateStatus(
|
|
433
|
-
throw new
|
|
394
|
+
const t = e.response.status;
|
|
395
|
+
if (!this.validateStatus(t))
|
|
396
|
+
throw new f(e);
|
|
434
397
|
}
|
|
435
398
|
}
|
|
436
|
-
class
|
|
399
|
+
class h extends a {
|
|
437
400
|
/**
|
|
438
401
|
* Creates a new ExchangeError instance.
|
|
439
402
|
*
|
|
440
403
|
* @param exchange - The FetchExchange object containing request/response/error information.
|
|
404
|
+
* @param errorMsg - An optional error message.
|
|
441
405
|
*/
|
|
442
|
-
constructor(t) {
|
|
443
|
-
const
|
|
444
|
-
super(
|
|
406
|
+
constructor(e, t) {
|
|
407
|
+
const s = t || e.error?.message || e.response?.statusText || `Request to ${e.request.url} failed during exchange`;
|
|
408
|
+
super(s, e.error), this.exchange = e, this.name = "ExchangeError", Object.setPrototypeOf(this, h.prototype);
|
|
445
409
|
}
|
|
446
410
|
}
|
|
447
|
-
class
|
|
411
|
+
class $ {
|
|
448
412
|
constructor() {
|
|
449
|
-
this.request = new
|
|
450
|
-
new
|
|
451
|
-
new
|
|
413
|
+
this.request = new l([
|
|
414
|
+
new N(),
|
|
415
|
+
new U(),
|
|
416
|
+
new D()
|
|
417
|
+
]), this.response = new l([
|
|
452
418
|
new M()
|
|
453
|
-
]), this.
|
|
454
|
-
new G()
|
|
455
|
-
]), this.error = new d();
|
|
419
|
+
]), this.error = new l();
|
|
456
420
|
}
|
|
457
421
|
/**
|
|
458
422
|
* Processes a FetchExchange through the interceptor pipeline.
|
|
@@ -540,23 +504,66 @@ class V {
|
|
|
540
504
|
* const result = await fetcher.exchange(exchange);
|
|
541
505
|
* ```
|
|
542
506
|
*/
|
|
543
|
-
async exchange(
|
|
507
|
+
async exchange(e) {
|
|
544
508
|
try {
|
|
545
|
-
return await this.request.intercept(
|
|
546
|
-
} catch (
|
|
547
|
-
if (
|
|
548
|
-
return
|
|
549
|
-
throw new
|
|
509
|
+
return await this.request.intercept(e), await this.response.intercept(e), e;
|
|
510
|
+
} catch (t) {
|
|
511
|
+
if (e.error = t, await this.error.intercept(e), !e.hasError())
|
|
512
|
+
return e;
|
|
513
|
+
throw new h(e);
|
|
550
514
|
}
|
|
551
515
|
}
|
|
552
516
|
}
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
},
|
|
517
|
+
class j {
|
|
518
|
+
constructor(e, t, s, o) {
|
|
519
|
+
this.attributes = {}, this.fetcher = e, this.request = t, this.response = s, this.error = o;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Checks if the exchange has an error.
|
|
523
|
+
*
|
|
524
|
+
* @returns true if an error is present, false otherwise
|
|
525
|
+
*/
|
|
526
|
+
hasError() {
|
|
527
|
+
return !!this.error;
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Checks if the exchange has a response.
|
|
531
|
+
*
|
|
532
|
+
* @returns true if a response is present, false otherwise
|
|
533
|
+
*/
|
|
534
|
+
hasResponse() {
|
|
535
|
+
return !!this.response;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Gets the required response object, throwing an error if no response is available.
|
|
539
|
+
*
|
|
540
|
+
* This getter ensures that a response object is available, and throws an ExchangeError
|
|
541
|
+
* with details about the request if no response was received. This is useful for
|
|
542
|
+
* guaranteeing that downstream code always has a valid Response object to work with.
|
|
543
|
+
*
|
|
544
|
+
* @throws {ExchangeError} If no response is available for the current exchange
|
|
545
|
+
* @returns The Response object for this exchange
|
|
546
|
+
*/
|
|
547
|
+
get requiredResponse() {
|
|
548
|
+
if (!this.response)
|
|
549
|
+
throw new h(
|
|
550
|
+
this,
|
|
551
|
+
`Request to ${this.request.url} failed with no response`
|
|
552
|
+
);
|
|
553
|
+
return this.response;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
function p(r, e) {
|
|
557
|
+
if (!(r === void 0 && e === void 0))
|
|
558
|
+
return e === void 0 ? r : r === void 0 ? e : { ...r, ...e };
|
|
559
|
+
}
|
|
560
|
+
const E = {
|
|
561
|
+
"Content-Type": R.APPLICATION_JSON
|
|
562
|
+
}, O = {
|
|
556
563
|
baseURL: "",
|
|
557
|
-
headers:
|
|
564
|
+
headers: E
|
|
558
565
|
};
|
|
559
|
-
class
|
|
566
|
+
class V {
|
|
560
567
|
/**
|
|
561
568
|
* Initializes a new Fetcher instance with optional configuration.
|
|
562
569
|
*
|
|
@@ -565,8 +572,8 @@ class H {
|
|
|
565
572
|
*
|
|
566
573
|
* @param options - Configuration options for the Fetcher instance
|
|
567
574
|
*/
|
|
568
|
-
constructor(
|
|
569
|
-
this.headers =
|
|
575
|
+
constructor(e = O) {
|
|
576
|
+
this.headers = E, this.urlBuilder = new b(e.baseURL), this.headers = e.headers ?? E, this.timeout = e.timeout, this.interceptors = e.interceptors ?? new $();
|
|
570
577
|
}
|
|
571
578
|
/**
|
|
572
579
|
* Executes an HTTP request with the specified URL and options.
|
|
@@ -579,16 +586,9 @@ class H {
|
|
|
579
586
|
* @returns Promise that resolves to the HTTP response
|
|
580
587
|
* @throws FetchError if the request fails and no response is generated
|
|
581
588
|
*/
|
|
582
|
-
async fetch(
|
|
583
|
-
const s =
|
|
584
|
-
s.url =
|
|
585
|
-
const o = await this.request(s);
|
|
586
|
-
if (!o.response)
|
|
587
|
-
throw new m(
|
|
588
|
-
o,
|
|
589
|
-
`Request to ${s.url} failed with no response`
|
|
590
|
-
);
|
|
591
|
-
return o.response;
|
|
589
|
+
async fetch(e, t = {}) {
|
|
590
|
+
const s = t;
|
|
591
|
+
return s.url = e, (await this.request(s)).requiredResponse;
|
|
592
592
|
}
|
|
593
593
|
/**
|
|
594
594
|
* Processes an HTTP request through the Fetcher's internal workflow.
|
|
@@ -601,12 +601,12 @@ class H {
|
|
|
601
601
|
* @returns Promise that resolves to a FetchExchange containing request/response data
|
|
602
602
|
* @throws Error if an unhandled error occurs during request processing
|
|
603
603
|
*/
|
|
604
|
-
async request(
|
|
605
|
-
const
|
|
606
|
-
...
|
|
607
|
-
headers:
|
|
608
|
-
timeout: S(
|
|
609
|
-
}, o = new
|
|
604
|
+
async request(e) {
|
|
605
|
+
const t = p(e.headers, this.headers), s = {
|
|
606
|
+
...e,
|
|
607
|
+
headers: t,
|
|
608
|
+
timeout: S(e.timeout, this.timeout)
|
|
609
|
+
}, o = new j(this, s);
|
|
610
610
|
return this.interceptors.exchange(o);
|
|
611
611
|
}
|
|
612
612
|
/**
|
|
@@ -620,10 +620,10 @@ class H {
|
|
|
620
620
|
* @param request - Additional request options
|
|
621
621
|
* @returns Promise that resolves to the HTTP response
|
|
622
622
|
*/
|
|
623
|
-
async methodFetch(
|
|
624
|
-
return this.fetch(
|
|
623
|
+
async methodFetch(e, t, s = {}) {
|
|
624
|
+
return this.fetch(t, {
|
|
625
625
|
...s,
|
|
626
|
-
method:
|
|
626
|
+
method: e
|
|
627
627
|
});
|
|
628
628
|
}
|
|
629
629
|
/**
|
|
@@ -636,8 +636,8 @@ class H {
|
|
|
636
636
|
* @param request - Request options excluding method and body
|
|
637
637
|
* @returns Promise that resolves to the HTTP response
|
|
638
638
|
*/
|
|
639
|
-
async get(
|
|
640
|
-
return this.methodFetch(c.GET,
|
|
639
|
+
async get(e, t = {}) {
|
|
640
|
+
return this.methodFetch(c.GET, e, t);
|
|
641
641
|
}
|
|
642
642
|
/**
|
|
643
643
|
* Makes a POST HTTP request.
|
|
@@ -648,8 +648,8 @@ class H {
|
|
|
648
648
|
* @param request - Request options including body and other parameters
|
|
649
649
|
* @returns Promise that resolves to the HTTP response
|
|
650
650
|
*/
|
|
651
|
-
async post(
|
|
652
|
-
return this.methodFetch(c.POST,
|
|
651
|
+
async post(e, t = {}) {
|
|
652
|
+
return this.methodFetch(c.POST, e, t);
|
|
653
653
|
}
|
|
654
654
|
/**
|
|
655
655
|
* Makes a PUT HTTP request.
|
|
@@ -660,8 +660,8 @@ class H {
|
|
|
660
660
|
* @param request - Request options including body and other parameters
|
|
661
661
|
* @returns Promise that resolves to the HTTP response
|
|
662
662
|
*/
|
|
663
|
-
async put(
|
|
664
|
-
return this.methodFetch(c.PUT,
|
|
663
|
+
async put(e, t = {}) {
|
|
664
|
+
return this.methodFetch(c.PUT, e, t);
|
|
665
665
|
}
|
|
666
666
|
/**
|
|
667
667
|
* Makes a DELETE HTTP request.
|
|
@@ -672,8 +672,8 @@ class H {
|
|
|
672
672
|
* @param request - Request options excluding method and body
|
|
673
673
|
* @returns Promise that resolves to the HTTP response
|
|
674
674
|
*/
|
|
675
|
-
async delete(
|
|
676
|
-
return this.methodFetch(c.DELETE,
|
|
675
|
+
async delete(e, t = {}) {
|
|
676
|
+
return this.methodFetch(c.DELETE, e, t);
|
|
677
677
|
}
|
|
678
678
|
/**
|
|
679
679
|
* Makes a PATCH HTTP request.
|
|
@@ -684,8 +684,8 @@ class H {
|
|
|
684
684
|
* @param request - Request options including body and other parameters
|
|
685
685
|
* @returns Promise that resolves to the HTTP response
|
|
686
686
|
*/
|
|
687
|
-
async patch(
|
|
688
|
-
return this.methodFetch(c.PATCH,
|
|
687
|
+
async patch(e, t = {}) {
|
|
688
|
+
return this.methodFetch(c.PATCH, e, t);
|
|
689
689
|
}
|
|
690
690
|
/**
|
|
691
691
|
* Makes a HEAD HTTP request.
|
|
@@ -697,8 +697,8 @@ class H {
|
|
|
697
697
|
* @param request - Request options excluding method and body
|
|
698
698
|
* @returns Promise that resolves to the HTTP response
|
|
699
699
|
*/
|
|
700
|
-
async head(
|
|
701
|
-
return this.methodFetch(c.HEAD,
|
|
700
|
+
async head(e, t = {}) {
|
|
701
|
+
return this.methodFetch(c.HEAD, e, t);
|
|
702
702
|
}
|
|
703
703
|
/**
|
|
704
704
|
* Makes an OPTIONS HTTP request.
|
|
@@ -710,12 +710,12 @@ class H {
|
|
|
710
710
|
* @param request - Request options excluding method and body
|
|
711
711
|
* @returns Promise that resolves to the HTTP response
|
|
712
712
|
*/
|
|
713
|
-
async options(
|
|
714
|
-
return this.methodFetch(c.OPTIONS,
|
|
713
|
+
async options(e, t = {}) {
|
|
714
|
+
return this.methodFetch(c.OPTIONS, e, t);
|
|
715
715
|
}
|
|
716
716
|
}
|
|
717
|
-
const
|
|
718
|
-
class
|
|
717
|
+
const m = "default";
|
|
718
|
+
class H {
|
|
719
719
|
constructor() {
|
|
720
720
|
this.registrar = /* @__PURE__ */ new Map();
|
|
721
721
|
}
|
|
@@ -728,8 +728,8 @@ class k {
|
|
|
728
728
|
* const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
729
729
|
* fetcherRegistrar.register('api', fetcher);
|
|
730
730
|
*/
|
|
731
|
-
register(
|
|
732
|
-
this.registrar.set(
|
|
731
|
+
register(e, t) {
|
|
732
|
+
this.registrar.set(e, t);
|
|
733
733
|
}
|
|
734
734
|
/**
|
|
735
735
|
* Unregister a Fetcher instance by name
|
|
@@ -742,8 +742,8 @@ class k {
|
|
|
742
742
|
* console.log('Fetcher unregistered successfully');
|
|
743
743
|
* }
|
|
744
744
|
*/
|
|
745
|
-
unregister(
|
|
746
|
-
return this.registrar.delete(
|
|
745
|
+
unregister(e) {
|
|
746
|
+
return this.registrar.delete(e);
|
|
747
747
|
}
|
|
748
748
|
/**
|
|
749
749
|
* Get a Fetcher instance by name
|
|
@@ -756,8 +756,8 @@ class k {
|
|
|
756
756
|
* // Use the fetcher
|
|
757
757
|
* }
|
|
758
758
|
*/
|
|
759
|
-
get(
|
|
760
|
-
return this.registrar.get(
|
|
759
|
+
get(e) {
|
|
760
|
+
return this.registrar.get(e);
|
|
761
761
|
}
|
|
762
762
|
/**
|
|
763
763
|
* Get a Fetcher instance by name, throwing an error if not found
|
|
@@ -773,11 +773,11 @@ class k {
|
|
|
773
773
|
* console.error('Fetcher not found:', error.message);
|
|
774
774
|
* }
|
|
775
775
|
*/
|
|
776
|
-
requiredGet(
|
|
777
|
-
const
|
|
778
|
-
if (!
|
|
779
|
-
throw new Error(`Fetcher ${
|
|
780
|
-
return
|
|
776
|
+
requiredGet(e) {
|
|
777
|
+
const t = this.get(e);
|
|
778
|
+
if (!t)
|
|
779
|
+
throw new Error(`Fetcher ${e} not found`);
|
|
780
|
+
return t;
|
|
781
781
|
}
|
|
782
782
|
/**
|
|
783
783
|
* Get the default Fetcher instance
|
|
@@ -788,7 +788,7 @@ class k {
|
|
|
788
788
|
* const defaultFetcher = fetcherRegistrar.default;
|
|
789
789
|
*/
|
|
790
790
|
get default() {
|
|
791
|
-
return this.requiredGet(
|
|
791
|
+
return this.requiredGet(m);
|
|
792
792
|
}
|
|
793
793
|
/**
|
|
794
794
|
* Set the default Fetcher instance
|
|
@@ -798,8 +798,8 @@ class k {
|
|
|
798
798
|
* const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
799
799
|
* fetcherRegistrar.default = fetcher;
|
|
800
800
|
*/
|
|
801
|
-
set default(
|
|
802
|
-
this.register(
|
|
801
|
+
set default(e) {
|
|
802
|
+
this.register(m, e);
|
|
803
803
|
}
|
|
804
804
|
/**
|
|
805
805
|
* Get a copy of all registered fetchers
|
|
@@ -815,31 +815,31 @@ class k {
|
|
|
815
815
|
return new Map(this.registrar);
|
|
816
816
|
}
|
|
817
817
|
}
|
|
818
|
-
const
|
|
819
|
-
function
|
|
818
|
+
const k = new H();
|
|
819
|
+
function Q(r, e) {
|
|
820
820
|
if (Object.keys(r).length === 0)
|
|
821
|
-
return
|
|
822
|
-
if (Object.keys(
|
|
821
|
+
return e;
|
|
822
|
+
if (Object.keys(e).length === 0)
|
|
823
823
|
return r;
|
|
824
|
-
const
|
|
825
|
-
path:
|
|
826
|
-
query:
|
|
824
|
+
const t = {
|
|
825
|
+
path: p(r.urlParams?.path, e.urlParams?.path),
|
|
826
|
+
query: p(r.urlParams?.query, e.urlParams?.query)
|
|
827
827
|
}, s = {
|
|
828
828
|
...r.headers,
|
|
829
|
-
...
|
|
830
|
-
}, o =
|
|
829
|
+
...e.headers
|
|
830
|
+
}, o = e.method ?? r.method, i = e.body ?? r.body, n = e.timeout ?? r.timeout, u = e.signal ?? r.signal;
|
|
831
831
|
return {
|
|
832
832
|
...r,
|
|
833
|
-
...
|
|
833
|
+
...e,
|
|
834
834
|
method: o,
|
|
835
|
-
urlParams:
|
|
835
|
+
urlParams: t,
|
|
836
836
|
headers: s,
|
|
837
837
|
body: i,
|
|
838
838
|
timeout: n,
|
|
839
839
|
signal: u
|
|
840
840
|
};
|
|
841
841
|
}
|
|
842
|
-
class
|
|
842
|
+
class J extends V {
|
|
843
843
|
/**
|
|
844
844
|
* Create a NamedFetcher instance and automatically register it with the global fetcherRegistrar
|
|
845
845
|
*
|
|
@@ -857,48 +857,47 @@ class X extends H {
|
|
|
857
857
|
* headers: { 'Authorization': 'Bearer token' }
|
|
858
858
|
* });
|
|
859
859
|
*/
|
|
860
|
-
constructor(
|
|
861
|
-
super(
|
|
860
|
+
constructor(e, t = O) {
|
|
861
|
+
super(t), this.name = e, k.register(e, this);
|
|
862
862
|
}
|
|
863
863
|
}
|
|
864
|
-
const
|
|
864
|
+
const Y = new J(m);
|
|
865
865
|
export {
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
M as FetchInterceptor,
|
|
866
|
+
z as ContentTypeHeader,
|
|
867
|
+
R as ContentTypeValues,
|
|
868
|
+
m as DEFAULT_FETCHER_NAME,
|
|
869
|
+
O as DEFAULT_OPTIONS,
|
|
870
|
+
h as ExchangeError,
|
|
871
|
+
C as FETCH_INTERCEPTOR_NAME,
|
|
872
|
+
L as FETCH_INTERCEPTOR_ORDER,
|
|
873
|
+
j as FetchExchange,
|
|
874
|
+
D as FetchInterceptor,
|
|
876
875
|
T as FetchTimeoutError,
|
|
877
|
-
|
|
876
|
+
V as Fetcher,
|
|
878
877
|
a as FetcherError,
|
|
879
|
-
|
|
878
|
+
H as FetcherRegistrar,
|
|
880
879
|
c as HttpMethod,
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
880
|
+
f as HttpStatusValidationError,
|
|
881
|
+
$ as InterceptorManager,
|
|
882
|
+
l as InterceptorRegistry,
|
|
883
|
+
J as NamedFetcher,
|
|
884
|
+
F as REQUEST_BODY_INTERCEPTOR_NAME,
|
|
885
|
+
q as REQUEST_BODY_INTERCEPTOR_ORDER,
|
|
886
|
+
U as RequestBodyInterceptor,
|
|
887
|
+
w as URL_RESOLVE_INTERCEPTOR_NAME,
|
|
888
|
+
I as URL_RESOLVE_INTERCEPTOR_ORDER,
|
|
889
|
+
b as UrlBuilder,
|
|
890
|
+
N as UrlResolveInterceptor,
|
|
891
|
+
B as VALIDATE_STATUS_INTERCEPTOR_NAME,
|
|
892
|
+
G as VALIDATE_STATUS_INTERCEPTOR_ORDER,
|
|
893
|
+
M as ValidateStatusInterceptor,
|
|
894
|
+
P as combineURLs,
|
|
895
|
+
Y as fetcher,
|
|
896
|
+
k as fetcherRegistrar,
|
|
897
|
+
g as isAbsoluteURL,
|
|
898
|
+
p as mergeRecords,
|
|
899
|
+
Q as mergeRequest,
|
|
901
900
|
S as resolveTimeout,
|
|
902
901
|
_ as timeoutFetch,
|
|
903
|
-
|
|
902
|
+
d as toSorted
|
|
904
903
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(s,
|
|
1
|
+
(function(s,a){typeof exports=="object"&&typeof module<"u"?a(exports):typeof define=="function"&&define.amd?define(["exports"],a):(s=typeof globalThis<"u"?globalThis:s||self,a(s.Fetcher={}))})(this,(function(s){"use strict";function a(r){return/^([a-z][a-z\d+\-.]*:)?\/\//i.test(r)}function A(r,e){return a(e)?e:e?r.replace(/\/?\/$/,"")+"/"+e.replace(/^\/+/,""):r}class S{constructor(e){this.baseURL=e}build(e,t){const n=t?.path,o=t?.query,u=A(this.baseURL,e);let i=this.interpolateUrl(u,n);if(o){const E=new URLSearchParams(o).toString();E&&(i+="?"+E)}return i}resolveRequestUrl(e){return this.build(e.url,e.urlParams)}interpolateUrl(e,t){return t?e.replace(/{([^}]+)}/g,(n,o)=>{const u=t[o];if(u===void 0)throw new Error(`Missing required path parameter: ${o}`);return String(u)}):e}}class h extends Error{constructor(e,t){const n=e||t?.message||"An error occurred in the fetcher";super(n),this.cause=t,this.name="FetcherError",t?.stack&&(this.stack=t.stack),Object.setPrototypeOf(this,h.prototype)}}class T extends h{constructor(e){const t=e.method||"GET",n=`Request timeout of ${e.timeout}ms exceeded for ${t} ${e.url}`;super(n),this.name="FetchTimeoutError",this.request=e,Object.setPrototypeOf(this,T.prototype)}}function g(r,e){return typeof r<"u"?r:e}async function P(r){const e=r.url,t=r.timeout,n=r;if(!t)return fetch(e,n);const o=new AbortController,u={...n,signal:o.signal};let i=null;const E=new Promise((K,z)=>{i=setTimeout(()=>{i&&clearTimeout(i);const k=new T(r);o.abort(k),z(k)},t)});try{return await Promise.race([fetch(e,u),E])}finally{i&&clearTimeout(i)}}const N="UrlResolveInterceptor",_=Number.MIN_SAFE_INTEGER+1e3;class F{constructor(){this.name=N,this.order=_}intercept(e){const t=e.request;t.url=e.fetcher.urlBuilder.resolveRequestUrl(t)}}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||{});const J="Content-Type";var l=(r=>(r.APPLICATION_JSON="application/json",r.TEXT_EVENT_STREAM="text/event-stream",r))(l||{});const b="RequestBodyInterceptor",U=_+1e3;class w{constructor(){this.name=b,this.order=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;const n={...t};n.body=JSON.stringify(t.body),n.headers||(n.headers={});const o=n.headers;o["Content-Type"]||(o["Content-Type"]=l.APPLICATION_JSON),e.request=n}}const C="FetchInterceptor",q=Number.MAX_SAFE_INTEGER-1e3;class L{constructor(){this.name=C,this.order=q}async intercept(e){e.response=await P(e.request)}}function R(r,e){return e?r.filter(e).sort((t,n)=>t.order-n.order):[...r].sort((t,n)=>t.order-n.order)}class m{constructor(e=[]){this.sortedInterceptors=[],this.sortedInterceptors=R(e)}get name(){return this.constructor.name}get order(){return Number.MIN_SAFE_INTEGER}get interceptors(){return[...this.sortedInterceptors]}use(e){return this.sortedInterceptors.some(t=>t.name===e.name)?!1:(this.sortedInterceptors=R([...this.sortedInterceptors,e]),!0)}eject(e){const t=this.sortedInterceptors;return this.sortedInterceptors=R(t,n=>n.name!==e),t.length!==this.sortedInterceptors.length}clear(){this.sortedInterceptors=[]}async intercept(e){for(const t of this.sortedInterceptors)await t.intercept(e)}}class f extends h{constructor(e){super(`Request failed with status code ${e.response?.status} for ${e.request.url}`),this.exchange=e,this.name="HttpStatusValidationError",Object.setPrototypeOf(this,f.prototype)}}const Q=r=>r>=200&&r<300,D="ValidateStatusInterceptor",v=Number.MAX_SAFE_INTEGER-1e3;class M{constructor(e=Q){this.validateStatus=e}get name(){return D}get order(){return v}intercept(e){if(!e.response)return;const t=e.response.status;if(!this.validateStatus(t))throw new f(e)}}class d extends h{constructor(e,t){const n=t||e.error?.message||e.response?.statusText||`Request to ${e.request.url} failed during exchange`;super(n,e.error),this.exchange=e,this.name="ExchangeError",Object.setPrototypeOf(this,d.prototype)}}class V{constructor(){this.request=new m([new F,new w,new L]),this.response=new m([new M]),this.error=new m}async exchange(e){try{return await this.request.intercept(e),await this.response.intercept(e),e}catch(t){if(e.error=t,await this.error.intercept(e),!e.hasError())return e;throw new d(e)}}}class B{constructor(e,t,n,o){this.attributes={},this.fetcher=e,this.request=t,this.response=n,this.error=o}hasError(){return!!this.error}hasResponse(){return!!this.response}get requiredResponse(){if(!this.response)throw new d(this,`Request to ${this.request.url} failed with no response`);return this.response}}function I(r,e){if(!(r===void 0&&e===void 0))return e===void 0?r:r===void 0?e:{...r,...e}}const p={"Content-Type":l.APPLICATION_JSON},O={baseURL:"",headers:p};class H{constructor(e=O){this.headers=p,this.urlBuilder=new S(e.baseURL),this.headers=e.headers??p,this.timeout=e.timeout,this.interceptors=e.interceptors??new V}async fetch(e,t={}){const n=t;return n.url=e,(await this.request(n)).requiredResponse}async request(e){const t=I(e.headers,this.headers),n={...e,headers:t,timeout:g(e.timeout,this.timeout)},o=new B(this,n);return this.interceptors.exchange(o)}async methodFetch(e,t,n={}){return this.fetch(t,{...n,method:e})}async get(e,t={}){return this.methodFetch(c.GET,e,t)}async post(e,t={}){return this.methodFetch(c.POST,e,t)}async put(e,t={}){return this.methodFetch(c.PUT,e,t)}async delete(e,t={}){return this.methodFetch(c.DELETE,e,t)}async patch(e,t={}){return this.methodFetch(c.PATCH,e,t)}async head(e,t={}){return this.methodFetch(c.HEAD,e,t)}async options(e,t={}){return this.methodFetch(c.OPTIONS,e,t)}}const y="default";class j{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 G=new j;function Y(r,e){if(Object.keys(r).length===0)return e;if(Object.keys(e).length===0)return r;const t={path:I(r.urlParams?.path,e.urlParams?.path),query:I(r.urlParams?.query,e.urlParams?.query)},n={...r.headers,...e.headers},o=e.method??r.method,u=e.body??r.body,i=e.timeout??r.timeout,E=e.signal??r.signal;return{...r,...e,method:o,urlParams:t,headers:n,body:u,timeout:i,signal:E}}class $ extends H{constructor(e,t=O){super(t),this.name=e,G.register(e,this)}}const X=new $(y);s.ContentTypeHeader=J,s.ContentTypeValues=l,s.DEFAULT_FETCHER_NAME=y,s.DEFAULT_OPTIONS=O,s.ExchangeError=d,s.FETCH_INTERCEPTOR_NAME=C,s.FETCH_INTERCEPTOR_ORDER=q,s.FetchExchange=B,s.FetchInterceptor=L,s.FetchTimeoutError=T,s.Fetcher=H,s.FetcherError=h,s.FetcherRegistrar=j,s.HttpMethod=c,s.HttpStatusValidationError=f,s.InterceptorManager=V,s.InterceptorRegistry=m,s.NamedFetcher=$,s.REQUEST_BODY_INTERCEPTOR_NAME=b,s.REQUEST_BODY_INTERCEPTOR_ORDER=U,s.RequestBodyInterceptor=w,s.URL_RESOLVE_INTERCEPTOR_NAME=N,s.URL_RESOLVE_INTERCEPTOR_ORDER=_,s.UrlBuilder=S,s.UrlResolveInterceptor=F,s.VALIDATE_STATUS_INTERCEPTOR_NAME=D,s.VALIDATE_STATUS_INTERCEPTOR_ORDER=v,s.ValidateStatusInterceptor=M,s.combineURLs=A,s.fetcher=X,s.fetcherRegistrar=G,s.isAbsoluteURL=a,s.mergeRecords=I,s.mergeRequest=Y,s.resolveTimeout=g,s.timeoutFetch=P,s.toSorted=R,Object.defineProperty(s,Symbol.toStringTag,{value:"Module"})}));
|
|
@@ -29,8 +29,9 @@ export declare class ExchangeError extends FetcherError {
|
|
|
29
29
|
* Creates a new ExchangeError instance.
|
|
30
30
|
*
|
|
31
31
|
* @param exchange - The FetchExchange object containing request/response/error information.
|
|
32
|
+
* @param errorMsg - An optional error message.
|
|
32
33
|
*/
|
|
33
|
-
constructor(exchange: FetchExchange);
|
|
34
|
+
constructor(exchange: FetchExchange, errorMsg?: string);
|
|
34
35
|
}
|
|
35
36
|
/**
|
|
36
37
|
* Collection of interceptor managers for the Fetcher client.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interceptorManager.d.ts","sourceRoot":"","sources":["../src/interceptorManager.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAGpD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,aAAc,SAAQ,YAAY;
|
|
1
|
+
{"version":3,"file":"interceptorManager.d.ts","sourceRoot":"","sources":["../src/interceptorManager.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC9C,OAAO,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAGpD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,qBAAa,aAAc,SAAQ,YAAY;aAOjB,QAAQ,EAAE,aAAa;IANnD;;;;;OAKG;gBACyB,QAAQ,EAAE,aAAa,EAAE,QAAQ,CAAC,EAAE,MAAM;CASvE;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AACH,qBAAa,kBAAkB;IAC7B;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,OAAO,EAAE,mBAAmB,CAIlC;IAEH;;;;;;;;;;;;OAYG;IACH,QAAQ,CAAC,QAAQ,EAAE,mBAAmB,CAEnC;IAEH;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,KAAK,EAAE,mBAAmB,CAA6B;IAEhE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAqFG;IACG,QAAQ,CAAC,aAAa,EAAE,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;CAqBrE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ahoo-wang/fetcher",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.6",
|
|
4
4
|
"description": "Ultra-lightweight (1.9kB) HTTP client with built-in path parameters and Axios-like API",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"fetch",
|
|
@@ -14,11 +14,12 @@
|
|
|
14
14
|
"repository": {
|
|
15
15
|
"type": "git",
|
|
16
16
|
"url": "https://github.com/Ahoo-Wang/fetcher.git",
|
|
17
|
-
"directory": "packages/
|
|
17
|
+
"directory": "packages/fetcher"
|
|
18
18
|
},
|
|
19
19
|
"bugs": {
|
|
20
20
|
"url": "https://github.com/Ahoo-Wang/fetcher/issues"
|
|
21
21
|
},
|
|
22
|
+
"type": "module",
|
|
22
23
|
"main": "./dist/index.umd.js",
|
|
23
24
|
"module": "./dist/index.es.js",
|
|
24
25
|
"types": "./dist/index.d.ts",
|