@superutils/fetch 1.5.3 → 1.5.5
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/README.md +128 -112
- package/dist/browser/index.min.js +1 -1
- package/dist/browser/index.min.js.map +1 -1
- package/dist/index.cjs +42 -56
- package/dist/index.d.cts +23 -54
- package/dist/index.d.ts +23 -54
- package/dist/index.js +45 -59
- package/package.json +4 -4
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
ResolveError,
|
|
4
4
|
ResolveIgnored,
|
|
5
5
|
TIMEOUT_FALLBACK,
|
|
6
|
-
TIMEOUT_MAX
|
|
6
|
+
TIMEOUT_MAX,
|
|
7
7
|
TimeoutPromise
|
|
8
8
|
} from "@superutils/promise";
|
|
9
9
|
|
|
@@ -19,7 +19,7 @@ import {
|
|
|
19
19
|
isPromise,
|
|
20
20
|
isUrlValid
|
|
21
21
|
} from "@superutils/core";
|
|
22
|
-
import { timeout as PromisE_timeout
|
|
22
|
+
import { timeout as PromisE_timeout } from "@superutils/promise";
|
|
23
23
|
|
|
24
24
|
// src/executeInterceptors.ts
|
|
25
25
|
import { fallbackIfFails, isFn } from "@superutils/core";
|
|
@@ -52,14 +52,14 @@ var getResponse = (url, options = {}) => {
|
|
|
52
52
|
{
|
|
53
53
|
...options,
|
|
54
54
|
retryIf: async (res, count, error) => {
|
|
55
|
-
var _a;
|
|
55
|
+
var _a, _b;
|
|
56
56
|
const { abortCtrl, retryIf, signal } = options;
|
|
57
|
-
if ((abortCtrl == null ? void 0 : abortCtrl.signal.aborted) || (signal == null ? void 0 : signal.aborted)) return false;
|
|
58
|
-
return !!((
|
|
57
|
+
if (((_a = abortCtrl == null ? void 0 : abortCtrl.signal) == null ? void 0 : _a.aborted) || (signal == null ? void 0 : signal.aborted)) return false;
|
|
58
|
+
return !!((_b = await fallbackIfFails2(
|
|
59
59
|
retryIf,
|
|
60
60
|
[res, count, error],
|
|
61
61
|
void 0
|
|
62
|
-
)) != null ?
|
|
62
|
+
)) != null ? _b : !!error || !(res == null ? void 0 : res.ok));
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
).catch(
|
|
@@ -177,22 +177,20 @@ var FetchError = class _FetchError extends Error {
|
|
|
177
177
|
};
|
|
178
178
|
|
|
179
179
|
// src/fetch.ts
|
|
180
|
+
var defaultErrorMsgs = Object.freeze({
|
|
181
|
+
aborted: "Request aborted",
|
|
182
|
+
invalidUrl: "Invalid URL",
|
|
183
|
+
parseFailed: "Failed to parse response as",
|
|
184
|
+
timedout: "Request timed out",
|
|
185
|
+
requestFailed: "Request failed with status code:"
|
|
186
|
+
});
|
|
180
187
|
var fetch = (url, options = {}) => {
|
|
181
188
|
var _a, _b, _c;
|
|
182
189
|
if (!isObj2(options)) options = {};
|
|
183
|
-
let fromPostClient = false;
|
|
184
|
-
if (options.fromPostClient) {
|
|
185
|
-
delete options.fromPostClient;
|
|
186
|
-
fromPostClient = true;
|
|
187
|
-
}
|
|
188
190
|
let response;
|
|
189
191
|
const opts = mergeOptions_default(
|
|
190
|
-
{
|
|
191
|
-
|
|
192
|
-
errMsgs: fetch.defaults.errMsgs,
|
|
193
|
-
timeout: TIMEOUT_MAX,
|
|
194
|
-
validateUrl: false
|
|
195
|
-
},
|
|
192
|
+
{ errMsgs: defaultErrorMsgs },
|
|
193
|
+
fetch.defaults,
|
|
196
194
|
options
|
|
197
195
|
);
|
|
198
196
|
opts.abortCtrl = opts.abortCtrl instanceof AbortController ? opts.abortCtrl : new AbortController();
|
|
@@ -201,11 +199,7 @@ var fetch = (url, options = {}) => {
|
|
|
201
199
|
(_c = opts.signal) != null ? _c : opts.signal = opts.abortCtrl.signal;
|
|
202
200
|
const { abortCtrl, as: parseAs, headers, onAbort, onTimeout } = opts;
|
|
203
201
|
opts.onAbort = async () => {
|
|
204
|
-
|
|
205
|
-
const err = (_f = (_e = (_c2 = await fallbackIfFails3(onAbort, [], void 0)) != null ? _c2 : (_b2 = (_a2 = opts.abortCtrl) == null ? void 0 : _a2.signal) == null ? void 0 : _b2.reason) != null ? _e : (_d = opts.signal) == null ? void 0 : _d.reason) != null ? _f : opts.errMsgs.aborted;
|
|
206
|
-
if (isError(err) && err.name === "AbortError") {
|
|
207
|
-
err.message = ["This operation was aborted"].includes(err.message) ? opts.errMsgs.aborted : err.message;
|
|
208
|
-
}
|
|
202
|
+
const err = await fallbackIfFails3(onAbort, [], void 0);
|
|
209
203
|
return await interceptErr(
|
|
210
204
|
isError(err) ? err : new Error(err),
|
|
211
205
|
url,
|
|
@@ -223,7 +217,7 @@ var fetch = (url, options = {}) => {
|
|
|
223
217
|
);
|
|
224
218
|
};
|
|
225
219
|
return PromisE_timeout(opts, async () => {
|
|
226
|
-
var _a2, _b2, _c2, _d
|
|
220
|
+
var _a2, _b2, _c2, _d;
|
|
227
221
|
try {
|
|
228
222
|
opts.body = await fallbackIfFails3(
|
|
229
223
|
opts.body,
|
|
@@ -240,17 +234,10 @@ var fetch = (url, options = {}) => {
|
|
|
240
234
|
(_b2 = opts.signal) != null ? _b2 : opts.signal = abortCtrl.signal;
|
|
241
235
|
if (validateUrl && !isUrlValid(url, false))
|
|
242
236
|
throw new Error(errMsgs.invalidUrl);
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
contentType = ContentType.APPLICATION_JSON;
|
|
248
|
-
}
|
|
249
|
-
const shouldStringifyBody = ["delete", "patch", "post", "put"].includes(
|
|
250
|
-
`${opts.method}`.toLowerCase()
|
|
251
|
-
) && !["undefined", "string"].includes(typeof body) && isObj2(body, true) && contentType === ContentType.APPLICATION_JSON;
|
|
252
|
-
if (shouldStringifyBody) opts.body = JSON.stringify(opts.body);
|
|
253
|
-
}
|
|
237
|
+
const stringify = ["delete", "patch", "post", "put"].includes(
|
|
238
|
+
`${opts.method}`.toLowerCase()
|
|
239
|
+
) && !["undefined", "string"].includes(typeof body) && isObj2(body, true) && headers.get("content-type") === ContentType.APPLICATION_JSON;
|
|
240
|
+
if (stringify) opts.body = JSON.stringify(opts.body);
|
|
254
241
|
response = await getResponse_default(url, opts);
|
|
255
242
|
response = await executeInterceptors_default(
|
|
256
243
|
response,
|
|
@@ -259,7 +246,7 @@ var fetch = (url, options = {}) => {
|
|
|
259
246
|
url,
|
|
260
247
|
opts
|
|
261
248
|
);
|
|
262
|
-
const status =
|
|
249
|
+
const status = response == null ? void 0 : response.status;
|
|
263
250
|
const isSuccess = status >= 200 && status < 300;
|
|
264
251
|
if (!isSuccess) {
|
|
265
252
|
const jsonError = await fallbackIfFails3(
|
|
@@ -287,7 +274,7 @@ var fetch = (url, options = {}) => {
|
|
|
287
274
|
result = await executeInterceptors_default(
|
|
288
275
|
result,
|
|
289
276
|
abortCtrl.signal,
|
|
290
|
-
(
|
|
277
|
+
(_d = opts.interceptors) == null ? void 0 : _d.result,
|
|
291
278
|
url,
|
|
292
279
|
opts
|
|
293
280
|
);
|
|
@@ -301,13 +288,7 @@ var fetch = (url, options = {}) => {
|
|
|
301
288
|
};
|
|
302
289
|
fetch.defaults = {
|
|
303
290
|
abortOnEarlyFinalize: true,
|
|
304
|
-
errMsgs: {
|
|
305
|
-
aborted: "Request aborted",
|
|
306
|
-
invalidUrl: "Invalid URL",
|
|
307
|
-
parseFailed: "Failed to parse response as",
|
|
308
|
-
timedout: "Request timed out",
|
|
309
|
-
requestFailed: "Request failed with status code:"
|
|
310
|
-
},
|
|
291
|
+
errMsgs: { ...defaultErrorMsgs },
|
|
311
292
|
// all error messages must be defined here
|
|
312
293
|
headers: new Headers(),
|
|
313
294
|
interceptors: {
|
|
@@ -355,17 +336,17 @@ var createClient = (fixedOptions, commonOptions, commonDeferOptions) => {
|
|
|
355
336
|
client.deferred = (deferOptions, defaultUrl, defaultOptions) => {
|
|
356
337
|
let _abortCtrl;
|
|
357
338
|
const fetchCb = (...args) => {
|
|
358
|
-
var _a, _b
|
|
359
|
-
const mergedOptions =
|
|
339
|
+
var _a, _b;
|
|
340
|
+
const mergedOptions = mergeOptions_default(
|
|
360
341
|
fetch_default.defaults,
|
|
361
342
|
commonOptions,
|
|
362
343
|
defaultOptions,
|
|
363
344
|
defaultUrl === void 0 ? args[1] : args[0],
|
|
364
345
|
fixedOptions
|
|
365
346
|
// fixed options will always override other options
|
|
366
|
-
)
|
|
367
|
-
(
|
|
368
|
-
(
|
|
347
|
+
);
|
|
348
|
+
(_a = mergedOptions.as) != null ? _a : mergedOptions.as = "json" /* json */;
|
|
349
|
+
(_b = _abortCtrl == null ? void 0 : _abortCtrl.abort) == null ? void 0 : _b.call(_abortCtrl);
|
|
369
350
|
_abortCtrl = new AbortController();
|
|
370
351
|
return fetch_default(
|
|
371
352
|
defaultUrl != null ? defaultUrl : args[0],
|
|
@@ -387,7 +368,6 @@ var createPostClient = (fixedOptions, commonOptions, commonDeferOptions) => {
|
|
|
387
368
|
function client(url, data, options) {
|
|
388
369
|
var _a, _b;
|
|
389
370
|
const mergedOptions = mergeOptions_default(
|
|
390
|
-
fetch_default.defaults,
|
|
391
371
|
commonOptions,
|
|
392
372
|
options,
|
|
393
373
|
fixedOptions
|
|
@@ -396,29 +376,35 @@ var createPostClient = (fixedOptions, commonOptions, commonDeferOptions) => {
|
|
|
396
376
|
(_a = mergedOptions.as) != null ? _a : mergedOptions.as = "json" /* json */;
|
|
397
377
|
mergedOptions.body = data != null ? data : mergedOptions.body;
|
|
398
378
|
(_b = mergedOptions.method) != null ? _b : mergedOptions.method = "post";
|
|
399
|
-
|
|
379
|
+
const headers = mergedOptions.headers;
|
|
380
|
+
if (!headers.get("content-type")) {
|
|
381
|
+
headers.set("content-type", ContentType.APPLICATION_JSON);
|
|
382
|
+
}
|
|
400
383
|
return fetch_default(url, mergedOptions);
|
|
401
384
|
}
|
|
402
385
|
client.deferred = (deferOptions, defaultUrl, defaultData, defaultOptions) => {
|
|
403
386
|
let _abortCtrl;
|
|
404
387
|
const postCb = (...args) => {
|
|
405
|
-
var _a, _b, _c, _d
|
|
388
|
+
var _a, _b, _c, _d;
|
|
406
389
|
if (defaultUrl !== void 0) args.splice(0, 0, defaultUrl);
|
|
407
390
|
if (defaultData !== void 0) args.splice(1, 0, defaultData);
|
|
408
|
-
const mergedOptions =
|
|
391
|
+
const mergedOptions = mergeOptions_default(
|
|
409
392
|
fetch_default.defaults,
|
|
410
393
|
commonOptions,
|
|
411
394
|
defaultOptions,
|
|
412
395
|
args[2],
|
|
413
396
|
fixedOptions
|
|
414
397
|
// fixed options will always override other options
|
|
415
|
-
)
|
|
416
|
-
(
|
|
417
|
-
(
|
|
398
|
+
);
|
|
399
|
+
(_a = mergedOptions.as) != null ? _a : mergedOptions.as = "json" /* json */;
|
|
400
|
+
(_b = _abortCtrl == null ? void 0 : _abortCtrl.abort) == null ? void 0 : _b.call(_abortCtrl);
|
|
418
401
|
_abortCtrl = new AbortController();
|
|
419
|
-
mergedOptions.body = (
|
|
420
|
-
(
|
|
421
|
-
|
|
402
|
+
mergedOptions.body = (_c = args[1]) != null ? _c : mergedOptions.body;
|
|
403
|
+
(_d = mergedOptions.method) != null ? _d : mergedOptions.method = "post";
|
|
404
|
+
const headers = mergedOptions.headers;
|
|
405
|
+
if (!headers.get("content-type")) {
|
|
406
|
+
headers.set("content-type", ContentType.APPLICATION_JSON);
|
|
407
|
+
}
|
|
422
408
|
return fetch_default(args[0], mergedOptions);
|
|
423
409
|
};
|
|
424
410
|
return deferredCallback2(postCb, {
|
|
@@ -463,7 +449,7 @@ export {
|
|
|
463
449
|
ResolveError,
|
|
464
450
|
ResolveIgnored,
|
|
465
451
|
TIMEOUT_FALLBACK,
|
|
466
|
-
|
|
452
|
+
TIMEOUT_MAX,
|
|
467
453
|
TimeoutPromise,
|
|
468
454
|
createClient,
|
|
469
455
|
createPostClient,
|
package/package.json
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
},
|
|
6
6
|
"description": "A lightweight `fetch` wrapper for browsers and Node.js, designed to simplify data fetching and reduce boilerplate.",
|
|
7
7
|
"dependencies": {
|
|
8
|
-
"@superutils/core": "^1.2.
|
|
9
|
-
"@superutils/promise": "^1.3.
|
|
8
|
+
"@superutils/core": "^1.2.7",
|
|
9
|
+
"@superutils/promise": "^1.3.5"
|
|
10
10
|
},
|
|
11
11
|
"files": [
|
|
12
12
|
"dist",
|
|
@@ -53,6 +53,6 @@
|
|
|
53
53
|
"module": "./dist/index.js",
|
|
54
54
|
"type": "module",
|
|
55
55
|
"types": "./dist/index.d.ts",
|
|
56
|
-
"version": "1.5.
|
|
57
|
-
"gitHead": "
|
|
56
|
+
"version": "1.5.5",
|
|
57
|
+
"gitHead": "758f9934f4b137321d1b8f3def412622ddb71ea5"
|
|
58
58
|
}
|