@ahoo-wang/fetcher 1.6.0 → 1.6.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 +15 -1
- package/dist/fetchExchange.d.ts.map +1 -1
- package/dist/index.es.js +56 -38
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +1 -1
package/dist/fetchExchange.d.ts
CHANGED
|
@@ -97,7 +97,7 @@ export declare class FetchExchange implements RequiredBy<FetchExchangeInit, 'att
|
|
|
97
97
|
/**
|
|
98
98
|
* The response object, undefined until the request completes successfully.
|
|
99
99
|
*/
|
|
100
|
-
|
|
100
|
+
private _response?;
|
|
101
101
|
/**
|
|
102
102
|
* Any error that occurred during the request processing, undefined if no error occurred.
|
|
103
103
|
*/
|
|
@@ -151,6 +151,20 @@ export declare class FetchExchange implements RequiredBy<FetchExchangeInit, 'att
|
|
|
151
151
|
* @returns true if an error is present, false otherwise
|
|
152
152
|
*/
|
|
153
153
|
hasError(): boolean;
|
|
154
|
+
/**
|
|
155
|
+
* Sets the response object for this exchange.
|
|
156
|
+
* Also invalidates the cached extracted result to ensure data consistency
|
|
157
|
+
* when the response changes.
|
|
158
|
+
*
|
|
159
|
+
* @param response - The Response object to set, or undefined to clear the response
|
|
160
|
+
*/
|
|
161
|
+
set response(response: Response | undefined);
|
|
162
|
+
/**
|
|
163
|
+
* Gets the response object for this exchange.
|
|
164
|
+
*
|
|
165
|
+
* @returns The response object if available, undefined otherwise
|
|
166
|
+
*/
|
|
167
|
+
get response(): Response | undefined;
|
|
154
168
|
/**
|
|
155
169
|
* Checks if the exchange has a response.
|
|
156
170
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fetchExchange.d.ts","sourceRoot":"","sources":["../src/fetchExchange.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC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QAAQ,CAAC,QAAQ,EAAE,QAAQ,GAAG,SAAS,EAG1C;IAED;;;;OAIG;IACH,IAAI,QAAQ,IAAI,QAAQ,GAAG,SAAS,CAEnC;IAED;;;;OAIG;IACH,WAAW,IAAI,OAAO;IAItB;;;;;;;;;OASG;IACH,IAAI,gBAAgB,IAAI,QAAQ,CAQ/B;IAED;;;;;OAKG;IACH,oBAAoB,CAAC,CAAC,GAAG,GAAG,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC;CAOhD"}
|
package/dist/index.es.js
CHANGED
|
@@ -5,8 +5,8 @@ function C(r, e) {
|
|
|
5
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
|
-
function
|
|
9
|
-
return r === 1 ?
|
|
8
|
+
function v(r) {
|
|
9
|
+
return r === 1 ? D : x;
|
|
10
10
|
}
|
|
11
11
|
function q(r, e, t) {
|
|
12
12
|
return t ? r.replace(e, (s, o) => {
|
|
@@ -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 x = new P(), l = class l {
|
|
103
103
|
/**
|
|
104
104
|
* Extracts path parameters from an Express-style URL string.
|
|
105
105
|
*
|
|
@@ -167,8 +167,8 @@ const L = new P(), l = class l {
|
|
|
167
167
|
};
|
|
168
168
|
l.PATH_PARAM_REGEX = /:([^/]+)/g;
|
|
169
169
|
let A = l;
|
|
170
|
-
const
|
|
171
|
-
class
|
|
170
|
+
const D = new A();
|
|
171
|
+
class L {
|
|
172
172
|
/**
|
|
173
173
|
* Initializes a new UrlBuilder instance.
|
|
174
174
|
*
|
|
@@ -187,7 +187,7 @@ class x {
|
|
|
187
187
|
* ```
|
|
188
188
|
*/
|
|
189
189
|
constructor(e, t) {
|
|
190
|
-
this.baseURL = e, this.urlTemplateResolver =
|
|
190
|
+
this.baseURL = e, this.urlTemplateResolver = v(t);
|
|
191
191
|
}
|
|
192
192
|
/**
|
|
193
193
|
* Builds a complete URL, including path parameter replacement and query parameter addition.
|
|
@@ -229,7 +229,7 @@ class x {
|
|
|
229
229
|
return this.build(e.url, e.urlParams);
|
|
230
230
|
}
|
|
231
231
|
}
|
|
232
|
-
class
|
|
232
|
+
class d extends Error {
|
|
233
233
|
/**
|
|
234
234
|
* Creates a new FetcherError instance.
|
|
235
235
|
*
|
|
@@ -238,10 +238,10 @@ class E extends Error {
|
|
|
238
238
|
*/
|
|
239
239
|
constructor(e, t) {
|
|
240
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,
|
|
241
|
+
super(s), this.cause = t, this.name = "FetcherError", t?.stack && (this.stack = t.stack), Object.setPrototypeOf(this, d.prototype);
|
|
242
242
|
}
|
|
243
243
|
}
|
|
244
|
-
class
|
|
244
|
+
class b extends d {
|
|
245
245
|
/**
|
|
246
246
|
* Creates a new FetchTimeoutError instance.
|
|
247
247
|
*
|
|
@@ -249,7 +249,7 @@ class I extends E {
|
|
|
249
249
|
*/
|
|
250
250
|
constructor(e) {
|
|
251
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,
|
|
252
|
+
super(s), this.name = "FetchTimeoutError", this.request = e, Object.setPrototypeOf(this, b.prototype);
|
|
253
253
|
}
|
|
254
254
|
}
|
|
255
255
|
function G(r, e) {
|
|
@@ -267,8 +267,8 @@ async function M(r) {
|
|
|
267
267
|
const u = new Promise((a, m) => {
|
|
268
268
|
n = setTimeout(() => {
|
|
269
269
|
n && clearTimeout(n);
|
|
270
|
-
const
|
|
271
|
-
o.abort(
|
|
270
|
+
const g = new b(r);
|
|
271
|
+
o.abort(g), m(g);
|
|
272
272
|
}, t);
|
|
273
273
|
});
|
|
274
274
|
try {
|
|
@@ -296,7 +296,7 @@ var c = /* @__PURE__ */ ((r) => (r.GET = "GET", r.POST = "POST", r.PUT = "PUT",
|
|
|
296
296
|
const ce = "Content-Type", R = class R {
|
|
297
297
|
};
|
|
298
298
|
R.APPLICATION_JSON = "application/json", R.TEXT_EVENT_STREAM = "text/event-stream";
|
|
299
|
-
let
|
|
299
|
+
let E = R;
|
|
300
300
|
const B = "RequestBodyInterceptor", $ = N + 1e3;
|
|
301
301
|
class X {
|
|
302
302
|
constructor() {
|
|
@@ -346,7 +346,7 @@ class X {
|
|
|
346
346
|
return;
|
|
347
347
|
e.request.body = JSON.stringify(t.body);
|
|
348
348
|
const s = e.ensureRequestHeaders();
|
|
349
|
-
s["Content-Type"] || (s["Content-Type"] =
|
|
349
|
+
s["Content-Type"] || (s["Content-Type"] = E.APPLICATION_JSON);
|
|
350
350
|
}
|
|
351
351
|
}
|
|
352
352
|
const V = "FetchInterceptor", J = Number.MAX_SAFE_INTEGER - 1e3;
|
|
@@ -479,11 +479,11 @@ class f {
|
|
|
479
479
|
await t.intercept(e);
|
|
480
480
|
}
|
|
481
481
|
}
|
|
482
|
-
class
|
|
482
|
+
class I extends d {
|
|
483
483
|
constructor(e) {
|
|
484
484
|
super(
|
|
485
485
|
`Request failed with status code ${e.response?.status} for ${e.request.url}`
|
|
486
|
-
), this.exchange = e, this.name = "HttpStatusValidationError", Object.setPrototypeOf(this,
|
|
486
|
+
), this.exchange = e, this.name = "HttpStatusValidationError", Object.setPrototypeOf(this, I.prototype);
|
|
487
487
|
}
|
|
488
488
|
}
|
|
489
489
|
const Y = (r) => r >= 200 && r < 300, z = "ValidateStatusInterceptor", Q = Number.MAX_SAFE_INTEGER - 1e3;
|
|
@@ -530,10 +530,10 @@ class K {
|
|
|
530
530
|
return;
|
|
531
531
|
const t = e.response.status;
|
|
532
532
|
if (!this.validateStatus(t))
|
|
533
|
-
throw new
|
|
533
|
+
throw new I(e);
|
|
534
534
|
}
|
|
535
535
|
}
|
|
536
|
-
class p extends
|
|
536
|
+
class p extends d {
|
|
537
537
|
/**
|
|
538
538
|
* Creates a new ExchangeError instance.
|
|
539
539
|
*
|
|
@@ -659,7 +659,7 @@ const Z = (r) => r, ee = (r) => r.requiredResponse, te = (r) => r.requiredRespon
|
|
|
659
659
|
};
|
|
660
660
|
class se {
|
|
661
661
|
constructor(e) {
|
|
662
|
-
this.fetcher = e.fetcher, this.request = e.request, this.resultExtractor = e.resultExtractor ?? i.Exchange, this.attributes = e.attributes ?? {}, this.
|
|
662
|
+
this.fetcher = e.fetcher, this.request = e.request, this.resultExtractor = e.resultExtractor ?? i.Exchange, this.attributes = e.attributes ?? {}, this._response = e.response, this.error = e.error;
|
|
663
663
|
}
|
|
664
664
|
/**
|
|
665
665
|
* Ensures that request headers object exists, creating it if necessary.
|
|
@@ -698,6 +698,24 @@ class se {
|
|
|
698
698
|
hasError() {
|
|
699
699
|
return !!this.error;
|
|
700
700
|
}
|
|
701
|
+
/**
|
|
702
|
+
* Sets the response object for this exchange.
|
|
703
|
+
* Also invalidates the cached extracted result to ensure data consistency
|
|
704
|
+
* when the response changes.
|
|
705
|
+
*
|
|
706
|
+
* @param response - The Response object to set, or undefined to clear the response
|
|
707
|
+
*/
|
|
708
|
+
set response(e) {
|
|
709
|
+
this._response = e, this.cachedExtractedResult = void 0;
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Gets the response object for this exchange.
|
|
713
|
+
*
|
|
714
|
+
* @returns The response object if available, undefined otherwise
|
|
715
|
+
*/
|
|
716
|
+
get response() {
|
|
717
|
+
return this._response;
|
|
718
|
+
}
|
|
701
719
|
/**
|
|
702
720
|
* Checks if the exchange has a response.
|
|
703
721
|
*
|
|
@@ -735,7 +753,7 @@ class se {
|
|
|
735
753
|
}
|
|
736
754
|
}
|
|
737
755
|
const y = {
|
|
738
|
-
"Content-Type":
|
|
756
|
+
"Content-Type": E.APPLICATION_JSON
|
|
739
757
|
}, S = {
|
|
740
758
|
baseURL: "",
|
|
741
759
|
headers: y
|
|
@@ -750,7 +768,7 @@ class oe {
|
|
|
750
768
|
* @param options - Configuration options for the Fetcher instance
|
|
751
769
|
*/
|
|
752
770
|
constructor(e = S) {
|
|
753
|
-
this.headers = y, this.urlBuilder = new
|
|
771
|
+
this.headers = y, this.urlBuilder = new L(e.baseURL, e.urlTemplateStyle), this.headers = e.headers ?? y, this.timeout = e.timeout, this.interceptors = e.interceptors ?? new W();
|
|
754
772
|
}
|
|
755
773
|
/**
|
|
756
774
|
* Processes an HTTP request through the Fetcher's internal workflow.
|
|
@@ -955,7 +973,7 @@ class oe {
|
|
|
955
973
|
return this.methodFetch(c.OPTIONS, e, t, s, o);
|
|
956
974
|
}
|
|
957
975
|
}
|
|
958
|
-
const
|
|
976
|
+
const _ = "default";
|
|
959
977
|
class ne {
|
|
960
978
|
constructor() {
|
|
961
979
|
this.registrar = /* @__PURE__ */ new Map();
|
|
@@ -1029,7 +1047,7 @@ class ne {
|
|
|
1029
1047
|
* const defaultFetcher = fetcherRegistrar.default;
|
|
1030
1048
|
*/
|
|
1031
1049
|
get default() {
|
|
1032
|
-
return this.requiredGet(
|
|
1050
|
+
return this.requiredGet(_);
|
|
1033
1051
|
}
|
|
1034
1052
|
/**
|
|
1035
1053
|
* Set the default Fetcher instance
|
|
@@ -1040,7 +1058,7 @@ class ne {
|
|
|
1040
1058
|
* fetcherRegistrar.default = fetcher;
|
|
1041
1059
|
*/
|
|
1042
1060
|
set default(e) {
|
|
1043
|
-
this.register(
|
|
1061
|
+
this.register(_, e);
|
|
1044
1062
|
}
|
|
1045
1063
|
/**
|
|
1046
1064
|
* Get a copy of all registered fetchers
|
|
@@ -1057,7 +1075,7 @@ class ne {
|
|
|
1057
1075
|
}
|
|
1058
1076
|
}
|
|
1059
1077
|
const ie = new ne();
|
|
1060
|
-
function
|
|
1078
|
+
function O(r, e) {
|
|
1061
1079
|
if (!(r === void 0 && e === void 0))
|
|
1062
1080
|
return e === void 0 ? r : r === void 0 ? e : { ...r, ...e };
|
|
1063
1081
|
}
|
|
@@ -1067,8 +1085,8 @@ function ae(r, e) {
|
|
|
1067
1085
|
if (Object.keys(e).length === 0)
|
|
1068
1086
|
return r;
|
|
1069
1087
|
const t = {
|
|
1070
|
-
path:
|
|
1071
|
-
query:
|
|
1088
|
+
path: O(r.urlParams?.path, e.urlParams?.path),
|
|
1089
|
+
query: O(r.urlParams?.query, e.urlParams?.query)
|
|
1072
1090
|
}, s = {
|
|
1073
1091
|
...r.headers,
|
|
1074
1092
|
...e.headers
|
|
@@ -1107,11 +1125,11 @@ class ue extends oe {
|
|
|
1107
1125
|
super(t), this.name = e, ie.register(e, this);
|
|
1108
1126
|
}
|
|
1109
1127
|
}
|
|
1110
|
-
const he = new ue(
|
|
1128
|
+
const he = new ue(_);
|
|
1111
1129
|
export {
|
|
1112
1130
|
ce as CONTENT_TYPE_HEADER,
|
|
1113
|
-
|
|
1114
|
-
|
|
1131
|
+
E as ContentTypeValues,
|
|
1132
|
+
_ as DEFAULT_FETCHER_NAME,
|
|
1115
1133
|
S as DEFAULT_OPTIONS,
|
|
1116
1134
|
p as ExchangeError,
|
|
1117
1135
|
Z as ExchangeResultExtractor,
|
|
@@ -1120,12 +1138,12 @@ export {
|
|
|
1120
1138
|
J as FETCH_INTERCEPTOR_ORDER,
|
|
1121
1139
|
se as FetchExchange,
|
|
1122
1140
|
k as FetchInterceptor,
|
|
1123
|
-
|
|
1141
|
+
b as FetchTimeoutError,
|
|
1124
1142
|
oe as Fetcher,
|
|
1125
|
-
|
|
1143
|
+
d as FetcherError,
|
|
1126
1144
|
ne as FetcherRegistrar,
|
|
1127
1145
|
c as HttpMethod,
|
|
1128
|
-
|
|
1146
|
+
I as HttpStatusValidationError,
|
|
1129
1147
|
W as InterceptorManager,
|
|
1130
1148
|
f as InterceptorRegistry,
|
|
1131
1149
|
te as JsonResultExtractor,
|
|
@@ -1139,24 +1157,24 @@ export {
|
|
|
1139
1157
|
H as URL_RESOLVE_INTERCEPTOR_NAME,
|
|
1140
1158
|
N as URL_RESOLVE_INTERCEPTOR_ORDER,
|
|
1141
1159
|
P as UriTemplateResolver,
|
|
1142
|
-
|
|
1160
|
+
L as UrlBuilder,
|
|
1143
1161
|
j as UrlResolveInterceptor,
|
|
1144
1162
|
U as UrlTemplateStyle,
|
|
1145
1163
|
z as VALIDATE_STATUS_INTERCEPTOR_NAME,
|
|
1146
1164
|
Q as VALIDATE_STATUS_INTERCEPTOR_ORDER,
|
|
1147
1165
|
K as ValidateStatusInterceptor,
|
|
1148
1166
|
C as combineURLs,
|
|
1149
|
-
|
|
1167
|
+
D as expressUrlTemplateResolver,
|
|
1150
1168
|
he as fetcher,
|
|
1151
1169
|
ie as fetcherRegistrar,
|
|
1152
|
-
|
|
1170
|
+
v as getUrlTemplateResolver,
|
|
1153
1171
|
F as isAbsoluteURL,
|
|
1154
|
-
|
|
1172
|
+
O as mergeRecords,
|
|
1155
1173
|
ae as mergeRequest,
|
|
1156
1174
|
G as resolveTimeout,
|
|
1157
1175
|
M as timeoutFetch,
|
|
1158
1176
|
T as toSorted,
|
|
1159
|
-
|
|
1177
|
+
x as uriTemplateResolver,
|
|
1160
1178
|
w as urlTemplateRegexExtract,
|
|
1161
1179
|
q as urlTemplateRegexResolve
|
|
1162
1180
|
};
|