qudoco-fe/lib/public-api.ts

28 lines
821 B
TypeScript
Raw Normal View History

2026-04-10 07:21:29 +00:00
const DEFAULT_CLIENT_KEY = "9ca7f706-a8b0-4520-b467-5e8321df36fb";
function clientKey() {
return process.env.NEXT_PUBLIC_X_CLIENT_KEY ?? DEFAULT_CLIENT_KEY;
}
function apiBase() {
const base = process.env.NEXT_PUBLIC_API_URL?.replace(/\/$/, "");
return base ?? "";
}
/** Server-side fetch for public landing data (requires `X-Client-Key`). */
export async function publicFetch<T>(path: string): Promise<T | null> {
const base = apiBase();
if (!base) return null;
const url = `${base}${path.startsWith("/") ? path : `/${path}`}`;
const res = await fetch(url, {
headers: {
"Content-Type": "application/json",
"X-Client-Key": clientKey(),
},
next: { revalidate: 60 },
});
if (!res.ok) return null;
const json = (await res.json()) as { data?: T };
return json.data ?? null;
}