ERP-node/frontend/lib/api/flowExternalDb.ts

140 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-10-20 17:50:27 +09:00
/**
* DB API
*/
import {
FlowExternalDbConnection,
CreateFlowExternalDbConnectionRequest,
UpdateFlowExternalDbConnectionRequest,
FlowExternalDbConnectionListResponse,
FlowExternalDbConnectionResponse,
FlowExternalDbConnectionTestResponse,
} from "@/types/flowExternalDb";
const API_BASE = "/api/flow-external-db";
/**
* DB API
*/
export const flowExternalDbApi = {
/**
* DB
*/
async getAll(activeOnly: boolean = false): Promise<FlowExternalDbConnectionListResponse> {
const query = activeOnly ? "?activeOnly=true" : "";
try {
const response = await fetch(`${API_BASE}${query}`, {
credentials: "include",
});
if (!response.ok) {
// 오류 발생 시 빈 목록 반환 (조용히 실패)
console.warn(`외부 DB 연결 목록 조회 실패: ${response.status} ${response.statusText}`);
return { success: false, data: [] };
}
return response.json();
} catch (error) {
// 네트워크 오류 등 예외 발생 시에도 빈 목록 반환
console.error("외부 DB 연결 목록 조회 오류:", error);
return { success: false, data: [] };
}
},
/**
* DB
*/
async getById(id: number): Promise<FlowExternalDbConnectionResponse> {
const response = await fetch(`${API_BASE}/${id}`, {
credentials: "include",
});
if (!response.ok) {
throw new Error(`외부 DB 연결 조회 실패: ${response.statusText}`);
}
return response.json();
},
/**
* DB
*/
async create(request: CreateFlowExternalDbConnectionRequest): Promise<FlowExternalDbConnectionResponse> {
const response = await fetch(API_BASE, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
credentials: "include",
body: JSON.stringify(request),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || "외부 DB 연결 생성 실패");
}
return response.json();
},
/**
* DB
*/
async update(id: number, request: UpdateFlowExternalDbConnectionRequest): Promise<FlowExternalDbConnectionResponse> {
const response = await fetch(`${API_BASE}/${id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
credentials: "include",
body: JSON.stringify(request),
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || "외부 DB 연결 수정 실패");
}
return response.json();
},
/**
* DB
*/
async delete(id: number): Promise<{ success: boolean; message: string }> {
const response = await fetch(`${API_BASE}/${id}`, {
method: "DELETE",
credentials: "include",
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || "외부 DB 연결 삭제 실패");
}
return response.json();
},
/**
* DB
*/
async testConnection(id: number): Promise<FlowExternalDbConnectionTestResponse> {
const response = await fetch(`${API_BASE}/${id}/test`, {
method: "POST",
credentials: "include",
});
const result = await response.json();
if (!response.ok) {
return {
success: false,
message: result.message || "연결 테스트 실패",
};
}
return result;
},
};