@ahoo-wang/fetcher 3.6.6 → 3.6.9
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/fetchInterceptor.d.ts.map +1 -1
- package/dist/index.es.js +255 -252
- 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/interceptor.d.ts +2 -0
- package/dist/interceptor.d.ts.map +1 -1
- package/dist/requestBodyInterceptor.d.ts +0 -1
- package/dist/requestBodyInterceptor.d.ts.map +1 -1
- package/dist/urlResolveInterceptor.d.ts +0 -1
- package/dist/urlResolveInterceptor.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/index.es.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
function
|
|
1
|
+
function M(e) {
|
|
2
2
|
return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(e);
|
|
3
3
|
}
|
|
4
|
-
function
|
|
5
|
-
return
|
|
4
|
+
function H(e, t) {
|
|
5
|
+
return M(t) ? t : t ? e.replace(/\/?\/$/, "") + "/" + t.replace(/^\/+/, "") : e;
|
|
6
6
|
}
|
|
7
|
-
var
|
|
8
|
-
function
|
|
9
|
-
return e === 1 ?
|
|
7
|
+
var j = /* @__PURE__ */ ((e) => (e[e.UriTemplate = 0] = "UriTemplate", e[e.Express = 1] = "Express", e))(j || {});
|
|
8
|
+
function $(e) {
|
|
9
|
+
return e === 1 ? X : V;
|
|
10
10
|
}
|
|
11
|
-
function
|
|
11
|
+
function U(e, t, r) {
|
|
12
12
|
return r ? e.replace(t, (s, o) => {
|
|
13
13
|
const n = r[o];
|
|
14
14
|
if (n === void 0)
|
|
@@ -16,7 +16,7 @@ function C(e, t, r) {
|
|
|
16
16
|
return encodeURIComponent(n);
|
|
17
17
|
}) : e;
|
|
18
18
|
}
|
|
19
|
-
function
|
|
19
|
+
function D(e, t) {
|
|
20
20
|
const r = [];
|
|
21
21
|
let s;
|
|
22
22
|
for (; (s = t.exec(e)) !== null; )
|
|
@@ -48,7 +48,7 @@ const h = class h {
|
|
|
48
48
|
* ```
|
|
49
49
|
*/
|
|
50
50
|
extractPathParams(t) {
|
|
51
|
-
return
|
|
51
|
+
return D(
|
|
52
52
|
t,
|
|
53
53
|
h.PATH_PARAM_REGEX
|
|
54
54
|
);
|
|
@@ -90,7 +90,7 @@ const h = class h {
|
|
|
90
90
|
* ```
|
|
91
91
|
*/
|
|
92
92
|
resolve(t, r) {
|
|
93
|
-
return
|
|
93
|
+
return U(
|
|
94
94
|
t,
|
|
95
95
|
h.PATH_PARAM_REGEX,
|
|
96
96
|
r
|
|
@@ -99,7 +99,7 @@ const h = class h {
|
|
|
99
99
|
};
|
|
100
100
|
h.PATH_PARAM_REGEX = /{([^}]+)}/g;
|
|
101
101
|
let P = h;
|
|
102
|
-
const
|
|
102
|
+
const V = new P(), l = class l {
|
|
103
103
|
/**
|
|
104
104
|
* Extracts path parameters from an Express-style URL string.
|
|
105
105
|
*
|
|
@@ -120,7 +120,7 @@ const $ = new P(), l = class l {
|
|
|
120
120
|
* ```
|
|
121
121
|
*/
|
|
122
122
|
extractPathParams(t) {
|
|
123
|
-
return
|
|
123
|
+
return D(
|
|
124
124
|
t,
|
|
125
125
|
l.PATH_PARAM_REGEX
|
|
126
126
|
);
|
|
@@ -158,7 +158,7 @@ const $ = new P(), l = class l {
|
|
|
158
158
|
* ```
|
|
159
159
|
*/
|
|
160
160
|
resolve(t, r) {
|
|
161
|
-
return
|
|
161
|
+
return U(
|
|
162
162
|
t,
|
|
163
163
|
l.PATH_PARAM_REGEX,
|
|
164
164
|
r
|
|
@@ -166,9 +166,9 @@ const $ = new P(), l = class l {
|
|
|
166
166
|
}
|
|
167
167
|
};
|
|
168
168
|
l.PATH_PARAM_REGEX = /:([^/]+)/g;
|
|
169
|
-
let
|
|
170
|
-
const
|
|
171
|
-
class
|
|
169
|
+
let _ = l;
|
|
170
|
+
const X = new _();
|
|
171
|
+
class k {
|
|
172
172
|
/**
|
|
173
173
|
* Initializes a new UrlBuilder instance.
|
|
174
174
|
*
|
|
@@ -187,7 +187,7 @@ class X {
|
|
|
187
187
|
* ```
|
|
188
188
|
*/
|
|
189
189
|
constructor(t, r) {
|
|
190
|
-
this.baseURL = t, this.urlTemplateResolver =
|
|
190
|
+
this.baseURL = t, this.urlTemplateResolver = $(r);
|
|
191
191
|
}
|
|
192
192
|
/**
|
|
193
193
|
* Builds a complete URL, including path parameter replacement and query parameter addition.
|
|
@@ -208,7 +208,7 @@ class X {
|
|
|
208
208
|
* ```
|
|
209
209
|
*/
|
|
210
210
|
build(t, r) {
|
|
211
|
-
const s = r?.path, o = r?.query, n =
|
|
211
|
+
const s = r?.path, o = r?.query, n = H(this.baseURL, t);
|
|
212
212
|
let u = this.urlTemplateResolver.resolve(n, s);
|
|
213
213
|
if (o) {
|
|
214
214
|
const a = new URLSearchParams(o).toString();
|
|
@@ -253,7 +253,7 @@ class E extends p {
|
|
|
253
253
|
super(s, t.error), this.exchange = t, this.name = "ExchangeError", Object.setPrototypeOf(this, E.prototype);
|
|
254
254
|
}
|
|
255
255
|
}
|
|
256
|
-
class
|
|
256
|
+
class g extends p {
|
|
257
257
|
/**
|
|
258
258
|
* Creates a new FetchTimeoutError instance.
|
|
259
259
|
*
|
|
@@ -261,13 +261,13 @@ class O extends p {
|
|
|
261
261
|
*/
|
|
262
262
|
constructor(t) {
|
|
263
263
|
const r = t.method || "GET", s = `Request timeout of ${t.timeout}ms exceeded for ${r} ${t.url}`;
|
|
264
|
-
super(s), this.name = "FetchTimeoutError", this.request = t, Object.setPrototypeOf(this,
|
|
264
|
+
super(s), this.name = "FetchTimeoutError", this.request = t, Object.setPrototypeOf(this, g.prototype);
|
|
265
265
|
}
|
|
266
266
|
}
|
|
267
|
-
function
|
|
267
|
+
function J(e, t) {
|
|
268
268
|
return typeof e < "u" ? e : t;
|
|
269
269
|
}
|
|
270
|
-
async function
|
|
270
|
+
async function Q(e) {
|
|
271
271
|
const t = e.url, r = e.timeout, s = e;
|
|
272
272
|
if (e.signal)
|
|
273
273
|
return await fetch(t, s);
|
|
@@ -279,8 +279,8 @@ async function J(e) {
|
|
|
279
279
|
const u = new Promise((a, T) => {
|
|
280
280
|
n = setTimeout(() => {
|
|
281
281
|
n && clearTimeout(n);
|
|
282
|
-
const
|
|
283
|
-
o.abort(
|
|
282
|
+
const S = new g(e);
|
|
283
|
+
o.abort(S), T(S);
|
|
284
284
|
}, r);
|
|
285
285
|
});
|
|
286
286
|
try {
|
|
@@ -289,41 +289,41 @@ async function J(e) {
|
|
|
289
289
|
n && clearTimeout(n);
|
|
290
290
|
}
|
|
291
291
|
}
|
|
292
|
-
const
|
|
292
|
+
const Y = (e) => e, z = (e) => e.requiredResponse, K = (e) => e.requiredResponse.json(), W = (e) => e.requiredResponse.text(), Z = (e) => e.requiredResponse.blob(), tt = (e) => e.requiredResponse.arrayBuffer(), et = (e) => e.requiredResponse.bytes(), I = {
|
|
293
293
|
/**
|
|
294
294
|
* Returns the original FetchExchange object
|
|
295
295
|
*/
|
|
296
|
-
Exchange:
|
|
296
|
+
Exchange: Y,
|
|
297
297
|
/**
|
|
298
298
|
* Extracts the raw Response object
|
|
299
299
|
*/
|
|
300
|
-
Response:
|
|
300
|
+
Response: z,
|
|
301
301
|
/**
|
|
302
302
|
* Parses and returns response body as JSON
|
|
303
303
|
*/
|
|
304
|
-
Json:
|
|
304
|
+
Json: K,
|
|
305
305
|
/**
|
|
306
306
|
* Returns response body as text
|
|
307
307
|
*/
|
|
308
|
-
Text:
|
|
308
|
+
Text: W,
|
|
309
309
|
/**
|
|
310
310
|
* Returns response body as a Blob
|
|
311
311
|
*/
|
|
312
|
-
Blob:
|
|
312
|
+
Blob: Z,
|
|
313
313
|
/**
|
|
314
314
|
* Returns response body as an ArrayBuffer
|
|
315
315
|
*/
|
|
316
|
-
ArrayBuffer:
|
|
316
|
+
ArrayBuffer: tt,
|
|
317
317
|
/**
|
|
318
318
|
* Returns response body as a Uint8Array
|
|
319
319
|
*/
|
|
320
|
-
Bytes:
|
|
320
|
+
Bytes: et
|
|
321
321
|
};
|
|
322
|
-
function
|
|
322
|
+
function N(e, t) {
|
|
323
323
|
if (!(e === void 0 && t === void 0))
|
|
324
324
|
return t === void 0 ? e : e === void 0 ? t : { ...e, ...t };
|
|
325
325
|
}
|
|
326
|
-
function
|
|
326
|
+
function rt(e, t) {
|
|
327
327
|
if (t ??= /* @__PURE__ */ new Map(), !e)
|
|
328
328
|
return t;
|
|
329
329
|
if (e instanceof Map) {
|
|
@@ -335,9 +335,9 @@ function et(e, t) {
|
|
|
335
335
|
t.set(r, s);
|
|
336
336
|
return t;
|
|
337
337
|
}
|
|
338
|
-
class
|
|
338
|
+
class st {
|
|
339
339
|
constructor(t) {
|
|
340
|
-
this.fetcher = t.fetcher, this.request = t.request, this.resultExtractor = t.resultExtractor ??
|
|
340
|
+
this.fetcher = t.fetcher, this.request = t.request, this.resultExtractor = t.resultExtractor ?? I.Exchange, this.attributes = rt(t.attributes), this._response = t.response, this.error = t.error;
|
|
341
341
|
}
|
|
342
342
|
/**
|
|
343
343
|
* Ensures that request headers object exists, creating it if necessary.
|
|
@@ -435,138 +435,14 @@ const c = "Content-Type", d = class d {
|
|
|
435
435
|
};
|
|
436
436
|
d.APPLICATION_JSON = "application/json", d.TEXT_EVENT_STREAM = "text/event-stream";
|
|
437
437
|
let R = d;
|
|
438
|
-
const
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
this.name = st, this.order = D;
|
|
442
|
-
}
|
|
443
|
-
/**
|
|
444
|
-
* Resolves the final URL by combining the base URL, path parameters, and query parameters.
|
|
445
|
-
*
|
|
446
|
-
* @param exchange - The fetch exchange containing the request information
|
|
447
|
-
*/
|
|
448
|
-
intercept(t) {
|
|
449
|
-
const r = t.request;
|
|
450
|
-
r.url = t.fetcher.urlBuilder.resolveRequestUrl(r);
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
const nt = "RequestBodyInterceptor", it = D + 1e3;
|
|
454
|
-
class ut {
|
|
455
|
-
constructor() {
|
|
456
|
-
this.name = nt, this.order = it;
|
|
457
|
-
}
|
|
458
|
-
/**
|
|
459
|
-
* Checks if the provided body is of a supported complex type that doesn't require JSON serialization.
|
|
460
|
-
*
|
|
461
|
-
* @param body - The request body to check
|
|
462
|
-
* @returns True if the body is an ArrayBuffer, TypedArray, DataView or ReadableStream, false otherwise
|
|
463
|
-
*/
|
|
464
|
-
isSupportedComplexBodyType(t) {
|
|
465
|
-
return t instanceof ArrayBuffer || ArrayBuffer.isView(t) || t instanceof ReadableStream;
|
|
466
|
-
}
|
|
467
|
-
/**
|
|
468
|
-
* Checks if the provided body is of a type that automatically appends Content-Type header.
|
|
469
|
-
*
|
|
470
|
-
* @param body - The request body to check
|
|
471
|
-
* @returns True if the body is a Blob, File, FormData or URLSearchParams, false otherwise
|
|
472
|
-
*/
|
|
473
|
-
isAutoAppendContentType(t) {
|
|
474
|
-
return t instanceof Blob || t instanceof File || t instanceof FormData || t instanceof URLSearchParams;
|
|
475
|
-
}
|
|
476
|
-
/**
|
|
477
|
-
* Attempts to convert request body to a valid fetch API body type.
|
|
478
|
-
*
|
|
479
|
-
* This method follows a specific processing order to handle different types of request bodies:
|
|
480
|
-
* 1. Check if the body is null or undefined and return early if so
|
|
481
|
-
* 2. Check if the body is a non-object type and return early if so
|
|
482
|
-
* 3. Check if the body is a type that automatically appends Content-Type header
|
|
483
|
-
* 4. Check if the body is a supported complex type that doesn't require JSON serialization
|
|
484
|
-
* 5. For plain objects, convert to JSON string and set Content-Type header to application/json
|
|
485
|
-
*
|
|
486
|
-
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body}
|
|
487
|
-
*
|
|
488
|
-
* Supported types:
|
|
489
|
-
* - a string
|
|
490
|
-
* - ArrayBuffer
|
|
491
|
-
* - TypedArray
|
|
492
|
-
* - DataView
|
|
493
|
-
* - Blob
|
|
494
|
-
* - File
|
|
495
|
-
* - URLSearchParams
|
|
496
|
-
* - FormData
|
|
497
|
-
* - ReadableStream
|
|
498
|
-
*
|
|
499
|
-
* For unsupported object types (like plain objects), they will be automatically
|
|
500
|
-
* converted to JSON strings.
|
|
501
|
-
*
|
|
502
|
-
* @param exchange - The exchange object containing the request to process
|
|
503
|
-
*
|
|
504
|
-
* @example
|
|
505
|
-
* // Plain object body will be converted to JSON
|
|
506
|
-
* const fetcher = new Fetcher();
|
|
507
|
-
* const exchange = new FetchExchange(
|
|
508
|
-
* fetcher,
|
|
509
|
-
* {
|
|
510
|
-
* body: { name: 'John', age: 30 }
|
|
511
|
-
* }
|
|
512
|
-
* );
|
|
513
|
-
* interceptor.intercept(exchange);
|
|
514
|
-
* // exchange.request.body will be '{"name":"John","age":30}'
|
|
515
|
-
* // exchange.request.headers will include 'Content-Type: application/json'
|
|
516
|
-
*/
|
|
517
|
-
intercept(t) {
|
|
518
|
-
const r = t.request;
|
|
519
|
-
if (r.body === void 0 || r.body === null || typeof r.body != "object")
|
|
520
|
-
return;
|
|
521
|
-
const s = t.ensureRequestHeaders();
|
|
522
|
-
if (this.isAutoAppendContentType(r.body)) {
|
|
523
|
-
s[c] && delete s[c];
|
|
524
|
-
return;
|
|
525
|
-
}
|
|
526
|
-
this.isSupportedComplexBodyType(r.body) || (t.request.body = JSON.stringify(r.body), s[c] || (s[c] = R.APPLICATION_JSON));
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
const at = "FetchInterceptor", ct = Number.MAX_SAFE_INTEGER - 1e3;
|
|
530
|
-
class ht {
|
|
531
|
-
constructor() {
|
|
532
|
-
this.name = at, this.order = ct;
|
|
533
|
-
}
|
|
534
|
-
/**
|
|
535
|
-
* Intercept and process HTTP requests.
|
|
536
|
-
*
|
|
537
|
-
* Executes the actual HTTP request and applies timeout control. This is the final
|
|
538
|
-
* step in the request processing chain, responsible for calling the timeoutFetch
|
|
539
|
-
* function to send the network request.
|
|
540
|
-
*
|
|
541
|
-
* @param exchange - Exchange object containing request information
|
|
542
|
-
*
|
|
543
|
-
* @throws {FetchTimeoutError} Throws timeout exception when request times out
|
|
544
|
-
*
|
|
545
|
-
* @example
|
|
546
|
-
* // Usually called internally by Fetcher
|
|
547
|
-
* const fetcher = new Fetcher();
|
|
548
|
-
* const exchange = new FetchExchange(
|
|
549
|
-
* fetcher,
|
|
550
|
-
* {
|
|
551
|
-
* url: 'https://api.example.com/users',
|
|
552
|
-
* method: 'GET',
|
|
553
|
-
* timeout: 5000
|
|
554
|
-
* }
|
|
555
|
-
* );
|
|
556
|
-
* await fetchInterceptor.intercept(exchange);
|
|
557
|
-
* console.log(exchange.response); // HTTP response object
|
|
558
|
-
*/
|
|
559
|
-
async intercept(t) {
|
|
560
|
-
t.response = await J(t.request);
|
|
561
|
-
}
|
|
562
|
-
}
|
|
563
|
-
const N = 0;
|
|
564
|
-
function x(e, t) {
|
|
565
|
-
return (e.order ?? N) - (t.order ?? N);
|
|
438
|
+
const x = 0;
|
|
439
|
+
function F(e, t) {
|
|
440
|
+
return (e.order ?? x) - (t.order ?? x);
|
|
566
441
|
}
|
|
567
442
|
function f(e, t) {
|
|
568
|
-
return t ? e.filter(t).sort(
|
|
443
|
+
return t ? e.filter(t).sort(F) : [...e].sort(F);
|
|
569
444
|
}
|
|
445
|
+
const ot = 1e3, q = ot * 10;
|
|
570
446
|
class m {
|
|
571
447
|
/**
|
|
572
448
|
* Initializes a new InterceptorRegistry instance.
|
|
@@ -660,22 +536,147 @@ class m {
|
|
|
660
536
|
await r.intercept(t);
|
|
661
537
|
}
|
|
662
538
|
}
|
|
663
|
-
|
|
539
|
+
const nt = "FetchInterceptor", L = Number.MAX_SAFE_INTEGER - q;
|
|
540
|
+
class it {
|
|
541
|
+
constructor() {
|
|
542
|
+
this.name = nt, this.order = L;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Intercept and process HTTP requests.
|
|
546
|
+
*
|
|
547
|
+
* Executes the actual HTTP request and applies timeout control. This is the final
|
|
548
|
+
* step in the request processing chain, responsible for calling the timeoutFetch
|
|
549
|
+
* function to send the network request.
|
|
550
|
+
*
|
|
551
|
+
* @param exchange - Exchange object containing request information
|
|
552
|
+
*
|
|
553
|
+
* @throws {FetchTimeoutError} Throws timeout exception when request times out
|
|
554
|
+
*
|
|
555
|
+
* @example
|
|
556
|
+
* // Usually called internally by Fetcher
|
|
557
|
+
* const fetcher = new Fetcher();
|
|
558
|
+
* const exchange = new FetchExchange(
|
|
559
|
+
* fetcher,
|
|
560
|
+
* {
|
|
561
|
+
* url: 'https://api.example.com/users',
|
|
562
|
+
* method: 'GET',
|
|
563
|
+
* timeout: 5000
|
|
564
|
+
* }
|
|
565
|
+
* );
|
|
566
|
+
* await fetchInterceptor.intercept(exchange);
|
|
567
|
+
* console.log(exchange.response); // HTTP response object
|
|
568
|
+
*/
|
|
569
|
+
async intercept(t) {
|
|
570
|
+
t.response = await Q(t.request);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
const ut = "UrlResolveInterceptor", at = L - q;
|
|
574
|
+
class ct {
|
|
575
|
+
constructor() {
|
|
576
|
+
this.name = ut, this.order = at;
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Resolves the final URL by combining the base URL, path parameters, and query parameters.
|
|
580
|
+
*
|
|
581
|
+
* @param exchange - The fetch exchange containing the request information
|
|
582
|
+
*/
|
|
583
|
+
intercept(t) {
|
|
584
|
+
const r = t.request;
|
|
585
|
+
r.url = t.fetcher.urlBuilder.resolveRequestUrl(r);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
const ht = "RequestBodyInterceptor", lt = Number.MIN_SAFE_INTEGER + q;
|
|
589
|
+
class Et {
|
|
590
|
+
constructor() {
|
|
591
|
+
this.name = ht, this.order = lt;
|
|
592
|
+
}
|
|
593
|
+
/**
|
|
594
|
+
* Checks if the provided body is of a supported complex type that doesn't require JSON serialization.
|
|
595
|
+
*
|
|
596
|
+
* @param body - The request body to check
|
|
597
|
+
* @returns True if the body is an ArrayBuffer, TypedArray, DataView or ReadableStream, false otherwise
|
|
598
|
+
*/
|
|
599
|
+
isSupportedComplexBodyType(t) {
|
|
600
|
+
return t instanceof ArrayBuffer || ArrayBuffer.isView(t) || t instanceof ReadableStream;
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Checks if the provided body is of a type that automatically appends Content-Type header.
|
|
604
|
+
*
|
|
605
|
+
* @param body - The request body to check
|
|
606
|
+
* @returns True if the body is a Blob, File, FormData or URLSearchParams, false otherwise
|
|
607
|
+
*/
|
|
608
|
+
isAutoAppendContentType(t) {
|
|
609
|
+
return t instanceof Blob || t instanceof File || t instanceof FormData || t instanceof URLSearchParams;
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Attempts to convert request body to a valid fetch API body type.
|
|
613
|
+
*
|
|
614
|
+
* This method follows a specific processing order to handle different types of request bodies:
|
|
615
|
+
* 1. Check if the body is null or undefined and return early if so
|
|
616
|
+
* 2. Check if the body is a non-object type and return early if so
|
|
617
|
+
* 3. Check if the body is a type that automatically appends Content-Type header
|
|
618
|
+
* 4. Check if the body is a supported complex type that doesn't require JSON serialization
|
|
619
|
+
* 5. For plain objects, convert to JSON string and set Content-Type header to application/json
|
|
620
|
+
*
|
|
621
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#setting_a_body}
|
|
622
|
+
*
|
|
623
|
+
* Supported types:
|
|
624
|
+
* - a string
|
|
625
|
+
* - ArrayBuffer
|
|
626
|
+
* - TypedArray
|
|
627
|
+
* - DataView
|
|
628
|
+
* - Blob
|
|
629
|
+
* - File
|
|
630
|
+
* - URLSearchParams
|
|
631
|
+
* - FormData
|
|
632
|
+
* - ReadableStream
|
|
633
|
+
*
|
|
634
|
+
* For unsupported object types (like plain objects), they will be automatically
|
|
635
|
+
* converted to JSON strings.
|
|
636
|
+
*
|
|
637
|
+
* @param exchange - The exchange object containing the request to process
|
|
638
|
+
*
|
|
639
|
+
* @example
|
|
640
|
+
* // Plain object body will be converted to JSON
|
|
641
|
+
* const fetcher = new Fetcher();
|
|
642
|
+
* const exchange = new FetchExchange(
|
|
643
|
+
* fetcher,
|
|
644
|
+
* {
|
|
645
|
+
* body: { name: 'John', age: 30 }
|
|
646
|
+
* }
|
|
647
|
+
* );
|
|
648
|
+
* interceptor.intercept(exchange);
|
|
649
|
+
* // exchange.request.body will be '{"name":"John","age":30}'
|
|
650
|
+
* // exchange.request.headers will include 'Content-Type: application/json'
|
|
651
|
+
*/
|
|
652
|
+
intercept(t) {
|
|
653
|
+
const r = t.request;
|
|
654
|
+
if (r.body === void 0 || r.body === null || typeof r.body != "object")
|
|
655
|
+
return;
|
|
656
|
+
const s = t.ensureRequestHeaders();
|
|
657
|
+
if (this.isAutoAppendContentType(r.body)) {
|
|
658
|
+
s[c] && delete s[c];
|
|
659
|
+
return;
|
|
660
|
+
}
|
|
661
|
+
this.isSupportedComplexBodyType(r.body) || (t.request.body = JSON.stringify(r.body), s[c] || (s[c] = R.APPLICATION_JSON));
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
class b extends E {
|
|
664
665
|
constructor(t) {
|
|
665
666
|
super(
|
|
666
667
|
t,
|
|
667
668
|
`Request failed with status code ${t.response?.status} for ${t.request.url}`
|
|
668
|
-
), this.name = "HttpStatusValidationError", Object.setPrototypeOf(this,
|
|
669
|
+
), this.name = "HttpStatusValidationError", Object.setPrototypeOf(this, b.prototype);
|
|
669
670
|
}
|
|
670
671
|
}
|
|
671
|
-
const
|
|
672
|
-
class
|
|
672
|
+
const Rt = (e) => e >= 200 && e < 300, dt = "ValidateStatusInterceptor", pt = Number.MAX_SAFE_INTEGER - 1e3, Tt = "__ignoreValidateStatus__";
|
|
673
|
+
class ft {
|
|
673
674
|
/**
|
|
674
675
|
* Creates a new ValidateStatusInterceptor instance.
|
|
675
676
|
*
|
|
676
677
|
* @param validateStatus - Function that determines if a status code is valid
|
|
677
678
|
*/
|
|
678
|
-
constructor(t =
|
|
679
|
+
constructor(t = Rt) {
|
|
679
680
|
this.validateStatus = t;
|
|
680
681
|
}
|
|
681
682
|
/**
|
|
@@ -684,7 +685,7 @@ class pt {
|
|
|
684
685
|
* @returns The name of this interceptor
|
|
685
686
|
*/
|
|
686
687
|
get name() {
|
|
687
|
-
return
|
|
688
|
+
return dt;
|
|
688
689
|
}
|
|
689
690
|
/**
|
|
690
691
|
* Gets the order of this interceptor.
|
|
@@ -692,7 +693,7 @@ class pt {
|
|
|
692
693
|
* @returns VALIDATE_STATUS_INTERCEPTOR_ORDER, indicating this interceptor should execute early
|
|
693
694
|
*/
|
|
694
695
|
get order() {
|
|
695
|
-
return
|
|
696
|
+
return pt;
|
|
696
697
|
}
|
|
697
698
|
/**
|
|
698
699
|
* Validates the response status code.
|
|
@@ -711,21 +712,21 @@ class pt {
|
|
|
711
712
|
* validation is skipped entirely, allowing any response status to pass through.
|
|
712
713
|
*/
|
|
713
714
|
intercept(t) {
|
|
714
|
-
if (t.attributes.get(
|
|
715
|
+
if (t.attributes.get(Tt) === !0 || !t.response)
|
|
715
716
|
return;
|
|
716
717
|
const r = t.response.status;
|
|
717
718
|
if (!this.validateStatus(r))
|
|
718
|
-
throw new
|
|
719
|
+
throw new b(t);
|
|
719
720
|
}
|
|
720
721
|
}
|
|
721
|
-
class
|
|
722
|
+
class mt {
|
|
722
723
|
constructor() {
|
|
723
724
|
this.request = new m([
|
|
724
|
-
new
|
|
725
|
-
new
|
|
726
|
-
new
|
|
725
|
+
new Et(),
|
|
726
|
+
new ct(),
|
|
727
|
+
new it()
|
|
727
728
|
]), this.response = new m([
|
|
728
|
-
new
|
|
729
|
+
new ft()
|
|
729
730
|
]), this.error = new m();
|
|
730
731
|
}
|
|
731
732
|
/**
|
|
@@ -824,14 +825,14 @@ class Tt {
|
|
|
824
825
|
}
|
|
825
826
|
}
|
|
826
827
|
}
|
|
827
|
-
function
|
|
828
|
+
function _t(e, t) {
|
|
828
829
|
if (Object.keys(e).length === 0)
|
|
829
830
|
return t;
|
|
830
831
|
if (Object.keys(t).length === 0)
|
|
831
832
|
return e;
|
|
832
833
|
const r = {
|
|
833
|
-
path:
|
|
834
|
-
query:
|
|
834
|
+
path: N(e.urlParams?.path, t.urlParams?.path),
|
|
835
|
+
query: N(e.urlParams?.query, t.urlParams?.query)
|
|
835
836
|
}, s = {
|
|
836
837
|
...e.headers,
|
|
837
838
|
...t.headers
|
|
@@ -850,21 +851,21 @@ function At(e, t) {
|
|
|
850
851
|
}
|
|
851
852
|
function A(e, t) {
|
|
852
853
|
return t && t.resultExtractor && t.attributes ? t : {
|
|
853
|
-
resultExtractor: t?.resultExtractor ?? e?.resultExtractor ??
|
|
854
|
+
resultExtractor: t?.resultExtractor ?? e?.resultExtractor ?? B.resultExtractor,
|
|
854
855
|
attributes: t?.attributes ?? e?.attributes
|
|
855
856
|
};
|
|
856
857
|
}
|
|
857
|
-
const
|
|
858
|
+
const y = {
|
|
858
859
|
[c]: R.APPLICATION_JSON
|
|
859
860
|
}, v = {
|
|
860
861
|
baseURL: "",
|
|
861
|
-
headers:
|
|
862
|
-
},
|
|
863
|
-
resultExtractor:
|
|
864
|
-
},
|
|
865
|
-
resultExtractor:
|
|
862
|
+
headers: y
|
|
863
|
+
}, B = {
|
|
864
|
+
resultExtractor: I.Exchange
|
|
865
|
+
}, C = {
|
|
866
|
+
resultExtractor: I.Response
|
|
866
867
|
};
|
|
867
|
-
class
|
|
868
|
+
class G {
|
|
868
869
|
/**
|
|
869
870
|
* Initializes a new Fetcher instance with optional configuration.
|
|
870
871
|
*
|
|
@@ -879,7 +880,7 @@ class B {
|
|
|
879
880
|
* @param options.interceptors - Interceptor manager for processing requests and responses.
|
|
880
881
|
*/
|
|
881
882
|
constructor(t = v) {
|
|
882
|
-
this.headers =
|
|
883
|
+
this.headers = y, this.urlBuilder = new k(t.baseURL, t.urlTemplateStyle), this.headers = t.headers ?? y, this.timeout = t.timeout, this.interceptors = t.interceptors ?? new mt();
|
|
883
884
|
}
|
|
884
885
|
/**
|
|
885
886
|
* Resolves a FetchRequest and RequestOptions into a FetchExchange object.
|
|
@@ -908,12 +909,12 @@ class B {
|
|
|
908
909
|
}, o = {
|
|
909
910
|
...t,
|
|
910
911
|
headers: s,
|
|
911
|
-
timeout:
|
|
912
|
+
timeout: J(t.timeout, this.timeout)
|
|
912
913
|
}, { resultExtractor: n, attributes: u } = A(
|
|
913
|
-
|
|
914
|
+
B,
|
|
914
915
|
r
|
|
915
916
|
);
|
|
916
|
-
return new
|
|
917
|
+
return new st({
|
|
917
918
|
fetcher: this,
|
|
918
919
|
request: o,
|
|
919
920
|
resultExtractor: n,
|
|
@@ -980,7 +981,7 @@ class B {
|
|
|
980
981
|
};
|
|
981
982
|
return await this.request(
|
|
982
983
|
o,
|
|
983
|
-
A(
|
|
984
|
+
A(C, s)
|
|
984
985
|
);
|
|
985
986
|
}
|
|
986
987
|
/**
|
|
@@ -1009,7 +1010,7 @@ class B {
|
|
|
1009
1010
|
};
|
|
1010
1011
|
return await this.request(
|
|
1011
1012
|
n,
|
|
1012
|
-
A(
|
|
1013
|
+
A(C, o)
|
|
1013
1014
|
);
|
|
1014
1015
|
}
|
|
1015
1016
|
/**
|
|
@@ -1168,7 +1169,7 @@ class B {
|
|
|
1168
1169
|
}
|
|
1169
1170
|
}
|
|
1170
1171
|
const w = "default";
|
|
1171
|
-
class
|
|
1172
|
+
class At {
|
|
1172
1173
|
constructor() {
|
|
1173
1174
|
this.registrar = /* @__PURE__ */ new Map();
|
|
1174
1175
|
}
|
|
@@ -1268,11 +1269,11 @@ class ft {
|
|
|
1268
1269
|
return new Map(this.registrar);
|
|
1269
1270
|
}
|
|
1270
1271
|
}
|
|
1271
|
-
const
|
|
1272
|
-
function
|
|
1273
|
-
return e ? e instanceof
|
|
1272
|
+
const O = new At();
|
|
1273
|
+
function yt(e, t) {
|
|
1274
|
+
return e ? e instanceof G ? e : O.requiredGet(e) : t ?? O.default;
|
|
1274
1275
|
}
|
|
1275
|
-
class
|
|
1276
|
+
class Pt extends G {
|
|
1276
1277
|
/**
|
|
1277
1278
|
* Create a NamedFetcher instance and automatically register it with the global fetcherRegistrar
|
|
1278
1279
|
*
|
|
@@ -1291,70 +1292,72 @@ class mt extends B {
|
|
|
1291
1292
|
* });
|
|
1292
1293
|
*/
|
|
1293
1294
|
constructor(t, r = v) {
|
|
1294
|
-
super(r), this.name = t,
|
|
1295
|
+
super(r), this.name = t, O.register(t, this);
|
|
1295
1296
|
}
|
|
1296
1297
|
}
|
|
1297
|
-
const
|
|
1298
|
+
const wt = new Pt(w);
|
|
1298
1299
|
export {
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1300
|
+
tt as ArrayBufferResultExtractor,
|
|
1301
|
+
q as BUILT_IN_INTERCEPTOR_ORDER_STEP,
|
|
1302
|
+
Z as BlobResultExtractor,
|
|
1303
|
+
et as BytesResultExtractor,
|
|
1302
1304
|
c as CONTENT_TYPE_HEADER,
|
|
1303
1305
|
R as ContentTypeValues,
|
|
1304
1306
|
w as DEFAULT_FETCHER_NAME,
|
|
1305
|
-
|
|
1307
|
+
C as DEFAULT_FETCH_OPTIONS,
|
|
1308
|
+
ot as DEFAULT_INTERCEPTOR_ORDER_STEP,
|
|
1306
1309
|
v as DEFAULT_OPTIONS,
|
|
1307
|
-
|
|
1310
|
+
B as DEFAULT_REQUEST_OPTIONS,
|
|
1308
1311
|
E as ExchangeError,
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1312
|
+
Y as ExchangeResultExtractor,
|
|
1313
|
+
_ as ExpressUrlTemplateResolver,
|
|
1314
|
+
nt as FETCH_INTERCEPTOR_NAME,
|
|
1315
|
+
L as FETCH_INTERCEPTOR_ORDER,
|
|
1316
|
+
st as FetchExchange,
|
|
1317
|
+
it as FetchInterceptor,
|
|
1318
|
+
g as FetchTimeoutError,
|
|
1319
|
+
G as Fetcher,
|
|
1317
1320
|
p as FetcherError,
|
|
1318
|
-
|
|
1321
|
+
At as FetcherRegistrar,
|
|
1319
1322
|
i as HttpMethod,
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
+
b as HttpStatusValidationError,
|
|
1324
|
+
Tt as IGNORE_VALIDATE_STATUS,
|
|
1325
|
+
mt as InterceptorManager,
|
|
1323
1326
|
m as InterceptorRegistry,
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1327
|
+
K as JsonResultExtractor,
|
|
1328
|
+
Pt as NamedFetcher,
|
|
1329
|
+
ht as REQUEST_BODY_INTERCEPTOR_NAME,
|
|
1330
|
+
lt as REQUEST_BODY_INTERCEPTOR_ORDER,
|
|
1331
|
+
Et as RequestBodyInterceptor,
|
|
1332
|
+
z as ResponseResultExtractor,
|
|
1333
|
+
I as ResultExtractors,
|
|
1334
|
+
W as TextResultExtractor,
|
|
1335
|
+
ut as URL_RESOLVE_INTERCEPTOR_NAME,
|
|
1336
|
+
at as URL_RESOLVE_INTERCEPTOR_ORDER,
|
|
1334
1337
|
P as UriTemplateResolver,
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1350
|
-
|
|
1338
|
+
k as UrlBuilder,
|
|
1339
|
+
ct as UrlResolveInterceptor,
|
|
1340
|
+
j as UrlTemplateStyle,
|
|
1341
|
+
dt as VALIDATE_STATUS_INTERCEPTOR_NAME,
|
|
1342
|
+
pt as VALIDATE_STATUS_INTERCEPTOR_ORDER,
|
|
1343
|
+
ft as ValidateStatusInterceptor,
|
|
1344
|
+
H as combineURLs,
|
|
1345
|
+
X as expressUrlTemplateResolver,
|
|
1346
|
+
wt as fetcher,
|
|
1347
|
+
O as fetcherRegistrar,
|
|
1348
|
+
yt as getFetcher,
|
|
1349
|
+
$ as getUrlTemplateResolver,
|
|
1350
|
+
M as isAbsoluteURL,
|
|
1351
|
+
rt as mergeRecordToMap,
|
|
1352
|
+
N as mergeRecords,
|
|
1353
|
+
_t as mergeRequest,
|
|
1351
1354
|
A as mergeRequestOptions,
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
+
J as resolveTimeout,
|
|
1356
|
+
F as sortOrder,
|
|
1357
|
+
Q as timeoutFetch,
|
|
1355
1358
|
f as toSorted,
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
+
V as uriTemplateResolver,
|
|
1360
|
+
D as urlTemplateRegexExtract,
|
|
1361
|
+
U as urlTemplateRegexResolve
|
|
1359
1362
|
};
|
|
1360
1363
|
//# sourceMappingURL=index.es.js.map
|