-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.js
74 lines (65 loc) · 2.11 KB
/
http.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import axios from 'axios';
import Config from 'react-native-config';
import { SecureStore } from 'expo';
const instance = axios.create({
// Config.API_ENDPOINT should be here (now is undefined - why?)
baseURL: 'https://young-falls-56718.herokuapp.com/api/v1'
});
instance.interceptors.request.use(
function(config) {
return SecureStore.getItemAsync('userToken').then(userToken => {
config.headers['Authorization'] = `Bearer ${userToken}`;
return Promise.resolve(config);
});
},
function(error) {
return Promise.reject(error);
}
);
instance.interceptors.response.use(
function(response) {
return response.data;
},
function(error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
if (error.response.status === 401) {
// redirectToLogin();
} else if (error.response.status === 403) {
// redirectToUnauthorized();
} else if (error.response.status === 403 && error.response.data.detail.match('CSRF Failed')) {
// Redirect to login, to fix csrf token issues
// redirectToLogin(true);
}
} else if (error.request) {
// The request was made but no response was received
// `error.request` is an instance of XMLHttpRequest in the browser and an instance of
// http.ClientRequest in node.js
console.error(error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.error('Error', error.message);
}
console.error(error.config);
return Promise.reject(error);
}
);
export function get(url, config) {
return instance.get(url, config);
}
export function post(url, data, config) {
return instance.post(url, data, config);
}
export function remove(url, config) {
return instance.delete(url, config);
}
export function put(url, data, config) {
return instance.put(url, data, config);
}
export function patch(url, data, config) {
return instance.patch(url, data, config);
}
export function concurrentRequests(requestsArray) {
return axios.all(requestsArray);
}