@superutils/fetch 1.4.1 → 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/index.js CHANGED
@@ -5,20 +5,18 @@ import { deferredCallback } from "@superutils/promise";
5
5
  import {
6
6
  fallbackIfFails as fallbackIfFails3,
7
7
  isError,
8
- isFn as isFn2,
8
+ isFn as isFn4,
9
9
  isPromise,
10
10
  isUrlValid
11
11
  } from "@superutils/core";
12
- import {
13
- timeout as PromisE_timeout
14
- } from "@superutils/promise";
12
+ import { timeout as PromisE_timeout } from "@superutils/promise";
15
13
 
16
14
  // src/executeInterceptors.ts
17
15
  import { fallbackIfFails, isFn } from "@superutils/core";
18
16
  var executeInterceptors = async (value, signal, interceptors, ...args) => {
19
17
  var _a;
20
18
  for (const interceptor of [...interceptors != null ? interceptors : []].filter(isFn)) {
21
- if (signal == null ? void 0 : signal.aborted) return value;
19
+ if (signal == null ? void 0 : signal.aborted) break;
22
20
  value = (_a = await fallbackIfFails(
23
21
  interceptor,
24
22
  [value, ...args],
@@ -30,11 +28,10 @@ var executeInterceptors = async (value, signal, interceptors, ...args) => {
30
28
  var executeInterceptors_default = executeInterceptors;
31
29
 
32
30
  // src/getResponse.ts
33
- import { fallbackIfFails as fallbackIfFails2, isPositiveInteger } from "@superutils/core";
31
+ import { fallbackIfFails as fallbackIfFails2, isFn as isFn2, isPositiveInteger } from "@superutils/core";
34
32
  import { retry } from "@superutils/promise";
35
33
  var getResponse = (url, options = {}) => {
36
- var _a;
37
- const fetchFunc = (_a = options.fetchFunc) != null ? _a : globalThis.fetch;
34
+ const fetchFunc = isFn2(options.fetchFunc) ? options.fetchFunc : globalThis.fetch;
38
35
  if (!isPositiveInteger(options.retry)) return fetchFunc(url, options);
39
36
  let attemptCount = 0;
40
37
  const response = retry(
@@ -45,14 +42,14 @@ var getResponse = (url, options = {}) => {
45
42
  {
46
43
  ...options,
47
44
  retryIf: async (res, count, error) => {
48
- var _a2;
45
+ var _a;
49
46
  const { abortCtrl, retryIf, signal } = options;
50
47
  if ((abortCtrl == null ? void 0 : abortCtrl.signal.aborted) || (signal == null ? void 0 : signal.aborted)) return false;
51
- return !!((_a2 = await fallbackIfFails2(
48
+ return !!((_a = await fallbackIfFails2(
52
49
  retryIf,
53
50
  [res, count, error],
54
51
  void 0
55
- )) != null ? _a2 : !!error || !(res == null ? void 0 : res.ok));
52
+ )) != null ? _a : !!error || !(res == null ? void 0 : res.ok));
56
53
  }
57
54
  }
58
55
  ).catch(
@@ -66,50 +63,46 @@ var getResponse = (url, options = {}) => {
66
63
  };
67
64
  var getResponse_default = getResponse;
68
65
 
69
- // src/mergeFetchOptions.ts
70
- import { isEmpty, isObj, objKeys } from "@superutils/core";
71
- var mergeFetchOptions = (...allOptions) => allOptions.reduce(
72
- (merged, next) => {
73
- var _a, _b, _c, _d, _e, _f, _g, _h, _i;
74
- next = isObj(next) ? next : {};
75
- const { errMsgs = {}, headers, interceptors: ints1 = {} } = merged;
76
- const { errMsgs: msgs2 = {}, interceptors: ints2 = {} } = next;
77
- next.headers && new Headers(next.headers).forEach((value, key) => {
78
- headers && headers.set(key, value);
79
- });
80
- for (const key of objKeys(msgs2)) {
81
- if (!isEmpty(msgs2[key])) errMsgs[key] = msgs2[key];
82
- }
66
+ // src/mergeOptions.ts
67
+ import { isArr, isFn as isFn3, isObj, objCopy } from "@superutils/core";
68
+ var mergeOptions = (...allOptions) => allOptions.reduce(
69
+ (merged, options) => {
70
+ var _a;
71
+ options = isObj(options) ? options : {};
72
+ const { headers, interceptors: ints1 = {} } = merged;
73
+ const { interceptors: ints2 = {} } = options;
74
+ options.headers && new Headers(options.headers).forEach(
75
+ (value, key) => headers.set(key, value)
76
+ );
83
77
  return {
84
78
  ...merged,
85
- ...next,
86
- errMsgs,
79
+ ...options,
80
+ errMsgs: objCopy(
81
+ options.errMsgs,
82
+ merged.errMsgs,
83
+ [],
84
+ "empty"
85
+ ),
87
86
  headers,
88
87
  interceptors: {
89
- error: [...(_a = ints1 == null ? void 0 : ints1.error) != null ? _a : [], ...(_b = ints2 == null ? void 0 : ints2.error) != null ? _b : []],
88
+ error: [...toArr(ints1 == null ? void 0 : ints1.error), ...toArr(ints2 == null ? void 0 : ints2.error)],
90
89
  request: [
91
- ...(_c = ints1 == null ? void 0 : ints1.request) != null ? _c : [],
92
- ...(_d = ints2 == null ? void 0 : ints2.request) != null ? _d : []
90
+ ...toArr(ints1 == null ? void 0 : ints1.request),
91
+ ...toArr(ints2 == null ? void 0 : ints2.request)
93
92
  ],
94
93
  response: [
95
- ...(_e = ints1 == null ? void 0 : ints1.response) != null ? _e : [],
96
- ...(_f = ints2 == null ? void 0 : ints2.response) != null ? _f : []
94
+ ...toArr(ints1 == null ? void 0 : ints1.response),
95
+ ...toArr(ints2 == null ? void 0 : ints2.response)
97
96
  ],
98
- result: [
99
- ...(_g = ints1 == null ? void 0 : ints1.result) != null ? _g : [],
100
- ...(_h = ints2 == null ? void 0 : ints2.result) != null ? _h : []
101
- ]
97
+ result: [...toArr(ints1 == null ? void 0 : ints1.result), ...toArr(ints2 == null ? void 0 : ints2.result)]
102
98
  },
103
- timeout: (_i = next.timeout) != null ? _i : merged.timeout
99
+ timeout: (_a = options.timeout) != null ? _a : merged.timeout
104
100
  };
105
101
  },
106
102
  { headers: new Headers() }
107
103
  );
108
- var mergeFetchOptions_default = mergeFetchOptions;
109
- var mergePartialOptions = (...optionsAr) => {
110
- optionsAr = optionsAr.filter((x) => !isEmpty(x));
111
- return optionsAr.length <= 1 ? optionsAr[0] : mergeFetchOptions(...optionsAr);
112
- };
104
+ var mergeOptions_default = mergeOptions;
105
+ var toArr = (x) => isArr(x) ? x : isFn3(x) ? [x] : [];
113
106
 
114
107
  // src/types/constants.ts
115
108
  var ContentType = {
@@ -173,20 +166,22 @@ var FetchError = class _FetchError extends Error {
173
166
  }
174
167
  };
175
168
 
176
- // src/types/options.ts
169
+ // src/types/index.ts
177
170
  import {
178
171
  ResolveError,
179
- ResolveIgnored
172
+ ResolveIgnored,
173
+ TIMEOUT_FALLBACK,
174
+ TIMEOUT_MAX,
175
+ TimeoutPromise
180
176
  } from "@superutils/promise";
181
177
 
182
178
  // src/fetch.ts
183
- var MAX_TIMEOUT = 2147483647;
184
179
  var fetch = (url, options = {}) => {
185
180
  var _a, _b, _c;
186
181
  let response;
187
- const opts = mergeFetchOptions_default(fetch.defaults, options);
182
+ const opts = mergeOptions_default(fetch.defaults, options);
188
183
  opts.abortCtrl = opts.abortCtrl instanceof AbortController ? opts.abortCtrl : new AbortController();
189
- (_a = opts.as) != null ? _a : opts.as = "json" /* json */;
184
+ (_a = opts.as) != null ? _a : opts.as = "response" /* response */;
190
185
  (_b = opts.method) != null ? _b : opts.method = "get";
191
186
  (_c = opts.signal) != null ? _c : opts.signal = opts.abortCtrl.signal;
192
187
  const { abortCtrl, as: parseAs, headers, onAbort, onTimeout } = opts;
@@ -213,7 +208,7 @@ var fetch = (url, options = {}) => {
213
208
  );
214
209
  };
215
210
  return PromisE_timeout(opts, async () => {
216
- var _a2, _b2, _c2, _d;
211
+ var _a2, _b2, _c2, _d, _e;
217
212
  try {
218
213
  let contentType = headers.get("content-type");
219
214
  if (!contentType) {
@@ -236,7 +231,7 @@ var fetch = (url, options = {}) => {
236
231
  ].find((x) => contentType.includes(x)) && !["undefined", "string"].includes(typeof body);
237
232
  if (shouldStringifyBody)
238
233
  opts.body = JSON.stringify(
239
- isFn2(body) ? fallbackIfFails3(body, [], void 0) : body
234
+ isFn4(body) ? fallbackIfFails3(body, [], void 0) : body
240
235
  );
241
236
  response = await getResponse_default(url, opts);
242
237
  response = await executeInterceptors_default(
@@ -246,7 +241,7 @@ var fetch = (url, options = {}) => {
246
241
  url,
247
242
  opts
248
243
  );
249
- const { status = 0 } = response;
244
+ const status = (_d = response == null ? void 0 : response.status) != null ? _d : 0;
250
245
  const isSuccess = status >= 200 && status < 300;
251
246
  if (!isSuccess) {
252
247
  const jsonError = await fallbackIfFails3(
@@ -261,7 +256,7 @@ var fetch = (url, options = {}) => {
261
256
  );
262
257
  }
263
258
  const parseFunc = response[parseAs];
264
- let result = !isFn2(parseFunc) ? response : parseFunc.bind(response)();
259
+ let result = !isFn4(parseFunc) ? response : parseFunc.bind(response)();
265
260
  if (isPromise(result))
266
261
  result = await result.catch(
267
262
  (err) => Promise.reject(
@@ -274,7 +269,7 @@ var fetch = (url, options = {}) => {
274
269
  result = await executeInterceptors_default(
275
270
  result,
276
271
  abortCtrl.signal,
277
- (_d = opts.interceptors) == null ? void 0 : _d.result,
272
+ (_e = opts.interceptors) == null ? void 0 : _e.result,
278
273
  url,
279
274
  opts
280
275
  );
@@ -327,75 +322,80 @@ var fetch_default = fetch;
327
322
 
328
323
  // src/createClient.ts
329
324
  var createClient = (fixedOptions, commonOptions, commonDeferOptions) => {
330
- const func = (url, options) => {
331
- return fetch_default(
332
- url,
333
- mergePartialOptions(
334
- commonOptions,
335
- options,
336
- fixedOptions
337
- )
325
+ function client(url, options) {
326
+ var _a;
327
+ const mergedOptions = mergeOptions_default(
328
+ commonOptions,
329
+ options,
330
+ fixedOptions
331
+ // fixed options will always override other options
338
332
  );
339
- };
340
- func.deferred = (deferOptions = {}, defaultUrl, defaultOptions) => {
333
+ (_a = mergedOptions.as) != null ? _a : mergedOptions.as = "json" /* json */;
334
+ return fetch_default(url, mergedOptions);
335
+ }
336
+ client.deferred = (deferOptions, defaultUrl, defaultOptions) => {
341
337
  let _abortCtrl;
342
338
  const fetchCb = (...args) => {
343
339
  var _a, _b, _c;
344
- const options = (_a = mergePartialOptions(
340
+ const mergedOptions = (_a = mergeOptions_default(
345
341
  commonOptions,
346
342
  defaultOptions,
347
343
  defaultUrl === void 0 ? args[1] : args[0],
348
344
  fixedOptions
345
+ // fixed options will always override other options
349
346
  )) != null ? _a : {};
350
- ((_b = _abortCtrl == null ? void 0 : _abortCtrl.signal) == null ? void 0 : _b.aborted) === false && ((_c = _abortCtrl == null ? void 0 : _abortCtrl.abort) == null ? void 0 : _c.call(_abortCtrl));
347
+ (_b = mergedOptions.as) != null ? _b : mergedOptions.as = "json" /* json */;
348
+ (_c = _abortCtrl == null ? void 0 : _abortCtrl.abort) == null ? void 0 : _c.call(_abortCtrl);
351
349
  _abortCtrl = new AbortController();
352
- const promise = fetch_default(
350
+ return fetch_default(
353
351
  defaultUrl != null ? defaultUrl : args[0],
354
- options
352
+ mergedOptions
355
353
  );
356
- return promise;
357
354
  };
358
355
  return deferredCallback(fetchCb, {
359
356
  ...commonDeferOptions,
360
357
  ...deferOptions
361
358
  });
362
359
  };
363
- return func;
360
+ return client;
364
361
  };
365
362
  var createClient_default = createClient;
366
363
 
367
364
  // src/createPostClient.ts
368
365
  import { deferredCallback as deferredCallback2 } from "@superutils/promise";
369
366
  var createPostClient = (fixedOptions, commonOptions, commonDeferOptions) => {
370
- const client = (url, data, options) => {
371
- var _a;
372
- const _options = mergePartialOptions(
367
+ function client(url, data, options) {
368
+ var _a, _b;
369
+ const mergedOptions = mergeOptions_default(
373
370
  commonOptions,
374
371
  options,
375
372
  fixedOptions
373
+ // fixed options will always override other options
376
374
  );
377
- _options.body = data;
378
- (_a = _options.method) != null ? _a : _options.method = "post";
379
- return fetch_default(url, _options);
380
- };
381
- client.deferred = (deferOptions = {}, defaultUrl, defaultData, defaultOptions) => {
375
+ (_a = mergedOptions.as) != null ? _a : mergedOptions.as = "json" /* json */;
376
+ mergedOptions.body = data;
377
+ (_b = mergedOptions.method) != null ? _b : mergedOptions.method = "post";
378
+ return fetch_default(url, mergedOptions);
379
+ }
380
+ client.deferred = (deferOptions, defaultUrl, defaultData, defaultOptions) => {
382
381
  let _abortCtrl;
383
382
  const postCb = (...args) => {
384
383
  var _a, _b, _c, _d, _e;
385
384
  if (defaultUrl !== void 0) args.splice(0, 0, defaultUrl);
386
385
  if (defaultData !== void 0) args.splice(1, 0, defaultData);
387
- const options = (_a = mergePartialOptions(
386
+ const mergedOptions = (_a = mergeOptions_default(
388
387
  commonOptions,
389
388
  defaultOptions,
390
389
  args[2],
391
390
  fixedOptions
391
+ // fixed options will always override other options
392
392
  )) != null ? _a : {};
393
- ((_b = _abortCtrl == null ? void 0 : _abortCtrl.signal) == null ? void 0 : _b.aborted) === false && ((_c = _abortCtrl == null ? void 0 : _abortCtrl.abort) == null ? void 0 : _c.call(_abortCtrl));
393
+ (_b = mergedOptions.as) != null ? _b : mergedOptions.as = "json" /* json */;
394
+ (_c = _abortCtrl == null ? void 0 : _abortCtrl.abort) == null ? void 0 : _c.call(_abortCtrl);
394
395
  _abortCtrl = new AbortController();
395
- options.body = (_d = args[1]) != null ? _d : options.body;
396
- (_e = options.method) != null ? _e : options.method = "post";
397
- const promise = fetch_default(args[0], options);
398
- return promise;
396
+ mergedOptions.body = (_d = args[1]) != null ? _d : mergedOptions.body;
397
+ (_e = mergedOptions.method) != null ? _e : mergedOptions.method = "post";
398
+ return fetch_default(args[0], mergedOptions);
399
399
  };
400
400
  return deferredCallback2(postCb, {
401
401
  ...commonDeferOptions,
@@ -406,25 +406,7 @@ var createPostClient = (fixedOptions, commonOptions, commonDeferOptions) => {
406
406
  };
407
407
  var createPostClient_default = createPostClient;
408
408
 
409
- // src/fetchResponse.ts
410
- var fetchResponse = (url, options) => {
411
- var _a;
412
- options != null ? options : options = {};
413
- (_a = options.as) != null ? _a : options.as = "response" /* response */;
414
- return fetch_default(url, options);
415
- };
416
- fetchResponse.defaults = fetch_default.defaults;
417
- Object.defineProperty(fetchResponse, "defaults", {
418
- get() {
419
- return fetch_default.defaults;
420
- },
421
- set(newDefaults) {
422
- fetch_default.defaults = newDefaults;
423
- }
424
- });
425
- var fetchResponse_default = fetchResponse;
426
-
427
- // src/defaultFetch.ts
409
+ // src/index.ts
428
410
  var methods = {
429
411
  /** Make HTTP requests with method GET */
430
412
  get: createClient_default({ method: "get" }),
@@ -441,32 +423,28 @@ var methods = {
441
423
  /** Make HTTP requests with method PUT */
442
424
  put: createPostClient_default({ method: "put" })
443
425
  };
444
- var defaultFetch = fetchResponse_default;
445
- defaultFetch.delete = methods.delete;
446
- defaultFetch.get = methods.get;
447
- defaultFetch.head = methods.head;
448
- defaultFetch.options = methods.options;
449
- defaultFetch.patch = methods.patch;
450
- defaultFetch.post = methods.post;
451
- defaultFetch.put = methods.put;
452
- var defaultFetch_default = defaultFetch;
453
-
454
- // src/index.ts
455
- var index_default = defaultFetch_default;
426
+ var fetch2 = fetch_default;
427
+ fetch2.delete = methods.delete;
428
+ fetch2.get = methods.get;
429
+ fetch2.head = methods.head;
430
+ fetch2.options = methods.options;
431
+ fetch2.patch = methods.patch;
432
+ fetch2.post = methods.post;
433
+ fetch2.put = methods.put;
434
+ var index_default = fetch2;
456
435
  export {
457
436
  ContentType,
458
437
  FetchAs,
459
438
  FetchError,
460
- MAX_TIMEOUT,
461
439
  ResolveError,
462
440
  ResolveIgnored,
441
+ TIMEOUT_FALLBACK,
442
+ TIMEOUT_MAX,
443
+ TimeoutPromise,
463
444
  createClient,
464
445
  createPostClient,
465
446
  index_default as default,
466
447
  executeInterceptors,
467
- fetch,
468
- fetchResponse,
469
- getResponse,
470
- mergeFetchOptions,
471
- mergePartialOptions
448
+ fetch2 as fetch,
449
+ mergeOptions
472
450
  };
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.1",
9
- "@superutils/promise": "^1.2.1"
8
+ "@superutils/core": "^1.2.2",
9
+ "@superutils/promise": "^1.3.0"
10
10
  },
11
11
  "files": [
12
12
  "dist",
@@ -21,7 +21,6 @@
21
21
  "typescript"
22
22
  ],
23
23
  "license": "MIT",
24
- "main": "dist/index.js",
25
24
  "name": "@superutils/fetch",
26
25
  "peerDependencies": {
27
26
  "@superutils/core": "^1.2.0",
@@ -37,14 +36,23 @@
37
36
  "scripts": {
38
37
  "_build": "tsc -p tsconfig.json",
39
38
  "_watch": "tsc -p tsconfig.json --watch",
40
- "build": "tsup src/index.ts --format esm --dts --clean --config ../../tsup.config.js",
39
+ "build": "tsup --config ../../tsup.config.js",
41
40
  "dev": "npm run build -- --watch",
42
41
  "start": "npm run build -- --watch",
43
42
  "test": "cd ../../ && npm run test fetch"
44
43
  },
45
44
  "sideEffects": false,
45
+ "exports": {
46
+ ".": {
47
+ "types": "./dist/index.d.ts",
48
+ "import": "./dist/index.js",
49
+ "require": "./dist/index.cjs"
50
+ }
51
+ },
52
+ "main": "./dist/index.cjs",
53
+ "module": "./dist/index.js",
46
54
  "type": "module",
47
- "types": "dist/index.d.ts",
48
- "version": "1.4.1",
49
- "gitHead": "a26f4b1bb701d99d4cb71443e18680ee3ea52974"
55
+ "types": "./dist/index.d.ts",
56
+ "version": "1.5.0",
57
+ "gitHead": "382c9c3b6be8e7aad67ef7f89a0b43a4c6c2fb90"
50
58
  }