Spaces:
Sleeping
Sleeping
File size: 1,105 Bytes
57a9d02 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /** Tüm backend çağrıları: JSON, cookie (session) ve isteğe bağlı Bearer token. */
import { getAuthToken } from "../auth/token.js";
export async function apiRequest(path, options = {}) {
const token = getAuthToken();
const response = await fetch(path, {
credentials: "include",
headers: {
"Content-Type": "application/json",
...(token ? { Authorization: `Bearer ${token}` } : {}),
...(options.headers || {}),
},
...options,
});
const data = await response.json().catch(() => ({}));
if (!response.ok) {
let msg = "Bir hata olustu";
const d = data.detail;
if (typeof d === "string") msg = d;
else if (Array.isArray(d))
msg = d.map((x) => (x && typeof x === "object" && x.msg ? x.msg : String(x))).join(", ");
else if (data.message) msg = data.message;
const err = new Error(msg);
err.status = response.status;
throw err;
}
return data;
}
export function isAuthError(err) {
if (!err) return false;
return (
err.status === 401 || err.message === "Yetkisiz erisim" || err.message === "Oturum yok"
);
}
|