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.
100 lines
3.1 KiB
100 lines
3.1 KiB
2 years ago
|
import axios from 'axios';
|
||
|
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', error);
|
||
|
if (error) {
|
||
|
// console.log('stringify', JSON.stringify(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) {
|
||
|
// console.log(err.response);
|
||
|
alert('Token expired, please re-login');
|
||
|
// clearAllState();
|
||
|
window.localStorage.clear();
|
||
|
// 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();
|
||
|
|