/** Requirements follow the files that will actually use the API. */ export function requiredProjectCapabilities(documents: boolean): string[] { return ["request_force_masks", ...(documents ? [ "document_hit_review", "document_review_mapping", "document_restoration", "comment_author_review", "revision_author_review", "document_image_policy", ] : [])]; } export async function checkProjectCapabilities( url: string, documents: boolean, fetcher: (url: string, init: RequestInit) => Promise, ): Promise { let response: Response; try { response = await fetcher(url, { signal: AbortSignal.timeout(15_000) }); } catch (error) { const timeout = error instanceof Error && /timeout|abort/i.test(error.name); throw new Error(`Velum: ${timeout ? "Zeitlimit bei der Prüfung der Projektfähigkeiten" : "Projekt-API nicht erreichbar"}: ${url}.`, { cause: error }); } if (response.status === 404 || response.status === 405) { throw new Error(`Velum: Projekt-API-Endpunkt fehlt (HTTP ${response.status}): ${url}. Backend aktualisieren oder Basis-URL prüfen.`); } if (!response.ok) { throw new Error(`Velum: Projektfähigkeiten nicht abrufbar (HTTP ${response.status}): ${url}. Anmeldung, Proxy und Backend prüfen.`); } if (!(response.headers.get("content-type") ?? "").includes("application/json")) { throw new Error(`Velum: Keine JSON-Antwort für ${url}. Anmeldung oder API-Weiterleitung prüfen.`); } const capabilities: unknown = await response.json(); if (!capabilities || typeof capabilities !== "object" || Array.isArray(capabilities)) { throw new Error(`Velum: Ungültige Projektfähigkeiten von ${url}.`); } const missing = requiredProjectCapabilities(documents) .filter((key) => (capabilities as Record)[key] !== true); if (missing.length) { throw new Error(`Velum: Backend unterstützt die ausgewählten ${documents ? "Office-/PDF-Dateien" : "Textdateien"} noch nicht vollständig. Fehlende API-Fähigkeiten: ${missing.join(", ")}. API: ${url}. Backend aktualisieren.`); } }