/* ============================================================================= * common_util_m02_call.ts * M02 서버 길 부름(PLAN 856-4) — 층 이름 · `/api/m02` 아래 부름 한 함수 · 409 = 판 다름. * M02 화면(`M02_MasterTemplete_Api_Fetch` 가 그대로 내보냄)과 공용 줄 쌓기 화면(`A00_Common/line_stack_*`)이 같이 씀 * — 공용 화면이 M02 를 거꾸로 부르지 않게 * ========================================================================== */ import { API_BASE_URL } from "@config/config_frontend"; /** 층 이름 — 서버와 같은 글 */ export type Layer = "system" | "company" | "personal" | "project"; /** 서버가 판 불일치로 막았을 때 · `detail` = 서버 몸 그대로(729-3 `{까닭, 남은줄}`) */ export class StaleError extends Error { constructor( message: string, readonly detail?: unknown, ) { super(message); } } /** `/api/m02` 아래 길 부름 — 409 = StaleError · 그 밖 실패 = 서버 까닭 글(하위 폴더도 씀) */ export async function call(path: string, init: RequestInit = {}): Promise { const response = await fetch(`${API_BASE_URL}/m02${path}`, { credentials: "include", headers: { "Content-Type": "application/json" }, ...init, }); const body = (await response.json().catch(() => ({}))) as { detail?: unknown } & T; if (response.status === 409) throw new StaleError(reason(body.detail, response.status), body.detail); if (!response.ok) throw new Error(reason(body.detail, response.status)); return body; } /** 서버가 막은 까닭 그대로 — 문장 · `{errors: [...]}` · 입력 검사 목록(`[{msg}]`) */ function reason(detail: unknown, status: number): string { if (typeof detail === "string") return detail; const why = (detail as { 까닭?: unknown } | null)?.까닭; if (typeof why === "string") return why; const list = Array.isArray(detail) ? detail : (detail as { errors?: unknown } | null)?.errors; const lines = Array.isArray(list) ? list.map((x) => (typeof x === "string" ? x : String((x as { msg?: unknown })?.msg ?? ""))) : []; return lines.filter(Boolean).join(" · ") || `HTTP ${status}`; }