Files
Aislo/B07_wf4_DesignDetail/B07_wf4_DesignDetail_Api_Fetch.ts
T

72 lines
2.4 KiB
TypeScript

/* =============================================================================
* B07_wf4_DesignDetail_Api_Fetch.ts
* 4차 워크플로우(상세 설계) API 클라이언트 — WebCAD PoC 임시 계약
*
* 백엔드 계약 (B07_wf4_DesignDetail_Router.py):
* GET /api/b07/poc/sample-drawing → 샘플 종단·횡단 도면 기하 JSON
* GET /api/b07/poc/sample-drawing/dxf → 동일 도면 DXF 다운로드
*
* ⚠️ PoC 전용: Phase 2에서 실제 B06 산출 데이터 계약으로 대체된다.
* 규칙: 오류 응답 {status:"error", message:"..."}을 Error로 변환.
* ========================================================================== */
import { API_BASE_URL, API_TIMEOUT_MS } from "@config/config_frontend";
export interface CadLayerJson {
name: string;
color_aci: number;
}
export interface CadEntityJson {
type: "LWPOLYLINE" | "LINE" | "TEXT";
layer: string;
/** LWPOLYLINE */
points?: [number, number][];
closed?: boolean;
/** LINE */
start?: [number, number];
end?: [number, number];
/** TEXT */
text?: string;
insert?: [number, number];
height?: number;
rotation?: number;
}
export interface CadDrawingJson {
layers: CadLayerJson[];
entities: CadEntityJson[];
}
/** 공통 fetch 헬퍼: 타임아웃 + 인증 쿠키 + 오류 응답 변환. */
async function requestJson<T>(path: string): Promise<T> {
const controller = new AbortController();
const timeoutId = window.setTimeout(() => controller.abort(), API_TIMEOUT_MS);
try {
const response = await fetch(`${API_BASE_URL}${path}`, {
method: "GET",
credentials: "include",
headers: { "Content-Type": "application/json" },
signal: controller.signal,
});
const payload = (await response.json()) as T & { message?: string };
if (!response.ok) {
throw new Error(payload.message ?? `HTTP ${response.status}`);
}
return payload;
} finally {
window.clearTimeout(timeoutId);
}
}
/** PoC 샘플 도면(종단·횡단 모사)의 기하 JSON을 조회한다. */
export async function fetchPocSampleDrawing(stations?: number): Promise<CadDrawingJson> {
const query = stations === undefined ? "" : `?stations=${stations}`;
return requestJson<CadDrawingJson>(`/b07/poc/sample-drawing${query}`);
}
/** PoC 샘플 도면 DXF 다운로드 URL (ezdxf 쓰기 왕복 검증용). */
export function getPocSampleDrawingDxfUrl(): string {
return `${API_BASE_URL}/b07/poc/sample-drawing/dxf`;
}