refactor: Update useAxios hook
This commit is contained in:
parent
4638846030
commit
7fd9ded5f8
@ -5,11 +5,22 @@ const AxiosType = {
|
|||||||
EXTERNAL: 'External',
|
EXTERNAL: 'External',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* axios 인스턴스 생성 후 반환
|
||||||
|
* @param {String} lang
|
||||||
|
* @returns http request instance - get, post, put, patch, delete (promise 접수사가 붙은 함수는 promise 반환)
|
||||||
|
*/
|
||||||
export function useAxios(lang = '') {
|
export function useAxios(lang = '') {
|
||||||
const getInstances = (url) => {
|
const getInstances = (url) => {
|
||||||
|
/**
|
||||||
|
* url이 http로 시작하면 외부 서버로 판단
|
||||||
|
*/
|
||||||
let type = AxiosType.INTERNAL
|
let type = AxiosType.INTERNAL
|
||||||
url.startsWith('http') ? (type = AxiosType.EXTERNAL) : ''
|
url.startsWith('http') ? (type = AxiosType.EXTERNAL) : ''
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 내부 서버로 요청 시 lang 헤더 추가
|
||||||
|
*/
|
||||||
let headerValue = {
|
let headerValue = {
|
||||||
Accept: 'application/json',
|
Accept: 'application/json',
|
||||||
}
|
}
|
||||||
@ -21,18 +32,13 @@ export function useAxios(lang = '') {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// request 추가 로직
|
||||||
axios.interceptors.request.use((config) => {
|
axios.interceptors.request.use((config) => {
|
||||||
// config['Authorization'] = localStorage.getItem('token')
|
|
||||||
//TODO: 인터셉터에서 추가 로직 구현
|
|
||||||
return config
|
return config
|
||||||
})
|
})
|
||||||
|
|
||||||
axios.interceptors.request.use(undefined, (error) => {
|
// response 추가 로직
|
||||||
//TODO: 인터셉터에서 에러 처리 로직 구현
|
axios.interceptors.request.use(undefined, (error) => {})
|
||||||
// if (error.isAxiosError && e.response?.status === 401) {
|
|
||||||
// localStorage.removeItem('token')
|
|
||||||
// }
|
|
||||||
})
|
|
||||||
|
|
||||||
const get = async ({ url }) => {
|
const get = async ({ url }) => {
|
||||||
return await getInstances(url)
|
return await getInstances(url)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user