@ahoo-wang/fetcher 1.1.0 → 1.2.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/index.es.js +100 -120
- 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/urlBuilder.d.ts +0 -28
- package/dist/urlBuilder.d.ts.map +1 -1
- package/dist/urls.d.ts +47 -0
- package/dist/urls.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.es.js
CHANGED
|
@@ -1,10 +1,23 @@
|
|
|
1
|
-
function
|
|
1
|
+
function _(r) {
|
|
2
2
|
return /^([a-z][a-z\d+\-.]*:)?\/\//i.test(r);
|
|
3
3
|
}
|
|
4
4
|
function b(r, t) {
|
|
5
|
-
return
|
|
5
|
+
return _(t) ? t : t ? r.replace(/\/?\/$/, "") + "/" + t.replace(/^\/+/, "") : r;
|
|
6
6
|
}
|
|
7
|
-
|
|
7
|
+
const A = /{([^}]+)}/g;
|
|
8
|
+
function Q(r) {
|
|
9
|
+
const t = r.match(A);
|
|
10
|
+
return t ? t.map((e) => e.slice(1, -1)) : [];
|
|
11
|
+
}
|
|
12
|
+
function S(r, t) {
|
|
13
|
+
return t ? r.replace(A, (e, s) => {
|
|
14
|
+
const o = t[s];
|
|
15
|
+
if (o === void 0)
|
|
16
|
+
throw new Error(`Missing required path parameter: ${s}`);
|
|
17
|
+
return String(o);
|
|
18
|
+
}) : r;
|
|
19
|
+
}
|
|
20
|
+
class w {
|
|
8
21
|
/**
|
|
9
22
|
* Initializes a new UrlBuilder instance.
|
|
10
23
|
*
|
|
@@ -37,11 +50,11 @@ class S {
|
|
|
37
50
|
* ```
|
|
38
51
|
*/
|
|
39
52
|
build(t, e) {
|
|
40
|
-
const s = e?.path, o = e?.query,
|
|
41
|
-
let n =
|
|
53
|
+
const s = e?.path, o = e?.query, u = b(this.baseURL, t);
|
|
54
|
+
let n = S(u, s);
|
|
42
55
|
if (o) {
|
|
43
|
-
const
|
|
44
|
-
|
|
56
|
+
const c = new URLSearchParams(o).toString();
|
|
57
|
+
c && (n += "?" + c);
|
|
45
58
|
}
|
|
46
59
|
return n;
|
|
47
60
|
}
|
|
@@ -57,41 +70,6 @@ class S {
|
|
|
57
70
|
resolveRequestUrl(t) {
|
|
58
71
|
return this.build(t.url, t.urlParams);
|
|
59
72
|
}
|
|
60
|
-
/**
|
|
61
|
-
* Replaces placeholders in the URL with path parameters.
|
|
62
|
-
*
|
|
63
|
-
* @param url - Path string containing placeholders, e.g., "http://localhost/users/{id}/posts/{postId}"
|
|
64
|
-
* @param path - Path parameter object used to replace placeholders in the URL
|
|
65
|
-
* @returns Path string with placeholders replaced
|
|
66
|
-
* @throws Error when required path parameters are missing
|
|
67
|
-
*
|
|
68
|
-
* @example
|
|
69
|
-
* ```typescript
|
|
70
|
-
* const urlBuilder = new UrlBuilder('https://api.example.com');
|
|
71
|
-
* const result = urlBuilder.interpolateUrl('/users/{id}/posts/{postId}', {
|
|
72
|
-
* path: { id: 123, postId: 456 }
|
|
73
|
-
* });
|
|
74
|
-
* // Result: https://api.example.com/users/123/posts/456
|
|
75
|
-
* ```
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* ```typescript
|
|
79
|
-
* // Missing required parameter throws an error
|
|
80
|
-
* try {
|
|
81
|
-
* urlBuilder.interpolateUrl('/users/{id}', { name: 'John' });
|
|
82
|
-
* } catch (error) {
|
|
83
|
-
* console.error(error.message); // "Missing required path parameter: id"
|
|
84
|
-
* }
|
|
85
|
-
* ```
|
|
86
|
-
*/
|
|
87
|
-
interpolateUrl(t, e) {
|
|
88
|
-
return e ? t.replace(/{([^}]+)}/g, (s, o) => {
|
|
89
|
-
const i = e[o];
|
|
90
|
-
if (i === void 0)
|
|
91
|
-
throw new Error(`Missing required path parameter: ${o}`);
|
|
92
|
-
return String(i);
|
|
93
|
-
}) : t;
|
|
94
|
-
}
|
|
95
73
|
}
|
|
96
74
|
class a extends Error {
|
|
97
75
|
/**
|
|
@@ -116,35 +94,35 @@ class R extends a {
|
|
|
116
94
|
super(s), this.name = "FetchTimeoutError", this.request = t, Object.setPrototypeOf(this, R.prototype);
|
|
117
95
|
}
|
|
118
96
|
}
|
|
119
|
-
function
|
|
97
|
+
function N(r, t) {
|
|
120
98
|
return typeof r < "u" ? r : t;
|
|
121
99
|
}
|
|
122
|
-
async function
|
|
100
|
+
async function F(r) {
|
|
123
101
|
const t = r.url, e = r.timeout, s = r;
|
|
124
102
|
if (!e)
|
|
125
103
|
return fetch(t, s);
|
|
126
|
-
const o = new AbortController(),
|
|
104
|
+
const o = new AbortController(), u = {
|
|
127
105
|
...s,
|
|
128
106
|
signal: o.signal
|
|
129
107
|
};
|
|
130
108
|
let n = null;
|
|
131
|
-
const
|
|
109
|
+
const c = new Promise((z, g) => {
|
|
132
110
|
n = setTimeout(() => {
|
|
133
111
|
n && clearTimeout(n);
|
|
134
112
|
const y = new R(r);
|
|
135
|
-
o.abort(y),
|
|
113
|
+
o.abort(y), g(y);
|
|
136
114
|
}, e);
|
|
137
115
|
});
|
|
138
116
|
try {
|
|
139
|
-
return await Promise.race([fetch(t,
|
|
117
|
+
return await Promise.race([fetch(t, u), c]);
|
|
140
118
|
} finally {
|
|
141
119
|
n && clearTimeout(n);
|
|
142
120
|
}
|
|
143
121
|
}
|
|
144
|
-
const
|
|
145
|
-
class
|
|
122
|
+
const q = "UrlResolveInterceptor", P = Number.MIN_SAFE_INTEGER + 1e3;
|
|
123
|
+
class U {
|
|
146
124
|
constructor() {
|
|
147
|
-
this.name =
|
|
125
|
+
this.name = q, this.order = P;
|
|
148
126
|
}
|
|
149
127
|
/**
|
|
150
128
|
* Resolves the final URL by combining the base URL, path parameters, and query parameters.
|
|
@@ -156,15 +134,15 @@ class F {
|
|
|
156
134
|
e.url = t.fetcher.urlBuilder.resolveRequestUrl(e);
|
|
157
135
|
}
|
|
158
136
|
}
|
|
159
|
-
var
|
|
160
|
-
const
|
|
137
|
+
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 || {});
|
|
138
|
+
const K = "Content-Type", d = class d {
|
|
161
139
|
};
|
|
162
140
|
d.APPLICATION_JSON = "application/json", d.TEXT_EVENT_STREAM = "text/event-stream";
|
|
163
141
|
let h = d;
|
|
164
|
-
const
|
|
165
|
-
class
|
|
142
|
+
const D = "RequestBodyInterceptor", L = P + 1e3;
|
|
143
|
+
class C {
|
|
166
144
|
constructor() {
|
|
167
|
-
this.name =
|
|
145
|
+
this.name = D, this.order = L;
|
|
168
146
|
}
|
|
169
147
|
/**
|
|
170
148
|
* Attempts to convert request body to a valid fetch API body type.
|
|
@@ -214,10 +192,10 @@ class D {
|
|
|
214
192
|
o["Content-Type"] || (o["Content-Type"] = h.APPLICATION_JSON), t.request = s;
|
|
215
193
|
}
|
|
216
194
|
}
|
|
217
|
-
const
|
|
218
|
-
class
|
|
195
|
+
const v = "FetchInterceptor", G = Number.MAX_SAFE_INTEGER - 1e3;
|
|
196
|
+
class M {
|
|
219
197
|
constructor() {
|
|
220
|
-
this.name =
|
|
198
|
+
this.name = v, this.order = G;
|
|
221
199
|
}
|
|
222
200
|
/**
|
|
223
201
|
* Intercept and process HTTP requests.
|
|
@@ -245,7 +223,7 @@ class v {
|
|
|
245
223
|
* console.log(exchange.response); // HTTP response object
|
|
246
224
|
*/
|
|
247
225
|
async intercept(t) {
|
|
248
|
-
t.response = await
|
|
226
|
+
t.response = await F(t.request);
|
|
249
227
|
}
|
|
250
228
|
}
|
|
251
229
|
function E(r, t) {
|
|
@@ -351,8 +329,8 @@ class f extends a {
|
|
|
351
329
|
), this.exchange = t, this.name = "HttpStatusValidationError", Object.setPrototypeOf(this, f.prototype);
|
|
352
330
|
}
|
|
353
331
|
}
|
|
354
|
-
const B = (r) => r >= 200 && r < 300,
|
|
355
|
-
class
|
|
332
|
+
const B = (r) => r >= 200 && r < 300, $ = "ValidateStatusInterceptor", j = Number.MAX_SAFE_INTEGER - 1e3;
|
|
333
|
+
class H {
|
|
356
334
|
/**
|
|
357
335
|
* Creates a new ValidateStatusInterceptor instance.
|
|
358
336
|
*
|
|
@@ -367,7 +345,7 @@ class $ {
|
|
|
367
345
|
* @returns The name of this interceptor
|
|
368
346
|
*/
|
|
369
347
|
get name() {
|
|
370
|
-
return
|
|
348
|
+
return $;
|
|
371
349
|
}
|
|
372
350
|
/**
|
|
373
351
|
* Gets the order of this interceptor.
|
|
@@ -375,7 +353,7 @@ class $ {
|
|
|
375
353
|
* @returns VALIDATE_STATUS_INTERCEPTOR_ORDER, indicating this interceptor should execute early
|
|
376
354
|
*/
|
|
377
355
|
get order() {
|
|
378
|
-
return
|
|
356
|
+
return j;
|
|
379
357
|
}
|
|
380
358
|
/**
|
|
381
359
|
* Validates the response status code.
|
|
@@ -410,14 +388,14 @@ class l extends a {
|
|
|
410
388
|
super(s, t.error), this.exchange = t, this.name = "ExchangeError", Object.setPrototypeOf(this, l.prototype);
|
|
411
389
|
}
|
|
412
390
|
}
|
|
413
|
-
class
|
|
391
|
+
class V {
|
|
414
392
|
constructor() {
|
|
415
393
|
this.request = new p([
|
|
416
|
-
new
|
|
417
|
-
new
|
|
418
|
-
new
|
|
394
|
+
new U(),
|
|
395
|
+
new C(),
|
|
396
|
+
new M()
|
|
419
397
|
]), this.response = new p([
|
|
420
|
-
new
|
|
398
|
+
new H()
|
|
421
399
|
]), this.error = new p();
|
|
422
400
|
}
|
|
423
401
|
/**
|
|
@@ -516,7 +494,7 @@ class j {
|
|
|
516
494
|
}
|
|
517
495
|
}
|
|
518
496
|
}
|
|
519
|
-
class
|
|
497
|
+
class k {
|
|
520
498
|
constructor(t, e, s, o) {
|
|
521
499
|
this.attributes = {}, this.fetcher = t, this.request = e, this.response = s, this.error = o;
|
|
522
500
|
}
|
|
@@ -557,11 +535,11 @@ class H {
|
|
|
557
535
|
}
|
|
558
536
|
const m = {
|
|
559
537
|
"Content-Type": h.APPLICATION_JSON
|
|
560
|
-
},
|
|
538
|
+
}, O = {
|
|
561
539
|
baseURL: "",
|
|
562
540
|
headers: m
|
|
563
541
|
};
|
|
564
|
-
class
|
|
542
|
+
class J {
|
|
565
543
|
/**
|
|
566
544
|
* Initializes a new Fetcher instance with optional configuration.
|
|
567
545
|
*
|
|
@@ -570,8 +548,8 @@ class V {
|
|
|
570
548
|
*
|
|
571
549
|
* @param options - Configuration options for the Fetcher instance
|
|
572
550
|
*/
|
|
573
|
-
constructor(t =
|
|
574
|
-
this.headers = m, this.urlBuilder = new
|
|
551
|
+
constructor(t = O) {
|
|
552
|
+
this.headers = m, this.urlBuilder = new w(t.baseURL), this.headers = t.headers ?? m, this.timeout = t.timeout, this.interceptors = t.interceptors ?? new V();
|
|
575
553
|
}
|
|
576
554
|
/**
|
|
577
555
|
* Executes an HTTP request with the specified URL and options.
|
|
@@ -606,8 +584,8 @@ class V {
|
|
|
606
584
|
}, s = {
|
|
607
585
|
...t,
|
|
608
586
|
headers: e,
|
|
609
|
-
timeout:
|
|
610
|
-
}, o = new
|
|
587
|
+
timeout: N(t.timeout, this.timeout)
|
|
588
|
+
}, o = new k(this, s);
|
|
611
589
|
return this.interceptors.exchange(o);
|
|
612
590
|
}
|
|
613
591
|
/**
|
|
@@ -638,7 +616,7 @@ class V {
|
|
|
638
616
|
* @returns Promise that resolves to the HTTP response
|
|
639
617
|
*/
|
|
640
618
|
async get(t, e = {}) {
|
|
641
|
-
return this.methodFetch(
|
|
619
|
+
return this.methodFetch(i.GET, t, e);
|
|
642
620
|
}
|
|
643
621
|
/**
|
|
644
622
|
* Makes a POST HTTP request.
|
|
@@ -650,7 +628,7 @@ class V {
|
|
|
650
628
|
* @returns Promise that resolves to the HTTP response
|
|
651
629
|
*/
|
|
652
630
|
async post(t, e = {}) {
|
|
653
|
-
return this.methodFetch(
|
|
631
|
+
return this.methodFetch(i.POST, t, e);
|
|
654
632
|
}
|
|
655
633
|
/**
|
|
656
634
|
* Makes a PUT HTTP request.
|
|
@@ -662,7 +640,7 @@ class V {
|
|
|
662
640
|
* @returns Promise that resolves to the HTTP response
|
|
663
641
|
*/
|
|
664
642
|
async put(t, e = {}) {
|
|
665
|
-
return this.methodFetch(
|
|
643
|
+
return this.methodFetch(i.PUT, t, e);
|
|
666
644
|
}
|
|
667
645
|
/**
|
|
668
646
|
* Makes a DELETE HTTP request.
|
|
@@ -674,7 +652,7 @@ class V {
|
|
|
674
652
|
* @returns Promise that resolves to the HTTP response
|
|
675
653
|
*/
|
|
676
654
|
async delete(t, e = {}) {
|
|
677
|
-
return this.methodFetch(
|
|
655
|
+
return this.methodFetch(i.DELETE, t, e);
|
|
678
656
|
}
|
|
679
657
|
/**
|
|
680
658
|
* Makes a PATCH HTTP request.
|
|
@@ -686,7 +664,7 @@ class V {
|
|
|
686
664
|
* @returns Promise that resolves to the HTTP response
|
|
687
665
|
*/
|
|
688
666
|
async patch(t, e = {}) {
|
|
689
|
-
return this.methodFetch(
|
|
667
|
+
return this.methodFetch(i.PATCH, t, e);
|
|
690
668
|
}
|
|
691
669
|
/**
|
|
692
670
|
* Makes a HEAD HTTP request.
|
|
@@ -699,7 +677,7 @@ class V {
|
|
|
699
677
|
* @returns Promise that resolves to the HTTP response
|
|
700
678
|
*/
|
|
701
679
|
async head(t, e = {}) {
|
|
702
|
-
return this.methodFetch(
|
|
680
|
+
return this.methodFetch(i.HEAD, t, e);
|
|
703
681
|
}
|
|
704
682
|
/**
|
|
705
683
|
* Makes an OPTIONS HTTP request.
|
|
@@ -712,11 +690,11 @@ class V {
|
|
|
712
690
|
* @returns Promise that resolves to the HTTP response
|
|
713
691
|
*/
|
|
714
692
|
async options(t, e = {}) {
|
|
715
|
-
return this.methodFetch(
|
|
693
|
+
return this.methodFetch(i.OPTIONS, t, e);
|
|
716
694
|
}
|
|
717
695
|
}
|
|
718
696
|
const T = "default";
|
|
719
|
-
class
|
|
697
|
+
class X {
|
|
720
698
|
constructor() {
|
|
721
699
|
this.registrar = /* @__PURE__ */ new Map();
|
|
722
700
|
}
|
|
@@ -816,12 +794,12 @@ class k {
|
|
|
816
794
|
return new Map(this.registrar);
|
|
817
795
|
}
|
|
818
796
|
}
|
|
819
|
-
const
|
|
797
|
+
const Y = new X();
|
|
820
798
|
function I(r, t) {
|
|
821
799
|
if (!(r === void 0 && t === void 0))
|
|
822
800
|
return t === void 0 ? r : r === void 0 ? t : { ...r, ...t };
|
|
823
801
|
}
|
|
824
|
-
function
|
|
802
|
+
function W(r, t) {
|
|
825
803
|
if (Object.keys(r).length === 0)
|
|
826
804
|
return t;
|
|
827
805
|
if (Object.keys(t).length === 0)
|
|
@@ -832,19 +810,19 @@ function Q(r, t) {
|
|
|
832
810
|
}, s = {
|
|
833
811
|
...r.headers,
|
|
834
812
|
...t.headers
|
|
835
|
-
}, o = t.method ?? r.method,
|
|
813
|
+
}, o = t.method ?? r.method, u = t.body ?? r.body, n = t.timeout ?? r.timeout, c = t.signal ?? r.signal;
|
|
836
814
|
return {
|
|
837
815
|
...r,
|
|
838
816
|
...t,
|
|
839
817
|
method: o,
|
|
840
818
|
urlParams: e,
|
|
841
819
|
headers: s,
|
|
842
|
-
body:
|
|
820
|
+
body: u,
|
|
843
821
|
timeout: n,
|
|
844
|
-
signal:
|
|
822
|
+
signal: c
|
|
845
823
|
};
|
|
846
824
|
}
|
|
847
|
-
class
|
|
825
|
+
class x extends J {
|
|
848
826
|
/**
|
|
849
827
|
* Create a NamedFetcher instance and automatically register it with the global fetcherRegistrar
|
|
850
828
|
*
|
|
@@ -862,48 +840,50 @@ class X extends V {
|
|
|
862
840
|
* headers: { 'Authorization': 'Bearer token' }
|
|
863
841
|
* });
|
|
864
842
|
*/
|
|
865
|
-
constructor(t, e =
|
|
866
|
-
super(e), this.name = t,
|
|
843
|
+
constructor(t, e = O) {
|
|
844
|
+
super(e), this.name = t, Y.register(t, this);
|
|
867
845
|
}
|
|
868
846
|
}
|
|
869
|
-
const
|
|
847
|
+
const Z = new x(T);
|
|
870
848
|
export {
|
|
871
|
-
|
|
849
|
+
K as CONTENT_TYPE_HEADER,
|
|
872
850
|
h as ContentTypeValues,
|
|
873
851
|
T as DEFAULT_FETCHER_NAME,
|
|
874
|
-
|
|
852
|
+
O as DEFAULT_OPTIONS,
|
|
875
853
|
l as ExchangeError,
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
854
|
+
v as FETCH_INTERCEPTOR_NAME,
|
|
855
|
+
G as FETCH_INTERCEPTOR_ORDER,
|
|
856
|
+
k as FetchExchange,
|
|
857
|
+
M as FetchInterceptor,
|
|
880
858
|
R as FetchTimeoutError,
|
|
881
|
-
|
|
859
|
+
J as Fetcher,
|
|
882
860
|
a as FetcherError,
|
|
883
|
-
|
|
884
|
-
|
|
861
|
+
X as FetcherRegistrar,
|
|
862
|
+
i as HttpMethod,
|
|
885
863
|
f as HttpStatusValidationError,
|
|
886
|
-
|
|
864
|
+
V as InterceptorManager,
|
|
887
865
|
p as InterceptorRegistry,
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
866
|
+
x as NamedFetcher,
|
|
867
|
+
D as REQUEST_BODY_INTERCEPTOR_NAME,
|
|
868
|
+
L as REQUEST_BODY_INTERCEPTOR_ORDER,
|
|
869
|
+
C as RequestBodyInterceptor,
|
|
870
|
+
q as URL_RESOLVE_INTERCEPTOR_NAME,
|
|
871
|
+
P as URL_RESOLVE_INTERCEPTOR_ORDER,
|
|
872
|
+
w as UrlBuilder,
|
|
873
|
+
U as UrlResolveInterceptor,
|
|
874
|
+
$ as VALIDATE_STATUS_INTERCEPTOR_NAME,
|
|
875
|
+
j as VALIDATE_STATUS_INTERCEPTOR_ORDER,
|
|
876
|
+
H as ValidateStatusInterceptor,
|
|
899
877
|
b as combineURLs,
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
878
|
+
Q as extractPathParams,
|
|
879
|
+
Z as fetcher,
|
|
880
|
+
Y as fetcherRegistrar,
|
|
881
|
+
S as interpolateUrl,
|
|
882
|
+
_ as isAbsoluteURL,
|
|
903
883
|
I as mergeRecords,
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
884
|
+
W as mergeRequest,
|
|
885
|
+
N as resolveTimeout,
|
|
886
|
+
F as timeoutFetch,
|
|
907
887
|
E as toSorted
|
|
908
888
|
};
|
|
909
889
|
//# sourceMappingURL=index.es.js.map
|