-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
47 lines (35 loc) · 1.02 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
var superagent = require('superagent');
module.exports = function superagent_adapter(opts, cb) {
var url = opts.url;
var method = opts.method;
var query = opts.query;
var body = opts.body;
// METHOD determines the function
// GET, PUT, POST, DELETE
var req = superagent(method, url);
req.set('Accept', 'application/json');
if (opts.headers) {
req.set(opts.headers);
}
if (query) {
req.query(query);
}
// TODO patch?
if (body && (method === 'POST' || method === 'PUT')) {
req.set('Content-Type', 'application/json');
req.send(body.toJSON ? body.toJSON() : body);
}
req.end(function(err, res) {
if (err) {
return cb(err);
}
var body = res.body || null;
if (res.status !== 200) {
var err = new Error();
err.status = res.status;
err.message = (body ? body.message : '');
return cb(err, body, res);
}
cb(null, body, res);
});
};