feat: add WebSocket hook and API client for frontend

This commit is contained in:
plenarius
2026-04-20 18:37:34 -04:00
parent 47d0ee60c0
commit bc434254ce
2 changed files with 103 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
const BASE = "/api";
async function request<T>(path: string, options?: RequestInit): Promise<T> {
const res = await fetch(`${BASE}${path}`, {
headers: { "Content-Type": "application/json" },
...options,
});
if (!res.ok) {
const body = await res.json().catch(() => ({ error: res.statusText }));
throw new Error(body.error || res.statusText);
}
return res.json();
}
export const api = {
health: () => request<{ status: string; wsClients: number }>("/health"),
workspace: {
getConfig: () => request<Record<string, unknown>>("/workspace/config"),
updateConfig: (data: Record<string, unknown>) =>
request("/workspace/config", { method: "PUT", body: JSON.stringify(data) }),
init: () => request("/workspace/init", { method: "POST" }),
},
docker: {
containers: () => request<Array<Record<string, string>>>("/docker/containers"),
pull: (image: string) =>
request("/docker/pull", { method: "POST", body: JSON.stringify({ image }) }),
start: (name: string) =>
request(`/docker/containers/${name}/start`, { method: "POST" }),
stop: (name: string) =>
request(`/docker/containers/${name}/stop`, { method: "POST" }),
restart: (name: string) =>
request(`/docker/containers/${name}/restart`, { method: "POST" }),
logs: (name: string, tail = 100) =>
request<{ logs: string }>(`/docker/containers/${name}/logs?tail=${tail}`),
},
};