Refactor: improve code modularity

This commit is contained in:
MangoPig
2026-06-29 03:06:18 +01:00
parent c47eae1381
commit fc5ef23af8
87 changed files with 5442 additions and 4769 deletions
@@ -0,0 +1,81 @@
import { resolveAPIBase } from "../../../lib/api";
import type { BootstrapStepKey } from "./BootstrapWizard.data";
const readBootstrapResponseBody = async (response: Response): Promise<unknown> => {
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<void> => {
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));
}
};