@ahoo-wang/fetcher 1.3.3 → 1.5.0
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/fetchRequest.d.ts +22 -0
- package/dist/fetchRequest.d.ts.map +1 -1
- package/dist/fetcher.d.ts +5 -2
- package/dist/fetcher.d.ts.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.es.js +267 -253
- 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/mergeRequest.d.ts.map +1 -1
- package/dist/resultExtractor.d.ts +42 -0
- package/dist/resultExtractor.d.ts.map +1 -0
- package/dist/timeout.d.ts +3 -2
- package/dist/timeout.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.es.js
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
function
|
|
1
|
+
function F(r) {
|
|
2
2
|
return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(r);
|
|
3
3
|
}
|
|
4
|
-
function
|
|
5
|
-
return
|
|
4
|
+
function C(r, t) {
|
|
5
|
+
return F(t) ? t : t ? r.replace(/\/?\/$/, "") + "/" + t.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
|
-
return r === 1 ?
|
|
9
|
+
return r === 1 ? v : L;
|
|
10
10
|
}
|
|
11
|
-
function
|
|
12
|
-
return
|
|
13
|
-
const
|
|
14
|
-
if (
|
|
11
|
+
function q(r, t, e) {
|
|
12
|
+
return e ? r.replace(t, (s, n) => {
|
|
13
|
+
const o = e[n];
|
|
14
|
+
if (o === void 0)
|
|
15
15
|
throw new Error(`Missing required path parameter: ${n}`);
|
|
16
|
-
return encodeURIComponent(
|
|
16
|
+
return encodeURIComponent(o);
|
|
17
17
|
}) : r;
|
|
18
18
|
}
|
|
19
|
-
function
|
|
20
|
-
const
|
|
19
|
+
function w(r, t) {
|
|
20
|
+
const e = [];
|
|
21
21
|
let s;
|
|
22
|
-
for (; (s =
|
|
23
|
-
|
|
24
|
-
return
|
|
22
|
+
for (; (s = t.exec(r)) !== null; )
|
|
23
|
+
e.push(s[1]);
|
|
24
|
+
return e;
|
|
25
25
|
}
|
|
26
|
-
const
|
|
26
|
+
const c = class c {
|
|
27
27
|
/**
|
|
28
28
|
* Extracts path parameters from a URL string.
|
|
29
29
|
*
|
|
@@ -47,10 +47,10 @@ const a = class a {
|
|
|
47
47
|
* // noParams = []
|
|
48
48
|
* ```
|
|
49
49
|
*/
|
|
50
|
-
extractPathParams(
|
|
51
|
-
return
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
extractPathParams(t) {
|
|
51
|
+
return w(
|
|
52
|
+
t,
|
|
53
|
+
c.PATH_PARAM_REGEX
|
|
54
54
|
);
|
|
55
55
|
}
|
|
56
56
|
/**
|
|
@@ -89,17 +89,17 @@ const a = class a {
|
|
|
89
89
|
* }
|
|
90
90
|
* ```
|
|
91
91
|
*/
|
|
92
|
-
resolve(
|
|
93
|
-
return
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
92
|
+
resolve(t, e) {
|
|
93
|
+
return q(
|
|
94
|
+
t,
|
|
95
|
+
c.PATH_PARAM_REGEX,
|
|
96
|
+
e
|
|
97
97
|
);
|
|
98
98
|
}
|
|
99
99
|
};
|
|
100
|
-
|
|
101
|
-
let
|
|
102
|
-
const L = new
|
|
100
|
+
c.PATH_PARAM_REGEX = /{([^}]+)}/g;
|
|
101
|
+
let f = c;
|
|
102
|
+
const L = new f(), 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 T(), h = class h {
|
|
|
119
119
|
* // noParams = []
|
|
120
120
|
* ```
|
|
121
121
|
*/
|
|
122
|
-
extractPathParams(
|
|
123
|
-
return
|
|
124
|
-
|
|
122
|
+
extractPathParams(t) {
|
|
123
|
+
return w(
|
|
124
|
+
t,
|
|
125
125
|
h.PATH_PARAM_REGEX
|
|
126
126
|
);
|
|
127
127
|
}
|
|
@@ -157,18 +157,18 @@ const L = new T(), h = class h {
|
|
|
157
157
|
* }
|
|
158
158
|
* ```
|
|
159
159
|
*/
|
|
160
|
-
resolve(
|
|
161
|
-
return
|
|
162
|
-
|
|
160
|
+
resolve(t, e) {
|
|
161
|
+
return q(
|
|
162
|
+
t,
|
|
163
163
|
h.PATH_PARAM_REGEX,
|
|
164
|
-
|
|
164
|
+
e
|
|
165
165
|
);
|
|
166
166
|
}
|
|
167
167
|
};
|
|
168
168
|
h.PATH_PARAM_REGEX = /:([^/]+)/g;
|
|
169
|
-
let
|
|
170
|
-
const
|
|
171
|
-
class
|
|
169
|
+
let P = h;
|
|
170
|
+
const v = new P();
|
|
171
|
+
class x {
|
|
172
172
|
/**
|
|
173
173
|
* Initializes a new UrlBuilder instance.
|
|
174
174
|
*
|
|
@@ -186,8 +186,8 @@ class v {
|
|
|
186
186
|
* const expressUrlBuilder = new UrlBuilder('https://api.example.com', UrlTemplateStyle.Express);
|
|
187
187
|
* ```
|
|
188
188
|
*/
|
|
189
|
-
constructor(
|
|
190
|
-
this.baseURL =
|
|
189
|
+
constructor(t, e) {
|
|
190
|
+
this.baseURL = t, this.urlTemplateResolver = D(e);
|
|
191
191
|
}
|
|
192
192
|
/**
|
|
193
193
|
* Builds a complete URL, including path parameter replacement and query parameter addition.
|
|
@@ -207,14 +207,14 @@ class v {
|
|
|
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
|
|
210
|
+
build(t, e) {
|
|
211
|
+
const s = e?.path, n = e?.query, o = C(this.baseURL, t);
|
|
212
|
+
let u = this.urlTemplateResolver.resolve(o, s);
|
|
213
213
|
if (n) {
|
|
214
|
-
const
|
|
215
|
-
|
|
214
|
+
const a = new URLSearchParams(n).toString();
|
|
215
|
+
a && (u += "?" + a);
|
|
216
216
|
}
|
|
217
|
-
return
|
|
217
|
+
return u;
|
|
218
218
|
}
|
|
219
219
|
/**
|
|
220
220
|
* Resolves a complete URL from a FetchRequest.
|
|
@@ -225,8 +225,8 @@ class v {
|
|
|
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(t) {
|
|
229
|
+
return this.build(t.url, t.urlParams);
|
|
230
230
|
}
|
|
231
231
|
}
|
|
232
232
|
class l extends Error {
|
|
@@ -236,71 +236,71 @@ 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(t, e) {
|
|
240
|
+
const s = t || e?.message || "An error occurred in the fetcher";
|
|
241
|
+
super(s), this.cause = e, this.name = "FetcherError", e?.stack && (this.stack = e.stack), Object.setPrototypeOf(this, l.prototype);
|
|
242
242
|
}
|
|
243
243
|
}
|
|
244
|
-
class
|
|
244
|
+
class I extends l {
|
|
245
245
|
/**
|
|
246
246
|
* Creates a new FetchTimeoutError instance.
|
|
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(t) {
|
|
251
|
+
const e = t.method || "GET", s = `Request timeout of ${t.timeout}ms exceeded for ${e} ${t.url}`;
|
|
252
|
+
super(s), this.name = "FetchTimeoutError", this.request = t, Object.setPrototypeOf(this, I.prototype);
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
|
-
function G(r,
|
|
256
|
-
return typeof r < "u" ? r :
|
|
255
|
+
function G(r, t) {
|
|
256
|
+
return typeof r < "u" ? r : t;
|
|
257
257
|
}
|
|
258
258
|
async function M(r) {
|
|
259
|
-
const
|
|
260
|
-
if (r.signal
|
|
261
|
-
return fetch(
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
259
|
+
const t = r.url, e = r.timeout, s = r;
|
|
260
|
+
if (r.signal)
|
|
261
|
+
return fetch(t, s);
|
|
262
|
+
if (!e)
|
|
263
|
+
return r.abortController && (s.signal = r.abortController.signal), fetch(t, s);
|
|
264
|
+
const n = r.abortController ?? new AbortController();
|
|
265
|
+
s.signal = n.signal;
|
|
266
266
|
let o = null;
|
|
267
|
-
const
|
|
267
|
+
const u = new Promise((a, p) => {
|
|
268
268
|
o = setTimeout(() => {
|
|
269
269
|
o && clearTimeout(o);
|
|
270
|
-
const
|
|
271
|
-
n.abort(
|
|
272
|
-
},
|
|
270
|
+
const b = new I(r);
|
|
271
|
+
n.abort(b), p(b);
|
|
272
|
+
}, e);
|
|
273
273
|
});
|
|
274
274
|
try {
|
|
275
|
-
return await Promise.race([fetch(
|
|
275
|
+
return await Promise.race([fetch(t, s), u]);
|
|
276
276
|
} finally {
|
|
277
277
|
o && clearTimeout(o);
|
|
278
278
|
}
|
|
279
279
|
}
|
|
280
|
-
const H = "UrlResolveInterceptor",
|
|
281
|
-
class
|
|
280
|
+
const H = "UrlResolveInterceptor", N = Number.MIN_SAFE_INTEGER + 1e3;
|
|
281
|
+
class j {
|
|
282
282
|
constructor() {
|
|
283
|
-
this.name = H, this.order =
|
|
283
|
+
this.name = H, this.order = N;
|
|
284
284
|
}
|
|
285
285
|
/**
|
|
286
286
|
* Resolves the final URL by combining the base URL, path parameters, and query parameters.
|
|
287
287
|
*
|
|
288
288
|
* @param exchange - The fetch exchange containing the request information
|
|
289
289
|
*/
|
|
290
|
-
intercept(
|
|
291
|
-
const
|
|
292
|
-
|
|
290
|
+
intercept(t) {
|
|
291
|
+
const e = t.request;
|
|
292
|
+
e.url = t.fetcher.urlBuilder.resolveRequestUrl(e);
|
|
293
293
|
}
|
|
294
294
|
}
|
|
295
|
-
var
|
|
296
|
-
const
|
|
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 at = "Content-Type", d = class d {
|
|
297
297
|
};
|
|
298
|
-
|
|
299
|
-
let
|
|
300
|
-
const
|
|
298
|
+
d.APPLICATION_JSON = "application/json", d.TEXT_EVENT_STREAM = "text/event-stream";
|
|
299
|
+
let E = d;
|
|
300
|
+
const B = "RequestBodyInterceptor", $ = N + 1e3;
|
|
301
301
|
class X {
|
|
302
302
|
constructor() {
|
|
303
|
-
this.name =
|
|
303
|
+
this.name = B, this.order = $;
|
|
304
304
|
}
|
|
305
305
|
/**
|
|
306
306
|
* Attempts to convert request body to a valid fetch API body type.
|
|
@@ -339,20 +339,20 @@ 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(t) {
|
|
343
|
+
const e = t.request;
|
|
344
|
+
if (e.body === void 0 || e.body === null || typeof e.body != "object" || e.body instanceof ArrayBuffer || ArrayBuffer.isView(e.body) || // Includes TypedArray and DataView
|
|
345
|
+
e.body instanceof Blob || e.body instanceof File || e.body instanceof URLSearchParams || e.body instanceof FormData || e.body instanceof ReadableStream)
|
|
346
346
|
return;
|
|
347
|
-
|
|
348
|
-
const s =
|
|
349
|
-
s["Content-Type"] || (s["Content-Type"] =
|
|
347
|
+
t.request.body = JSON.stringify(e.body);
|
|
348
|
+
const s = t.ensureRequestHeaders();
|
|
349
|
+
s["Content-Type"] || (s["Content-Type"] = E.APPLICATION_JSON);
|
|
350
350
|
}
|
|
351
351
|
}
|
|
352
|
-
const
|
|
352
|
+
const V = "FetchInterceptor", J = Number.MAX_SAFE_INTEGER - 1e3;
|
|
353
353
|
class k {
|
|
354
354
|
constructor() {
|
|
355
|
-
this.name =
|
|
355
|
+
this.name = V, this.order = J;
|
|
356
356
|
}
|
|
357
357
|
/**
|
|
358
358
|
* Intercept and process HTTP requests.
|
|
@@ -379,14 +379,14 @@ 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(t) {
|
|
383
|
+
t.response = await M(t.request);
|
|
384
384
|
}
|
|
385
385
|
}
|
|
386
|
-
function
|
|
387
|
-
return
|
|
386
|
+
function m(r, t) {
|
|
387
|
+
return t ? r.filter(t).sort((e, s) => e.order - s.order) : [...r].sort((e, s) => e.order - s.order);
|
|
388
388
|
}
|
|
389
|
-
class
|
|
389
|
+
class T {
|
|
390
390
|
/**
|
|
391
391
|
* Initializes a new InterceptorRegistry instance.
|
|
392
392
|
*
|
|
@@ -396,8 +396,8 @@ class R {
|
|
|
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 =
|
|
399
|
+
constructor(t = []) {
|
|
400
|
+
this.sortedInterceptors = [], this.sortedInterceptors = m(t);
|
|
401
401
|
}
|
|
402
402
|
/**
|
|
403
403
|
* Gets the name of this interceptor registry.
|
|
@@ -434,10 +434,10 @@ class R {
|
|
|
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(t) {
|
|
438
|
+
return this.sortedInterceptors.some((e) => e.name === t.name) ? !1 : (this.sortedInterceptors = m([
|
|
439
439
|
...this.sortedInterceptors,
|
|
440
|
-
|
|
440
|
+
t
|
|
441
441
|
]), !0);
|
|
442
442
|
}
|
|
443
443
|
/**
|
|
@@ -447,12 +447,12 @@ class R {
|
|
|
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
|
|
452
|
-
return this.sortedInterceptors =
|
|
453
|
-
|
|
454
|
-
(s) => s.name !==
|
|
455
|
-
),
|
|
450
|
+
eject(t) {
|
|
451
|
+
const e = this.sortedInterceptors;
|
|
452
|
+
return this.sortedInterceptors = m(
|
|
453
|
+
e,
|
|
454
|
+
(s) => s.name !== t
|
|
455
|
+
), e.length !== this.sortedInterceptors.length;
|
|
456
456
|
}
|
|
457
457
|
/**
|
|
458
458
|
* Removes all interceptors from this registry.
|
|
@@ -474,27 +474,27 @@ class R {
|
|
|
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(t) {
|
|
478
|
+
for (const e of this.sortedInterceptors)
|
|
479
|
+
await e.intercept(t);
|
|
480
480
|
}
|
|
481
481
|
}
|
|
482
|
-
class
|
|
483
|
-
constructor(
|
|
482
|
+
class g extends l {
|
|
483
|
+
constructor(t) {
|
|
484
484
|
super(
|
|
485
|
-
`Request failed with status code ${
|
|
486
|
-
), this.exchange =
|
|
485
|
+
`Request failed with status code ${t.response?.status} for ${t.request.url}`
|
|
486
|
+
), this.exchange = t, this.name = "HttpStatusValidationError", Object.setPrototypeOf(this, g.prototype);
|
|
487
487
|
}
|
|
488
488
|
}
|
|
489
|
-
const
|
|
490
|
-
class
|
|
489
|
+
const Y = (r) => r >= 200 && r < 300, z = "ValidateStatusInterceptor", Q = Number.MAX_SAFE_INTEGER - 1e3;
|
|
490
|
+
class K {
|
|
491
491
|
/**
|
|
492
492
|
* Creates a new ValidateStatusInterceptor instance.
|
|
493
493
|
*
|
|
494
494
|
* @param validateStatus - Function that determines if a status code is valid
|
|
495
495
|
*/
|
|
496
|
-
constructor(
|
|
497
|
-
this.validateStatus =
|
|
496
|
+
constructor(t = Y) {
|
|
497
|
+
this.validateStatus = t;
|
|
498
498
|
}
|
|
499
499
|
/**
|
|
500
500
|
* Gets the name of this interceptor.
|
|
@@ -502,7 +502,7 @@ class Q {
|
|
|
502
502
|
* @returns The name of this interceptor
|
|
503
503
|
*/
|
|
504
504
|
get name() {
|
|
505
|
-
return
|
|
505
|
+
return z;
|
|
506
506
|
}
|
|
507
507
|
/**
|
|
508
508
|
* Gets the order of this interceptor.
|
|
@@ -510,7 +510,7 @@ class Q {
|
|
|
510
510
|
* @returns VALIDATE_STATUS_INTERCEPTOR_ORDER, indicating this interceptor should execute early
|
|
511
511
|
*/
|
|
512
512
|
get order() {
|
|
513
|
-
return
|
|
513
|
+
return Q;
|
|
514
514
|
}
|
|
515
515
|
/**
|
|
516
516
|
* Validates the response status code.
|
|
@@ -525,35 +525,35 @@ class Q {
|
|
|
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(t) {
|
|
529
|
+
if (!t.response)
|
|
530
530
|
return;
|
|
531
|
-
const
|
|
532
|
-
if (!this.validateStatus(
|
|
533
|
-
throw new
|
|
531
|
+
const e = t.response.status;
|
|
532
|
+
if (!this.validateStatus(e))
|
|
533
|
+
throw new g(t);
|
|
534
534
|
}
|
|
535
535
|
}
|
|
536
|
-
class
|
|
536
|
+
class R extends l {
|
|
537
537
|
/**
|
|
538
538
|
* Creates a new ExchangeError instance.
|
|
539
539
|
*
|
|
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(t, e) {
|
|
544
|
+
const s = e || t.error?.message || t.response?.statusText || `Request to ${t.request.url} failed during exchange`;
|
|
545
|
+
super(s, t.error), this.exchange = t, this.name = "ExchangeError", Object.setPrototypeOf(this, R.prototype);
|
|
546
546
|
}
|
|
547
547
|
}
|
|
548
|
-
class
|
|
548
|
+
class W {
|
|
549
549
|
constructor() {
|
|
550
|
-
this.request = new
|
|
551
|
-
new
|
|
550
|
+
this.request = new T([
|
|
551
|
+
new j(),
|
|
552
552
|
new X(),
|
|
553
553
|
new k()
|
|
554
|
-
]), this.response = new
|
|
555
|
-
new
|
|
556
|
-
]), this.error = new
|
|
554
|
+
]), this.response = new T([
|
|
555
|
+
new K()
|
|
556
|
+
]), this.error = new T();
|
|
557
557
|
}
|
|
558
558
|
/**
|
|
559
559
|
* Processes a FetchExchange through the interceptor pipeline.
|
|
@@ -641,19 +641,19 @@ class K {
|
|
|
641
641
|
* const result = await fetcher.exchange(exchange);
|
|
642
642
|
* ```
|
|
643
643
|
*/
|
|
644
|
-
async exchange(
|
|
644
|
+
async exchange(t) {
|
|
645
645
|
try {
|
|
646
|
-
return await this.request.intercept(
|
|
647
|
-
} catch (
|
|
648
|
-
if (
|
|
649
|
-
return
|
|
650
|
-
throw new
|
|
646
|
+
return await this.request.intercept(t), await this.response.intercept(t), t;
|
|
647
|
+
} catch (e) {
|
|
648
|
+
if (t.error = e, await this.error.intercept(t), !t.hasError())
|
|
649
|
+
return t;
|
|
650
|
+
throw new R(t);
|
|
651
651
|
}
|
|
652
652
|
}
|
|
653
653
|
}
|
|
654
|
-
class
|
|
655
|
-
constructor(
|
|
656
|
-
this.attributes = {}, this.fetcher =
|
|
654
|
+
class Z {
|
|
655
|
+
constructor(t, e, s, n) {
|
|
656
|
+
this.attributes = {}, this.fetcher = t, this.request = e, this.response = s, this.error = n;
|
|
657
657
|
}
|
|
658
658
|
/**
|
|
659
659
|
* Ensures that request headers object exists, creating it if necessary.
|
|
@@ -712,20 +712,25 @@ class W {
|
|
|
712
712
|
*/
|
|
713
713
|
get requiredResponse() {
|
|
714
714
|
if (!this.response)
|
|
715
|
-
throw new
|
|
715
|
+
throw new R(
|
|
716
716
|
this,
|
|
717
717
|
`Request to ${this.request.url} failed with no response`
|
|
718
718
|
);
|
|
719
719
|
return this.response;
|
|
720
720
|
}
|
|
721
721
|
}
|
|
722
|
-
const
|
|
723
|
-
|
|
724
|
-
|
|
722
|
+
const tt = (r) => r, et = (r) => r.requiredResponse, rt = (r) => r.requiredResponse.json(), st = (r) => r.requiredResponse.text(), O = {
|
|
723
|
+
Exchange: tt,
|
|
724
|
+
Response: et,
|
|
725
|
+
Json: rt,
|
|
726
|
+
Text: st
|
|
727
|
+
}, A = {
|
|
728
|
+
"Content-Type": E.APPLICATION_JSON
|
|
729
|
+
}, S = {
|
|
725
730
|
baseURL: "",
|
|
726
|
-
headers:
|
|
731
|
+
headers: A
|
|
727
732
|
};
|
|
728
|
-
class
|
|
733
|
+
class ot {
|
|
729
734
|
/**
|
|
730
735
|
* Initializes a new Fetcher instance with optional configuration.
|
|
731
736
|
*
|
|
@@ -734,8 +739,8 @@ class Z {
|
|
|
734
739
|
*
|
|
735
740
|
* @param options - Configuration options for the Fetcher instance
|
|
736
741
|
*/
|
|
737
|
-
constructor(
|
|
738
|
-
this.headers =
|
|
742
|
+
constructor(t = S) {
|
|
743
|
+
this.headers = A, this.urlBuilder = new x(t.baseURL, t.urlTemplateStyle), this.headers = t.headers ?? A, this.timeout = t.timeout, this.interceptors = t.interceptors ?? new W();
|
|
739
744
|
}
|
|
740
745
|
/**
|
|
741
746
|
* Executes an HTTP request with the specified URL and options.
|
|
@@ -748,9 +753,9 @@ class Z {
|
|
|
748
753
|
* @returns Promise that resolves to the HTTP response
|
|
749
754
|
* @throws FetchError if the request fails and no response is generated
|
|
750
755
|
*/
|
|
751
|
-
async fetch(
|
|
752
|
-
const s =
|
|
753
|
-
return s.url =
|
|
756
|
+
async fetch(t, e = {}) {
|
|
757
|
+
const s = e;
|
|
758
|
+
return s.url = t, this.request(s, O.Response);
|
|
754
759
|
}
|
|
755
760
|
/**
|
|
756
761
|
* Processes an HTTP request through the Fetcher's internal workflow.
|
|
@@ -760,19 +765,22 @@ class Z {
|
|
|
760
765
|
* for interceptor processing.
|
|
761
766
|
*
|
|
762
767
|
* @param request - Complete request configuration object
|
|
763
|
-
* @
|
|
768
|
+
* @param resultExtractor - Function to extract the desired result from the exchange.
|
|
769
|
+
* Defaults to ExchangeResultExtractor which returns the entire exchange object.
|
|
770
|
+
* @returns Promise that resolves to the extracted result based on resultExtractor
|
|
764
771
|
* @throws Error if an unhandled error occurs during request processing
|
|
765
772
|
*/
|
|
766
|
-
|
|
767
|
-
|
|
773
|
+
// @ts-expect-error - Required to bypass type checking for resultExtractor default value assignment
|
|
774
|
+
async request(t, e = O.Exchange) {
|
|
775
|
+
const s = {
|
|
768
776
|
...this.headers,
|
|
769
|
-
...
|
|
770
|
-
},
|
|
771
|
-
...
|
|
772
|
-
headers:
|
|
773
|
-
timeout: G(
|
|
774
|
-
},
|
|
775
|
-
return this.interceptors.exchange(
|
|
777
|
+
...t.headers
|
|
778
|
+
}, n = {
|
|
779
|
+
...t,
|
|
780
|
+
headers: s,
|
|
781
|
+
timeout: G(t.timeout, this.timeout)
|
|
782
|
+
}, o = new Z(this, n);
|
|
783
|
+
return await this.interceptors.exchange(o), e(o);
|
|
776
784
|
}
|
|
777
785
|
/**
|
|
778
786
|
* Internal helper method for making HTTP requests with a specific method.
|
|
@@ -785,10 +793,10 @@ class Z {
|
|
|
785
793
|
* @param request - Additional request options
|
|
786
794
|
* @returns Promise that resolves to the HTTP response
|
|
787
795
|
*/
|
|
788
|
-
async methodFetch(
|
|
789
|
-
return this.fetch(
|
|
796
|
+
async methodFetch(t, e, s = {}) {
|
|
797
|
+
return this.fetch(e, {
|
|
790
798
|
...s,
|
|
791
|
-
method:
|
|
799
|
+
method: t
|
|
792
800
|
});
|
|
793
801
|
}
|
|
794
802
|
/**
|
|
@@ -801,8 +809,8 @@ class Z {
|
|
|
801
809
|
* @param request - Request options excluding method and body
|
|
802
810
|
* @returns Promise that resolves to the HTTP response
|
|
803
811
|
*/
|
|
804
|
-
async get(
|
|
805
|
-
return this.methodFetch(
|
|
812
|
+
async get(t, e = {}) {
|
|
813
|
+
return this.methodFetch(i.GET, t, e);
|
|
806
814
|
}
|
|
807
815
|
/**
|
|
808
816
|
* Makes a POST HTTP request.
|
|
@@ -813,8 +821,8 @@ class Z {
|
|
|
813
821
|
* @param request - Request options including body and other parameters
|
|
814
822
|
* @returns Promise that resolves to the HTTP response
|
|
815
823
|
*/
|
|
816
|
-
async post(
|
|
817
|
-
return this.methodFetch(
|
|
824
|
+
async post(t, e = {}) {
|
|
825
|
+
return this.methodFetch(i.POST, t, e);
|
|
818
826
|
}
|
|
819
827
|
/**
|
|
820
828
|
* Makes a PUT HTTP request.
|
|
@@ -825,8 +833,8 @@ class Z {
|
|
|
825
833
|
* @param request - Request options including body and other parameters
|
|
826
834
|
* @returns Promise that resolves to the HTTP response
|
|
827
835
|
*/
|
|
828
|
-
async put(
|
|
829
|
-
return this.methodFetch(
|
|
836
|
+
async put(t, e = {}) {
|
|
837
|
+
return this.methodFetch(i.PUT, t, e);
|
|
830
838
|
}
|
|
831
839
|
/**
|
|
832
840
|
* Makes a DELETE HTTP request.
|
|
@@ -837,8 +845,8 @@ class Z {
|
|
|
837
845
|
* @param request - Request options excluding method and body
|
|
838
846
|
* @returns Promise that resolves to the HTTP response
|
|
839
847
|
*/
|
|
840
|
-
async delete(
|
|
841
|
-
return this.methodFetch(
|
|
848
|
+
async delete(t, e = {}) {
|
|
849
|
+
return this.methodFetch(i.DELETE, t, e);
|
|
842
850
|
}
|
|
843
851
|
/**
|
|
844
852
|
* Makes a PATCH HTTP request.
|
|
@@ -849,8 +857,8 @@ class Z {
|
|
|
849
857
|
* @param request - Request options including body and other parameters
|
|
850
858
|
* @returns Promise that resolves to the HTTP response
|
|
851
859
|
*/
|
|
852
|
-
async patch(
|
|
853
|
-
return this.methodFetch(
|
|
860
|
+
async patch(t, e = {}) {
|
|
861
|
+
return this.methodFetch(i.PATCH, t, e);
|
|
854
862
|
}
|
|
855
863
|
/**
|
|
856
864
|
* Makes a HEAD HTTP request.
|
|
@@ -862,8 +870,8 @@ class Z {
|
|
|
862
870
|
* @param request - Request options excluding method and body
|
|
863
871
|
* @returns Promise that resolves to the HTTP response
|
|
864
872
|
*/
|
|
865
|
-
async head(
|
|
866
|
-
return this.methodFetch(
|
|
873
|
+
async head(t, e = {}) {
|
|
874
|
+
return this.methodFetch(i.HEAD, t, e);
|
|
867
875
|
}
|
|
868
876
|
/**
|
|
869
877
|
* Makes an OPTIONS HTTP request.
|
|
@@ -875,12 +883,12 @@ class Z {
|
|
|
875
883
|
* @param request - Request options excluding method and body
|
|
876
884
|
* @returns Promise that resolves to the HTTP response
|
|
877
885
|
*/
|
|
878
|
-
async options(
|
|
879
|
-
return this.methodFetch(
|
|
886
|
+
async options(t, e = {}) {
|
|
887
|
+
return this.methodFetch(i.OPTIONS, t, e);
|
|
880
888
|
}
|
|
881
889
|
}
|
|
882
|
-
const
|
|
883
|
-
class
|
|
890
|
+
const y = "default";
|
|
891
|
+
class nt {
|
|
884
892
|
constructor() {
|
|
885
893
|
this.registrar = /* @__PURE__ */ new Map();
|
|
886
894
|
}
|
|
@@ -893,8 +901,8 @@ class ee {
|
|
|
893
901
|
* const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
894
902
|
* fetcherRegistrar.register('api', fetcher);
|
|
895
903
|
*/
|
|
896
|
-
register(
|
|
897
|
-
this.registrar.set(
|
|
904
|
+
register(t, e) {
|
|
905
|
+
this.registrar.set(t, e);
|
|
898
906
|
}
|
|
899
907
|
/**
|
|
900
908
|
* Unregister a Fetcher instance by name
|
|
@@ -907,8 +915,8 @@ class ee {
|
|
|
907
915
|
* console.log('Fetcher unregistered successfully');
|
|
908
916
|
* }
|
|
909
917
|
*/
|
|
910
|
-
unregister(
|
|
911
|
-
return this.registrar.delete(
|
|
918
|
+
unregister(t) {
|
|
919
|
+
return this.registrar.delete(t);
|
|
912
920
|
}
|
|
913
921
|
/**
|
|
914
922
|
* Get a Fetcher instance by name
|
|
@@ -921,8 +929,8 @@ class ee {
|
|
|
921
929
|
* // Use the fetcher
|
|
922
930
|
* }
|
|
923
931
|
*/
|
|
924
|
-
get(
|
|
925
|
-
return this.registrar.get(
|
|
932
|
+
get(t) {
|
|
933
|
+
return this.registrar.get(t);
|
|
926
934
|
}
|
|
927
935
|
/**
|
|
928
936
|
* Get a Fetcher instance by name, throwing an error if not found
|
|
@@ -938,11 +946,11 @@ class ee {
|
|
|
938
946
|
* console.error('Fetcher not found:', error.message);
|
|
939
947
|
* }
|
|
940
948
|
*/
|
|
941
|
-
requiredGet(
|
|
942
|
-
const
|
|
943
|
-
if (!
|
|
944
|
-
throw new Error(`Fetcher ${
|
|
945
|
-
return
|
|
949
|
+
requiredGet(t) {
|
|
950
|
+
const e = this.get(t);
|
|
951
|
+
if (!e)
|
|
952
|
+
throw new Error(`Fetcher ${t} not found`);
|
|
953
|
+
return e;
|
|
946
954
|
}
|
|
947
955
|
/**
|
|
948
956
|
* Get the default Fetcher instance
|
|
@@ -953,7 +961,7 @@ class ee {
|
|
|
953
961
|
* const defaultFetcher = fetcherRegistrar.default;
|
|
954
962
|
*/
|
|
955
963
|
get default() {
|
|
956
|
-
return this.requiredGet(
|
|
964
|
+
return this.requiredGet(y);
|
|
957
965
|
}
|
|
958
966
|
/**
|
|
959
967
|
* Set the default Fetcher instance
|
|
@@ -963,8 +971,8 @@ class ee {
|
|
|
963
971
|
* const fetcher = new Fetcher({ baseURL: 'https://api.example.com' });
|
|
964
972
|
* fetcherRegistrar.default = fetcher;
|
|
965
973
|
*/
|
|
966
|
-
set default(
|
|
967
|
-
this.register(
|
|
974
|
+
set default(t) {
|
|
975
|
+
this.register(y, t);
|
|
968
976
|
}
|
|
969
977
|
/**
|
|
970
978
|
* Get a copy of all registered fetchers
|
|
@@ -980,35 +988,36 @@ class ee {
|
|
|
980
988
|
return new Map(this.registrar);
|
|
981
989
|
}
|
|
982
990
|
}
|
|
983
|
-
const
|
|
984
|
-
function
|
|
985
|
-
if (!(r === void 0 &&
|
|
986
|
-
return
|
|
991
|
+
const it = new nt();
|
|
992
|
+
function _(r, t) {
|
|
993
|
+
if (!(r === void 0 && t === void 0))
|
|
994
|
+
return t === void 0 ? r : r === void 0 ? t : { ...r, ...t };
|
|
987
995
|
}
|
|
988
|
-
function
|
|
996
|
+
function ct(r, t) {
|
|
989
997
|
if (Object.keys(r).length === 0)
|
|
990
|
-
return
|
|
991
|
-
if (Object.keys(
|
|
998
|
+
return t;
|
|
999
|
+
if (Object.keys(t).length === 0)
|
|
992
1000
|
return r;
|
|
993
|
-
const
|
|
994
|
-
path:
|
|
995
|
-
query:
|
|
1001
|
+
const e = {
|
|
1002
|
+
path: _(r.urlParams?.path, t.urlParams?.path),
|
|
1003
|
+
query: _(r.urlParams?.query, t.urlParams?.query)
|
|
996
1004
|
}, s = {
|
|
997
1005
|
...r.headers,
|
|
998
|
-
...
|
|
999
|
-
}, n =
|
|
1006
|
+
...t.headers
|
|
1007
|
+
}, n = t.method ?? r.method, o = t.body ?? r.body, u = t.timeout ?? r.timeout, a = t.signal ?? r.signal, p = t.abortController ?? r.abortController;
|
|
1000
1008
|
return {
|
|
1001
1009
|
...r,
|
|
1002
|
-
...
|
|
1010
|
+
...t,
|
|
1003
1011
|
method: n,
|
|
1004
|
-
urlParams:
|
|
1012
|
+
urlParams: e,
|
|
1005
1013
|
headers: s,
|
|
1006
|
-
body:
|
|
1007
|
-
timeout:
|
|
1008
|
-
signal:
|
|
1014
|
+
body: o,
|
|
1015
|
+
timeout: u,
|
|
1016
|
+
signal: a,
|
|
1017
|
+
abortController: p
|
|
1009
1018
|
};
|
|
1010
1019
|
}
|
|
1011
|
-
class
|
|
1020
|
+
class ut extends ot {
|
|
1012
1021
|
/**
|
|
1013
1022
|
* Create a NamedFetcher instance and automatically register it with the global fetcherRegistrar
|
|
1014
1023
|
*
|
|
@@ -1026,56 +1035,61 @@ class re extends Z {
|
|
|
1026
1035
|
* headers: { 'Authorization': 'Bearer token' }
|
|
1027
1036
|
* });
|
|
1028
1037
|
*/
|
|
1029
|
-
constructor(
|
|
1030
|
-
super(
|
|
1038
|
+
constructor(t, e = S) {
|
|
1039
|
+
super(e), this.name = t, it.register(t, this);
|
|
1031
1040
|
}
|
|
1032
1041
|
}
|
|
1033
|
-
const
|
|
1042
|
+
const ht = new ut(y);
|
|
1034
1043
|
export {
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
V as
|
|
1043
|
-
|
|
1044
|
+
at as CONTENT_TYPE_HEADER,
|
|
1045
|
+
E as ContentTypeValues,
|
|
1046
|
+
y as DEFAULT_FETCHER_NAME,
|
|
1047
|
+
S as DEFAULT_OPTIONS,
|
|
1048
|
+
R as ExchangeError,
|
|
1049
|
+
tt as ExchangeResultExtractor,
|
|
1050
|
+
P as ExpressUrlTemplateResolver,
|
|
1051
|
+
V as FETCH_INTERCEPTOR_NAME,
|
|
1052
|
+
J as FETCH_INTERCEPTOR_ORDER,
|
|
1053
|
+
Z as FetchExchange,
|
|
1044
1054
|
k as FetchInterceptor,
|
|
1045
|
-
|
|
1046
|
-
|
|
1055
|
+
I as FetchTimeoutError,
|
|
1056
|
+
ot as Fetcher,
|
|
1047
1057
|
l as FetcherError,
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1058
|
+
nt as FetcherRegistrar,
|
|
1059
|
+
i as HttpMethod,
|
|
1060
|
+
g as HttpStatusValidationError,
|
|
1061
|
+
W as InterceptorManager,
|
|
1062
|
+
T as InterceptorRegistry,
|
|
1063
|
+
rt as JsonResultExtractor,
|
|
1064
|
+
ut as NamedFetcher,
|
|
1065
|
+
B as REQUEST_BODY_INTERCEPTOR_NAME,
|
|
1066
|
+
$ as REQUEST_BODY_INTERCEPTOR_ORDER,
|
|
1056
1067
|
X as RequestBodyInterceptor,
|
|
1068
|
+
et as ResponseResultExtractor,
|
|
1069
|
+
O as ResultExtractors,
|
|
1070
|
+
st as TextResultExtractor,
|
|
1057
1071
|
H as URL_RESOLVE_INTERCEPTOR_NAME,
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1072
|
+
N as URL_RESOLVE_INTERCEPTOR_ORDER,
|
|
1073
|
+
f as UriTemplateResolver,
|
|
1074
|
+
x as UrlBuilder,
|
|
1075
|
+
j as UrlResolveInterceptor,
|
|
1062
1076
|
U as UrlTemplateStyle,
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1077
|
+
z as VALIDATE_STATUS_INTERCEPTOR_NAME,
|
|
1078
|
+
Q as VALIDATE_STATUS_INTERCEPTOR_ORDER,
|
|
1079
|
+
K as ValidateStatusInterceptor,
|
|
1080
|
+
C as combineURLs,
|
|
1081
|
+
v as expressUrlTemplateResolver,
|
|
1082
|
+
ht as fetcher,
|
|
1083
|
+
it as fetcherRegistrar,
|
|
1070
1084
|
D as getUrlTemplateResolver,
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1085
|
+
F as isAbsoluteURL,
|
|
1086
|
+
_ as mergeRecords,
|
|
1087
|
+
ct as mergeRequest,
|
|
1074
1088
|
G as resolveTimeout,
|
|
1075
1089
|
M as timeoutFetch,
|
|
1076
|
-
|
|
1090
|
+
m as toSorted,
|
|
1077
1091
|
L as uriTemplateResolver,
|
|
1078
|
-
|
|
1079
|
-
|
|
1092
|
+
w as urlTemplateRegexExtract,
|
|
1093
|
+
q as urlTemplateRegexResolve
|
|
1080
1094
|
};
|
|
1081
1095
|
//# sourceMappingURL=index.es.js.map
|