import axios from 'axios'; import { useRoute } from 'vue-router'; import { useToastStore } from '@s/toastStore'; import { useLoadingStore } from "@s/loadingStore"; const $api = axios.create({ baseURL: import.meta.env.VITE_API_URL, timeout: 300000, withCredentials: true, }); /** * Default Content-Type : json * form 사용 시 옵션에 { isFromDate : true } 추가 */ $api.interceptors.request.use( function (config) { const loadingStore = useLoadingStore(); loadingStore.startLoading(); let contentType = 'application/json'; if (config.isFormData) contentType = 'multipart/form-data'; config.headers['Content-Type'] = contentType; config.headers['X-Requested-With'] = 'XMLHttpRequest'; return config; }, function (error) { const loadingStore = useLoadingStore(); loadingStore.stopLoading(); // 요청 오류가 있는 작업 수행 return Promise.reject(error); }, ); // 응답 인터셉터 추가하기 $api.interceptors.response.use( function (response) { const loadingStore = useLoadingStore(); loadingStore.stopLoading(); // 2xx 범위의 응답 처리 return response; }, function (error) { const loadingStore = useLoadingStore(); loadingStore.stopLoading(); const toastStore = useToastStore(); // 오류 응답 처리 if (error.response) { switch (error.response.status) { case 401: if (!error.config.headers.isLogin) { toastStore.onToast('인증이 필요합니다.', 'e'); } break; case 403: toastStore.onToast('접근 권한이 없습니다.', 'e'); break; case 404: toastStore.onToast('요청한 페이지를 찾을 수 없습니다.', 'e'); break; case 500: toastStore.onToast('서버 오류가 발생했습니다.', 'e'); break; default: toastStore.onToast('알 수 없는 오류가 발생했습니다.', 'e'); } } else if (error.request) { // 요청이 전송되었으나 응답을 받지 못한 경우 toastStore.onToast('서버와 통신할 수 없습니다.', 'e'); } else { // 요청 설정 중에 오류가 발생한 경우 toastStore.onToast('요청 중 오류가 발생했습니다.', 'e'); } return Promise.reject(error); }, ); export default $api;