const BASE = "/api"; async function request(path: string, options?: RequestInit): Promise { 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 interface FileNode { name: string; path: string; type: "file" | "directory"; children?: FileNode[]; } export const api = { health: () => request<{ status: string; wsClients: number }>("/health"), editor: { tree: (repo: string) => request(`/editor/tree/${repo}`), readFile: (repo: string, filePath: string) => request<{ content: string }>(`/editor/file/${repo}/${filePath}`), writeFile: (repo: string, filePath: string, content: string) => request(`/editor/file/${repo}/${filePath}`, { method: "PUT", body: JSON.stringify({ content }), }), deleteFile: (repo: string, filePath: string) => request(`/editor/file/${repo}/${filePath}`, { method: "DELETE" }), search: (repo: string, query: string, opts?: Record) => request<{ matches: Array<{ file: string; line: number; column: number; text: string; matchLength: number }>; totalMatches: number; filesSearched: number; }>("/editor/search", { method: "POST", body: JSON.stringify({ repo, query, ...opts }) }), }, workspace: { getConfig: () => request>("/workspace/config"), updateConfig: (data: Record) => request("/workspace/config", { method: "PUT", body: JSON.stringify(data) }), init: () => request("/workspace/init", { method: "POST" }), }, build: { compileModule: (target?: string) => request("/build/module/compile", { method: "POST", body: JSON.stringify({ target }) }), packModule: (target?: string) => request("/build/module/pack", { method: "POST", body: JSON.stringify({ target }) }), deploy: () => request("/build/deploy", { method: "POST" }), compileSingle: (filePath: string) => request("/build/compile-single", { method: "POST", body: JSON.stringify({ filePath }) }), buildHaks: () => request("/build/haks", { method: "POST" }), buildNwnx: (target?: string) => request("/build/nwnx", { method: "POST", body: JSON.stringify({ target }) }), }, docker: { containers: () => request>>("/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}`), }, };