Spaces:
Sleeping

Muhasebe / frontend /src /api /client.js
Caner7's picture
Add FastAPI backend, React frontend, Docker and project docs
57a9d02
Raw History Blame Contribute Delete
1.11 kB
/** 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"
);
}