Add timeout to API requests

This commit is contained in:
Tiago Yamamoto 2025-12-22 19:51:54 -03:00
parent 3f87b61838
commit aab5822f48

View file

@ -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();