first commit
This commit is contained in:
26
frontend/lib/api.ts
Normal file
26
frontend/lib/api.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
export const API_BASE = process.env.NEXT_PUBLIC_API_BASE || "http://localhost:8000";
|
||||
export const WS_BASE = process.env.NEXT_PUBLIC_WS_BASE || "ws://localhost:8000";
|
||||
|
||||
export async function apiFetch(path: string, opts: RequestInit = {}) {
|
||||
const res = await fetch(`${API_BASE}${path}`, {
|
||||
...opts,
|
||||
credentials: "include",
|
||||
headers: {
|
||||
...(opts.headers || {}),
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
});
|
||||
if (!res.ok) {
|
||||
const txt = await res.text().catch(() => "");
|
||||
throw new Error(txt || `HTTP ${res.status}`);
|
||||
}
|
||||
const ct = res.headers.get("content-type") || "";
|
||||
if (ct.includes("application/json")) return res.json();
|
||||
return res.text();
|
||||
}
|
||||
|
||||
export function getCsrfFromCookie(): string | null {
|
||||
if (typeof document === "undefined") return null;
|
||||
const m = document.cookie.match(/(?:^|; )mt_csrf=([^;]+)/);
|
||||
return m ? decodeURIComponent(m[1]) : null;
|
||||
}
|
||||
Reference in New Issue
Block a user