Files
Aislo/common_util/common_util_m02_call.ts
T
eomsangdonandClaude Opus 5.5 e4b9b3e5a3 refactor(859-5 ②): 구조물 정의 코드 M02 StructOptions → M01_MasterData/StructOptions
- 파일 23 개 git mv · 이름 M02_SO_* → M01_SO_* · 내용 · 동작 · 화면 글 그대로
- 서버 주소 /api/m02 → /api/m01(이름만 · 10 길 · 수 469 그대로) · main.py 등록 이름 m01_so_*
- 화면 부름 = common_util_m02_call 의 call 에 base 인자(기본 /m02 · 구조물 정의만 /m01)
- import 고침 = B05 · B07 · B08 · M01 · M02 · main · 시험 · 도우미
- 소스 글자만 보던 시험 셋 지움(구조물 정의 옮김에 걸림 · 결과를 안 봄):
  test_m02_structure_options 좌측 줄 · test_m02_structure_option_groups 좌측 정의 줄 · test_136_3 M02 미리보기 단추 CSS
- test_553_5 부품 훑기 폴더 = 옮긴 자리

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 21:42:33 +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` 아래 길 부름(`base` = 다른 묶음 · M01 구조물 정의 `/m01`) — 409 = StaleError · 그 밖 실패 = 서버 까닭 글(하위 폴더도 씀) */
export async function call<T>(path: string, init: RequestInit = {}, base = "/m02"): Promise<T> {
const response = await fetch(`${API_BASE_URL}${base}${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}`;
}