260 lines
7.4 KiB
TypeScript
260 lines
7.4 KiB
TypeScript
// 외부 DB 연결 API 클라이언트
|
|
// 작성일: 2024-12-17
|
|
|
|
// API 기본 설정
|
|
const getApiBaseUrl = () => {
|
|
if (process.env.NODE_ENV === "development") {
|
|
return "http://localhost:8080/api";
|
|
}
|
|
return "/api";
|
|
};
|
|
|
|
// 타입 정의
|
|
export interface ExternalDbConnection {
|
|
id?: number;
|
|
connection_name: string;
|
|
description?: string;
|
|
db_type: "mysql" | "postgresql" | "oracle" | "mssql" | "sqlite";
|
|
host: string;
|
|
port: number;
|
|
database_name: string;
|
|
username: string;
|
|
password: string;
|
|
connection_timeout?: number;
|
|
query_timeout?: number;
|
|
max_connections?: number;
|
|
ssl_enabled?: string;
|
|
ssl_cert_path?: string;
|
|
connection_options?: Record<string, unknown>;
|
|
company_code: string;
|
|
is_active: string;
|
|
created_date?: Date;
|
|
created_by?: string;
|
|
updated_date?: Date;
|
|
updated_by?: string;
|
|
}
|
|
|
|
export interface ExternalDbConnectionFilter {
|
|
db_type?: string;
|
|
is_active?: string;
|
|
company_code?: string;
|
|
search?: string;
|
|
}
|
|
|
|
export interface ApiResponse<T> {
|
|
success: boolean;
|
|
data?: T;
|
|
message?: string;
|
|
error?: string;
|
|
}
|
|
|
|
// DB 타입 옵션
|
|
export const DB_TYPE_OPTIONS = [
|
|
{ value: "mysql", label: "MySQL" },
|
|
{ value: "postgresql", label: "PostgreSQL" },
|
|
{ value: "oracle", label: "Oracle" },
|
|
{ value: "mssql", label: "SQL Server" },
|
|
{ value: "sqlite", label: "SQLite" },
|
|
];
|
|
|
|
// DB 타입별 기본 설정
|
|
export const DB_TYPE_DEFAULTS = {
|
|
mysql: { port: 3306, driver: "mysql2" },
|
|
postgresql: { port: 5432, driver: "pg" },
|
|
oracle: { port: 1521, driver: "oracledb" },
|
|
mssql: { port: 1433, driver: "mssql" },
|
|
sqlite: { port: 0, driver: "sqlite3" },
|
|
};
|
|
|
|
// 활성 상태 옵션
|
|
export const ACTIVE_STATUS_OPTIONS = [
|
|
{ value: "Y", label: "활성" },
|
|
{ value: "N", label: "비활성" },
|
|
{ value: "ALL", label: "전체" },
|
|
];
|
|
|
|
// API 클라이언트 클래스
|
|
export class ExternalDbConnectionAPI {
|
|
private static getAuthHeaders() {
|
|
const token = localStorage.getItem("authToken");
|
|
return {
|
|
"Content-Type": "application/json",
|
|
...(token && { Authorization: `Bearer ${token}` }),
|
|
};
|
|
}
|
|
|
|
private static async handleResponse<T>(response: Response): Promise<ApiResponse<T>> {
|
|
try {
|
|
// 응답이 JSON인지 확인
|
|
const contentType = response.headers.get("content-type");
|
|
if (!contentType || !contentType.includes("application/json")) {
|
|
throw new Error(`서버에서 JSON이 아닌 응답을 받았습니다: ${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
const data = await response.json();
|
|
|
|
if (!response.ok) {
|
|
return {
|
|
success: false,
|
|
message: data.message || `HTTP ${response.status}: ${response.statusText}`,
|
|
error: data.error,
|
|
};
|
|
}
|
|
|
|
return data;
|
|
} catch (error) {
|
|
console.error("API 응답 처리 오류:", error);
|
|
return {
|
|
success: false,
|
|
message: error instanceof Error ? error.message : "알 수 없는 오류가 발생했습니다.",
|
|
error: error instanceof Error ? error.message : "Unknown error",
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 외부 DB 연결 목록 조회
|
|
*/
|
|
static async getConnections(filter?: ExternalDbConnectionFilter): Promise<ApiResponse<ExternalDbConnection[]>> {
|
|
try {
|
|
const params = new URLSearchParams();
|
|
|
|
if (filter) {
|
|
Object.entries(filter).forEach(([key, value]) => {
|
|
if (value && value.trim()) {
|
|
params.append(key, value.trim());
|
|
}
|
|
});
|
|
}
|
|
|
|
const url = `${getApiBaseUrl()}/external-db-connections${params.toString() ? `?${params.toString()}` : ""}`;
|
|
|
|
const response = await fetch(url, {
|
|
method: "GET",
|
|
headers: this.getAuthHeaders(),
|
|
});
|
|
|
|
return this.handleResponse<ExternalDbConnection[]>(response);
|
|
} catch (error) {
|
|
console.error("외부 DB 연결 목록 조회 오류:", error);
|
|
return {
|
|
success: false,
|
|
message: "네트워크 오류가 발생했습니다.",
|
|
error: error instanceof Error ? error.message : "Network error",
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 특정 외부 DB 연결 조회
|
|
*/
|
|
static async getConnectionById(id: number): Promise<ApiResponse<ExternalDbConnection>> {
|
|
try {
|
|
const response = await fetch(`${getApiBaseUrl()}/external-db-connections/${id}`, {
|
|
method: "GET",
|
|
headers: this.getAuthHeaders(),
|
|
});
|
|
|
|
return this.handleResponse<ExternalDbConnection>(response);
|
|
} catch (error) {
|
|
console.error("외부 DB 연결 조회 오류:", error);
|
|
return {
|
|
success: false,
|
|
message: "네트워크 오류가 발생했습니다.",
|
|
error: error instanceof Error ? error.message : "Network error",
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 새 외부 DB 연결 생성
|
|
*/
|
|
static async createConnection(data: ExternalDbConnection): Promise<ApiResponse<ExternalDbConnection>> {
|
|
try {
|
|
const response = await fetch(`${getApiBaseUrl()}/external-db-connections`, {
|
|
method: "POST",
|
|
headers: this.getAuthHeaders(),
|
|
body: JSON.stringify(data),
|
|
});
|
|
|
|
return this.handleResponse<ExternalDbConnection>(response);
|
|
} catch (error) {
|
|
console.error("외부 DB 연결 생성 오류:", error);
|
|
return {
|
|
success: false,
|
|
message: "네트워크 오류가 발생했습니다.",
|
|
error: error instanceof Error ? error.message : "Network error",
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 외부 DB 연결 수정
|
|
*/
|
|
static async updateConnection(
|
|
id: number,
|
|
data: Partial<ExternalDbConnection>,
|
|
): Promise<ApiResponse<ExternalDbConnection>> {
|
|
try {
|
|
const response = await fetch(`${getApiBaseUrl()}/external-db-connections/${id}`, {
|
|
method: "PUT",
|
|
headers: this.getAuthHeaders(),
|
|
body: JSON.stringify(data),
|
|
});
|
|
|
|
return this.handleResponse<ExternalDbConnection>(response);
|
|
} catch (error) {
|
|
console.error("외부 DB 연결 수정 오류:", error);
|
|
return {
|
|
success: false,
|
|
message: "네트워크 오류가 발생했습니다.",
|
|
error: error instanceof Error ? error.message : "Network error",
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 외부 DB 연결 삭제
|
|
*/
|
|
static async deleteConnection(id: number): Promise<ApiResponse<void>> {
|
|
try {
|
|
const response = await fetch(`${getApiBaseUrl()}/external-db-connections/${id}`, {
|
|
method: "DELETE",
|
|
headers: this.getAuthHeaders(),
|
|
});
|
|
|
|
return this.handleResponse<void>(response);
|
|
} catch (error) {
|
|
console.error("외부 DB 연결 삭제 오류:", error);
|
|
return {
|
|
success: false,
|
|
message: "네트워크 오류가 발생했습니다.",
|
|
error: error instanceof Error ? error.message : "Network error",
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 지원하는 DB 타입 목록 조회
|
|
*/
|
|
static async getSupportedTypes(): Promise<
|
|
ApiResponse<{ types: typeof DB_TYPE_OPTIONS; defaults: typeof DB_TYPE_DEFAULTS }>
|
|
> {
|
|
try {
|
|
const response = await fetch(`${getApiBaseUrl()}/external-db-connections/types/supported`, {
|
|
method: "GET",
|
|
headers: this.getAuthHeaders(),
|
|
});
|
|
|
|
return this.handleResponse<{ types: typeof DB_TYPE_OPTIONS; defaults: typeof DB_TYPE_DEFAULTS }>(response);
|
|
} catch (error) {
|
|
console.error("지원 DB 타입 조회 오류:", error);
|
|
return {
|
|
success: false,
|
|
message: "네트워크 오류가 발생했습니다.",
|
|
error: error instanceof Error ? error.message : "Network error",
|
|
};
|
|
}
|
|
}
|
|
}
|