Upload backup.ts
Browse files- frontend/src/api/backup.ts +121 -0
frontend/src/api/backup.ts
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Backup API client.
|
| 3 |
+
*
|
| 4 |
+
* File : frontend/src/api/backup.ts (new)
|
| 5 |
+
* Purpose : call the backend routes in backend/app/backup_routes.py.
|
| 6 |
+
* Consumers: ExportBackupButton, ImportBackupPanel.
|
| 7 |
+
*
|
| 8 |
+
* Same-origin path, matching BASE = "/api/v1/protocols" in api/protocols.ts.
|
| 9 |
+
*/
|
| 10 |
+
const API_ROOT = "/api/v1";
|
| 11 |
+
const MAX_BACKUP_BYTES = 25 * 1024 * 1024;
|
| 12 |
+
|
| 13 |
+
export interface SectionPlan {
|
| 14 |
+
total: number;
|
| 15 |
+
new: number;
|
| 16 |
+
duplicate: number;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
export interface RestorePlan {
|
| 20 |
+
protocol_id: string;
|
| 21 |
+
protocol_exists: boolean;
|
| 22 |
+
sections: Record<string, SectionPlan>;
|
| 23 |
+
search_run: string;
|
| 24 |
+
synthesis: string;
|
| 25 |
+
warnings: string[];
|
| 26 |
+
restored: boolean;
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
export type BackupDocument = Record<string, unknown>;
|
| 30 |
+
|
| 31 |
+
export class BackupApiError extends Error {}
|
| 32 |
+
|
| 33 |
+
/** Turn any error body the backend can send into one readable message. */
|
| 34 |
+
function describeError(status: number, body: unknown): string {
|
| 35 |
+
const detail = (body as { detail?: unknown } | null)?.detail;
|
| 36 |
+
if (typeof detail === "string") return detail;
|
| 37 |
+
if (Array.isArray(detail)) {
|
| 38 |
+
// FastAPI's own validation errors (e.g. the file isn't a backup at all)
|
| 39 |
+
return "This file is not a valid backup file.";
|
| 40 |
+
}
|
| 41 |
+
if (detail && typeof detail === "object") {
|
| 42 |
+
const d = detail as { message?: string; problems?: string[]; total_problems?: number };
|
| 43 |
+
const problems = (d.problems ?? []).map((p) => `• ${p}`).join("\n");
|
| 44 |
+
const more =
|
| 45 |
+
d.total_problems && d.problems && d.total_problems > d.problems.length
|
| 46 |
+
? `\n…and ${d.total_problems - d.problems.length} more.`
|
| 47 |
+
: "";
|
| 48 |
+
return `${d.message ?? "The backup file has problems."}\n${problems}${more}`.trim();
|
| 49 |
+
}
|
| 50 |
+
return `Request failed (${status}).`;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
async function readBody(res: Response): Promise<unknown> {
|
| 54 |
+
try {
|
| 55 |
+
return await res.json();
|
| 56 |
+
} catch {
|
| 57 |
+
return null;
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
async function post<T>(path: string, body: BackupDocument): Promise<T> {
|
| 62 |
+
let res: Response;
|
| 63 |
+
try {
|
| 64 |
+
res = await fetch(`${API_ROOT}${path}`, {
|
| 65 |
+
method: "POST",
|
| 66 |
+
headers: { "Content-Type": "application/json" },
|
| 67 |
+
body: JSON.stringify(body),
|
| 68 |
+
});
|
| 69 |
+
} catch {
|
| 70 |
+
throw new BackupApiError("Could not reach the server. Check your connection and try again.");
|
| 71 |
+
}
|
| 72 |
+
const data = await readBody(res);
|
| 73 |
+
if (!res.ok) throw new BackupApiError(describeError(res.status, data));
|
| 74 |
+
return data as T;
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
/** Read a chosen file and check it is JSON. Does not contact the server. */
|
| 78 |
+
export async function readBackupFile(file: File): Promise<BackupDocument> {
|
| 79 |
+
if (file.size > MAX_BACKUP_BYTES) {
|
| 80 |
+
throw new BackupApiError("This file is too large to be a backup (limit 25 MB).");
|
| 81 |
+
}
|
| 82 |
+
let parsed: unknown;
|
| 83 |
+
try {
|
| 84 |
+
parsed = JSON.parse(await file.text());
|
| 85 |
+
} catch {
|
| 86 |
+
throw new BackupApiError("This file is not valid JSON, so it cannot be a backup.");
|
| 87 |
+
}
|
| 88 |
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
| 89 |
+
throw new BackupApiError("This file is not a backup file.");
|
| 90 |
+
}
|
| 91 |
+
return parsed as BackupDocument;
|
| 92 |
+
}
|
| 93 |
+
|
| 94 |
+
export function previewBackup(doc: BackupDocument): Promise<RestorePlan> {
|
| 95 |
+
return post<RestorePlan>("/backup/preview", doc);
|
| 96 |
+
}
|
| 97 |
+
|
| 98 |
+
export function restoreBackup(doc: BackupDocument, merge: boolean): Promise<RestorePlan> {
|
| 99 |
+
return post<RestorePlan>(`/backup/restore?merge=${merge ? "true" : "false"}`, doc);
|
| 100 |
+
}
|
| 101 |
+
|
| 102 |
+
/** Download one project's backup as a saved file (not shown in the browser). */
|
| 103 |
+
export async function downloadBackup(protocolId: string): Promise<void> {
|
| 104 |
+
let res: Response;
|
| 105 |
+
try {
|
| 106 |
+
res = await fetch(`${API_ROOT}/protocols/${encodeURIComponent(protocolId)}/backup`);
|
| 107 |
+
} catch {
|
| 108 |
+
throw new BackupApiError("Could not reach the server. Check your connection and try again.");
|
| 109 |
+
}
|
| 110 |
+
if (!res.ok) throw new BackupApiError(describeError(res.status, await readBody(res)));
|
| 111 |
+
const blob = await res.blob();
|
| 112 |
+
const safeId = protocolId.replace(/[^A-Za-z0-9_-]/g, "_");
|
| 113 |
+
const url = URL.createObjectURL(blob);
|
| 114 |
+
const link = document.createElement("a");
|
| 115 |
+
link.href = url;
|
| 116 |
+
link.download = `project-backup-${safeId}.json`;
|
| 117 |
+
document.body.appendChild(link);
|
| 118 |
+
link.click();
|
| 119 |
+
link.remove();
|
| 120 |
+
URL.revokeObjectURL(url);
|
| 121 |
+
}
|