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

277 lines
8.2 KiB
TypeScript
Raw Normal View History

2025-08-21 09:41:46 +09:00
import axios, { AxiosResponse, AxiosError } from "axios";
2025-10-02 17:22:25 +09:00
// API URL 동적 설정 - 환경변수 우선 사용
2025-09-04 15:18:25 +09:00
const getApiBaseUrl = (): string => {
2025-10-02 17:22:25 +09:00
// 1. 환경변수가 있으면 우선 사용
if (process.env.NEXT_PUBLIC_API_URL) {
return process.env.NEXT_PUBLIC_API_URL;
}
// 2. 클라이언트 사이드에서 동적 설정
2025-09-04 15:18:25 +09:00
if (typeof window !== "undefined") {
const currentHost = window.location.hostname;
2025-09-04 16:01:33 +09:00
const currentPort = window.location.port;
2025-10-23 18:03:11 +09:00
const protocol = window.location.protocol;
2025-11-11 16:59:16 +09:00
// 프로덕션 환경: logistream.kpslp.kr → 백엔드는 같은 도메인 8080 포트
if (currentHost === "logistream.kpslp.kr") {
return `${protocol}//${currentHost}:8080/api`;
}
2025-10-23 18:03:11 +09:00
// 프로덕션 환경: v1.vexplor.com → api.vexplor.com
if (currentHost === "v1.vexplor.com") {
return "https://api.vexplor.com/api";
}
2025-09-04 15:18:25 +09:00
2025-10-13 19:15:52 +09:00
// 로컬 개발환경: localhost:9771 또는 localhost:3000 → localhost:8080
2025-09-16 16:16:41 +09:00
if (
(currentHost === "localhost" || currentHost === "127.0.0.1") &&
(currentPort === "9771" || currentPort === "3000")
) {
2025-10-13 19:15:52 +09:00
return "http://localhost:8080/api";
2025-09-04 15:18:25 +09:00
}
}
// 3. 기본값 (서버사이드 빌드 시)
return process.env.NEXT_PUBLIC_API_URL || "http://localhost:8080/api";
2025-09-04 15:18:25 +09:00
};
// 매번 호출 시 동적으로 계산 (getter 함수)
export const getAPIBaseURL = getApiBaseUrl;
// 하위 호환성을 위해 유지하되, 동적으로 계산되도록 수정
let _cachedApiBaseUrl: string | null = null;
export const API_BASE_URL = (() => {
if (_cachedApiBaseUrl === null || typeof window !== "undefined") {
_cachedApiBaseUrl = getApiBaseUrl();
}
return _cachedApiBaseUrl;
})();
2025-08-21 09:41:46 +09:00
2025-10-01 16:53:35 +09:00
// 이미지 URL을 완전한 URL로 변환하는 함수
export const getFullImageUrl = (imagePath: string): string => {
// 이미 전체 URL인 경우 그대로 반환
if (imagePath.startsWith("http://") || imagePath.startsWith("https://")) {
return imagePath;
}
// /uploads로 시작하는 상대 경로인 경우 API 서버 주소 추가
if (imagePath.startsWith("/uploads")) {
const baseUrl = API_BASE_URL.replace("/api", ""); // /api 제거
return `${baseUrl}${imagePath}`;
}
return imagePath;
};
2025-08-21 09:41:46 +09:00
// JWT 토큰 관리 유틸리티
const TokenManager = {
getToken: (): string | null => {
if (typeof window !== "undefined") {
return localStorage.getItem("authToken");
}
return null;
},
isTokenExpired: (token: string): boolean => {
try {
const payload = JSON.parse(atob(token.split(".")[1]));
return payload.exp * 1000 < Date.now();
} catch {
return true;
}
},
};
2025-09-04 15:39:29 +09:00
// Axios 인스턴스 생성
2025-08-21 09:41:46 +09:00
export const apiClient = axios.create({
2025-09-04 15:39:29 +09:00
baseURL: API_BASE_URL,
timeout: 30000, // 30초로 증가 (다중 커넥션 처리 시간 고려)
2025-08-21 09:41:46 +09:00
headers: {
"Content-Type": "application/json",
},
withCredentials: true, // 쿠키 포함
});
// 요청 인터셉터
apiClient.interceptors.request.use(
(config) => {
// JWT 토큰 추가
const token = TokenManager.getToken();
2025-08-21 09:41:46 +09:00
if (token && !TokenManager.isTokenExpired(token)) {
config.headers.Authorization = `Bearer ${token}`;
} else if (token && TokenManager.isTokenExpired(token)) {
console.warn("❌ 토큰이 만료되었습니다.");
2025-08-21 09:41:46 +09:00
// 토큰 제거
if (typeof window !== "undefined") {
localStorage.removeItem("authToken");
}
} else {
console.warn("⚠️ 토큰이 없습니다.");
2025-08-21 09:41:46 +09:00
}
2025-09-05 21:52:19 +09:00
// FormData 요청 시 Content-Type 자동 처리
if (config.data instanceof FormData) {
delete config.headers["Content-Type"];
}
2025-08-25 17:22:20 +09:00
// 언어 정보를 쿼리 파라미터에 추가 (GET 요청 시에만)
2025-08-21 09:41:46 +09:00
if (config.method?.toUpperCase() === "GET") {
2025-08-26 18:33:04 +09:00
// 우선순위: 전역 변수 > localStorage > 기본값
let currentLang = "KR"; // 기본값
if (typeof window !== "undefined") {
// 1순위: 전역 변수에서 확인
if ((window as unknown as { __GLOBAL_USER_LANG?: string }).__GLOBAL_USER_LANG) {
currentLang = (window as unknown as { __GLOBAL_USER_LANG: string }).__GLOBAL_USER_LANG;
2025-08-26 18:33:04 +09:00
}
// 2순위: localStorage에서 확인 (새 창이나 페이지 새로고침 시)
else {
const storedLocale = localStorage.getItem("userLocale");
if (storedLocale) {
currentLang = storedLocale;
}
}
}
2025-08-21 09:41:46 +09:00
if (config.params) {
config.params.userLang = currentLang;
} else {
config.params = { userLang: currentLang };
}
}
return config;
},
(error) => {
console.error("❌ API 요청 오류:", error);
2025-08-21 09:41:46 +09:00
return Promise.reject(error);
},
);
// 응답 인터셉터
apiClient.interceptors.response.use(
(response: AxiosResponse) => {
return response;
},
(error: AxiosError) => {
const status = error.response?.status;
const url = error.config?.url;
// 409 에러 (중복 데이터)는 조용하게 처리
if (status === 409) {
// 중복 검사 API와 관계도 저장은 완전히 조용하게 처리
if (url?.includes("/check-duplicate") || url?.includes("/dataflow-diagrams")) {
// 중복 검사와 관계도 중복 이름은 정상적인 비즈니스 로직이므로 콘솔 출력 없음
return Promise.reject(error);
}
// 일반 409 에러는 간단한 로그만 출력
console.warn("⚠️ 데이터 중복:", {
url: url,
message: (error.response?.data as { message?: string })?.message || "중복된 데이터입니다.",
});
return Promise.reject(error);
}
// 다른 에러들은 기존처럼 상세 로그 출력
console.error("❌ API 응답 오류:", {
status: status,
2025-08-21 09:41:46 +09:00
statusText: error.response?.statusText,
url: url,
2025-08-21 09:41:46 +09:00
data: error.response?.data,
message: error.message,
headers: error.config?.headers,
2025-08-21 09:41:46 +09:00
});
// 401 에러 시 상세 정보 출력
if (status === 401) {
console.error("🚨 401 Unauthorized 오류 상세 정보:", {
url: url,
method: error.config?.method,
headers: error.config?.headers,
requestData: error.config?.data,
responseData: error.response?.data,
token: TokenManager.getToken() ? "존재" : "없음",
});
}
2025-08-21 09:41:46 +09:00
// 401 에러 시 토큰 제거 및 로그인 페이지로 리다이렉트
if (status === 401 && typeof window !== "undefined") {
2025-08-21 09:41:46 +09:00
localStorage.removeItem("authToken");
// 로그인 페이지가 아닌 경우에만 리다이렉트
if (window.location.pathname !== "/login") {
window.location.href = "/login";
}
}
return Promise.reject(error);
},
);
// 공통 응답 타입
export interface ApiResponse<T = unknown> {
2025-08-21 09:41:46 +09:00
success: boolean;
data?: T;
message?: string;
errorCode?: string;
}
2025-09-03 16:38:10 +09:00
// 사용자 정보 타입
export interface UserInfo {
userId: string;
userName: string;
deptName?: string;
companyCode?: string;
userType?: string;
userTypeName?: string;
email?: string;
photo?: string;
locale?: string;
isAdmin?: boolean;
}
// 현재 사용자 정보 조회
export const getCurrentUser = async (): Promise<ApiResponse<UserInfo>> => {
try {
const response = await apiClient.get("/auth/me");
return response.data;
} catch (error: any) {
console.error("현재 사용자 정보 조회 실패:", error);
return {
success: false,
message: error.response?.data?.message || error.message || "사용자 정보를 가져올 수 없습니다.",
errorCode: error.response?.data?.errorCode,
};
}
};
2025-08-21 09:41:46 +09:00
// API 호출 헬퍼 함수
export const apiCall = async <T>(
method: "GET" | "POST" | "PUT" | "DELETE",
url: string,
data?: unknown,
2025-08-21 09:41:46 +09:00
): Promise<ApiResponse<T>> => {
try {
const response = await apiClient.request({
method,
url,
data,
});
return response.data;
} catch (error: unknown) {
2025-08-21 09:41:46 +09:00
console.error("API 호출 실패:", error);
const axiosError = error as AxiosError;
2025-08-21 09:41:46 +09:00
return {
success: false,
message:
(axiosError.response?.data as { message?: string })?.message ||
axiosError.message ||
"알 수 없는 오류가 발생했습니다.",
errorCode: (axiosError.response?.data as { errorCode?: string })?.errorCode,
2025-08-21 09:41:46 +09:00
};
}
};