@superutils/fetch 1.1.6 → 1.1.8

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.d.ts CHANGED
@@ -274,7 +274,7 @@ type FetchRetryOptions = Omit<Partial<RetryOptions>, 'retry' | 'retryIf'> & {
274
274
  /**
275
275
  * Generic fetch interceptor type
276
276
  */
277
- type Interceptor<T, TArgs extends unknown[], TArgsCb extends unknown[] = [value: T, ...TArgs]> = (...args: TArgsCb) => ValueOrPromise<void> | ValueOrPromise<T>;
277
+ type Interceptor<T, TArgs extends unknown[]> = (...args: [value: T, ...TArgs]) => ValueOrPromise<void> | ValueOrPromise<T>;
278
278
  type PostBody = Record<string, unknown> | BodyInit | null;
279
279
  type PostArgs<OmitMethod = false> = [
280
280
  url: string | URL,
package/dist/index.js CHANGED
@@ -16,7 +16,7 @@ var executeInterceptors = async (value, interceptors, ...args) => {
16
16
  for (const interceptor of interceptors.filter(isFn)) {
17
17
  value = (_a = await fallbackIfFails(
18
18
  interceptor,
19
- [value, args],
19
+ [value, ...args],
20
20
  void 0
21
21
  )) != null ? _a : value;
22
22
  }
@@ -28,27 +28,22 @@ var executeInterceptors_default = executeInterceptors;
28
28
  import PromisE from "@superutils/promise";
29
29
  import { isPositiveInteger } from "@superutils/core";
30
30
  var getResponse = async (...[url, options = {}]) => {
31
+ const fetchFunc = globalThis.fetch;
32
+ if (!isPositiveInteger(options.retry)) return fetchFunc(url, options);
31
33
  let attemptCount = 0;
32
- const doFetch = async () => {
33
- attemptCount++;
34
- return globalThis.fetch(url, options).catch(
35
- (err) => err.message === "Failed to fetch" ? (
36
- // catch network errors to allow retries
37
- new Response(null, {
38
- status: 0,
39
- statusText: "Network Error"
40
- })
41
- ) : Promise.reject(err)
42
- );
43
- };
44
- if (!isPositiveInteger(options.retry)) return doFetch();
45
- const response = PromisE.retry(doFetch, {
46
- ...options,
47
- retryIf: async (res, count) => {
48
- var _a;
49
- return (res == null ? void 0 : res.ok) === false || await ((_a = options == null ? void 0 : options.retryIf) == null ? void 0 : _a.call(options, res, count)) === true;
34
+ const response = PromisE.retry(
35
+ () => {
36
+ attemptCount++;
37
+ return fetchFunc(url, options);
38
+ },
39
+ {
40
+ ...options,
41
+ retryIf: async (res, count) => {
42
+ var _a;
43
+ return (res == null ? void 0 : res.ok) === false || await ((_a = options == null ? void 0 : options.retryIf) == null ? void 0 : _a.call(options, res, count)) === true;
44
+ }
50
45
  }
51
- }).catch(
46
+ ).catch(
52
47
  (err) => Promise.reject(
53
48
  new Error(`Request failed after attempt #${attemptCount}`, {
54
49
  cause: err
@@ -237,6 +232,7 @@ fetch.defaults = {
237
232
  response: [],
238
233
  result: []
239
234
  },
235
+ /** Request timeout duration in milliseconds. Default: `0` */
240
236
  timeout: 0
241
237
  };
242
238
  var fetch_default = fetch;
@@ -269,16 +265,12 @@ import PromisE4 from "@superutils/promise";
269
265
  // src/post.ts
270
266
  import { isFn as isFn3, isStr } from "@superutils/core";
271
267
  function post(...[url = "", data, options = {}]) {
272
- return fetch_default(
273
- url,
274
- mergeFetchOptions_default(
275
- {
276
- method: "post",
277
- body: isStr(data) ? data : JSON.stringify(isFn3(data) ? data() : data)
278
- },
279
- options
280
- )
281
- );
268
+ var _a;
269
+ (_a = options.method) != null ? _a : options.method = "post";
270
+ return fetch_default(url, {
271
+ ...options,
272
+ body: isStr(data) ? data : JSON.stringify(isFn3(data) ? data() : data)
273
+ });
282
274
  }
283
275
 
284
276
  // src/postDeferred.ts
package/package.json CHANGED
@@ -45,5 +45,5 @@
45
45
  "sideEffects": false,
46
46
  "type": "module",
47
47
  "types": "dist/index.d.ts",
48
- "version": "1.1.6"
48
+ "version": "1.1.8"
49
49
  }