79 lines
2.6 KiB
TypeScript
79 lines
2.6 KiB
TypeScript
/**
|
|
* 📄 API Helpers
|
|
* 공통 코드 cs-service API 호출 유틸리티
|
|
*
|
|
* - 검색 데이터 조회 및 파라미터 처리
|
|
* - 상위 코드로 하위 코드 조회 기능
|
|
* - URL 파라미터 생성 및 필터링
|
|
* - 데이터 페이징 및 총 개수 반환
|
|
* - Axios 기반 HTTP 요청 처리
|
|
* - 타입 안전성 보장을 위한 제네릭 활용
|
|
*
|
|
* 🔗 사용 API
|
|
* - 하위코드페이지 캐시 목록 조회: GET /api/v1/code/cache/{upCd} (cms-service)
|
|
*/
|
|
import { getCmsData, getData } from "../api";
|
|
|
|
/** CMS 공통코드 캐시 API가 반환하는 하위코드의 공통 계약이다. */
|
|
export type CommonCodeItem = {
|
|
lwpoCd: string;
|
|
upCd: string;
|
|
lwpoCdNm: string;
|
|
unit: string;
|
|
prntLwpoCd: string | null;
|
|
};
|
|
|
|
/**
|
|
* 기존 CMS 캐시 조회 URL을 유지하면서 상위코드에 속한 하위코드를 조회한다.
|
|
* 화면이 잘못된 응답을 정상 코드 목록으로 사용하지 않도록 배열 계약을 확인한다.
|
|
*/
|
|
export const getCommonCodes = async (
|
|
upCd: string,
|
|
): Promise<CommonCodeItem[]> => {
|
|
const normalizedUpCd = upCd.trim();
|
|
if (!normalizedUpCd) return [];
|
|
|
|
const data = await getCmsData(
|
|
`/api/v1/code/cache/${encodeURIComponent(normalizedUpCd)}`,
|
|
);
|
|
if (!Array.isArray(data)) {
|
|
throw new Error(`공통코드 응답 형식이 올바르지 않습니다: ${normalizedUpCd}`);
|
|
}
|
|
|
|
return data
|
|
.map((item: any) => ({
|
|
lwpoCd: String(item?.lwpoCd ?? "").trim(),
|
|
upCd: String(item?.upCd ?? normalizedUpCd).trim(),
|
|
lwpoCdNm: String(item?.lwpoCdNm ?? "").trim(),
|
|
unit: String(item?.unit ?? "").trim(),
|
|
prntLwpoCd:
|
|
item?.prntLwpoCd === null || item?.prntLwpoCd === undefined
|
|
? null
|
|
: String(item.prntLwpoCd).trim(),
|
|
}))
|
|
.filter((item) => item.lwpoCd.length > 0);
|
|
};
|
|
|
|
export const searchData = async<T extends Record<string, any>>(url: string, paramObj: T) => {
|
|
const params = new URLSearchParams(
|
|
Object.entries(paramObj)
|
|
.filter(([_, value]) => value !== undefined && value !== '') // 값이 있는 항목만 포함
|
|
.reduce((acc, [key, value]) => {
|
|
acc[key] = String(value);
|
|
return acc;
|
|
}, {} as Record<string, string>)
|
|
);
|
|
const data = await getData(`${url}?${params.toString()}`);
|
|
return data && { data: data.content, total: data.totalElements }
|
|
}
|
|
|
|
// 상위코드로 하위코드 조회
|
|
export const searchCmmnCd = async (upCd: string) => {
|
|
const data = await getCommonCodes(upCd);
|
|
const filteredData = data.map((item: any) => ({
|
|
lwpoCd: item.lwpoCd,
|
|
lwpoCdNm: item.lwpoCdNm,
|
|
}));
|
|
return filteredData;
|
|
};
|