Merge branch 'main' of http://39.117.244.52:3000/kjs/ERP-node into feature/report
This commit is contained in:
@@ -1,7 +1,13 @@
|
||||
import axios, { AxiosResponse, AxiosError } from "axios";
|
||||
|
||||
// API URL 동적 설정 - 환경별 명확한 분리
|
||||
// API URL 동적 설정 - 환경변수 우선 사용
|
||||
const getApiBaseUrl = (): string => {
|
||||
// 1. 환경변수가 있으면 우선 사용
|
||||
if (process.env.NEXT_PUBLIC_API_URL) {
|
||||
return process.env.NEXT_PUBLIC_API_URL;
|
||||
}
|
||||
|
||||
// 2. 클라이언트 사이드에서 동적 설정
|
||||
if (typeof window !== "undefined") {
|
||||
const currentHost = window.location.hostname;
|
||||
const currentPort = window.location.port;
|
||||
@@ -13,18 +19,10 @@ const getApiBaseUrl = (): string => {
|
||||
) {
|
||||
return "http://localhost:8080/api";
|
||||
}
|
||||
|
||||
// 서버 환경에서 localhost:5555 → 39.117.244.52:8080
|
||||
if ((currentHost === "localhost" || currentHost === "127.0.0.1") && currentPort === "5555") {
|
||||
return "http://39.117.244.52:8080/api";
|
||||
}
|
||||
|
||||
// 기타 서버 환경 (내부/외부 IP): → 39.117.244.52:8080
|
||||
return "http://39.117.244.52:8080/api";
|
||||
}
|
||||
|
||||
// 서버 사이드 렌더링 기본값
|
||||
return "http://39.117.244.52:8080/api";
|
||||
// 3. 기본값
|
||||
return "http://localhost:8080/api";
|
||||
};
|
||||
|
||||
export const API_BASE_URL = getApiBaseUrl();
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
import { Company, CompanyFormData } from "@/types/company";
|
||||
import { apiClient } from "./client";
|
||||
|
||||
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://39.117.244.52:8080/api";
|
||||
const API_BASE_URL = process.env.NEXT_PUBLIC_API_URL || "http://localhost:8080/api";
|
||||
|
||||
// API 응답 타입 정의
|
||||
interface ApiResponse<T = any> {
|
||||
|
||||
@@ -34,6 +34,12 @@ export interface ValidationResult {
|
||||
*/
|
||||
// API URL 동적 설정 - 기존 client.ts와 동일한 로직
|
||||
const getApiBaseUrl = (): string => {
|
||||
// 1. 환경변수가 있으면 우선 사용
|
||||
if (process.env.NEXT_PUBLIC_API_URL) {
|
||||
return process.env.NEXT_PUBLIC_API_URL;
|
||||
}
|
||||
|
||||
// 2. 클라이언트 사이드에서 동적 설정
|
||||
if (typeof window !== "undefined") {
|
||||
const currentHost = window.location.hostname;
|
||||
const currentPort = window.location.port;
|
||||
@@ -45,18 +51,10 @@ const getApiBaseUrl = (): string => {
|
||||
) {
|
||||
return "http://localhost:8080/api";
|
||||
}
|
||||
|
||||
// 서버 환경에서 localhost:5555 → 39.117.244.52:8080
|
||||
if ((currentHost === "localhost" || currentHost === "127.0.0.1") && currentPort === "5555") {
|
||||
return "http://39.117.244.52:8080/api";
|
||||
}
|
||||
|
||||
// 기타 서버 환경 (내부/외부 IP): → 39.117.244.52:8080
|
||||
return "http://39.117.244.52:8080/api";
|
||||
}
|
||||
|
||||
// 서버 사이드 렌더링 기본값
|
||||
return "http://39.117.244.52:8080/api";
|
||||
// 3. 기본값
|
||||
return "http://localhost:8080/api";
|
||||
};
|
||||
|
||||
export class ExternalCallAPI {
|
||||
|
||||
@@ -86,33 +86,34 @@ export interface MailSendResult {
|
||||
// API 기본 설정
|
||||
// ============================================
|
||||
|
||||
const API_BASE_URL = '/api/mail';
|
||||
import { apiClient } from './client';
|
||||
|
||||
async function fetchApi<T>(
|
||||
endpoint: string,
|
||||
options: RequestInit = {}
|
||||
options: { method?: string; data?: unknown } = {}
|
||||
): Promise<T> {
|
||||
const response = await fetch(`${API_BASE_URL}${endpoint}`, {
|
||||
...options,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...options.headers,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Unknown error' }));
|
||||
throw new Error(error.message || `HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const result = await response.json();
|
||||
const { method = 'GET', data } = options;
|
||||
|
||||
// 백엔드가 { success: true, data: ... } 형식으로 반환하는 경우 처리
|
||||
if (result.success && result.data !== undefined) {
|
||||
return result.data as T;
|
||||
try {
|
||||
const response = await apiClient({
|
||||
url: `/mail${endpoint}`,
|
||||
method,
|
||||
data,
|
||||
});
|
||||
|
||||
// 백엔드가 { success: true, data: ... } 형식으로 반환하는 경우 처리
|
||||
if (response.data.success && response.data.data !== undefined) {
|
||||
return response.data.data as T;
|
||||
}
|
||||
|
||||
return response.data as T;
|
||||
} catch (error: unknown) {
|
||||
if (error && typeof error === 'object' && 'response' in error) {
|
||||
const axiosError = error as { response?: { data?: { message?: string }; status?: number } };
|
||||
throw new Error(axiosError.response?.data?.message || `HTTP ${axiosError.response?.status}`);
|
||||
}
|
||||
throw new Error('Unknown error');
|
||||
}
|
||||
|
||||
return result as T;
|
||||
}
|
||||
|
||||
// ============================================
|
||||
@@ -141,7 +142,7 @@ export async function createMailAccount(
|
||||
): Promise<MailAccount> {
|
||||
return fetchApi<MailAccount>('/accounts', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -154,7 +155,7 @@ export async function updateMailAccount(
|
||||
): Promise<MailAccount> {
|
||||
return fetchApi<MailAccount>(`/accounts/${id}`, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(data),
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -167,6 +168,15 @@ export async function deleteMailAccount(id: string): Promise<{ success: boolean
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* SMTP 연결 테스트
|
||||
*/
|
||||
export async function testMailAccountConnection(id: string): Promise<{ success: boolean; message: string }> {
|
||||
return fetchApi<{ success: boolean; message: string }>(`/accounts/${id}/test-connection`, {
|
||||
method: 'POST',
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* SMTP 연결 테스트
|
||||
*/
|
||||
@@ -208,7 +218,7 @@ export async function createMailTemplate(
|
||||
): Promise<MailTemplate> {
|
||||
return fetchApi<MailTemplate>('/templates-file', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -221,7 +231,7 @@ export async function updateMailTemplate(
|
||||
): Promise<MailTemplate> {
|
||||
return fetchApi<MailTemplate>(`/templates-file/${id}`, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(data),
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -243,7 +253,7 @@ export async function previewMailTemplate(
|
||||
): Promise<{ html: string }> {
|
||||
return fetchApi<{ html: string }>(`/templates-file/${id}/preview`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ sampleData }),
|
||||
data: { sampleData },
|
||||
});
|
||||
}
|
||||
|
||||
@@ -257,7 +267,7 @@ export async function previewMailTemplate(
|
||||
export async function sendMail(data: SendMailDto): Promise<MailSendResult> {
|
||||
return fetchApi<MailSendResult>('/send/simple', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user