18 lines
577 B
TypeScript
18 lines
577 B
TypeScript
const API_BASE = 'http://localhost:8000';
|
|
|
|
export async function fetchApi<T>(endpoint: string, options?: RequestInit): Promise<T> {
|
|
const url = endpoint.startsWith('http') ? endpoint : `${API_BASE}${endpoint}`;
|
|
const res = await fetch(url, {
|
|
...options,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
...(options?.headers || {}),
|
|
},
|
|
});
|
|
if (!res.ok) {
|
|
const errData = await res.json().catch(() => ({ detail: res.statusText }));
|
|
throw new Error(errData.detail || `Request failed with status ${res.status}`);
|
|
}
|
|
return res.json();
|
|
}
|