1234567891011121314151617181920212223242526272829303132333435363738394041 |
- import axios, { AxiosRequestHeaders } from 'axios';
- import qs from 'qs';
-
- const defaultConfig = {
- baseURL: import.meta.env.VITE_BASE_URL
- };
-
- const instance = axios.create(Object.assign({}, defaultConfig));
-
- instance.interceptors.request.use(
- (config) => {
- ('isAuth' in config.headers) ? null : config.headers.isAuth = true
- if (config.headers.isAuth) {
- config.headers.Authorization = `Bearer ${import.meta.env.VITE_AUTHORIZATION}`;
- }
- const data = config.data || false
- if (
- config.method?.toUpperCase() === 'POST' &&
- (config.headers as AxiosRequestHeaders)['Content-Type'] ===
- 'application/x-www-form-urlencoded'
- ) {
- config.data = qs.stringify(data)
- }
- return config;
- },
- (error) => {
- return Promise.reject(error);
- }
- );
- instance.interceptors.response.use(
- (response) => {
- if (response.data) {
- return response.data;
- }
- return response;
- },
- (error) => {
- return Promise.reject(error);
- }
- );
- export default instance;
|