Spaces:
Running
Running
| 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' }), | |
| } | |