geo/frontend/lib/api/detection.ts

55 lines
1.8 KiB
TypeScript

import { fetchWithAuth } from "./client";
function buildQuery(params: Record<string, string | number | boolean | undefined>): string {
const qs = Object.entries(params)
.filter(([, v]) => v !== undefined)
.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
.join("&");
return qs ? `?${qs}` : "";
}
export interface DetectionTask {
id: string;
query_id: string;
platforms: string[];
frequency: string;
status: string;
last_run_at: string | null;
next_run_at: string | null;
created_at: string;
}
export interface DetectionTaskList {
items: DetectionTask[];
total: number;
}
export interface DetectionTaskCreate {
query_id: string;
platforms: string[];
frequency: string;
}
export interface DetectionTaskUpdate {
platforms?: string[];
frequency?: string;
status?: string;
}
export const detectionApi = {
listTasks: (token: string, params?: { skip?: number; limit?: number; status?: string }) =>
fetchWithAuth(`/api/v1/detection/tasks${buildQuery(params || {})}`, {}, token) as Promise<DetectionTaskList>,
createTask: (token: string, data: DetectionTaskCreate) =>
fetchWithAuth("/api/v1/detection/tasks", { method: "POST", body: JSON.stringify(data) }, token) as Promise<DetectionTask>,
updateTask: (token: string, taskId: string, data: DetectionTaskUpdate) =>
fetchWithAuth(`/api/v1/detection/tasks/${taskId}`, { method: "PUT", body: JSON.stringify(data) }, token) as Promise<DetectionTask>,
deleteTask: (token: string, taskId: string) =>
fetchWithAuth(`/api/v1/detection/tasks/${taskId}`, { method: "DELETE" }, token) as Promise<void>,
triggerTask: (token: string, taskId: string) =>
fetchWithAuth(`/api/v1/detection/tasks/${taskId}/trigger`, { method: "POST" }, token) as Promise<DetectionTask>,
};