2026-05-19 00:30:20 +02:00
|
|
|
const API_BASE = process.env.NEXT_PUBLIC_API_URL ?? 'http://localhost:4000';
|
|
|
|
|
|
2026-05-21 00:26:44 +02:00
|
|
|
export async function apiFetch<T = unknown>(path: string, init: RequestInit = {}): Promise<T> {
|
2026-05-19 00:30:20 +02:00
|
|
|
const res = await fetch(`${API_BASE}${path}`, {
|
|
|
|
|
credentials: 'include',
|
|
|
|
|
cache: 'no-store',
|
|
|
|
|
...init,
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
...(init.headers ?? {}),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
if (!res.ok) {
|
|
|
|
|
let detail: unknown = undefined;
|
|
|
|
|
try {
|
|
|
|
|
detail = await res.json();
|
|
|
|
|
} catch {}
|
|
|
|
|
const err = new Error(`api_error_${res.status}`);
|
|
|
|
|
(err as unknown as { detail?: unknown }).detail = detail;
|
|
|
|
|
(err as unknown as { status?: number }).status = res.status;
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
return (await res.json()) as T;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-21 00:26:44 +02:00
|
|
|
/** Absolute API URL for a path — use for full-page navigations (OAuth redirects). */
|
|
|
|
|
export function apiUrl(path: string): string {
|
|
|
|
|
return `${API_BASE}${path}`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 00:30:20 +02:00
|
|
|
export function apiWebSocketURL(path: string): string {
|
|
|
|
|
const httpBase = API_BASE;
|
|
|
|
|
const wsBase = httpBase.replace(/^http/, 'ws');
|
|
|
|
|
return `${wsBase}${path}`;
|
|
|
|
|
}
|