@ahoo-wang/fetcher 1.5.0 → 1.5.1
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 +40 -4
- package/dist/fetchExchange.d.ts.map +1 -1
- package/dist/fetcher.d.ts +75 -10
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/index.es.js +282 -205
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/resultExtractor.d.ts.map +1 -1
- package/dist/types.d.ts +18 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.es.js
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
function F(r) {
|
|
2
2
|
return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(r);
|
|
3
3
|
}
|
|
4
|
-
function C(r,
|
|
5
|
-
return F(
|
|
4
|
+
function C(r, e) {
|
|
5
|
+
return F(e) ? e : e ? r.replace(/\/?\/$/, "") + "/" + e.replace(/^\/+/, "") : r;
|
|
6
6
|
}
|
|
7
7
|
var U = /* @__PURE__ */ ((r) => (r[r.UriTemplate = 0] = "UriTemplate", r[r.Express = 1] = "Express", r))(U || {});
|
|
8
8
|
function D(r) {
|
|
9
9
|
return r === 1 ? v : L;
|
|
10
10
|
}
|
|
11
|
-
function q(r,
|
|
12
|
-
return
|
|
13
|
-
const
|
|
14
|
-
if (
|
|
15
|
-
throw new Error(`Missing required path parameter: ${
|
|
16
|
-
return encodeURIComponent(
|
|
11
|
+
function q(r, e, t) {
|
|
12
|
+
return t ? r.replace(e, (s, o) => {
|
|
13
|
+
const n = t[o];
|
|
14
|
+
if (n === void 0)
|
|
15
|
+
throw new Error(`Missing required path parameter: ${o}`);
|
|
16
|
+
return encodeURIComponent(n);
|
|
17
17
|
}) : r;
|
|
18
18
|
}
|
|
19
|
-
function w(r,
|
|
20
|
-
const
|
|
19
|
+
function w(r, e) {
|
|
20
|
+
const t = [];
|
|
21
21
|
let s;
|
|
22
|
-
for (; (s =
|
|
23
|
-
|
|
24
|
-
return
|
|
22
|
+
for (; (s = e.exec(r)) !== null; )
|
|
23
|
+
t.push(s[1]);
|
|
24
|
+
return t;
|
|
25
25
|
}
|
|
26
26
|
const c = class c {
|
|
27
27
|
/**
|
|
@@ -47,9 +47,9 @@ const c = class c {
|
|
|
47
47
|
* // noParams = []
|
|
48
48
|
* ```
|
|
49
49
|
*/
|
|
50
|
-
extractPathParams(
|
|
50
|
+
extractPathParams(e) {
|
|
51
51
|
return w(
|
|
52
|
-
|
|
52
|
+
e,
|
|
53
53
|
c.PATH_PARAM_REGEX
|
|
54
54
|
);
|
|
55
55
|
}
|
|
@@ -89,17 +89,17 @@ const c = class c {
|
|
|
89
89
|
* }
|
|
90
90
|
* ```
|
|
91
91
|
*/
|
|
92
|
-
resolve(
|
|
92
|
+
resolve(e, t) {
|
|
93
93
|
return q(
|
|
94
|
-
|
|
94
|
+
e,
|
|
95
95
|
c.PATH_PARAM_REGEX,
|
|
96
|
-
|
|
96
|
+
t
|
|
97
97
|
);
|
|
98
98
|
}
|
|
99
99
|
};
|
|
100
100
|
c.PATH_PARAM_REGEX = /{([^}]+)}/g;
|
|
101
|
-
let
|
|
102
|
-
const L = new
|
|
101
|
+
let P = c;
|
|
102
|
+
const L = new P(), h = class h {
|
|
103
103
|
/**
|
|
104
104
|
* Extracts path parameters from an Express-style URL string.
|
|
105
105
|
*
|
|
@@ -119,9 +119,9 @@ const L = new f(), h = class h {
|
|
|
119
119
|
* // noParams = []
|
|
120
120
|
* ```
|
|
121
121
|
*/
|
|
122
|
-
extractPathParams(
|
|
122
|
+
extractPathParams(e) {
|
|
123
123
|
return w(
|
|
124
|
-
|
|
124
|
+
e,
|
|
125
125
|
h.PATH_PARAM_REGEX
|
|
126
126
|
);
|
|
127
127
|
}
|
|
@@ -157,18 +157,18 @@ const L = new f(), h = class h {
|
|
|
157
157
|
* }
|
|
158
158
|
* ```
|
|
159
159
|
*/
|
|
160
|
-
resolve(
|
|
160
|
+
resolve(e, t) {
|
|
161
161
|
return q(
|
|
162
|
-
|
|
162
|
+
e,
|
|
163
163
|
h.PATH_PARAM_REGEX,
|
|
164
|
-
|
|
164
|
+
t
|
|
165
165
|
);
|
|
166
166
|
}
|
|
167
167
|
};
|
|
168
168
|
h.PATH_PARAM_REGEX = /:([^/]+)/g;
|
|
169
|
-
let
|
|
170
|
-
const v = new
|
|
171
|
-
class
|
|
169
|
+
let A = h;
|
|
170
|
+
const v = new A();
|
|
171
|
+
class G {
|
|
172
172
|
/**
|
|
173
173
|
* Initializes a new UrlBuilder instance.
|
|
174
174
|
*
|
|
@@ -186,8 +186,8 @@ class x {
|
|
|
186
186
|
* const expressUrlBuilder = new UrlBuilder('https://api.example.com', UrlTemplateStyle.Express);
|
|
187
187
|
* ```
|
|
188
188
|
*/
|
|
189
|
-
constructor(
|
|
190
|
-
this.baseURL =
|
|
189
|
+
constructor(e, t) {
|
|
190
|
+
this.baseURL = e, this.urlTemplateResolver = D(t);
|
|
191
191
|
}
|
|
192
192
|
/**
|
|
193
193
|
* Builds a complete URL, including path parameter replacement and query parameter addition.
|
|
@@ -207,11 +207,11 @@ class x {
|
|
|
207
207
|
* // Result: https://api.example.com/users/123/posts/456?filter=active&limit=10
|
|
208
208
|
* ```
|
|
209
209
|
*/
|
|
210
|
-
build(
|
|
211
|
-
const s =
|
|
212
|
-
let u = this.urlTemplateResolver.resolve(
|
|
213
|
-
if (
|
|
214
|
-
const a = new URLSearchParams(
|
|
210
|
+
build(e, t) {
|
|
211
|
+
const s = t?.path, o = t?.query, n = C(this.baseURL, e);
|
|
212
|
+
let u = this.urlTemplateResolver.resolve(n, s);
|
|
213
|
+
if (o) {
|
|
214
|
+
const a = new URLSearchParams(o).toString();
|
|
215
215
|
a && (u += "?" + a);
|
|
216
216
|
}
|
|
217
217
|
return u;
|
|
@@ -225,8 +225,8 @@ class x {
|
|
|
225
225
|
* @param request - The FetchRequest containing URL and URL parameters
|
|
226
226
|
* @returns Complete resolved URL string
|
|
227
227
|
*/
|
|
228
|
-
resolveRequestUrl(
|
|
229
|
-
return this.build(
|
|
228
|
+
resolveRequestUrl(e) {
|
|
229
|
+
return this.build(e.url, e.urlParams);
|
|
230
230
|
}
|
|
231
231
|
}
|
|
232
232
|
class l extends Error {
|
|
@@ -236,9 +236,9 @@ class l extends Error {
|
|
|
236
236
|
* @param errorMsg - Optional error message. If not provided, will use the cause's message or a default message.
|
|
237
237
|
* @param cause - Optional underlying error that caused this error.
|
|
238
238
|
*/
|
|
239
|
-
constructor(
|
|
240
|
-
const s =
|
|
241
|
-
super(s), this.cause =
|
|
239
|
+
constructor(e, t) {
|
|
240
|
+
const s = e || t?.message || "An error occurred in the fetcher";
|
|
241
|
+
super(s), this.cause = t, this.name = "FetcherError", t?.stack && (this.stack = t.stack), Object.setPrototypeOf(this, l.prototype);
|
|
242
242
|
}
|
|
243
243
|
}
|
|
244
244
|
class I extends l {
|
|
@@ -247,34 +247,34 @@ class I extends l {
|
|
|
247
247
|
*
|
|
248
248
|
* @param request - The request options that timed out
|
|
249
249
|
*/
|
|
250
|
-
constructor(
|
|
251
|
-
const
|
|
252
|
-
super(s), this.name = "FetchTimeoutError", this.request =
|
|
250
|
+
constructor(e) {
|
|
251
|
+
const t = e.method || "GET", s = `Request timeout of ${e.timeout}ms exceeded for ${t} ${e.url}`;
|
|
252
|
+
super(s), this.name = "FetchTimeoutError", this.request = e, Object.setPrototypeOf(this, I.prototype);
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
|
-
function
|
|
256
|
-
return typeof r < "u" ? r :
|
|
255
|
+
function M(r, e) {
|
|
256
|
+
return typeof r < "u" ? r : e;
|
|
257
257
|
}
|
|
258
|
-
async function
|
|
259
|
-
const
|
|
258
|
+
async function x(r) {
|
|
259
|
+
const e = r.url, t = r.timeout, s = r;
|
|
260
260
|
if (r.signal)
|
|
261
|
-
return fetch(
|
|
262
|
-
if (!
|
|
263
|
-
return r.abortController && (s.signal = r.abortController.signal), fetch(
|
|
264
|
-
const
|
|
265
|
-
s.signal =
|
|
266
|
-
let
|
|
261
|
+
return fetch(e, s);
|
|
262
|
+
if (!t)
|
|
263
|
+
return r.abortController && (s.signal = r.abortController.signal), fetch(e, s);
|
|
264
|
+
const o = r.abortController ?? new AbortController();
|
|
265
|
+
s.signal = o.signal;
|
|
266
|
+
let n = null;
|
|
267
267
|
const u = new Promise((a, p) => {
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
const
|
|
271
|
-
|
|
272
|
-
},
|
|
268
|
+
n = setTimeout(() => {
|
|
269
|
+
n && clearTimeout(n);
|
|
270
|
+
const O = new I(r);
|
|
271
|
+
o.abort(O), p(O);
|
|
272
|
+
}, t);
|
|
273
273
|
});
|
|
274
274
|
try {
|
|
275
|
-
return await Promise.race([fetch(
|
|
275
|
+
return await Promise.race([fetch(e, s), u]);
|
|
276
276
|
} finally {
|
|
277
|
-
|
|
277
|
+
n && clearTimeout(n);
|
|
278
278
|
}
|
|
279
279
|
}
|
|
280
280
|
const H = "UrlResolveInterceptor", N = Number.MIN_SAFE_INTEGER + 1e3;
|
|
@@ -287,13 +287,13 @@ class j {
|
|
|
287
287
|
*
|
|
288
288
|
* @param exchange - The fetch exchange containing the request information
|
|
289
289
|
*/
|
|
290
|
-
intercept(
|
|
291
|
-
const
|
|
292
|
-
|
|
290
|
+
intercept(e) {
|
|
291
|
+
const t = e.request;
|
|
292
|
+
t.url = e.fetcher.urlBuilder.resolveRequestUrl(t);
|
|
293
293
|
}
|
|
294
294
|
}
|
|
295
295
|
var i = /* @__PURE__ */ ((r) => (r.GET = "GET", r.POST = "POST", r.PUT = "PUT", r.DELETE = "DELETE", r.PATCH = "PATCH", r.HEAD = "HEAD", r.OPTIONS = "OPTIONS", r))(i || {});
|
|
296
|
-
const
|
|
296
|
+
const ae = "Content-Type", d = class d {
|
|
297
297
|
};
|
|
298
298
|
d.APPLICATION_JSON = "application/json", d.TEXT_EVENT_STREAM = "text/event-stream";
|
|
299
299
|
let E = d;
|
|
@@ -339,13 +339,13 @@ class X {
|
|
|
339
339
|
* // exchange.request.body will be '{"name":"John","age":30}'
|
|
340
340
|
* // exchange.request.headers will include 'Content-Type: application/json'
|
|
341
341
|
*/
|
|
342
|
-
intercept(
|
|
343
|
-
const
|
|
344
|
-
if (
|
|
345
|
-
|
|
342
|
+
intercept(e) {
|
|
343
|
+
const t = e.request;
|
|
344
|
+
if (t.body === void 0 || t.body === null || typeof t.body != "object" || t.body instanceof ArrayBuffer || ArrayBuffer.isView(t.body) || // Includes TypedArray and DataView
|
|
345
|
+
t.body instanceof Blob || t.body instanceof File || t.body instanceof URLSearchParams || t.body instanceof FormData || t.body instanceof ReadableStream)
|
|
346
346
|
return;
|
|
347
|
-
|
|
348
|
-
const s =
|
|
347
|
+
e.request.body = JSON.stringify(t.body);
|
|
348
|
+
const s = e.ensureRequestHeaders();
|
|
349
349
|
s["Content-Type"] || (s["Content-Type"] = E.APPLICATION_JSON);
|
|
350
350
|
}
|
|
351
351
|
}
|
|
@@ -379,12 +379,12 @@ class k {
|
|
|
379
379
|
* await fetchInterceptor.intercept(exchange);
|
|
380
380
|
* console.log(exchange.response); // HTTP response object
|
|
381
381
|
*/
|
|
382
|
-
async intercept(
|
|
383
|
-
|
|
382
|
+
async intercept(e) {
|
|
383
|
+
e.response = await x(e.request);
|
|
384
384
|
}
|
|
385
385
|
}
|
|
386
|
-
function m(r,
|
|
387
|
-
return
|
|
386
|
+
function m(r, e) {
|
|
387
|
+
return e ? r.filter(e).sort((t, s) => t.order - s.order) : [...r].sort((t, s) => t.order - s.order);
|
|
388
388
|
}
|
|
389
389
|
class T {
|
|
390
390
|
/**
|
|
@@ -396,8 +396,8 @@ class T {
|
|
|
396
396
|
* The provided interceptors will be sorted by their order property immediately
|
|
397
397
|
* upon construction.
|
|
398
398
|
*/
|
|
399
|
-
constructor(
|
|
400
|
-
this.sortedInterceptors = [], this.sortedInterceptors = m(
|
|
399
|
+
constructor(e = []) {
|
|
400
|
+
this.sortedInterceptors = [], this.sortedInterceptors = m(e);
|
|
401
401
|
}
|
|
402
402
|
/**
|
|
403
403
|
* Gets the name of this interceptor registry.
|
|
@@ -434,10 +434,10 @@ class T {
|
|
|
434
434
|
*
|
|
435
435
|
* After adding, interceptors are automatically sorted by their order property.
|
|
436
436
|
*/
|
|
437
|
-
use(
|
|
438
|
-
return this.sortedInterceptors.some((
|
|
437
|
+
use(e) {
|
|
438
|
+
return this.sortedInterceptors.some((t) => t.name === e.name) ? !1 : (this.sortedInterceptors = m([
|
|
439
439
|
...this.sortedInterceptors,
|
|
440
|
-
|
|
440
|
+
e
|
|
441
441
|
]), !0);
|
|
442
442
|
}
|
|
443
443
|
/**
|
|
@@ -447,12 +447,12 @@ class T {
|
|
|
447
447
|
* @returns True if an interceptor was removed, false if no interceptor with the
|
|
448
448
|
* given name was found
|
|
449
449
|
*/
|
|
450
|
-
eject(
|
|
451
|
-
const
|
|
450
|
+
eject(e) {
|
|
451
|
+
const t = this.sortedInterceptors;
|
|
452
452
|
return this.sortedInterceptors = m(
|
|
453
|
-
|
|
454
|
-
(s) => s.name !==
|
|
455
|
-
),
|
|
453
|
+
t,
|
|
454
|
+
(s) => s.name !== e
|
|
455
|
+
), t.length !== this.sortedInterceptors.length;
|
|
456
456
|
}
|
|
457
457
|
/**
|
|
458
458
|
* Removes all interceptors from this registry.
|
|
@@ -474,16 +474,16 @@ class T {
|
|
|
474
474
|
* If any interceptor throws an error, the execution chain is broken and the error
|
|
475
475
|
* is propagated to the caller.
|
|
476
476
|
*/
|
|
477
|
-
async intercept(
|
|
478
|
-
for (const
|
|
479
|
-
await
|
|
477
|
+
async intercept(e) {
|
|
478
|
+
for (const t of this.sortedInterceptors)
|
|
479
|
+
await t.intercept(e);
|
|
480
480
|
}
|
|
481
481
|
}
|
|
482
|
-
class
|
|
483
|
-
constructor(
|
|
482
|
+
class b extends l {
|
|
483
|
+
constructor(e) {
|
|
484
484
|
super(
|
|
485
|
-
`Request failed with status code ${
|
|
486
|
-
), this.exchange =
|
|
485
|
+
`Request failed with status code ${e.response?.status} for ${e.request.url}`
|
|
486
|
+
), this.exchange = e, this.name = "HttpStatusValidationError", Object.setPrototypeOf(this, b.prototype);
|
|
487
487
|
}
|
|
488
488
|
}
|
|
489
489
|
const Y = (r) => r >= 200 && r < 300, z = "ValidateStatusInterceptor", Q = Number.MAX_SAFE_INTEGER - 1e3;
|
|
@@ -493,8 +493,8 @@ class K {
|
|
|
493
493
|
*
|
|
494
494
|
* @param validateStatus - Function that determines if a status code is valid
|
|
495
495
|
*/
|
|
496
|
-
constructor(
|
|
497
|
-
this.validateStatus =
|
|
496
|
+
constructor(e = Y) {
|
|
497
|
+
this.validateStatus = e;
|
|
498
498
|
}
|
|
499
499
|
/**
|
|
500
500
|
* Gets the name of this interceptor.
|
|
@@ -525,12 +525,12 @@ class K {
|
|
|
525
525
|
* preventing other response handlers from attempting to process them. Valid responses
|
|
526
526
|
* proceed through the rest of the response interceptor chain normally.
|
|
527
527
|
*/
|
|
528
|
-
intercept(
|
|
529
|
-
if (!
|
|
528
|
+
intercept(e) {
|
|
529
|
+
if (!e.response)
|
|
530
530
|
return;
|
|
531
|
-
const
|
|
532
|
-
if (!this.validateStatus(
|
|
533
|
-
throw new
|
|
531
|
+
const t = e.response.status;
|
|
532
|
+
if (!this.validateStatus(t))
|
|
533
|
+
throw new b(e);
|
|
534
534
|
}
|
|
535
535
|
}
|
|
536
536
|
class R extends l {
|
|
@@ -540,9 +540,9 @@ class R extends l {
|
|
|
540
540
|
* @param exchange - The FetchExchange object containing request/response/error information.
|
|
541
541
|
* @param errorMsg - An optional error message.
|
|
542
542
|
*/
|
|
543
|
-
constructor(
|
|
544
|
-
const s =
|
|
545
|
-
super(s,
|
|
543
|
+
constructor(e, t) {
|
|
544
|
+
const s = t || e.error?.message || e.response?.statusText || `Request to ${e.request.url} failed during exchange`;
|
|
545
|
+
super(s, e.error), this.exchange = e, this.name = "ExchangeError", Object.setPrototypeOf(this, R.prototype);
|
|
546
546
|
}
|
|
547
547
|
}
|
|
548
548
|
class W {
|
|
@@ -641,19 +641,19 @@ class W {
|
|
|
641
641
|
* const result = await fetcher.exchange(exchange);
|
|
642
642
|
* ```
|
|
643
643
|
*/
|
|
644
|
-
async exchange(
|
|
644
|
+
async exchange(e) {
|
|
645
645
|
try {
|
|
646
|
-
return await this.request.intercept(
|
|
647
|
-
} catch (
|
|
648
|
-
if (
|
|
649
|
-
return
|
|
650
|
-
throw new R(
|
|
646
|
+
return await this.request.intercept(e), await this.response.intercept(e), e;
|
|
647
|
+
} catch (t) {
|
|
648
|
+
if (e.error = t, await this.error.intercept(e), !e.hasError())
|
|
649
|
+
return e;
|
|
650
|
+
throw new R(e);
|
|
651
651
|
}
|
|
652
652
|
}
|
|
653
653
|
}
|
|
654
654
|
class Z {
|
|
655
|
-
constructor(
|
|
656
|
-
this.
|
|
655
|
+
constructor(e) {
|
|
656
|
+
this.fetcher = e.fetcher, this.request = e.request, this.attributes = e.attributes ?? {}, this.response = e.response, this.error = e.error;
|
|
657
657
|
}
|
|
658
658
|
/**
|
|
659
659
|
* Ensures that request headers object exists, creating it if necessary.
|
|
@@ -719,18 +719,18 @@ class Z {
|
|
|
719
719
|
return this.response;
|
|
720
720
|
}
|
|
721
721
|
}
|
|
722
|
-
const
|
|
723
|
-
Exchange:
|
|
724
|
-
Response:
|
|
725
|
-
Json:
|
|
726
|
-
Text:
|
|
727
|
-
},
|
|
722
|
+
const ee = (r) => r, te = (r) => r.requiredResponse, re = (r) => r.requiredResponse.json(), se = (r) => r.requiredResponse.text(), f = {
|
|
723
|
+
Exchange: ee,
|
|
724
|
+
Response: te,
|
|
725
|
+
Json: re,
|
|
726
|
+
Text: se
|
|
727
|
+
}, y = {
|
|
728
728
|
"Content-Type": E.APPLICATION_JSON
|
|
729
729
|
}, S = {
|
|
730
730
|
baseURL: "",
|
|
731
|
-
headers:
|
|
731
|
+
headers: y
|
|
732
732
|
};
|
|
733
|
-
class
|
|
733
|
+
class oe {
|
|
734
734
|
/**
|
|
735
735
|
* Initializes a new Fetcher instance with optional configuration.
|
|
736
736
|
*
|
|
@@ -739,8 +739,8 @@ class ot {
|
|
|
739
739
|
*
|
|
740
740
|
* @param options - Configuration options for the Fetcher instance
|
|
741
741
|
*/
|
|
742
|
-
constructor(
|
|
743
|
-
this.headers =
|
|
742
|
+
constructor(e = S) {
|
|
743
|
+
this.headers = y, this.urlBuilder = new G(e.baseURL, e.urlTemplateStyle), this.headers = e.headers ?? y, this.timeout = e.timeout, this.interceptors = e.interceptors ?? new W();
|
|
744
744
|
}
|
|
745
745
|
/**
|
|
746
746
|
* Executes an HTTP request with the specified URL and options.
|
|
@@ -750,12 +750,15 @@ class ot {
|
|
|
750
750
|
*
|
|
751
751
|
* @param url - The URL path for the request (relative to baseURL if set)
|
|
752
752
|
* @param request - Request configuration including headers, body, parameters, etc.
|
|
753
|
+
* @param attributes - Optional shared attributes that can be accessed by interceptors
|
|
754
|
+
* throughout the request lifecycle. These attributes allow passing
|
|
755
|
+
* custom data between different interceptors.
|
|
753
756
|
* @returns Promise that resolves to the HTTP response
|
|
754
757
|
* @throws FetchError if the request fails and no response is generated
|
|
755
758
|
*/
|
|
756
|
-
async fetch(
|
|
757
|
-
const
|
|
758
|
-
return
|
|
759
|
+
async fetch(e, t = {}, s) {
|
|
760
|
+
const o = t;
|
|
761
|
+
return o.url = e, this.request(o, f.Response, s);
|
|
759
762
|
}
|
|
760
763
|
/**
|
|
761
764
|
* Processes an HTTP request through the Fetcher's internal workflow.
|
|
@@ -767,20 +770,66 @@ class ot {
|
|
|
767
770
|
* @param request - Complete request configuration object
|
|
768
771
|
* @param resultExtractor - Function to extract the desired result from the exchange.
|
|
769
772
|
* Defaults to ExchangeResultExtractor which returns the entire exchange object.
|
|
773
|
+
* @param attributes - Optional shared attributes that can be accessed by interceptors
|
|
774
|
+
* throughout the request lifecycle. These attributes allow passing
|
|
775
|
+
* custom data between different interceptors.
|
|
770
776
|
* @returns Promise that resolves to the extracted result based on resultExtractor
|
|
771
777
|
* @throws Error if an unhandled error occurs during request processing
|
|
772
778
|
*/
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
const s = {
|
|
779
|
+
async request(e, t = f.Exchange, s) {
|
|
780
|
+
const o = {
|
|
776
781
|
...this.headers,
|
|
777
|
-
...
|
|
782
|
+
...e.headers
|
|
778
783
|
}, n = {
|
|
779
|
-
...
|
|
780
|
-
headers:
|
|
781
|
-
timeout:
|
|
782
|
-
}
|
|
783
|
-
return
|
|
784
|
+
...e,
|
|
785
|
+
headers: o,
|
|
786
|
+
timeout: M(e.timeout, this.timeout)
|
|
787
|
+
};
|
|
788
|
+
return this.exchange(
|
|
789
|
+
{ request: n, attributes: s },
|
|
790
|
+
t
|
|
791
|
+
);
|
|
792
|
+
}
|
|
793
|
+
/**
|
|
794
|
+
* Executes an HTTP request exchange and processes the result using a result extractor.
|
|
795
|
+
*
|
|
796
|
+
* This method creates a FetchExchange object, runs it through the interceptor chain,
|
|
797
|
+
* and then applies a result extractor function to transform the exchange into the desired result type.
|
|
798
|
+
* It's the core method that powers all HTTP request functionality in the Fetcher.
|
|
799
|
+
*
|
|
800
|
+
* @template R - The type of result to return after processing the exchange
|
|
801
|
+
* @param exchangeInit - The initial configuration for the exchange, excluding fetcher, response, and error properties
|
|
802
|
+
* @param resultExtractor - A function that transforms the completed exchange into the desired result type.
|
|
803
|
+
* Defaults to returning the exchange object itself.
|
|
804
|
+
* @returns A Promise that resolves to the result of applying the resultExtractor to the completed exchange
|
|
805
|
+
*
|
|
806
|
+
* @example
|
|
807
|
+
* ```typescript
|
|
808
|
+
* // Basic usage with default result extractor (returns the exchange)
|
|
809
|
+
* const exchange = await fetcher.exchange({
|
|
810
|
+
* request: { url: '/api/users', method: 'GET' }
|
|
811
|
+
* });
|
|
812
|
+
* console.log(exchange.response.status);
|
|
813
|
+
*
|
|
814
|
+
* // Using a custom result extractor to get the response object
|
|
815
|
+
* const response = await fetcher.exchange(
|
|
816
|
+
* { request: { url: '/api/users', method: 'GET' } },
|
|
817
|
+
* (exchange) => exchange.requiredResponse
|
|
818
|
+
* );
|
|
819
|
+
*
|
|
820
|
+
* // Using a built-in result extractor to get JSON data
|
|
821
|
+
* const users = await fetcher.exchange(
|
|
822
|
+
* { request: { url: '/api/users', method: 'GET' } },
|
|
823
|
+
* ResultExtractors.Json
|
|
824
|
+
* );
|
|
825
|
+
* ```
|
|
826
|
+
*/
|
|
827
|
+
async exchange(e, t = f.Exchange) {
|
|
828
|
+
const s = new Z({
|
|
829
|
+
...e,
|
|
830
|
+
fetcher: this
|
|
831
|
+
});
|
|
832
|
+
return await this.interceptors.exchange(s), t(s);
|
|
784
833
|
}
|
|
785
834
|
/**
|
|
786
835
|
* Internal helper method for making HTTP requests with a specific method.
|
|
@@ -791,13 +840,20 @@ class ot {
|
|
|
791
840
|
* @param method - The HTTP method to use for the request
|
|
792
841
|
* @param url - The URL path for the request
|
|
793
842
|
* @param request - Additional request options
|
|
843
|
+
* @param attributes - Optional shared attributes that can be accessed by interceptors
|
|
844
|
+
* throughout the request lifecycle. These attributes allow passing
|
|
845
|
+
* custom data between different interceptors.
|
|
794
846
|
* @returns Promise that resolves to the HTTP response
|
|
795
847
|
*/
|
|
796
|
-
async methodFetch(
|
|
797
|
-
return this.fetch(
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
848
|
+
async methodFetch(e, t, s = {}, o) {
|
|
849
|
+
return this.fetch(
|
|
850
|
+
t,
|
|
851
|
+
{
|
|
852
|
+
...s,
|
|
853
|
+
method: e
|
|
854
|
+
},
|
|
855
|
+
o
|
|
856
|
+
);
|
|
801
857
|
}
|
|
802
858
|
/**
|
|
803
859
|
* Makes a GET HTTP request.
|
|
@@ -807,10 +863,13 @@ class ot {
|
|
|
807
863
|
*
|
|
808
864
|
* @param url - The URL path for the request
|
|
809
865
|
* @param request - Request options excluding method and body
|
|
866
|
+
* @param attributes - Optional shared attributes that can be accessed by interceptors
|
|
867
|
+
* throughout the request lifecycle. These attributes allow passing
|
|
868
|
+
* custom data between different interceptors.
|
|
810
869
|
* @returns Promise that resolves to the HTTP response
|
|
811
870
|
*/
|
|
812
|
-
async get(
|
|
813
|
-
return this.methodFetch(i.GET, t,
|
|
871
|
+
async get(e, t = {}, s) {
|
|
872
|
+
return this.methodFetch(i.GET, e, t, s);
|
|
814
873
|
}
|
|
815
874
|
/**
|
|
816
875
|
* Makes a POST HTTP request.
|
|
@@ -819,10 +878,13 @@ class ot {
|
|
|
819
878
|
*
|
|
820
879
|
* @param url - The URL path for the request
|
|
821
880
|
* @param request - Request options including body and other parameters
|
|
881
|
+
* @param attributes - Optional shared attributes that can be accessed by interceptors
|
|
882
|
+
* throughout the request lifecycle. These attributes allow passing
|
|
883
|
+
* custom data between different interceptors.
|
|
822
884
|
* @returns Promise that resolves to the HTTP response
|
|
823
885
|
*/
|
|
824
|
-
async post(
|
|
825
|
-
return this.methodFetch(i.POST, t,
|
|
886
|
+
async post(e, t = {}, s) {
|
|
887
|
+
return this.methodFetch(i.POST, e, t, s);
|
|
826
888
|
}
|
|
827
889
|
/**
|
|
828
890
|
* Makes a PUT HTTP request.
|
|
@@ -831,10 +893,13 @@ class ot {
|
|
|
831
893
|
*
|
|
832
894
|
* @param url - The URL path for the request
|
|
833
895
|
* @param request - Request options including body and other parameters
|
|
896
|
+
* @param attributes - Optional shared attributes that can be accessed by interceptors
|
|
897
|
+
* throughout the request lifecycle. These attributes allow passing
|
|
898
|
+
* custom data between different interceptors.
|
|
834
899
|
* @returns Promise that resolves to the HTTP response
|
|
835
900
|
*/
|
|
836
|
-
async put(
|
|
837
|
-
return this.methodFetch(i.PUT, t,
|
|
901
|
+
async put(e, t = {}, s) {
|
|
902
|
+
return this.methodFetch(i.PUT, e, t, s);
|
|
838
903
|
}
|
|
839
904
|
/**
|
|
840
905
|
* Makes a DELETE HTTP request.
|
|
@@ -843,10 +908,13 @@ class ot {
|
|
|
843
908
|
*
|
|
844
909
|
* @param url - The URL path for the request
|
|
845
910
|
* @param request - Request options excluding method and body
|
|
911
|
+
* @param attributes - Optional shared attributes that can be accessed by interceptors
|
|
912
|
+
* throughout the request lifecycle. These attributes allow passing
|
|
913
|
+
* custom data between different interceptors.
|
|
846
914
|
* @returns Promise that resolves to the HTTP response
|
|
847
915
|
*/
|
|
848
|
-
async delete(
|
|
849
|
-
return this.methodFetch(i.DELETE, t,
|
|
916
|
+
async delete(e, t = {}, s) {
|
|
917
|
+
return this.methodFetch(i.DELETE, e, t, s);
|
|
850
918
|
}
|
|
851
919
|
/**
|
|
852
920
|
* Makes a PATCH HTTP request.
|
|
@@ -855,10 +923,13 @@ class ot {
|
|
|
855
923
|
*
|
|
856
924
|
* @param url - The URL path for the request
|
|
857
925
|
* @param request - Request options including body and other parameters
|
|
926
|
+
* @param attributes - Optional shared attributes that can be accessed by interceptors
|
|
927
|
+
* throughout the request lifecycle. These attributes allow passing
|
|
928
|
+
* custom data between different interceptors.
|
|
858
929
|
* @returns Promise that resolves to the HTTP response
|
|
859
930
|
*/
|
|
860
|
-
async patch(
|
|
861
|
-
return this.methodFetch(i.PATCH, t,
|
|
931
|
+
async patch(e, t = {}, s) {
|
|
932
|
+
return this.methodFetch(i.PATCH, e, t, s);
|
|
862
933
|
}
|
|
863
934
|
/**
|
|
864
935
|
* Makes a HEAD HTTP request.
|
|
@@ -868,10 +939,13 @@ class ot {
|
|
|
868
939
|
*
|
|
869
940
|
* @param url - The URL path for the request
|
|
870
941
|
* @param request - Request options excluding method and body
|
|
942
|
+
* @param attributes - Optional shared attributes that can be accessed by interceptors
|
|
943
|
+
* throughout the request lifecycle. These attributes allow passing
|
|
944
|
+
* custom data between different interceptors.
|
|
871
945
|
* @returns Promise that resolves to the HTTP response
|
|
872
946
|
*/
|
|
873
|
-
async head(
|
|
874
|
-
return this.methodFetch(i.HEAD, t,
|
|
947
|
+
async head(e, t = {}, s) {
|
|
948
|
+
return this.methodFetch(i.HEAD, e, t, s);
|
|
875
949
|
}
|
|
876
950
|
/**
|
|
877
951
|
* Makes an OPTIONS HTTP request.
|
|
@@ -881,14 +955,17 @@ class ot {
|
|
|
881
955
|
*
|
|
882
956
|
* @param url - The URL path for the request
|
|
883
957
|
* @param request - Request options excluding method and body
|
|
958
|
+
* @param attributes - Optional shared attributes that can be accessed by interceptors
|
|
959
|
+
* throughout the request lifecycle. These attributes allow passing
|
|
960
|
+
* custom data between different interceptors.
|
|
884
961
|
* @returns Promise that resolves to the HTTP response
|
|
885
962
|
*/
|
|
886
|
-
async options(
|
|
887
|
-
return this.methodFetch(i.OPTIONS, t,
|
|
963
|
+
async options(e, t = {}, s) {
|
|
964
|
+
return this.methodFetch(i.OPTIONS, e, t, s);
|
|
888
965
|
}
|
|
889
966
|
}
|
|
890
|
-
const
|
|
891
|
-
class
|
|
967
|
+
const g = "default";
|
|
968
|
+
class ne {
|
|
892
969
|
constructor() {
|
|
893
970
|
this.registrar = /* @__PURE__ */ new Map();
|
|
894
971
|
}
|
|
@@ -901,8 +978,8 @@ class nt {
|
|
|
901
978
|
* const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
902
979
|
* fetcherRegistrar.register('api', fetcher);
|
|
903
980
|
*/
|
|
904
|
-
register(
|
|
905
|
-
this.registrar.set(
|
|
981
|
+
register(e, t) {
|
|
982
|
+
this.registrar.set(e, t);
|
|
906
983
|
}
|
|
907
984
|
/**
|
|
908
985
|
* Unregister a Fetcher instance by name
|
|
@@ -915,8 +992,8 @@ class nt {
|
|
|
915
992
|
* console.log('Fetcher unregistered successfully');
|
|
916
993
|
* }
|
|
917
994
|
*/
|
|
918
|
-
unregister(
|
|
919
|
-
return this.registrar.delete(
|
|
995
|
+
unregister(e) {
|
|
996
|
+
return this.registrar.delete(e);
|
|
920
997
|
}
|
|
921
998
|
/**
|
|
922
999
|
* Get a Fetcher instance by name
|
|
@@ -929,8 +1006,8 @@ class nt {
|
|
|
929
1006
|
* // Use the fetcher
|
|
930
1007
|
* }
|
|
931
1008
|
*/
|
|
932
|
-
get(
|
|
933
|
-
return this.registrar.get(
|
|
1009
|
+
get(e) {
|
|
1010
|
+
return this.registrar.get(e);
|
|
934
1011
|
}
|
|
935
1012
|
/**
|
|
936
1013
|
* Get a Fetcher instance by name, throwing an error if not found
|
|
@@ -946,11 +1023,11 @@ class nt {
|
|
|
946
1023
|
* console.error('Fetcher not found:', error.message);
|
|
947
1024
|
* }
|
|
948
1025
|
*/
|
|
949
|
-
requiredGet(
|
|
950
|
-
const
|
|
951
|
-
if (!
|
|
952
|
-
throw new Error(`Fetcher ${
|
|
953
|
-
return
|
|
1026
|
+
requiredGet(e) {
|
|
1027
|
+
const t = this.get(e);
|
|
1028
|
+
if (!t)
|
|
1029
|
+
throw new Error(`Fetcher ${e} not found`);
|
|
1030
|
+
return t;
|
|
954
1031
|
}
|
|
955
1032
|
/**
|
|
956
1033
|
* Get the default Fetcher instance
|
|
@@ -961,7 +1038,7 @@ class nt {
|
|
|
961
1038
|
* const defaultFetcher = fetcherRegistrar.default;
|
|
962
1039
|
*/
|
|
963
1040
|
get default() {
|
|
964
|
-
return this.requiredGet(
|
|
1041
|
+
return this.requiredGet(g);
|
|
965
1042
|
}
|
|
966
1043
|
/**
|
|
967
1044
|
* Set the default Fetcher instance
|
|
@@ -971,8 +1048,8 @@ class nt {
|
|
|
971
1048
|
* const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
972
1049
|
* fetcherRegistrar.default = fetcher;
|
|
973
1050
|
*/
|
|
974
|
-
set default(
|
|
975
|
-
this.register(
|
|
1051
|
+
set default(e) {
|
|
1052
|
+
this.register(g, e);
|
|
976
1053
|
}
|
|
977
1054
|
/**
|
|
978
1055
|
* Get a copy of all registered fetchers
|
|
@@ -988,36 +1065,36 @@ class nt {
|
|
|
988
1065
|
return new Map(this.registrar);
|
|
989
1066
|
}
|
|
990
1067
|
}
|
|
991
|
-
const
|
|
992
|
-
function _(r,
|
|
993
|
-
if (!(r === void 0 &&
|
|
994
|
-
return
|
|
1068
|
+
const ie = new ne();
|
|
1069
|
+
function _(r, e) {
|
|
1070
|
+
if (!(r === void 0 && e === void 0))
|
|
1071
|
+
return e === void 0 ? r : r === void 0 ? e : { ...r, ...e };
|
|
995
1072
|
}
|
|
996
|
-
function
|
|
1073
|
+
function ce(r, e) {
|
|
997
1074
|
if (Object.keys(r).length === 0)
|
|
998
|
-
return
|
|
999
|
-
if (Object.keys(
|
|
1075
|
+
return e;
|
|
1076
|
+
if (Object.keys(e).length === 0)
|
|
1000
1077
|
return r;
|
|
1001
|
-
const
|
|
1002
|
-
path: _(r.urlParams?.path,
|
|
1003
|
-
query: _(r.urlParams?.query,
|
|
1078
|
+
const t = {
|
|
1079
|
+
path: _(r.urlParams?.path, e.urlParams?.path),
|
|
1080
|
+
query: _(r.urlParams?.query, e.urlParams?.query)
|
|
1004
1081
|
}, s = {
|
|
1005
1082
|
...r.headers,
|
|
1006
|
-
...
|
|
1007
|
-
},
|
|
1083
|
+
...e.headers
|
|
1084
|
+
}, o = e.method ?? r.method, n = e.body ?? r.body, u = e.timeout ?? r.timeout, a = e.signal ?? r.signal, p = e.abortController ?? r.abortController;
|
|
1008
1085
|
return {
|
|
1009
1086
|
...r,
|
|
1010
|
-
...
|
|
1011
|
-
method:
|
|
1012
|
-
urlParams:
|
|
1087
|
+
...e,
|
|
1088
|
+
method: o,
|
|
1089
|
+
urlParams: t,
|
|
1013
1090
|
headers: s,
|
|
1014
|
-
body:
|
|
1091
|
+
body: n,
|
|
1015
1092
|
timeout: u,
|
|
1016
1093
|
signal: a,
|
|
1017
1094
|
abortController: p
|
|
1018
1095
|
};
|
|
1019
1096
|
}
|
|
1020
|
-
class
|
|
1097
|
+
class ue extends oe {
|
|
1021
1098
|
/**
|
|
1022
1099
|
* Create a NamedFetcher instance and automatically register it with the global fetcherRegistrar
|
|
1023
1100
|
*
|
|
@@ -1035,43 +1112,43 @@ class ut extends ot {
|
|
|
1035
1112
|
* headers: { 'Authorization': 'Bearer token' }
|
|
1036
1113
|
* });
|
|
1037
1114
|
*/
|
|
1038
|
-
constructor(
|
|
1039
|
-
super(
|
|
1115
|
+
constructor(e, t = S) {
|
|
1116
|
+
super(t), this.name = e, ie.register(e, this);
|
|
1040
1117
|
}
|
|
1041
1118
|
}
|
|
1042
|
-
const
|
|
1119
|
+
const he = new ue(g);
|
|
1043
1120
|
export {
|
|
1044
|
-
|
|
1121
|
+
ae as CONTENT_TYPE_HEADER,
|
|
1045
1122
|
E as ContentTypeValues,
|
|
1046
|
-
|
|
1123
|
+
g as DEFAULT_FETCHER_NAME,
|
|
1047
1124
|
S as DEFAULT_OPTIONS,
|
|
1048
1125
|
R as ExchangeError,
|
|
1049
|
-
|
|
1050
|
-
|
|
1126
|
+
ee as ExchangeResultExtractor,
|
|
1127
|
+
A as ExpressUrlTemplateResolver,
|
|
1051
1128
|
V as FETCH_INTERCEPTOR_NAME,
|
|
1052
1129
|
J as FETCH_INTERCEPTOR_ORDER,
|
|
1053
1130
|
Z as FetchExchange,
|
|
1054
1131
|
k as FetchInterceptor,
|
|
1055
1132
|
I as FetchTimeoutError,
|
|
1056
|
-
|
|
1133
|
+
oe as Fetcher,
|
|
1057
1134
|
l as FetcherError,
|
|
1058
|
-
|
|
1135
|
+
ne as FetcherRegistrar,
|
|
1059
1136
|
i as HttpMethod,
|
|
1060
|
-
|
|
1137
|
+
b as HttpStatusValidationError,
|
|
1061
1138
|
W as InterceptorManager,
|
|
1062
1139
|
T as InterceptorRegistry,
|
|
1063
|
-
|
|
1064
|
-
|
|
1140
|
+
re as JsonResultExtractor,
|
|
1141
|
+
ue as NamedFetcher,
|
|
1065
1142
|
B as REQUEST_BODY_INTERCEPTOR_NAME,
|
|
1066
1143
|
$ as REQUEST_BODY_INTERCEPTOR_ORDER,
|
|
1067
1144
|
X as RequestBodyInterceptor,
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1145
|
+
te as ResponseResultExtractor,
|
|
1146
|
+
f as ResultExtractors,
|
|
1147
|
+
se as TextResultExtractor,
|
|
1071
1148
|
H as URL_RESOLVE_INTERCEPTOR_NAME,
|
|
1072
1149
|
N as URL_RESOLVE_INTERCEPTOR_ORDER,
|
|
1073
|
-
|
|
1074
|
-
|
|
1150
|
+
P as UriTemplateResolver,
|
|
1151
|
+
G as UrlBuilder,
|
|
1075
1152
|
j as UrlResolveInterceptor,
|
|
1076
1153
|
U as UrlTemplateStyle,
|
|
1077
1154
|
z as VALIDATE_STATUS_INTERCEPTOR_NAME,
|
|
@@ -1079,14 +1156,14 @@ export {
|
|
|
1079
1156
|
K as ValidateStatusInterceptor,
|
|
1080
1157
|
C as combineURLs,
|
|
1081
1158
|
v as expressUrlTemplateResolver,
|
|
1082
|
-
|
|
1083
|
-
|
|
1159
|
+
he as fetcher,
|
|
1160
|
+
ie as fetcherRegistrar,
|
|
1084
1161
|
D as getUrlTemplateResolver,
|
|
1085
1162
|
F as isAbsoluteURL,
|
|
1086
1163
|
_ as mergeRecords,
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1164
|
+
ce as mergeRequest,
|
|
1165
|
+
M as resolveTimeout,
|
|
1166
|
+
x as timeoutFetch,
|
|
1090
1167
|
m as toSorted,
|
|
1091
1168
|
L as uriTemplateResolver,
|
|
1092
1169
|
w as urlTemplateRegexExtract,
|