Files
Catalyst/js/db.js
josh 6e40413385
All checks were successful
Build / test (push) Successful in 9m28s
Build / release (push) Successful in 1s
Build / build (push) Successful in 25s
claude went crazy
2026-03-28 02:35:00 -04:00

58 lines
2.0 KiB
JavaScript

// API client — replaces the sql.js database layer.
// Swap these fetch() calls for any other transport when needed.
const BASE = '/api';
async function api(path, options = {}) {
const res = await fetch(BASE + path, options);
if (res.status === 204) return null;
return res.json().then(data => ({ ok: res.ok, status: res.status, data }));
}
// ── Queries ───────────────────────────────────────────────────────────────────
async function getInstances(filters = {}) {
const params = new URLSearchParams(
Object.entries(filters).filter(([, v]) => v)
);
const res = await fetch(`${BASE}/instances?${params}`);
return res.json();
}
async function getInstance(vmid) {
const res = await fetch(`${BASE}/instances/${vmid}`);
if (res.status === 404) return null;
return res.json();
}
async function getDistinctStacks() {
const res = await fetch(`${BASE}/instances/stacks`);
return res.json();
}
// ── Mutations ─────────────────────────────────────────────────────────────────
async function createInstance(data) {
const { ok, data: body } = await api('/instances', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
if (!ok) return { ok: false, error: body.error ?? body.errors?.[0] ?? 'error creating instance' };
return { ok: true };
}
async function updateInstance(vmid, data) {
const { ok, data: body } = await api(`/instances/${vmid}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
if (!ok) return { ok: false, error: body.error ?? body.errors?.[0] ?? 'error updating instance' };
return { ok: true };
}
async function deleteInstance(vmid) {
await api(`/instances/${vmid}`, { method: 'DELETE' });
}