init
This commit is contained in:
28
frontend/src/api.js
Normal file
28
frontend/src/api.js
Normal file
@@ -0,0 +1,28 @@
|
||||
async function req(pfad, opts = {}) {
|
||||
const r = await fetch('/api' + pfad, {
|
||||
headers: { 'content-type': 'application/json' },
|
||||
...opts,
|
||||
body: opts.body ? JSON.stringify(opts.body) : undefined,
|
||||
})
|
||||
if (!r.ok) {
|
||||
let detail = r.statusText
|
||||
try { detail = (await r.json()).detail || detail } catch { /* leer */ }
|
||||
throw new Error(detail)
|
||||
}
|
||||
const ct = r.headers.get('content-type') || ''
|
||||
return ct.includes('json') ? r.json() : r.text()
|
||||
}
|
||||
|
||||
const e = encodeURIComponent
|
||||
export const api = {
|
||||
topics: () => req('/topics'),
|
||||
anlegen: (name, hinweise = '') => req('/topics', { method: 'POST', body: { name, hinweise } }),
|
||||
loeschen: (t) => req(`/topics/${e(t)}`, { method: 'DELETE' }),
|
||||
start: (t) => req(`/topics/${e(t)}/start`, { method: 'POST' }),
|
||||
pause: (t) => req(`/topics/${e(t)}/pause`, { method: 'POST' }),
|
||||
reset: (t) => req(`/topics/${e(t)}/reset`, { method: 'POST' }),
|
||||
state: (t) => req(`/topics/${e(t)}/state`),
|
||||
guide: (t) => req(`/topics/${e(t)}/guide`),
|
||||
guideMdUrl: (t) => `/api/topics/${e(t)}/guide.md`,
|
||||
pdfUrl: (t) => `/api/topics/${e(t)}/buch.pdf`,
|
||||
}
|
||||
Reference in New Issue
Block a user