CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (push) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (push) Canceled after 0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Canceled after 0s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Canceled after 0s
CI / Web (dms-browser) (push) Canceled after 0s
CI / Web (pdf-viewer) (push) Canceled after 0s
CI / Web (shl) (push) Canceled after 0s
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
73 lines
2.5 KiB
TypeScript
73 lines
2.5 KiB
TypeScript
// UX-10: einziger Weg zu den DMS-Endpunkten. Standard ist der Token-Modus (Bearer aus dem Core-Modul-Token);
|
|
// NEXT_PUBLIC_AUTH_MODE=caller ist der abschaltbare Uebergangsmodus und faellt bei Anmeldefehlern nie stillschweigend zurueck.
|
|
import { createSession, describeHttpError, redirectToLogin, type Session } from "@nexarch/shl";
|
|
|
|
export function authMode(): "token" | "caller" {
|
|
return process.env.NEXT_PUBLIC_AUTH_MODE === "caller" ? "caller" : "token";
|
|
}
|
|
|
|
let session: Session | undefined;
|
|
|
|
export function getSession(): Session {
|
|
if (!session) {
|
|
const coreBase = process.env.NEXT_PUBLIC_CORE_API_BASE;
|
|
if (!coreBase) {
|
|
throw new Error("NEXT_PUBLIC_CORE_API_BASE ist nicht gesetzt (Umgebungsvariable erforderlich)");
|
|
}
|
|
session = createSession({
|
|
coreBase,
|
|
onLoginRequired: () => redirectToLogin(process.env.NEXT_PUBLIC_LOGIN_URL),
|
|
});
|
|
}
|
|
return session;
|
|
}
|
|
|
|
export function statusMessage(res: { status: number }, prefix: string): string {
|
|
if (res.status === 401 || res.status === 403 || res.status === 503) return describeHttpError(res.status);
|
|
return `${prefix} (${res.status})`;
|
|
}
|
|
|
|
export function resetSessionForTests(): void {
|
|
session = undefined;
|
|
}
|
|
|
|
export function apiFetch(input: string, init?: RequestInit): Promise<Response> {
|
|
if (authMode() === "caller") return fetch(input, init);
|
|
return getSession().fetch(input, init);
|
|
}
|
|
|
|
export async function currentUserId(): Promise<string> {
|
|
if (authMode() === "caller") {
|
|
const c = process.env.NEXT_PUBLIC_USER_ID;
|
|
if (!c) throw new Error("NEXT_PUBLIC_USER_ID ist nicht gesetzt (Umgebungsvariable erforderlich)");
|
|
return c;
|
|
}
|
|
const s = getSession();
|
|
await s.token();
|
|
const id = s.identity();
|
|
if (!id) throw new Error("Nicht angemeldet");
|
|
return id.userId;
|
|
}
|
|
|
|
// Ein Link kann keinen Authorization-Header senden: Inhalte werden per apiFetch geladen und lokal gespeichert.
|
|
export async function downloadFile(url: string, fallbackName: string): Promise<void> {
|
|
const res = await apiFetch(url);
|
|
if (!res.ok) {
|
|
throw new Error(describeHttpError(res.status));
|
|
}
|
|
const cd = res.headers.get("Content-Disposition") ?? "";
|
|
const m = /filename\*?=(?:UTF-8'')?"?([^";]+)"?/i.exec(cd);
|
|
const name = m ? decodeURIComponent(m[1]) : fallbackName;
|
|
const blobUrl = URL.createObjectURL(await res.blob());
|
|
try {
|
|
const a = document.createElement("a");
|
|
a.href = blobUrl;
|
|
a.download = name;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
} finally {
|
|
setTimeout(() => URL.revokeObjectURL(blobUrl), 0);
|
|
}
|
|
}
|