feat: add WebSocket hook and API client for frontend
This commit is contained in:
@@ -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}`),
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user