const base = '/api' async function req(path, opts = {}) { const res = await fetch(base + path, { headers: { 'Content-Type': 'application/json' }, ...opts, }) if (!res.ok) { const text = await res.text().catch(() => '') throw new Error(`${res.status}: ${text || res.statusText}`) } if (res.status === 204) return null return res.json() } export const api = { status: () => req('/status'), config: () => req('/config'), putConfig: (cfg) => req('/config', { method: 'PUT', body: JSON.stringify(cfg) }), domains: () => req('/domains'), addDomain: (d) => req('/domains', { method: 'POST', body: JSON.stringify(d) }), deleteDomain: (name) => req(`/domains/${encodeURIComponent(name)}`, { method: 'DELETE' }), patchDomain: (name, body) => req(`/domains/${encodeURIComponent(name)}`, { method: 'PATCH', body: JSON.stringify(body), }), logs: (params = {}) => { const q = new URLSearchParams( Object.fromEntries(Object.entries(params).filter(([, v]) => v !== '' && v != null)) ).toString() return req('/logs' + (q ? `?${q}` : '')) }, clearLogs: (params = {}) => { const q = new URLSearchParams( Object.fromEntries(Object.entries(params).filter(([, v]) => v !== '' && v != null)) ).toString() return req('/logs' + (q ? `?${q}` : ''), { method: 'DELETE' }) }, restart: () => req('/proxy/restart', { method: 'POST' }), }