-
Notifications
You must be signed in to change notification settings - Fork 8
/
ApiEngine.js
63 lines (55 loc) · 1.83 KB
/
ApiEngine.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
// ref: <https://github.com/erikras/react-redux-universal-hot-example/blob/master/src/helpers/ApiClient.js>
import superagent from 'superagent';
import getPort from '../../server/utils/getPort';
const BASE = process.env.BROWSER ? '' : `http://localhost:${getPort()}`;
const methods = ['get', 'post', 'put', 'patch', 'del'];
function formatUrl(path) {
return `${BASE}${path}`;
}
export default class ApiEngine {
constructor(req) {
methods.forEach((method) => {
this[method] = (path, { params, data, files } = {}) => {
return new Promise((resolve, reject) => {
const request = superagent[method](formatUrl(path));
if (params) {
request.query(params);
}
if (!process.env.BROWSER && req.get('cookie')) {
request.set('cookie', req.get('cookie'));
}
if (data) {
request.send(data);
}
if (files) {
let formData = new FormData();
Object.keys(files).forEach((name) => {
formData.append(name, files[name]);
});
request.send(formData);
}
request.end((err, { body } = {}) => {
if (err) {
return reject(body || err);
}
if (body.isError) {
return reject(body.errors);
}
return resolve(body);
});
});
};
});
}
/*
* There's a V8 bug where, when using Babel, exporting classes with only
* constructors sometimes fails. Until it's patched, this is a solution to
* "ApiClient is not defined" from issue #14.
* https://github.com/erikras/react-redux-universal-hot-example/issues/14
*
* Relevant Babel bug (but they claim it's V8): https://phabricator.babeljs.io/T2455
*
* Remove it at your own risk.
*/
empty() {}
}