-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.ts
More file actions
103 lines (85 loc) · 3.64 KB
/
Copy pathapi.ts
File metadata and controls
103 lines (85 loc) · 3.64 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import type { FixturePoint, RequestCase, ServiceKind, ServiceStatus } from "../shared/types";
export type ServicesResponse = Record<ServiceKind, ServiceStatus>;
export type ReportResponse = {
markdownPath: string;
jsonPath: string;
markdown: string;
html: string;
};
export async function getConfig(): Promise<{ hasTomTomApiKey: boolean }> {
const response = await fetch("/api/config");
return response.json();
}
export async function getFixtures(profile?: string): Promise<{ points: FixturePoint[]; seed: number }> {
const path = profile ? `/api/fixtures?profile=${encodeURIComponent(profile)}` : "/api/fixtures";
const response = await fetch(path);
return response.json();
}
export async function getCases(profile: string): Promise<RequestCase[]> {
const response = await fetch(`/api/cases?profile=${encodeURIComponent(profile)}`);
return response.json();
}
export async function getServices(): Promise<ServicesResponse> {
const response = await fetch("/api/services");
ensureOk(response);
return response.json();
}
export async function checkService(kind: ServiceKind, baseUrl?: string): Promise<ServiceStatus> {
return postJson<ServiceStatus>(`/api/services/${kind}/check`, baseUrl === undefined ? undefined : { baseUrl });
}
export async function configureService(kind: ServiceKind, baseUrl: string, sourcePath: string): Promise<ServiceStatus> {
return postJson<ServiceStatus>(`/api/services/${kind}/config`, { baseUrl, sourcePath });
}
export async function startService(kind: ServiceKind, baseUrl: string, sourcePath: string): Promise<ServiceStatus> {
return postJson<ServiceStatus>(`/api/services/${kind}/start`, { baseUrl, sourcePath });
}
export async function stopServices(): Promise<ServicesResponse> {
return postJson<ServicesResponse>("/api/services/stop");
}
export async function startRun(profile: string, requestDelaySeconds: number): Promise<{ state: string; totalCases: number }> {
const response = await fetch("/api/run/start", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ profile, requestDelaySeconds })
});
if (!response.ok) {
const body = await response.json().catch(() => undefined);
const unavailable = Array.isArray(body?.unavailable) ? `: ${body.unavailable.join(", ")}` : "";
throw new Error(`${body?.error ?? `Request failed with status ${response.status}`}${unavailable}`);
}
return response.json();
}
export async function pauseRun(): Promise<void> {
await postJson("/api/run/pause");
}
export async function resumeRun(): Promise<void> {
await postJson("/api/run/resume");
}
export async function updateRunDelay(requestDelaySeconds: number): Promise<void> {
await postJson("/api/run/delay", { requestDelaySeconds });
}
export async function stopRun(): Promise<void> {
await postJson("/api/run/stop");
}
export function connectEvents(onEvent: (event: unknown) => void): () => void {
const source = new EventSource("/api/events");
source.onmessage = (message) => onEvent(JSON.parse(message.data));
return () => source.close();
}
export async function saveReport(): Promise<ReportResponse> {
const response = await fetch("/api/report/save", { method: "POST" });
ensureOk(response);
return response.json();
}
async function postJson<T>(path: string, body?: unknown): Promise<T> {
const response = await fetch(path, {
method: "POST",
headers: { "content-type": "application/json" },
body: body === undefined ? undefined : JSON.stringify(body)
});
ensureOk(response);
return response.json();
}
function ensureOk(response: Response): void {
if (!response.ok) throw new Error(`Request failed with status ${response.status}`);
}