fix: proxy API through nginx, add client-side logging
All checks were successful
Deploy Web Client / deploy (push) Successful in 35s
All checks were successful
Deploy Web Client / deploy (push) Successful in 35s
This commit is contained in:
parent
920eb99a6e
commit
e8a7852986
@ -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<string, string> {
|
function getAuthHeaders(): Record<string, string> {
|
||||||
if (typeof window !== "undefined") {
|
if (typeof window !== "undefined") {
|
||||||
@ -9,13 +10,24 @@ function getAuthHeaders(): Record<string, string> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function request<T>(path: string, opts?: RequestInit): Promise<T> {
|
async function request<T>(path: string, opts?: RequestInit): Promise<T> {
|
||||||
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 },
|
headers: { "Content-Type": "application/json", ...getAuthHeaders(), ...opts?.headers },
|
||||||
...opts,
|
...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;
|
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
|
// Types
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user