import type { ConfidenceGroup, ConfidenceRatingCreate, ConfidenceRatingEntry, ExtractionRecordCreate, ExtractionRecordEntry, PrismaCounts, Protocol, ProtocolDraft, ReviewOptions, RobAssessmentCreate, RobAssessmentEntry, RobReference, RobSummaryItem, ScreeningRecordCreate, ScreeningRecordEntry, SearchRun, SynthesisResult, } from "../types"; const BASE = "/api/v1/protocols"; async function handle(resp: Response): Promise { if (!resp.ok) { const body = await resp.json().catch(() => ({})); throw new Error(formatDetail(body.detail) || `Request failed (${resp.status})`); } return resp.json(); } /** FastAPI returns validation errors as a list of objects — make them readable. */ function formatDetail(detail: unknown): string { if (typeof detail === "string") return detail; if (Array.isArray(detail)) { return detail .map((d) => (d && typeof d === "object" && "msg" in d ? String((d as { msg: unknown }).msg) : String(d))) .join("; "); } return ""; } export async function listProtocols(): Promise { return handle(await fetch(BASE)); } export async function getProtocol(id: string): Promise { return handle(await fetch(`${BASE}/${id}`)); } export async function createProtocol(draft: ProtocolDraft): Promise { return handle( await fetch(BASE, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(draft), }) ); } export async function updateProtocol( id: string, updates: Partial ): Promise { return handle( await fetch(`${BASE}/${id}`, { method: "PATCH", headers: { "Content-Type": "application/json" }, body: JSON.stringify(updates), }) ); } export async function lockProtocol(id: string): Promise { return handle(await fetch(`${BASE}/${id}/lock`, { method: "POST" })); } /** Documented change to a locked protocol: only the changed fields plus a reason. */ export async function amendProtocol( id: string, changes: Partial, reason: string ): Promise { return handle( await fetch(`${BASE}/${id}/amend`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ ...changes, reason }), }) ); } export async function deleteProtocol(id: string): Promise { const resp = await fetch(`${BASE}/${id}`, { method: "DELETE" }); if (!resp.ok) { const body = await resp.json().catch(() => ({})); throw new Error(formatDetail(body.detail) || `Request failed (${resp.status})`); } } /** First run needs no reason; re-runs require one (kept in the search history). */ export async function runSearch(protocolId: string, reason?: string): Promise { return handle( await fetch(`${BASE}/${protocolId}/search`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ reason: reason ?? null }), }) ); } export async function getSearchRun(protocolId: string): Promise { const resp = await fetch(`${BASE}/${protocolId}/search`); if (resp.status === 404) return null; return handle(resp); } export async function recordScreeningDecision( protocolId: string, payload: ScreeningRecordCreate ): Promise { return handle( await fetch(`${BASE}/${protocolId}/screening`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }) ); } export async function listScreeningRecords( protocolId: string ): Promise { return handle(await fetch(`${BASE}/${protocolId}/screening`)); } export async function recordExtraction( protocolId: string, payload: ExtractionRecordCreate ): Promise { return handle( await fetch(`${BASE}/${protocolId}/extraction`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }) ); } export async function listExtractionRecords( protocolId: string ): Promise { return handle(await fetch(`${BASE}/${protocolId}/extraction`)); } export async function getPrismaCounts(protocolId: string): Promise { const resp = await fetch(`${BASE}/${protocolId}/prisma`); if (resp.status === 404) return null; return handle(resp); } export async function generateSynthesis(protocolId: string): Promise { return handle(await fetch(`${BASE}/${protocolId}/synthesis`, { method: "POST" })); } export async function getSynthesis(protocolId: string): Promise { const resp = await fetch(`${BASE}/${protocolId}/synthesis`); if (resp.status === 404) return null; return handle(resp); } // --- Review type / domain dropdown options --- let reviewOptionsCache: Promise | null = null; /** Dropdown options from the backend (fetched once per page load). */ export function getReviewOptions(): Promise { if (!reviewOptionsCache) { reviewOptionsCache = fetch("/api/v1/review-options") .then((resp) => handle(resp)) .catch((err) => { reviewOptionsCache = null; throw err; }); } return reviewOptionsCache; } // --- Risk of bias (OHAT) --- let robReferenceCache: Promise | null = null; let robReferenceValue: RobReference | null = null; /** The OHAT questions, designs and ratings (fetched once per page load). */ export function getRobReference(): Promise { if (!robReferenceCache) { robReferenceCache = fetch("/api/v1/rob/reference") .then((resp) => handle(resp)) .then((ref) => { robReferenceValue = ref; return ref; }) .catch((err) => { robReferenceCache = null; throw err; }); } return robReferenceCache; } /** The reference data if it has already loaded (lets components render immediately). */ export function peekRobReference(): RobReference | null { return robReferenceValue; } export async function listRobAssessments(protocolId: string): Promise { return handle(await fetch(`${BASE}/${protocolId}/rob`)); } export async function getRobSummary(protocolId: string): Promise { return handle(await fetch(`${BASE}/${protocolId}/rob/summary`)); } export async function recordRobAssessment( protocolId: string, payload: RobAssessmentCreate ): Promise { return handle( await fetch(`${BASE}/${protocolId}/rob`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }) ); } // --- Confidence in the body of evidence (OHAT) --- export async function listConfidenceGroups(protocolId: string): Promise { return handle(await fetch(`${BASE}/${protocolId}/confidence`)); } export async function rateConfidence( protocolId: string, payload: ConfidenceRatingCreate ): Promise { return handle( await fetch(`${BASE}/${protocolId}/confidence`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(payload), }) ); }