/** * API client for the orchestrator backend. */ import type { Job, ApiResponse, JobsListResponse, PipelineStatusResponse, JobSource, PipelineRun } from '../../shared/types'; const API_BASE = '/api'; async function fetchApi( endpoint: string, options?: RequestInit ): Promise { const response = await fetch(`${API_BASE}${endpoint}`, { ...options, headers: { 'Content-Type': 'application/json', ...options?.headers, }, }); const data: ApiResponse = await response.json(); if (!data.success) { throw new Error(data.error || 'API request failed'); } return data.data as T; } // Jobs API export async function getJobs(statuses?: string[]): Promise { const query = statuses?.length ? `?status=${statuses.join(',')}` : ''; return fetchApi(`/jobs${query}`); } export async function getJob(id: string): Promise { return fetchApi(`/jobs/${id}`); } export async function updateJob( id: string, update: Partial ): Promise { return fetchApi(`/jobs/${id}`, { method: 'PATCH', body: JSON.stringify(update), }); } export async function processJob(id: string): Promise { return fetchApi(`/jobs/${id}/process`, { method: 'POST', }); } export async function markAsApplied(id: string): Promise { return fetchApi(`/jobs/${id}/apply`, { method: 'POST', }); } export async function rejectJob(id: string): Promise { return fetchApi(`/jobs/${id}/reject`, { method: 'POST', }); } // Pipeline API export async function getPipelineStatus(): Promise { return fetchApi('/pipeline/status'); } export async function getPipelineRuns(): Promise { return fetchApi('/pipeline/runs'); } export async function runPipeline(config?: { topN?: number; minSuitabilityScore?: number; sources?: JobSource[]; }): Promise<{ message: string }> { return fetchApi<{ message: string }>('/pipeline/run', { method: 'POST', body: JSON.stringify(config || {}), }); } // Database API export async function clearDatabase(): Promise<{ message: string; jobsDeleted: number; runsDeleted: number; }> { return fetchApi<{ message: string; jobsDeleted: number; runsDeleted: number; }>('/database', { method: 'DELETE', }); } // Bulk operations export async function processAllDiscovered(): Promise<{ message: string; count: number; }> { return fetchApi<{ message: string; count: number; }>('/jobs/process-discovered', { method: 'POST', }); }