work
This commit is contained in:
+6
-10
@@ -2,18 +2,14 @@ export async function api(path, options = {}) {
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 12000);
|
||||
try {
|
||||
const response = await fetch(path, {
|
||||
...options,
|
||||
headers: { "content-type": "application/json", ...(options.headers || {}) },
|
||||
signal: controller.signal,
|
||||
});
|
||||
const headers = new Headers(options.headers || {});
|
||||
if (!(options.body instanceof FormData) && !headers.has("content-type")) headers.set("content-type", "application/json");
|
||||
const response = await fetch(path, { ...options, headers, signal: controller.signal });
|
||||
const data = await response.json().catch(() => ({}));
|
||||
if (!response.ok) throw new Error(data.error || `Błąd ${response.status}`);
|
||||
if (!response.ok) throw new Error(data.error || `Error ${response.status}`);
|
||||
return data;
|
||||
} catch (error) {
|
||||
if (error.name === "AbortError") throw new Error("Przekroczono czas odpowiedzi serwera");
|
||||
if (error.name === "AbortError") throw new Error("Timed out");
|
||||
throw error;
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
}
|
||||
} finally { clearTimeout(timeout); }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user