diff --git a/src/lib/api.ts b/src/lib/api.ts index 2289a5d..40dd3a1 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -1,4 +1,5 @@ -const API_BASE = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8100"; +// API proxied through nginx on same domain +const API_BASE = process.env.NEXT_PUBLIC_API_URL || ""; function getAuthHeaders(): Record { if (typeof window !== "undefined") { @@ -9,13 +10,24 @@ function getAuthHeaders(): Record { } async function request(path: string, opts?: RequestInit): Promise { - const res = await fetch(`${API_BASE}${path}`, { + const url = `${API_BASE}${path}`; + console.log(`[API] ${opts?.method || "GET"} ${url}`); + + const res = await fetch(url, { headers: { "Content-Type": "application/json", ...getAuthHeaders(), ...opts?.headers }, ...opts, }); - if (!res.ok) throw new Error(`API ${res.status}: ${await res.text()}`); + + if (!res.ok) { + const body = await res.text(); + console.error(`[API] ${res.status} ${url}`, body); + throw new Error(`API ${res.status}: ${body}`); + } + if (res.status === 204) return undefined as T; - return res.json(); + const data = await res.json(); + console.log(`[API] ${res.status} ${url}`, data); + return data; } // Types