51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
const DEFAULT_GANTT_API_BASE_URL = "http://localhost:3000/api";
|
|
|
|
function normalizeBaseUrl(url: string): string {
|
|
return url.replace(/\/+$/, "");
|
|
}
|
|
|
|
export function getGanttApiBaseUrl(): string {
|
|
const configured =
|
|
process.env.GANTT_API_BASE_URL ||
|
|
process.env.NEXT_PUBLIC_GANTT_API_BASE_URL ||
|
|
DEFAULT_GANTT_API_BASE_URL;
|
|
return normalizeBaseUrl(configured);
|
|
}
|
|
|
|
function buildAuthHeaders(): HeadersInit {
|
|
const headers: Record<string, string> = {};
|
|
|
|
if (process.env.GANTT_API_BEARER_TOKEN) {
|
|
headers.Authorization = `Bearer ${process.env.GANTT_API_BEARER_TOKEN}`;
|
|
}
|
|
|
|
if (process.env.GANTT_API_COOKIE) {
|
|
headers.Cookie = process.env.GANTT_API_COOKIE;
|
|
}
|
|
|
|
return headers;
|
|
}
|
|
|
|
export async function fetchGanttApi<T>(endpoint: string): Promise<T> {
|
|
const baseUrl = getGanttApiBaseUrl();
|
|
const response = await fetch(`${baseUrl}${endpoint}`, {
|
|
method: "GET",
|
|
cache: "no-store",
|
|
headers: {
|
|
Accept: "application/json",
|
|
...buildAuthHeaders(),
|
|
},
|
|
});
|
|
|
|
const payload = (await response.json().catch(() => null)) as
|
|
| { error?: string; message?: string }
|
|
| null;
|
|
|
|
if (!response.ok) {
|
|
const details = payload?.error || payload?.message || response.statusText;
|
|
throw new Error(`gantt-board API request failed (${response.status} ${endpoint}): ${details}`);
|
|
}
|
|
|
|
return payload as T;
|
|
}
|