repospace / frontend /src /api /client.ts
2Btainment's picture
Initial RepoSpace MVP: FastAPI + constrained sandbox + React UI
3c07134 verified
Raw History Blame Contribute Delete
1.49 kB
import type { LogLine, Session } from "../types";
async function parseError(res: Response): Promise<string> {
try {
const j = await res.json();
if (typeof j.detail === "string") return j.detail;
if (j.detail?.message) return j.detail.message;
return JSON.stringify(j.detail || j);
} catch {
return res.statusText || `HTTP ${res.status}`;
}
}
export async function createSession(
repoUrl: string,
githubToken?: string,
ref?: string
): Promise<Session> {
const res = await fetch("/api/sessions", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
repo_url: repoUrl,
github_token: githubToken || undefined,
ref: ref || undefined,
}),
});
if (!res.ok) throw new Error(await parseError(res));
return res.json();
}
export async function getSession(id: string): Promise<Session> {
const res = await fetch(`/api/sessions/${id}`);
if (!res.ok) throw new Error(await parseError(res));
return res.json();
}
export async function getLogs(id: string, after = 0): Promise<{ lines: LogLine[]; next_after: number }> {
const res = await fetch(`/api/sessions/${id}/logs?after=${after}`);
if (!res.ok) throw new Error(await parseError(res));
return res.json();
}
export async function deleteSession(id: string): Promise<void> {
const res = await fetch(`/api/sessions/${id}`, { method: "DELETE" });
if (!res.ok && res.status !== 404) throw new Error(await parseError(res));
}