Merge pull request #27 from rede5/codex/fix-loading-issue-with-users-j268w2
Add timeout handling for API requests
This commit is contained in:
commit
4b51feeacd
1 changed files with 17 additions and 8 deletions
|
|
@ -52,14 +52,23 @@ async function apiRequest<T>(
|
||||||
const url = `${baseUrl}${cleanEndpoint}`;
|
const url = `${baseUrl}${cleanEndpoint}`;
|
||||||
console.log(`[API Request] ${url}`); // Debug log
|
console.log(`[API Request] ${url}`); // Debug log
|
||||||
|
|
||||||
const res = await fetch(url, {
|
const controller = new AbortController();
|
||||||
...options,
|
const timeout = setTimeout(() => controller.abort(), 15000);
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
let res: Response;
|
||||||
...(token && { Authorization: `Bearer ${token}` }),
|
try {
|
||||||
...options.headers,
|
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) {
|
if (!res.ok) {
|
||||||
const error = await res.text();
|
const error = await res.text();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue