Last active
September 14, 2023 06:44
-
-
Save Ah-ae/790037dff4ab897cfc3bee4904cb410a to your computer and use it in GitHub Desktop.
axios instance 생성
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios, { AxiosRequestConfig, AxiosInstance } from 'axios'; | |
import { DEMO_BASE_URL } from '../utils/commons'; | |
import { getToken } from '../utils/tokenManagement'; | |
// 인증 정보가 필요하지 않은 instance | |
export const baseAPI = (url: string, options?: AxiosRequestConfig) => { | |
return axios.create({ | |
baseURL: url, | |
headers: { | |
Accept: 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
...options, | |
}); | |
}; | |
// 인증 정보가 필요한 instance | |
const interceptors = (instance: AxiosInstance) => { | |
instance.interceptors.request.use( | |
(config) => { | |
const token = getToken(); | |
if (token) { | |
config.headers.Authorization = `Token ${token}`; | |
} | |
return config; | |
}, | |
(error) => { | |
console.error(error); | |
return Promise.reject(error); | |
}, | |
); | |
}; | |
const authAPI = (url: string, options?: AxiosRequestConfig) => { | |
const instance = axios.create({ | |
baseURL: url, | |
headers: { | |
Accept: 'application/json', | |
}, | |
withCredentials: true, | |
...options, | |
}); | |
interceptors(instance); | |
return instance; | |
}; | |
export const baseInstance = baseAPI(DEMO_BASE_URL); | |
export const authInstance = authAPI(DEMO_BASE_URL); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment