Spaces:
Running
Running
File size: 742 Bytes
abd0b64 5a264f5 b2ffd25 5a264f5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | const BASE = ''
async function request(path, options = {}) {
const url = path.startsWith('/') ? `${BASE}${path}` : `${BASE}/${path}`
const res = await fetch(url, {
credentials: 'include',
headers: { 'Content-Type': 'application/json', ...(options.headers || {}) },
...options,
})
const json = await res.json().catch(() => ({}))
if (!res.ok) throw new Error(json.error || `HTTP ${res.status}`)
return json
}
export const api = {
get: (path) => request(path),
post: (path, body) => request(path, { method: 'POST', body: JSON.stringify(body) }),
put: (path, body) => request(path, { method: 'PUT', body: JSON.stringify(body) }),
del: (path) => request(path, { method: 'DELETE' }),
}
|