export type Snapshot = { id: string; orbit_id?: string; taken_at: string; orbit_name: string; size_bytes: number; capture?: { job_id: string; application_id: string; engine: "postgres" | "mysql"; format: string; restore_ready: boolean } | null; }; export type BackupSource = "filesystem" | "database"; export type BackupAccess = "hub" | "outpost"; export type Orbit = { id: string; name: string; provider: string; source: BackupSource | null; access: BackupAccess; last_status: string; dry_run: boolean; schedule?: string | null; decay_preset?: string; retention?: RetentionPolicy | null; application_id?: string | null; capture_schedule?: { hour: number; minute: number; timezone: string } | null; capture_retention?: { mode: "age"; days: number } | { mode: "count"; keep: number } | null; retention_source?: "orbit" | "profile" | "default"; }; export type RetentionPolicy = { keep_daily: number; keep_weekly: number; keep_monthly: number; keep_yearly: number; }; export type RetentionSettings = { decay_preset: string } | { retention: RetentionPolicy }; export type CreateOrbitInput = { name: string; schedule?: string; dry_run: boolean; } & RetentionSettings & ( | { source: "filesystem"; access: "hub"; provider_config: { paths: string[] } } | { source: "database"; access: "hub"; provider_config: { engine: "postgres" | "mysql"; transport?: "direct"; host: string; port?: number; database: string; user: string; password_file: string; tls?: { mode: "verify_full_with_ca" | "verify_identity_with_ca"; ca_file: string }; } } | { source: BackupSource; access: "outpost"; provider_config: Record } ); export type OperatorSession = { login_enabled: boolean; authorized: boolean; actor: string | null; csrf: string | null; }; let csrf: string | null = null; export async function fetchOperatorSession(signal?: AbortSignal): Promise { const session = await api("/api/v1/operator/session", { signal }); csrf = session.csrf; return session; } export async function loginOperator(password: string): Promise { const session = await api("/api/v1/operator/login", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ password }), }); csrf = session.csrf; return session; } export async function logoutOperator(): Promise { await request("/api/v1/operator/session", { method: "DELETE" }); csrf = null; } async function request(path: string, init?: RequestInit): Promise { const headers = new Headers(init?.headers); if (csrf && init?.method && init.method !== "GET") headers.set("X-Tessera-CSRF", csrf); const r = await fetch(path, { ...init, headers }); if (!r.ok) { const text = await r.text().catch(() => ""); throw new Error(text || `${path} failed`); } return r; } async function api(path: string, init?: RequestInit): Promise { const r = await request(path, init); if (r.status === 204) return undefined as T; return r.json(); } export async function fetchHealth(): Promise { const r = await fetch("/health"); if (!r.ok) throw new Error("hub offline"); return r.text(); } export async function fetchSnapshots(): Promise { return api("/api/v1/snapshots"); } export async function fetchOrbits(): Promise { return api("/api/v1/orbits"); } export async function createOrbit(input: CreateOrbitInput): Promise { return api("/api/v1/orbits", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(input), }); } export async function updateOrbitRetention(id: string, settings: RetentionSettings): Promise { await api(`/api/v1/orbits/${id}/retention`, { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify(settings), }); } export async function startPairing(): Promise<{ code: string; expires_at: string }> { return api("/api/v1/wormhole/pairing", { method: "POST" }); } export async function runOrbit(id: string): Promise { const response = await request(`/api/v1/orbits/${id}/run`, { method: "POST" }); return response.headers.get("X-Tessera-Snapshot-Id"); } export async function startReconstitute( snapshotId: string, target: string, ): Promise<{ session_id: string; session: { seconds_remaining: number; state: string }; }> { return api("/api/v1/reconstitute", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ snapshot_id: snapshotId, target_path: target }), }); } export async function abortReconstitute(sessionId: string): Promise { await api(`/api/v1/reconstitute/${sessionId}`, { method: "DELETE" }); } export async function pollReconstitute(sessionId: string) { const response = await fetch(`/api/v1/reconstitute/${sessionId}`); if (response.status === 404) return { state: "unavailable", seconds_remaining: 0 }; if (!response.ok) throw new Error("Could not read restore status"); return response.json() as Promise<{ state: string; seconds_remaining: number }>; } export type CaptureProfile = { application_id: string; orbit_id: string; outpost_id: string; format: "postgres_custom" | "postgres_sql" | "mysql_sql" | "filesystem"; schedule?: Orbit["capture_schedule"]; }; export function registerCaptureProfile(profile: CaptureProfile): Promise { return api("/api/v1/capture-profiles", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(profile), }); } export type CaptureJob = { id: string; profile: CaptureProfile; actor: string; status: "queued" | "running" | "completed" | "failed"; phase: string; created_at: number; snapshot_id: string | null; offsite_status: "pending" | "failed" | "copied" | null; }; export type CaptureEvent = { at: number; phase: string }; export function fetchCaptureProfiles(): Promise { return api("/api/v1/capture-profiles"); } export function enqueueCapture(application: string): Promise { return api(`/api/v1/capture-profiles/${application}/jobs`, { method: "POST" }); } export function fetchCaptureJobs(application: string, status: string): Promise { const filter = new URLSearchParams(); if (application) filter.set("application_id", application); if (status) filter.set("status", status); return api(`/api/v1/capture-jobs?${filter}`); } export function fetchCaptureEvents(job: string): Promise { return api(`/api/v1/capture-jobs/${job}/events`); } export function deleteSnapshot(snapshot: string): Promise { return api(`/api/v1/snapshots/${snapshot}`, { method: "DELETE" }); } export type Outpost = { id: string; name: string | null; last_seen: string | null; pairing_state: string }; export function fetchOutposts(): Promise { return api("/api/v1/wormhole/outposts"); } export type RestoreTarget = { id: string; name: string; outpost_id: string; engine: "postgres" | "mysql"; application_id: string; identity: { instance: string; database: string }; }; export type SnapshotRestoreTarget = RestoreTarget & { ineligible_reason: string | null }; export function registerRestoreTarget(target: RestoreTarget): Promise { return api("/api/v1/restore-targets", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(target), }); } export type RestoreJob = { id: string; snapshot_id: string; target: RestoreTarget; status: "queued" | "running" | "applying" | "failed" | "completed" | "recovery_required" | "reconciled" | "cancelled"; phase: string; safety_snapshot_id: string | null; actor: string; /** Seconds since the epoch. */ created_at: number; updated_at: number; }; /** The newest restore jobs across every snapshot. Older hubs answer 404 or 405. */ export function fetchRecentRestoreJobs(limit = 100): Promise { return api(`/api/v1/restore-jobs?limit=${limit}`); } export function fetchRestoreTargets(snapshot: string): Promise { return api(`/api/v1/snapshots/${snapshot}/restore-targets`); } export function fetchRestoreJobs(snapshot: string): Promise { return api(`/api/v1/snapshots/${snapshot}/restore-jobs`); } export function enqueueRestore(snapshot: string, target: string, confirmation: string): Promise { return api("/api/v1/restore-jobs", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ snapshot_id: snapshot, target_id: target, confirmation }), }); } export function cancelRestore(job: string): Promise { return api(`/api/v1/restore-jobs/${job}`, { method: "DELETE" }); } export function reconcileRestore(job: string, confirmation: string): Promise { return api(`/api/v1/restore-jobs/${job}/reconcile`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ confirmation }), }); }