Sadum1's picture
Update frontend/src/api/protocols.ts
9d45e6c verified
Raw History Blame Contribute Delete
7.35 kB
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<T>(resp: Response): Promise<T> {
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<Protocol[]> {
return handle(await fetch(BASE));
}
export async function getProtocol(id: string): Promise<Protocol> {
return handle(await fetch(`${BASE}/${id}`));
}
export async function createProtocol(draft: ProtocolDraft): Promise<Protocol> {
return handle(
await fetch(BASE, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(draft),
})
);
}
export async function updateProtocol(
id: string,
updates: Partial<ProtocolDraft>
): Promise<Protocol> {
return handle(
await fetch(`${BASE}/${id}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(updates),
})
);
}
export async function lockProtocol(id: string): Promise<Protocol> {
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<ProtocolDraft>,
reason: string
): Promise<Protocol> {
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<void> {
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<SearchRun> {
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<SearchRun | null> {
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<ScreeningRecordEntry> {
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<ScreeningRecordEntry[]> {
return handle(await fetch(`${BASE}/${protocolId}/screening`));
}
export async function recordExtraction(
protocolId: string,
payload: ExtractionRecordCreate
): Promise<ExtractionRecordEntry> {
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<ExtractionRecordEntry[]> {
return handle(await fetch(`${BASE}/${protocolId}/extraction`));
}
export async function getPrismaCounts(protocolId: string): Promise<PrismaCounts | null> {
const resp = await fetch(`${BASE}/${protocolId}/prisma`);
if (resp.status === 404) return null;
return handle(resp);
}
export async function generateSynthesis(protocolId: string): Promise<SynthesisResult> {
return handle(await fetch(`${BASE}/${protocolId}/synthesis`, { method: "POST" }));
}
export async function getSynthesis(protocolId: string): Promise<SynthesisResult | null> {
const resp = await fetch(`${BASE}/${protocolId}/synthesis`);
if (resp.status === 404) return null;
return handle(resp);
}
// --- Review type / domain dropdown options ---
let reviewOptionsCache: Promise<ReviewOptions> | null = null;
/** Dropdown options from the backend (fetched once per page load). */
export function getReviewOptions(): Promise<ReviewOptions> {
if (!reviewOptionsCache) {
reviewOptionsCache = fetch("/api/v1/review-options")
.then((resp) => handle<ReviewOptions>(resp))
.catch((err) => {
reviewOptionsCache = null;
throw err;
});
}
return reviewOptionsCache;
}
// --- Risk of bias (OHAT) ---
let robReferenceCache: Promise<RobReference> | null = null;
let robReferenceValue: RobReference | null = null;
/** The OHAT questions, designs and ratings (fetched once per page load). */
export function getRobReference(): Promise<RobReference> {
if (!robReferenceCache) {
robReferenceCache = fetch("/api/v1/rob/reference")
.then((resp) => handle<RobReference>(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<RobAssessmentEntry[]> {
return handle(await fetch(`${BASE}/${protocolId}/rob`));
}
export async function getRobSummary(protocolId: string): Promise<RobSummaryItem[]> {
return handle(await fetch(`${BASE}/${protocolId}/rob/summary`));
}
export async function recordRobAssessment(
protocolId: string,
payload: RobAssessmentCreate
): Promise<RobAssessmentEntry> {
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<ConfidenceGroup[]> {
return handle(await fetch(`${BASE}/${protocolId}/confidence`));
}
export async function rateConfidence(
protocolId: string,
payload: ConfidenceRatingCreate
): Promise<ConfidenceRatingEntry> {
return handle(
await fetch(`${BASE}/${protocolId}/confidence`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
})
);
}