-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (35 loc) · 1.01 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import axios from "axios";
import Cookies from "js-cookie";
class ApiWrapper {
constructor(config) {
this.baseURL = config.baseURL;
this.loginURL = config.loginURL;
this.cookieName = config.cookieName;
const getCookie = key => Cookies.get(key);
const Authorization = getCookie(this.cookieName);
let headers = {};
if (Authorization)
headers.Authorization = Authorization;
this.apiClient = axios.create({
baseURL: this.baseURL,
timeout: 10000,
headers
})
this.apiClient.interceptors.response.use(
response => {
return response.data;
},
error => {
if (error.response.status > 400) {
this.onError();
} else {
return error;
}
}
);
}
onError() {
window.location = this.loginURL;
}
}
export default ApiWrapper