File size: 1,490 Bytes
3c07134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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));
}