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