-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (50 loc) · 1.83 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'use strict';
var _flyd = require('flyd');
var _flyd2 = _interopRequireDefault(_flyd);
var _ramda = require('ramda');
var _ramda2 = _interopRequireDefault(_ramda);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var request = function request(os) {
var streams = { load: _flyd2.default.stream(), progress: _flyd2.default.stream(), error: _flyd2.default.stream(), abort: _flyd2.default.stream() };
var req = new XMLHttpRequest();
req.addEventListener('load', function (ev) {
var content = req.getResponseHeader('Content-Type');
// Try to parse body if it is JSON -- but don't throw exceptions here
if (content && content.match('json')) {
try {
req.body = JSON.parse(req.response);
} catch (e) {}
}
streams.load(req);
});
req.addEventListener('progress', streams.progress);
req.addEventListener('error', streams.error);
req.addEventListener('abort', streams.abort);
// Parse and append the query parameters, if provided as an object
if (os.query) {
var keyVals = _ramda2.default.toPairs(os.query);
var joinedWithEquals = _ramda2.default.map(_ramda2.default.apply(function (key, val) {
return key + '=' + String(val);
}), keyVals);
os.path += "?" + _ramda2.default.join('&', joinedWithEquals);
}
req.open(os.method, (os.url || '') + os.path, true);
if (os.send && (os.send.constructor === Object || os.send.constructor === Array)) {
os.send = JSON.stringify(os.send);
}
if (os.headers) {
for (var key in os.headers) {
req.setRequestHeader(key, os.headers[key]);
}
}
req.withCredentials = os.withCredentials;
req.send(os.send);
return streams;
};
request.toFormData = function (obj) {
var fd = new FormData();
for (var key in obj) {
fd.append(key, obj[key]);
}return fd;
};
module.exports = request;