import axios from 'axios' import queryString from 'query-string' import { NEXT_PUBLIC_SITE_URL } from '../configs/index' import { store } from '../state' import { userActions } from '../state/user' import { CustomError } from '../utils/errorHelper' import { sentryLogger } from '../utils/logger/sentry' import { getUserData } from '../utils/storage' // import { setupCache } from 'axios-cache-adapter' // const cache = setupCache({ maxAge: 15 * 60 * 100, exclude: { query: false } }) // const noCache = setupCache({ maxAge: 0 }) // Set up default config for http requests here // Please have a look at here `https://github.com/axios/axios#request- config` for the full list of configs const axiosService = axios.create({ baseURL: `${NEXT_PUBLIC_SITE_URL}/api`, headers: { 'content-type': 'application/json', }, paramsSerializer: (params) => queryString.stringify(params), // adapter: cache.adapter, }) axiosService.interceptors.request.use(async (config) => { const userData = getUserData() const token = 'accessToken' in userData ? userData.accessToken : '' if (token) { config.headers.Authorization = `Bearer ${token}` } return config }) axiosService.interceptors.response.use( (response) => { // check them is success if (response && response.data) { return response.data } return response }, (error) => { if (error.response.status === 401) { store.dispatch({ type: userActions.logout.type, payload: { isShowToast: false }, }) throw new CustomError('Login session has expired. Please login again!') } sentryLogger(error.response) throw error.response }, ) export default axiosService