96 lines
2.9 KiB
TypeScript
96 lines
2.9 KiB
TypeScript
/**
|
|
* INVYONE 메타 API 클라이언트
|
|
* DB 테이블 메타 → FieldConfig 변환 API
|
|
*/
|
|
|
|
import { apiClient } from './client';
|
|
import type { FieldConfig, FieldRef } from '@/types/invyone-component';
|
|
|
|
/**
|
|
* 접근 가능한 테이블 목록 조회
|
|
* ★ 일반 API는 Record<string, any> — 별도 인터페이스 정의 안 함
|
|
*/
|
|
export async function getMetaTableList(): Promise<Record<string, any>[]> {
|
|
const res = await apiClient.get('/meta/tables');
|
|
return res.data.data;
|
|
}
|
|
|
|
/**
|
|
* 특정 테이블의 FieldConfig[] 반환
|
|
* ★ fields만 FieldConfig[] 타입 (invyone-component.ts 규격 예외)
|
|
*/
|
|
export async function getMetaFields(tableName: string): Promise<{
|
|
table_name: string;
|
|
table_label: string;
|
|
primary_key: string | null;
|
|
fields: FieldConfig[];
|
|
[key: string]: any;
|
|
}> {
|
|
const res = await apiClient.get(`/meta/tables/${tableName}/fields`);
|
|
const data = res.data.data;
|
|
|
|
// 백엔드 snake_case → 프론트 camelCase 변환 (FieldConfig 규격 맞춤)
|
|
if (data?.fields) {
|
|
data.fields = data.fields.map(toFieldConfig);
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
/**
|
|
* 백엔드 응답 → FieldConfig 변환
|
|
* 대부분 키가 단일 단어라 변환 불필요, ref 내부만 camelCase 변환
|
|
*/
|
|
function toFieldConfig(raw: Record<string, any>): FieldConfig {
|
|
const field: FieldConfig = {
|
|
column: raw.column,
|
|
label: raw.label,
|
|
type: raw.type,
|
|
visible: raw.visible,
|
|
order: raw.order,
|
|
required: raw.required,
|
|
editable: raw.editable,
|
|
};
|
|
|
|
if (raw.width != null) field.width = raw.width;
|
|
if (raw.align != null) field.align = raw.align;
|
|
if (raw.defaultValue != null || raw.default_value != null) {
|
|
field.defaultValue = raw.defaultValue ?? raw.default_value;
|
|
}
|
|
if (raw.placeholder != null) field.placeholder = raw.placeholder;
|
|
if (raw.options != null) field.options = raw.options;
|
|
if (raw.format != null) field.format = raw.format;
|
|
if (raw.computed != null) field.computed = raw.computed;
|
|
if (raw.pk != null) field.pk = raw.pk;
|
|
if (raw.system != null) field.system = raw.system;
|
|
if (raw.searchable != null) field.searchable = raw.searchable;
|
|
if (raw.sortable != null) field.sortable = raw.sortable;
|
|
|
|
// ref: snake_case → camelCase 변환
|
|
if (raw.ref != null) {
|
|
field.ref = toFieldRef(raw.ref);
|
|
}
|
|
|
|
return field;
|
|
}
|
|
|
|
function toFieldRef(raw: Record<string, any>): FieldRef {
|
|
return {
|
|
table: raw.table,
|
|
valueColumn: raw.value_column ?? raw.valueColumn,
|
|
displayColumn: raw.display_column ?? raw.displayColumn,
|
|
...(raw.search_columns || raw.searchColumns
|
|
? { searchColumns: raw.search_columns ?? raw.searchColumns }
|
|
: {}),
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 테이블 간 업무 관계 조회 (Phase 5 — 제어 모드)
|
|
* table_relationships 기반, 필드 참조(table_type_columns)와 별도
|
|
*/
|
|
export async function getMetaRelations(tableName: string): Promise<Record<string, any>[]> {
|
|
const res = await apiClient.get(`/meta/tables/${tableName}/relations`);
|
|
return res.data.data ?? [];
|
|
}
|