daily-briefing/web/src/setup/api.ts

101 lines
2.6 KiB
TypeScript
Raw Normal View History

const API_BASE = "/api";
let setupToken: string | null = null;
export function setSetupToken(token: string): void {
setupToken = token;
}
export function getSetupToken(): string | null {
return setupToken;
}
async function setupFetch(
path: string,
options: RequestInit = {},
): Promise<Response> {
const headers: Record<string, string> = {
"Content-Type": "application/json",
...((options.headers as Record<string, string>) || {}),
};
if (setupToken) {
headers["Authorization"] = `Bearer ${setupToken}`;
}
return fetch(`${API_BASE}${path}`, { ...options, headers });
}
// --- Status ---
export interface SetupStatus {
setup_complete: boolean;
integrations: Array<{ type: string; name: string; enabled: boolean }>;
}
export async function getSetupStatus(): Promise<SetupStatus> {
const res = await fetch(`${API_BASE}/setup/status`);
if (!res.ok) throw new Error("Failed to check setup status");
return res.json();
}
// --- Admin Creation ---
export async function createAdmin(
password: string,
): Promise<{ token: string; username: string }> {
const res = await fetch(`${API_BASE}/setup/admin`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ password }),
});
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.detail || "Admin-Erstellung fehlgeschlagen");
}
const data = await res.json();
setSetupToken(data.token);
return data;
}
// --- Integration Config ---
export async function saveIntegration(
type: string,
config: Record<string, unknown>,
enabled: boolean,
): Promise<Record<string, unknown>> {
const res = await setupFetch(`/setup/integration/${type}`, {
method: "PUT",
body: JSON.stringify({ config, enabled }),
});
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.detail || "Speichern fehlgeschlagen");
}
return res.json();
}
export async function testIntegrationConfig(
type: string,
config: Record<string, unknown>,
): Promise<{ success: boolean; message: string }> {
const res = await setupFetch(`/setup/integration/${type}/test`, {
method: "POST",
body: JSON.stringify({ config }),
});
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.detail || "Test fehlgeschlagen");
}
return res.json();
}
// --- Complete ---
export async function completeSetup(): Promise<void> {
const res = await setupFetch("/setup/complete", { method: "POST" });
if (!res.ok) {
const body = await res.json().catch(() => ({}));
throw new Error(body.detail || "Setup-Abschluss fehlgeschlagen");
}
}