// Duenner Client fuer RET-06-API (archive/internal/retentionapi) — enthaelt // keine eigene Fristen-/RBAC-Logik, nur Datenabruf und -weitergabe. Der // RBAC-Check selbst passiert serverseitig ueber RET-08 (echter RBAC-06-Aufruf), // dieser Client meldet nur, ob der Zugriff erlaubt war (403) oder nicht. export class ForbiddenError extends Error {} export class ApiError extends Error {} function apiBase(): string { const base = process.env.NEXT_PUBLIC_RETENTION_API_URL; if (!base) { throw new Error("NEXT_PUBLIC_RETENTION_API_URL ist nicht gesetzt (Umgebungsvariable erforderlich)"); } return base; } // userRole wird bis zu einer zentralen Session-/Identitaets-Loesung im // Frontend als Umgebungsvariable/Query mitgegeben und als X-User-Role-Header // an RET-06-API durchgereicht, das ihn seinerseits gegen RBAC-06 prueft // (RET-08) - das Frontend trifft selbst KEINE Autorisierungsentscheidung. function userRoleHeader(): Record { const role = process.env.NEXT_PUBLIC_USER_ROLE; return role ? { "X-User-Role": role } : {}; } async function req(path: string, init?: RequestInit): Promise { const res = await fetch(`${apiBase()}${path}`, { ...init, headers: { ...userRoleHeader(), ...(init?.body ? { "Content-Type": "application/json" } : {}), ...init?.headers, }, }); if (res.status === 403) { throw new ForbiddenError("Zugriff verweigert: Ihre Rolle ist nicht zur Fristenverwaltung berechtigt."); } if (!res.ok) { const body = await res.json().catch(() => ({})); throw new ApiError(body.error ?? `Anfrage fehlgeschlagen (${res.status})`); } if (res.status === 200 && res.headers.get("content-length") === "0") { return undefined as T; } return res.json().catch(() => undefined as T); } export interface ClassRule { RetentionClass: string; Duration: string; Active: boolean; } export interface ExpiringObject { RetentionObjectID: string; ObjectType: string; ObjectReference: string; RetentionClass: string; DueDate: string; } export function fetchClassRules(): Promise { return req("/retention-classes"); } export function configureClassRule(retentionClass: string, duration: string): Promise { return req("/retention-classes", { method: "POST", body: JSON.stringify({ retention_class: retentionClass, duration }), }); } export function deactivateClassRule(retentionClass: string): Promise { return req(`/retention-classes/${encodeURIComponent(retentionClass)}/deactivate`, { method: "POST", }); } export function fetchExpiringPreview(): Promise { return req("/retention-classes/preview"); }