import axios , { AxiosRequestConfig , AxiosError } from 'axios' ; import { setLoading } from 'actions/app.action' ; export type IConfig = AxiosRequestConfig & { showSpinner ? : boolean ; } ; type IAxiosResponse = AxiosError & { config : { showSpinner ? : boolean ; } ; } ; function getAccessToken ( ) { const accessToken = window . localStorage . getItem ( 'accessToken' ) ; return accessToken ; } const requestConfig = { baseURL : process . env . REACT_APP_ENDPOINT_URL , timeout : 5000 , showSpinner : false , } ; export const axiosInstance = axios . create ( requestConfig ) ; export default function initRequest ( store ) { let requestCount = 0 ; function decreaseRequestCount ( ) { requestCount -= 1 ; if ( requestCount === 0 ) { store . dispatch ( setLoading ( false ) ) ; } } axiosInstance . interceptors . request . use ( ( config ) => { if ( config . showSpinner ) { requestCount += 1 ; store . dispatch ( setLoading ( true ) ) ; } const accessToken = getAccessToken ( ) ; if ( accessToken ) { config . headers [ 'x-auth-token' ] = accessToken ; } return config ; } , ( error ) => { if ( error . config . showSpinner ) { decreaseRequestCount ( ) ; } return Promise . reject ( error ) ; } ) ; axiosInstance . interceptors . response . use ( ( res ) => { if ( res . config . showSpinner ) { decreaseRequestCount ( ) ; } return res ; } , ( error ) => { if ( ( error && error . config . showSpinner ) || error . code === 'ECONNABORTED' ) { decreaseRequestCount ( ) ; } if ( error . code === 'ECONNABORTED' ) { store . dispatch ( setLoading ( false ) ) ; } switch ( error . response ?. status ) { case 400 : { break ; } case 500 : { break ; } default : break ; } return Promise . reject ( error ) ; } ) ; }