Backend for Custom Frontend OSPRO Surveyor Indonesia
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

112 lines
3.8 KiB

import axios from 'axios';
import { toast } from "react-toastify";
export default class RequestApi {
// static Request() {
// // axios.interceptors.request.use(function (config) {
// // const token = localStorage.getItem('token')
// // config.headers.Authorization = token;
// // return config;
// // });
// const token = localStorage.getItem('token')
// let instance = axios.create({
// headers: {
// 'Content-Type': 'application/json',
// "Authorization": `Bearer ${token}`
// }
// })
// instance.interceptors.response.use(
// (response) => response,
// async (error) => {
// // const originalRequest = error.config;
// if (error.response.status === 307 || error.response.status === 403) {
// console.log(error.response);
// }
// return Promise.reject(error);
// }
// );
// return instance;
// }
static Request() {
axios.interceptors.response.use(
response => response,
async (error) => {
// console.log('error axios', JSON.stringify(error));
if (error) {
// console.log('stringify', JSON.stringify(error));
const err = JSON.parse(JSON.stringify(error));
console.log('error', err);
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) {
// console.log(err.response);
// 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;
}
}
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();