16 lines
424 B
JavaScript
16 lines
424 B
JavaScript
|
|
export async function api(url, options = {}) {
|
||
|
|
const res = await fetch(url, {
|
||
|
|
credentials: 'include',
|
||
|
|
headers: { 'Content-Type': 'application/json', ...options.headers },
|
||
|
|
...options,
|
||
|
|
body: options.body ? JSON.stringify(options.body) : undefined,
|
||
|
|
})
|
||
|
|
|
||
|
|
if (!res.ok) {
|
||
|
|
const error = await res.json().catch(() => ({}))
|
||
|
|
throw new Error(error.error || `HTTP ${res.status}`)
|
||
|
|
}
|
||
|
|
|
||
|
|
return res.json()
|
||
|
|
}
|