10cheon00 / drf-practice

0 stars 0 forks source link

Created axios instance to add interceptor. #24

Closed 10cheon00 closed 3 years ago

10cheon00 commented 3 years ago

Axios Config

axios api를 사용할 때 여러가지 옵션을 줄 수 있다.

interceptor를 사용해 에러를 핸들링할 필요성이 생겼다. 그런데 instance를 만들어야만 중복을 제거하면서 명시적으로 이용할 수 있었다. 더불어 기존 폴더 구조도 axios를 난잡하게 이용할 가능성이 있었다.

store/
|-- AxiosApi.js (ArticleApi, ProfileApi를 포함하는 모듈)
|-- AxiosApi/
|   |-- ArticleApi.js
|   `-- ProfileApi.js
`-- store.js (AxiosApi를 포함)

이 구조에서 axios instance를 만들고 관리할 공간이 없었다. AxiosApi내에 있는 vuex 모듈들은 axios를 import하고 있었다. 새로 instance를 만들어 두고 그걸 import하는 방법으로 바꾸었다.

store/
|-- BackendApi/
|   |-- articleApi.js (axiosWrapper를 포함)
|   |-- axiosWrapper.js 
|   |-- module.js (articleApi, profileApi를 포함)
|   `-- profileApi.js (axiosWrapper를 포함)
`-- store.js (BackendApi/module를 포함)

store <- module <- [ articleApi, profileApi, ... ] <- axiosWrapper 의존성 방향이 명확해졌다.

이제 axios({ ... })말고 axiosInstance({ ... })로 바꾸어 axios를 사용할 때마다 interceptor를 사용할 수 있게 되었다.