20 lines
681 B
JavaScript
20 lines
681 B
JavaScript
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 data = await response.json().catch(() => ({}));
|
|
if (!response.ok) throw new Error(data.error || `Błąd ${response.status}`);
|
|
return data;
|
|
} catch (error) {
|
|
if (error.name === "AbortError") throw new Error("Przekroczono czas odpowiedzi serwera");
|
|
throw error;
|
|
} finally {
|
|
clearTimeout(timeout);
|
|
}
|
|
}
|