52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import axios from 'axios';
|
|
import { getApiBaseUrl } from './apiBase';
|
|
|
|
// 개발: Vite 프록시 /api → localhost:4000
|
|
// 사설망 IP 접속: 자동으로 같은 IP:4000 백엔드
|
|
// Vercel 배포: Render API (VITE_API_URL로 오버라이드 가능)
|
|
const baseURL = getApiBaseUrl();
|
|
|
|
export const apiClient = axios.create({
|
|
baseURL,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
export function fileViewUrl(fileId: string): string {
|
|
return `${baseURL}/files/${fileId}/view`;
|
|
}
|
|
|
|
export function fileDownloadUrl(fileId: string): string {
|
|
return `${baseURL}/files/${fileId}/download`;
|
|
}
|
|
|
|
export function fileHwpPreviewUrl(fileId: string): string {
|
|
return `${baseURL}/files/${fileId}/hwp-preview`;
|
|
}
|
|
|
|
apiClient.interceptors.request.use((config) => {
|
|
if (config.data instanceof FormData) {
|
|
delete config.headers['Content-Type'];
|
|
}
|
|
return config;
|
|
});
|
|
|
|
apiClient.interceptors.response.use(
|
|
(res) => res,
|
|
(error) => Promise.reject(error),
|
|
);
|
|
|
|
export function getApiErrorMessage(err: unknown, fallback: string): string {
|
|
const ax = err as {
|
|
response?: { data?: { message?: string }; status?: number };
|
|
message?: string;
|
|
code?: string;
|
|
};
|
|
if (ax.response?.data?.message) return ax.response.data.message;
|
|
if (!ax.response && (ax.code === 'ERR_NETWORK' || ax.message?.includes('Network Error'))) {
|
|
return '서버에 연결할 수 없습니다. 서버 PC에서 서버시작.bat 이 실행 중인지 확인해 주세요.';
|
|
}
|
|
return ax.message || fallback;
|
|
}
|