leistungsbilanz-ts/src/frontend/components/cable-sizing-api.ts

44 lines
1.3 KiB
TypeScript

// Thin fetch client for the cable-sizing module, kept separate from
// ../utils/api.ts so the module stays self-contained (see
// docs/cable-sizing-module.md).
import type {
CableSizingAlert,
CableSizingInput,
CableSizingResult,
} from "../../cable-sizing/domain/cable-sizing-calculation";
import {
WRITE_REQUEST_HEADER_NAME,
WRITE_REQUEST_HEADER_VALUE,
} from "../../shared/constants/request-security";
export interface CalculateCableSizingResponse {
result: CableSizingResult;
alerts: CableSizingAlert[];
}
export async function calculateCableSizing(
input: CableSizingInput
): Promise<CalculateCableSizingResponse> {
const response = await fetch("/api/cable-sizing/calculate", {
method: "POST",
headers: {
"Content-Type": "application/json",
[WRITE_REQUEST_HEADER_NAME]: WRITE_REQUEST_HEADER_VALUE,
},
body: JSON.stringify(input),
cache: "no-store",
});
if (!response.ok) {
let details = await response.text();
try {
const parsed = JSON.parse(details) as { error?: unknown };
if (typeof parsed.error === "string") {
details = parsed.error;
}
} catch {
// keep raw text
}
throw new Error(details || `Anfrage fehlgeschlagen (Status ${response.status})`);
}
return response.json() as Promise<CalculateCableSizingResponse>;
}