Add timeout to API requests
This commit is contained in:
parent
3f87b61838
commit
aab5822f48
1 changed files with 17 additions and 8 deletions
|
|
@ -52,14 +52,23 @@ async function apiRequest<T>(
|
|||
const url = `${baseUrl}${cleanEndpoint}`;
|
||||
console.log(`[API Request] ${url}`); // Debug log
|
||||
|
||||
const res = await fetch(url, {
|
||||
...options,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(token && { Authorization: `Bearer ${token}` }),
|
||||
...options.headers,
|
||||
},
|
||||
});
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 15000);
|
||||
|
||||
let res: Response;
|
||||
try {
|
||||
res = await fetch(url, {
|
||||
...options,
|
||||
signal: controller.signal,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...(token && { Authorization: `Bearer ${token}` }),
|
||||
...options.headers,
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
|
||||
if (!res.ok) {
|
||||
const error = await res.text();
|
||||
|
|
|
|||
Loading…
Reference in a new issue