78 lines
3.0 KiB
TypeScript
78 lines
3.0 KiB
TypeScript
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 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<FileNode[]>(`/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<string, unknown>) =>
|
|
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<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" }),
|
|
},
|
|
|
|
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<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}`),
|
|
},
|
|
};
|