xsfetch 0.1.0 → 0.1.2
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 +2 -3
- package/dist/index.js +15 -19
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
interface CreateFetchOptions {
|
|
2
|
+
debug: boolean;
|
|
2
3
|
retry: number;
|
|
3
|
-
/** @internal */
|
|
4
|
-
retryCount: number;
|
|
5
4
|
retryDelay: number;
|
|
6
5
|
retryStatusCodes: number[];
|
|
7
6
|
}
|
|
8
|
-
declare const createFetch: (userOptions
|
|
7
|
+
declare const createFetch: (userOptions?: Partial<CreateFetchOptions>) => typeof globalThis.fetch;
|
|
9
8
|
|
|
10
9
|
export { type CreateFetchOptions, createFetch };
|
package/dist/index.js
CHANGED
|
@@ -2,29 +2,25 @@
|
|
|
2
2
|
var sleep = async (delay) => delay === 0 ? Promise.resolve() : new Promise((resolve) => setTimeout(resolve, delay));
|
|
3
3
|
|
|
4
4
|
// src/index.ts
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
};
|
|
14
|
-
const xsfetch = async (
|
|
5
|
+
var defaults = {
|
|
6
|
+
debug: false,
|
|
7
|
+
retry: 3,
|
|
8
|
+
retryDelay: 500,
|
|
9
|
+
// https://github.com/unjs/ofetch#%EF%B8%8F-auto-retry
|
|
10
|
+
retryStatusCodes: [408, 409, 425, 429, 500, 502, 503, 504]
|
|
11
|
+
};
|
|
12
|
+
var createFetch = (userOptions = {}) => {
|
|
13
|
+
const options = Object.assign({}, defaults, userOptions);
|
|
14
|
+
const xsfetch = async (retriesLeft, input, init) => {
|
|
15
15
|
const res = await fetch(input, init);
|
|
16
|
-
if (
|
|
17
|
-
await sleep(options2.retryDelay);
|
|
18
|
-
return async () => xsfetch({
|
|
19
|
-
...options2,
|
|
20
|
-
retryCount: options2.retryCount + 1
|
|
21
|
-
}, input, init);
|
|
22
|
-
} else {
|
|
16
|
+
if (res.ok || retriesLeft === 0 || !options.retryStatusCodes.includes(res.status))
|
|
23
17
|
return res;
|
|
24
|
-
|
|
18
|
+
options.debug && console.warn("[xsfetch] Failed, retrying... Times left:", retriesLeft);
|
|
19
|
+
await sleep(options.retryDelay);
|
|
20
|
+
return async () => xsfetch(retriesLeft - 1, input, init);
|
|
25
21
|
};
|
|
26
22
|
return async (input, init) => {
|
|
27
|
-
let res = await xsfetch(options, input, init);
|
|
23
|
+
let res = await xsfetch(options.retry, input, init);
|
|
28
24
|
while (typeof res === "function")
|
|
29
25
|
res = await res();
|
|
30
26
|
return res;
|