import { resolveAPIBase } from "../../../lib/api"; import type { BootstrapStepKey } from "./BootstrapWizard.data"; const readBootstrapResponseBody = async (response: Response): Promise => { const raw = await response.text(); if (!raw.trim()) { return null; } try { return JSON.parse(raw); } catch { return raw; } }; const readBootstrapResponseError = (step: BootstrapStepKey, data: unknown): string => { const fallback = `Bootstrap ${step} request failed.`; if (typeof data === "string") { const message = data.trim(); return message || fallback; } if (!data || typeof data !== "object") { return fallback; } const record = data as { error?: string; message?: string; requestId?: string; }; const message = typeof record.message === "string" ? record.message.trim() : ""; const errorCode = typeof record.error === "string" ? record.error.trim() : ""; const requestId = typeof record.requestId === "string" ? record.requestId.trim() : ""; if (!message && !errorCode && !requestId) { return fallback; } const details: string[] = []; if (errorCode) { details.push(`code: ${errorCode}`); } if (requestId) { details.push(`request: ${requestId}`); } if (message && details.length > 0) { return `${message} (${details.join(", ")})`; } if (message) { return message; } return `${fallback} (${details.join(", ")})`; }; export const submitBootstrapStepRequest = async ( step: BootstrapStepKey, payload: unknown, ): Promise => { const response = await fetch(`${resolveAPIBase()}/bootstrap/steps/${step}`, { method: "POST", headers: { "Content-Type": "application/json", Accept: "application/json", }, body: JSON.stringify(payload), }); const data = await readBootstrapResponseBody(response); if (!response.ok) { throw new Error(readBootstrapResponseError(step, data)); } };