-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
HttpUtils.js
85 lines (77 loc) · 2.6 KB
/
HttpUtils.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
75
76
77
78
79
80
81
82
83
84
85
import _ from 'underscore';
import CONFIG from '../CONFIG';
import CONST from '../CONST';
// To avoid a circular dependency, we can't include Log here, so instead, we define an empty logging method and expose the setLogger method to set the logger from outside this file
let info = () => {};
/**
* Send an HTTP request, and attempt to resolve the json response.
* If there is a network error, we'll set the application offline.
*
* @param {String} url
* @param {String} method
* @param {Object} body
* @returns {Promise}
*/
function processHTTPRequest(url, method = 'get', body = null) {
return fetch(url, {
method,
body,
})
.then(response => response.json());
}
/**
* Makes XHR request
* @param {String} command the name of the API command
* @param {Object} data parameters for the API command
* @param {String} type HTTP request type (get/post)
* @param {Boolean} shouldUseSecure should we use the secure server
* @returns {Promise}
*/
function xhr(command, data, type = CONST.NETWORK.METHOD.POST, shouldUseSecure = false) {
if (command !== 'Log') {
info('Making API request', false, {
command,
type,
shouldUseSecure,
rvl: data.returnValueList,
});
}
const formData = new FormData();
_.each(data, (val, key) => formData.append(key, val));
const apiRoot = shouldUseSecure ? CONFIG.EXPENSIFY.URL_EXPENSIFY_SECURE : CONFIG.EXPENSIFY.URL_API_ROOT;
return processHTTPRequest(`${apiRoot}api?command=${command}`, type, formData)
.then((response) => {
if (command !== 'Log') {
info('Finished API request', false, {
command,
type,
shouldUseSecure,
jsonCode: response.jsonCode,
requestID: response.requestID,
});
}
return response;
});
}
/**
* Just download a file from the web server.
*
* @param {String} relativePath From the website root, NOT the API root. (no leading slash, ., or ..)
* @returns {Promise}
*/
function download(relativePath) {
const siteRoot = CONFIG.EXPENSIFY.URL_EXPENSIFY_CASH;
// Strip leading slashes and periods from relative path, if present
const strippedRelativePath = relativePath.charAt(0) === '/' || relativePath.charAt(0) === '.'
? relativePath.slice(relativePath.indexOf('/') + 1)
: relativePath;
return processHTTPRequest(`${siteRoot}${strippedRelativePath}`);
}
function setLogger(logger) {
info = logger.info;
}
export default {
setLogger,
download,
xhr,
};