-
Notifications
You must be signed in to change notification settings - Fork 1
/
http.js
66 lines (56 loc) · 2.14 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
import axios from 'axios';
import {detectIE} from './util';
/** Prepare header as layer of control */
export const getHeaders = (method) => {
/** Append header */
let headers = {
Authorization: 'Bearer <<access_token>>'
};
if (method === 'post' || method === 'put')
headers['Content-Type'] = 'application/json';
/** Deal with IE aggressive caching
http://stackoverflow.com/questions/2848945/prevent-ie-caching */
if (detectIE() && method === 'get') {
headers['Cache-Control'] = 'no-cache, no-store, must-revalidate';
headers['Pragma'] = 'no-cache';
headers['Expires'] = '0';
}
return headers;
};
/** Parse error response */
export const getError = (res) => {
let err = null;
let response = res.response;
if (response && response.data) {
err = response.data;
} else if (response) {
err = new Error(response.statusText);
err.status = response.status;
} else {
err = new Error(res || 'HTTP Error');
err.status = 0;
}
return err;
};
/** Wrap every request for additional layer of control */
export const request = (opts, responseSelector) => {
opts.method = opts.method || 'get';
opts.headers = getHeaders(opts.method);
return axios(opts)
.then(res => {
//console.log('HTTP ', `${opts.method.toUpperCase()} ${opts.url}: `, res);
return responseSelector(res);
})
.catch(res => {
let err = getError(res);
console.log('HTTP ', `${opts.method} ${opts.url}: `, err, res);
throw err;
});
};
const dataResponseSelector = res => res.data;
/** Export wrapped RESTful methods */
export const get = (url, params) => request({url, params}, dataResponseSelector);
export const post = (url, data) => request({method: 'post', url, data}, dataResponseSelector);
export const put = (url, data) => request({method: 'put', url, data}, dataResponseSelector);
export const patch = (url, data) => request({method: 'patch', url, data}, dataResponseSelector);
export const remove = (url, data) => request({method: 'delete', url, data}, dataResponseSelector);