Spaces:
Running
Running
File size: 587 Bytes
5539271 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | interface FetchOptions extends RequestInit {
skipContentType?: boolean
}
export async function apiFetch<T = unknown>(url: string, options: FetchOptions = {}): Promise<T> {
const headers: Record<string, string> = { ...(options.headers as Record<string, string>) }
if (!options.skipContentType) {
headers['Content-Type'] = 'application/json'
}
const response = await fetch(url, {
...options,
headers,
})
if (!response.ok) throw new Error(`API error: ${response.status}`)
if (response.status === 204) return null as T
return response.json() as Promise<T>
}
|