import axios from 'axios'; import { toast } from "react-toastify"; export default class RequestApi { static Request() { axios.interceptors.response.use( response => response, async (error) => { if (error) { const err = JSON.parse(JSON.stringify(error)); if (err.name === 'AxiosError' && err.code === 'ERR_NETWORK') { alert(err.message); return; } if (err.response) { if (err.response.status === 307 || err.response.status === 403 || err.response.status === 401) { toast.error("Token expired, please re-login"); // clearAllState(); window.localStorage.clear(); window.location.reload(); // this.props.history.replace('/login'); return; } } if (err && err.message && err.message.includes('401')) { // alert('Token expired, please re-login'); toast.error("Token expired, please re-login"); // clearAllState(); window.localStorage.clear(); window.location.reload(); // this.props.history.replace('/login'); return; } return Promise.reject(error); } } ); return axios; } static Header() { let header = { headers: { "Content-Type": "application/json", // 'Cache-Control': 'no-cache' } } return header; } static HeaderWithToken() { const token = localStorage.getItem('token') let header = { headers: { "Content-Type": "application/json", "Authorization": `Bearer ${token}`, // 'Cache-Control': 'no-cache', } } return header; } static HeaderMultipart() { const token = localStorage.getItem('token') let header = { headers: { "Content-Type": "multipart/form-data", "Authorization": `Bearer ${token}`, // 'Cache-Control': 'no-cache', } } return header; } } export const AXIOS = RequestApi.Request();