File size: 1,976 Bytes
3806142
 
 
 
 
 
 
bc2711b
3806142
 
bc2711b
3806142
 
 
bc2711b
3806142
 
 
bc2711b
3806142
 
 
 
 
 
bc2711b
 
 
 
 
3806142
 
 
 
bc2711b
3806142
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc2711b
 
 
 
 
3806142
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
import type { AnalyzeResponse, BuildStatus } from "../types";

async function errMsg(res: Response): Promise<string> {
  try {
    const j = await res.json();
    const d = j.detail;
    if (typeof d === "string") return d;
    if (d?.message) return `${d.message}${d.code ? ` [${d.code}]` : ""}`;
    return JSON.stringify(d || j);
  } catch {
    return `HTTP ${res.status} ${res.statusText}`;
  }
}

export async function analyzeSpace(space_url: string, hf_token?: string): Promise<AnalyzeResponse> {
  const res = await fetch("/api/analyze", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ space_url, hf_token: hf_token || undefined }),
  });
  if (!res.ok) throw new Error(await errMsg(res));
  return res.json();
}

export async function startBuild(
  space_url: string,
  target: string,
  mode: string,
  hf_token?: string,
  options: Record<string, unknown> = {}
): Promise<{ build_id: string; status: string }> {
  const res = await fetch("/api/build", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ space_url, target, mode, options, hf_token: hf_token || undefined }),
  });
  if (!res.ok) throw new Error(await errMsg(res));
  return res.json();
}

export async function getBuild(id: string): Promise<BuildStatus> {
  const res = await fetch(`/api/build/${id}`);
  if (!res.ok) throw new Error(await errMsg(res));
  return res.json();
}

export async function getLogs(id: string): Promise<string> {
  const res = await fetch(`/api/build/${id}/logs`);
  if (!res.ok) throw new Error(await errMsg(res));
  return res.text();
}

export async function deleteBuild(id: string): Promise<void> {
  const res = await fetch(`/api/build/${id}`, { method: "DELETE" });
  if (!res.ok) throw new Error(await errMsg(res));
}

export function downloadUrl(id: string, target: string): string {
  return `/api/build/${id}/download?target=${encodeURIComponent(target)}`;
}