52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
const BASE_URL = import.meta.env.VITE_API_URL ?? '';
|
|
|
|
export interface ImageBuildStatus {
|
|
imageName: string | null;
|
|
builtAt: string | null;
|
|
lastMessage: string;
|
|
isBuilding: boolean;
|
|
}
|
|
|
|
export interface BuildHistoryRecord {
|
|
id: string;
|
|
status: 'Running' | 'Succeeded' | 'Failed';
|
|
startedAt: string;
|
|
durationMs: number | null;
|
|
commitSha: string | null;
|
|
imageDigest: string | null;
|
|
}
|
|
|
|
export async function getImageStatus(): Promise<ImageBuildStatus> {
|
|
const res = await fetch(`${BASE_URL}/api/image/status`);
|
|
if (!res.ok) throw new Error(`Failed to get image status: ${res.statusText}`);
|
|
return res.json();
|
|
}
|
|
|
|
export async function getImageBuildHistory(limit = 30): Promise<BuildHistoryRecord[]> {
|
|
const res = await fetch(`${BASE_URL}/api/image/history?limit=${limit}`);
|
|
if (!res.ok) throw new Error(`Failed to get build history: ${res.statusText}`);
|
|
return res.json();
|
|
}
|
|
|
|
export function triggerImageBuild(
|
|
onLine: (line: string) => void,
|
|
onDone: (success: boolean) => void,
|
|
onError: (err: Event) => void,
|
|
): EventSource {
|
|
const source = new EventSource(`${BASE_URL}/api/image/build-stream`);
|
|
source.onmessage = (e) => {
|
|
try {
|
|
const msg = JSON.parse(e.data);
|
|
if (msg.done) { onDone(true); source.close(); }
|
|
else if (msg.line) onLine(msg.line);
|
|
} catch { /* ignore */ }
|
|
};
|
|
source.onerror = (e) => { onDone(false); onError(e); };
|
|
return source;
|
|
}
|
|
|
|
export async function startImageBuild(): Promise<void> {
|
|
const res = await fetch(`${BASE_URL}/api/image/build`, { method: 'POST' });
|
|
if (!res.ok) throw new Error(`Build trigger failed: ${res.statusText}`);
|
|
}
|