import axios , { AxiosInstance } from 'axios' ; import util from 'util' ; param baseURL export const createAxiosInstance = ( baseURL ) => { const instance = axios . create ( { baseURL } ) ; /**
     * * 백엔드에 요청을 바로 보낼 수 있는 래핑 함수 입니다.
     */ installDefaults ( instance ) ; if ( process . browser && process . env . NODE_ENV === 'development' ) { installLogger ( instance ) ; } return instance ; } ; /**
 * Axios 사용시 추천되는 기본 옵션들을 설치합니다.
 */ export const installDefaults = ( instance ) => { instance . defaults . headers = { ... instance . defaults . headers , Pragma : 'no-cache' , 'Cache-Control' : 'no-cache' } ; instance . defaults . withCredentials = true ; } ; /**
 * 네트워크 요청 디버깅용 로거를 설치합니다.
 */ export const installLogger = ( instance ) => { instance . interceptors . request . use ( ( config ) => { window . console . log ( `%c📦 API 요청 송신  주소:${ config . url } 유형:${ config . method ?. toUpperCase ( ) }` , 'color: #E19A56;' , config . params ) ; return config ; } , ( error ) => { if ( ! process . browser ) { const now = new Date ( ) ; console . error ( `${ now . toISOString ( ) } [ ${ error . config . method . toUpperCase ( ) } ] :` , error . request . path ) ; console . log ( '\n[ Stack ] : ' , error ) ; if ( error . request ) { console . log ( '\n[ RequestHeader ] : ' , util . inspect ( error . request . _header ) ) ; } if ( error . config ) { console . log ( '\n[ ConfigHeader ] : ' , util . inspect ( error . config . headers ) ) ; console . log ( '\n[ RequestQuery ] : ' , util . inspect ( error . config . params ) ) ; console . log ( '\n[ RequestBody ] : ' , util . inspect ( error . config . data ) ) ; } if ( error . response ) { console . log ( '\n[ ResponseHeader ] : ' , util . inspect ( error . response . headers ) ) ; console . log ( '\n[ ResponseData ] : ' , util . inspect ( error . response . data ) ) ; } console . log ( '======================================================\n' ) ; } return Promise . reject ( error ) ; } ) ; instance . interceptors . response . use ( ( response ) => { window . console . log ( `%c📫 API 응답 수신  주소:${ response . config . url } 유형:${ response . config . method ?. toUpperCase ( ) }` , 'color: #31B4D9;' , response ) ; return response ; } , ( error ) => { return Promise . reject ( error ) ; } ) ; } ; export const setToken = ( instance , accessToken ) : void => { if ( accessToken ) { instance . defaults . headers . common . Authorization = `Bearer ${ accessToken }` ; } else { delete instance . defaults . headers . common . Authorization ; } } ;