Skip to content

Commit 5038967

Browse files
committed
feat: make fetchAdvanced drop-in & make timeout optional (closes #30)
1 parent 4a58caa commit 5038967

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed

.changeset/smooth-suits-cheat.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sv443-network/userutils": minor
3+
---
4+
5+
`fetchAdvanced` is now a drop-in replacement and timeout can now optionally be disabled

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -1059,14 +1059,15 @@ window.addEventListener("resize", debounce((event) => {
10591059
### fetchAdvanced()
10601060
Usage:
10611061
```ts
1062-
fetchAdvanced(url: string, options?: {
1062+
fetchAdvanced(input: string | Request | URL, options?: {
10631063
timeout?: number,
10641064
// any other options from fetch() except for signal
10651065
}): Promise<Response>
10661066
```
10671067

1068-
A wrapper around the native `fetch()` function that adds options like a timeout property.
1069-
The timeout will default to 10 seconds if left undefined.
1068+
A drop-in replacement for the native `fetch()` function that adds options like a timeout property.
1069+
The timeout will default to 10 seconds if left undefined. Set it to a negative number to disable the timeout.
1070+
Note that the `signal` option will be overwritten if passed.
10701071

10711072
<details><summary><b>Example - click to view</b></summary>
10721073

@@ -1077,10 +1078,12 @@ fetchAdvanced("https://jokeapi.dev/joke/Any?safe-mode", {
10771078
timeout: 5000,
10781079
// also accepts any other fetch options like headers:
10791080
headers: {
1080-
"Accept": "application/json",
1081+
"Accept": "text/plain",
10811082
},
10821083
}).then(async (response) => {
1083-
console.log("Data:", await response.json());
1084+
console.log("Fetch data:", await response.text());
1085+
}).catch((err) => {
1086+
console.error("Fetch error:", err);
10841087
});
10851088
```
10861089

lib/misc.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,30 @@ export function debounce<TFunc extends (...args: TArgs[]) => void, TArgs = any>(
6363
}
6464

6565
/** Options for the `fetchAdvanced()` function */
66-
export type FetchAdvancedOpts = RequestInit & Partial<{
67-
/** Timeout in milliseconds after which the fetch call will be canceled with an AbortController signal */
68-
timeout: number;
69-
}>;
66+
export type FetchAdvancedOpts = Omit<
67+
RequestInit & Partial<{
68+
/** Timeout in milliseconds after which the fetch call will be canceled with an AbortController signal */
69+
timeout: number;
70+
}>,
71+
"signal"
72+
>;
7073

7174
/** Calls the fetch API with special options like a timeout */
72-
export async function fetchAdvanced(url: string, options: FetchAdvancedOpts = {}) {
75+
export async function fetchAdvanced(input: RequestInfo | URL, options: FetchAdvancedOpts = {}) {
7376
const { timeout = 10000 } = options;
7477

75-
const controller = new AbortController();
76-
const id = setTimeout(() => controller.abort(), timeout);
78+
let signalOpts: Partial<RequestInit> = {},
79+
id: NodeJS.Timeout | undefined = undefined;
7780

78-
const res = await fetch(url, {
81+
if(timeout >= 0) {
82+
const controller = new AbortController();
83+
id = setTimeout(() => controller.abort(), timeout);
84+
signalOpts = { signal: controller.signal };
85+
}
86+
87+
const res = await fetch(input, {
7988
...options,
80-
signal: controller.signal,
89+
...signalOpts,
8190
});
8291

8392
clearTimeout(id);

0 commit comments

Comments
 (0)