forked from idx3d/co-request
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (65 loc) · 1.62 KB
/
index.js
File metadata and controls
75 lines (65 loc) · 1.62 KB
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
var request = require('request');
var __slice = Array.prototype.slice;
/**
* Thunkify a request method.
*
* @param {Function} fn
* @return {Function}
*/
var thunkifyRequestMethod = function (fn) {
return function () {
var args = __slice.call(arguments);
var context = this;
return function (done) {
// Concatinate the callback manually to avoid array arguments from co.
return fn.apply(context, args.concat(function (err, res) {
done(err, res);
}));
};
};
};
/**
* Thunkify a request function.
*
* @param {Function} request
* @return {Function}
*/
var thunkifyRequest = function (request) {
var fn = thunkifyRequestMethod(request);
// Regular request methods that don't need be thunkified.
fn.jar = request.jar;
fn.cookie = request.cookie;
// Attach all request methods.
['get', 'patch', 'post', 'put', 'head', 'del'].forEach(function (method) {
fn[method] = thunkifyRequestMethod(request[method]);
});
return fn;
};
/**
* Export a thunkified request function.
*
* @type {Function}
*/
exports = module.exports = thunkifyRequest(request);
/**
* Export the Request instance.
*
* @type {Function}
*/
exports.Request = request.Request;
/**
* Export the defaults method and return a thunkified request instance.
*
* @return {Function}
*/
exports.defaults = function () {
return thunkifyRequest(request.defaults.apply(request, arguments));
};
/**
* Export the forever agent method and return a thunkified request instance.
*
* @return {Function}
*/
exports.forever = function () {
return thunkifyRequest(request.forever.apply(request, arguments));
};