Files
Aislo/common_util/common_util_m02_call.ts
T
eomsangdonandClaude Opus 5.5 a8d6b426d0 refactor(856-4): 거꾸로 부름 끊기 본문 — 앞 커밋(bd41d8799)은 이름 옮김만 담김 · 이 커밋과 한 묶음
- 앞 커밋 글 그대로(층 · 부름 common_util_m02_call · 찾아 넣기 창 StackFind 주입 · 단위 고르기 부품 상자 · 합계 상자 A00 · 기계 칸 부품)
- 앞 커밋에서 git add 가 옛 경로에 걸려 고친 파일이 빠짐 — 두 커밋을 같이 올림

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 21:02:55 +09:00

48 lines
2.2 KiB
TypeScript

/* =============================================================================
* 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<T>(path: string, init: RequestInit = {}): Promise<T> {
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}`;
}