File size: 4,102 Bytes
2eef5d6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | const API_BASE = import.meta.env.VITE_API_URL || "/api/v1";
function getAdminToken(): string | null {
if (typeof window === "undefined") return null;
try {
const saved = localStorage.getItem("auth_admin");
if (saved) return JSON.parse(saved).token;
} catch {}
return null;
}
export async function backupDatabase(): Promise<void> {
const token = getAdminToken();
const res = await fetch(`${API_BASE}/admin/backup`, {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
},
});
if (!res.ok) {
const text = await res.text().catch(() => "");
let err: { error?: string } = {};
if (text) try { err = JSON.parse(text); } catch {}
throw new Error(err.error || `Backup failed: ${res.status}`);
}
const blob = await res.blob();
const disposition = res.headers.get("Content-Disposition") || "";
const filenameMatch = disposition.match(/filename="?(.+?)"?$/);
const filename = filenameMatch?.[1] || `backup_${new Date().toISOString().slice(0, 10)}.sql`;
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
export interface ExportData {
tables: Record<string, unknown[]>;
exported_at: string;
}
export async function exportDatabase(): Promise<void> {
const token = getAdminToken();
const res = await fetch(`${API_BASE}/admin/export`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
if (!res.ok) {
const text = await res.text().catch(() => "");
let err: { error?: string } = {};
if (text) try { err = JSON.parse(text); } catch {}
throw new Error(err.error || `Export failed: ${res.status}`);
}
const blob = await res.blob();
const disposition = res.headers.get("Content-Disposition") || "";
const filenameMatch = disposition.match(/filename="?(.+?)"?$/);
const filename = filenameMatch?.[1] || `export_${new Date().toISOString().slice(0, 10)}.sql`;
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = filename;
a.click();
URL.revokeObjectURL(url);
}
export interface ImportResult {
message: string;
executed: number;
failed: number;
}
export interface Setting {
key: string;
value: string;
description?: string;
}
export async function getSettings(): Promise<Setting[]> {
const token = getAdminToken();
const res = await fetch(`${API_BASE}/settings`, {
headers: {
Authorization: `Bearer ${token}`,
},
});
if (!res.ok) {
const text = await res.text().catch(() => "");
let err: { error?: string } = {};
if (text) try { err = JSON.parse(text); } catch {}
throw new Error(err.error || `Failed to load settings: ${res.status}`);
}
return res.json();
}
export async function updateSettings(settings: { key: string; value: string }[]): Promise<Setting[]> {
const token = getAdminToken();
const res = await fetch(`${API_BASE}/settings`, {
method: "PUT",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ settings }),
});
if (!res.ok) {
const text = await res.text().catch(() => "");
let err: { detail?: string; error?: string; message?: string } = {};
if (text) try { err = JSON.parse(text); } catch {}
throw new Error(err.detail || err.error || err.message || `Failed to update settings: ${res.status}`);
}
return res.json();
}
export async function importDatabase(file: File): Promise<ImportResult> {
const token = getAdminToken();
const formData = new FormData();
formData.append("file", file);
const res = await fetch(`${API_BASE}/admin/import`, {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
},
body: formData,
});
if (!res.ok) {
const text = await res.text().catch(() => "");
let err: { error?: string } = {};
if (text) try { err = JSON.parse(text); } catch {}
throw new Error(err.error || `Import failed: ${res.status}`);
}
return res.json();
}
|