init
This commit is contained in:
43
static/js/api.js
Normal file
43
static/js/api.js
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* static/js/api.js
|
||||
* Thin fetch wrappers for all HTTP API endpoints.
|
||||
*/
|
||||
|
||||
const BASE = '';
|
||||
|
||||
async function request(method, path, body) {
|
||||
const opts = {
|
||||
method,
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
};
|
||||
if (body !== undefined) opts.body = JSON.stringify(body);
|
||||
|
||||
const res = await fetch(BASE + path, opts);
|
||||
if (res.status === 204) return null;
|
||||
|
||||
const data = await res.json();
|
||||
if (!res.ok) {
|
||||
const msg = data?.detail || `HTTP ${res.status}`;
|
||||
throw new Error(msg);
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
export const api = {
|
||||
// Health
|
||||
health: () => request('GET', '/healthz'),
|
||||
|
||||
// Status
|
||||
status: () => request('GET', '/api/status'),
|
||||
|
||||
// Instructions
|
||||
listInstructions: (status='all') => request('GET', `/api/instructions?status=${status}`),
|
||||
createInstruction: (content) => request('POST', '/api/instructions', { content }),
|
||||
updateInstruction: (id, content) => request('PATCH', `/api/instructions/${id}`, { content }),
|
||||
deleteInstruction: (id) => request('DELETE', `/api/instructions/${id}`),
|
||||
|
||||
// Config
|
||||
getConfig: () => request('GET', '/api/config'),
|
||||
updateConfig: (patch) => request('PATCH', '/api/config', patch),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user