40 lines
955 B
TypeScript
40 lines
955 B
TypeScript
const API_BASE = import.meta.env.VITE_API_BASE;
|
|
const API_KEY = import.meta.env.VITE_API_KEY;
|
|
|
|
export async function apiGet<T>(path: string): Promise<T> {
|
|
console.log("API_BASE:", API_BASE);
|
|
console.log("Calling:", `${API_BASE}${path}`);
|
|
|
|
const response = await fetch(`${API_BASE}${path}`, {
|
|
headers: {
|
|
"X-API-Key": API_KEY,
|
|
},
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`API error ${response.status}`);
|
|
}
|
|
|
|
return response.json();
|
|
}
|
|
|
|
export async function apiPost<TResponse, TBody>(
|
|
path: string,
|
|
body: TBody
|
|
): Promise<TResponse> {
|
|
const response = await fetch(`${API_BASE}${path}`, {
|
|
method: "POST",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"X-API-Key": API_KEY,
|
|
},
|
|
body: JSON.stringify(body),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
const error = await response.json().catch(() => null);
|
|
throw new Error(error?.error || `API error ${response.status}`);
|
|
}
|
|
|
|
return response.json();
|
|
} |