import type { Health, Job, JobEvent, JobStats } from "./types"; const API_BASE_URL = import.meta.env.VITE_API_BASE_URL ?? "http://localhost:8000/api"; async function request(path: string, init?: RequestInit): Promise { const response = await fetch(`${API_BASE_URL}${path}`, { headers: { "Content-Type": "application/json", ...(init?.headers ?? {}) }, ...init }); if (!response.ok) { const body = await response.text(); throw new Error(body || response.statusText); } return response.json() as Promise; } export type CreateJobBody = { type: string; payload?: Record; priority?: number; available_at?: string | null; max_attempts?: number; idempotency_key?: string | null; }; export const api = { health: () => request("/health/"), listJobs: () => request("/jobs/"), getJob: (jobId: string) => request(`/jobs/${jobId}/`), createJob: (body: CreateJobBody) => request("/jobs/", { method: "POST", body: JSON.stringify(body) }), retryJob: (jobId: string) => request(`/jobs/${jobId}/retry/`, { method: "POST" }), getStats: () => request("/jobs/stats/"), listJobEvents: (jobId: string) => request(`/jobs/${jobId}/events/`), listEvents: (filters: { after_id?: number; limit?: number; job_id?: string; type?: string } = {}) => { const params = new URLSearchParams(); Object.entries(filters).forEach(([key, value]) => { if (value !== undefined && value !== null && value !== "") params.set(key, String(value)); }); const query = params.toString(); return request(`/job-events/${query ? `?${query}` : ""}`); } };