Pranav_rs
remove unnecessary auth files
e91185c
raw
history blame contribute delete
845 Bytes
const BASE_URL = 'http://localhost:8000';
export async function fetchApi<T>(
endpoint: string,
options: RequestInit = {}
): Promise<T> {
const headers = new Headers(options.headers || {});
// Default to JSON content type for non-FormData requests
if (!headers.has('Content-Type') && !(options.body instanceof FormData)) {
headers.set('Content-Type', 'application/json');
}
const response = await fetch(`${BASE_URL}${endpoint}`, {
...options,
headers,
});
if (!response.ok) {
let errorMsg = 'An unexpected error occurred';
try {
const errorData = await response.json();
errorMsg = errorData.detail || errorMsg;
} catch { /* ignore parse error */ }
throw new Error(errorMsg);
}
// Handle empty responses
if (response.status === 204) return {} as T;
return response.json();
}