Files
nexarch/web/notifications/lib/api.ts
T

46 lines
1.5 KiB
TypeScript

const API_BASE = process.env.NEXT_PUBLIC_CORE_API_BASE ?? "";
export class ApiError extends Error {}
async function req<T>(path: string, init?: RequestInit): Promise<T> {
const res = await fetch(`${API_BASE}${path}`, {
credentials: "include",
headers: init?.body ? { "Content-Type": "application/json" } : undefined,
...init,
});
const data = await res.json().catch(() => ({}));
if (!res.ok) {
throw new ApiError(data.error ?? data.message ?? "Unbekannter Fehler");
}
return data as T;
}
export interface Preference {
TenantSlug: string;
UserID: string;
EventType: string;
Channel: string;
Enabled: boolean;
}
// Bekannte Ereignistypen/Kanäle — das Backend erzwingt keine feste Liste
// (jedes Modul kann eigene event_type-Werte an EnqueueIfAllowed übergeben),
// diese Liste ist der aktuell bekannte Stand fürs Frontend-Formular.
export const KNOWN_EVENT_TYPES = ["welcome", "invoice_ready", "password_reset", "security_alert"] as const;
export const KNOWN_CHANNELS = ["email", "in_app"] as const;
export function fetchMyPreferences(): Promise<Preference[]> {
return req("/notifications/preferences");
}
export function setPreference(eventType: string, channel: string, enabled: boolean): Promise<void> {
return req("/notifications/preferences", {
method: "POST",
body: JSON.stringify({ event_type: eventType, channel, enabled }),
});
}
export function fetchTenantOverview(): Promise<Preference[]> {
return req("/notifications/preferences/tenant");
}