Comparing version
//Shows thunked methods in acion (se moore at https://github.com/mikeal/request/blob/master/README.md) | ||
var co = require('co') | ||
, request = require('../'); | ||
"use strict"; | ||
let co = require('co'); | ||
let request = require('../'); | ||
co(function* () { | ||
//Get method | ||
var getResponse = yield request.get('http://nodejs.org'); | ||
var body = getResponse.body; | ||
let getResponse = yield request.get('http://nodejs.org'); | ||
let body = getResponse.body; | ||
console.log('Get Response: ', getResponse); | ||
console.log('Get Body: ', body); | ||
//Post method | ||
var postResponse = yield request.post('http://nodejs.org'); | ||
let postResponse = yield request.post('http://nodejs.org'); | ||
console.log('Post Response: ', postResponse); | ||
})(); | ||
}).catch(function (err) { | ||
console.err(err); | ||
}); |
//Shows entire HTTP response and its body | ||
var co = require('co') | ||
, request = require('../'); | ||
"use strict"; | ||
let co = require('co'); | ||
let request = require('../'); | ||
co(function* () { | ||
var response = yield request('http://google.com'); | ||
var body = response.body; | ||
let response = yield request('http://google.com'); | ||
let body = response.body; | ||
console.log('Response: ', response); | ||
console.log('Body: ', body); | ||
})(); | ||
console.log('Response: ', response); | ||
console.log('Body: ', body); | ||
}).catch(function (err) { | ||
console.err(err); | ||
}); |
69
index.js
@@ -1,6 +0,8 @@ | ||
var request = require('request'); | ||
var __slice = Array.prototype.slice; | ||
"use strict"; | ||
let request = require("request"); | ||
let __slice = Array.prototype.slice; | ||
/** | ||
* Thunkify a request method. | ||
* Promisify a request method. | ||
* | ||
@@ -10,19 +12,24 @@ * @param {Function} fn | ||
*/ | ||
var thunkifyRequestMethod = function (fn) { | ||
var context = this; | ||
let promisifyRequestMethod = function (fn) { | ||
let context = this; | ||
return function () { | ||
var args = __slice.call(arguments); | ||
return function () { | ||
let args = __slice.call(arguments); //Array.from(arguments) is not available. | ||
return function (done) { | ||
// Concatenate the callback manually to avoid array arguments from co. | ||
return fn.apply(context, args.concat(function (err, res) { | ||
done(err, res); | ||
})); | ||
}; | ||
}; | ||
return new Promise(function (resolve, reject) { | ||
// Concatenate the callback manually to avoid array arguments from co. | ||
return fn.apply(context, args.concat(function (err) { | ||
if (err) { | ||
reject(err); | ||
} else { | ||
resolve.apply(this, __slice.call(arguments, 1)); | ||
} | ||
})); | ||
}); | ||
} | ||
}; | ||
/** | ||
* Thunkify a request function. | ||
* Promisify a request function. | ||
* | ||
@@ -32,23 +39,23 @@ * @param {Function} request | ||
*/ | ||
var thunkifyRequest = function (request) { | ||
var fn = thunkifyRequestMethod(request); | ||
let promisifyRequest = function (request) { | ||
let fn = promisifyRequestMethod(request); | ||
// Regular request methods that don't need be thunkified. | ||
fn.jar = request.jar; | ||
fn.cookie = request.cookie; | ||
// Regular request methods that don't need be promisified. | ||
fn.jar = request.jar; | ||
fn.cookie = request.cookie; | ||
// Attach all request methods. | ||
['get', 'patch', 'post', 'put', 'head', 'del'].forEach(function (method) { | ||
fn[method] = thunkifyRequestMethod.call(request, request[method]); | ||
}); | ||
// Attach all request methods. | ||
["get", "patch", "post", "put", "head", "del"].forEach(function (method) { | ||
fn[method] = promisifyRequestMethod.call(request, request[method]); | ||
}); | ||
return fn; | ||
return fn; | ||
}; | ||
/** | ||
* Export a thunkified request function. | ||
* Export a promisified request function. | ||
* | ||
* @type {Function} | ||
*/ | ||
exports = module.exports = thunkifyRequest(request); | ||
exports = module.exports = promisifyRequest(request); | ||
@@ -63,3 +70,3 @@ /** | ||
/** | ||
* Export the defaults method and return a thunkified request instance. | ||
* Export the defaults method and return a promisified request instance. | ||
* | ||
@@ -69,7 +76,7 @@ * @return {Function} | ||
exports.defaults = function () { | ||
return thunkifyRequest(request.defaults.apply(request, arguments)); | ||
return promisifyRequest(request.defaults.apply(request, arguments)); | ||
}; | ||
/** | ||
* Export the forever agent method and return a thunkified request instance. | ||
* Export the forever agent method and return a promisified request instance. | ||
* | ||
@@ -79,3 +86,3 @@ * @return {Function} | ||
exports.forever = function () { | ||
return thunkifyRequest(request.forever.apply(request, arguments)); | ||
return promisifyRequest(request.forever.apply(request, arguments)); | ||
}; |
{ | ||
"name": "co-request", | ||
"description": "co-request thunkify wrapper for request", | ||
"description": "co-request promisify wrapper for request", | ||
"main": "./", | ||
@@ -10,3 +10,3 @@ "author": { | ||
}, | ||
"version": "0.2.1", | ||
"version": "1.0.0", | ||
"scripts": { | ||
@@ -16,3 +16,2 @@ "test": "node_modules/.bin/mocha --harmony test/test.js" | ||
"keywords": [ | ||
"thunks", | ||
"request", | ||
@@ -19,0 +18,0 @@ "rest", |
@@ -23,14 +23,18 @@ co-request | ||
```js | ||
var co = require('co') | ||
, request = require('co-request'); | ||
"use strict"; | ||
let co = require("co"); | ||
let request = require("co-request"); | ||
co(function* () { | ||
// You can also pass options object, see http://github.com/mikeal/request docs | ||
var result = yield request('http://google.com'); | ||
var response = result; | ||
var body = result.body; | ||
let result = yield request("http://google.com"); | ||
let response = result; | ||
let body = result.body; | ||
console.log('Response: ', response); | ||
console.log('Body: ', body); | ||
})(); | ||
console.log("Response: ", response); | ||
console.log("Body: ", body); | ||
}).catch(function (err) { | ||
console.err(err); | ||
}); | ||
``` | ||
@@ -41,7 +45,9 @@ | ||
```js | ||
"use strict"; | ||
co(function* () { | ||
var result = yield request({ | ||
uri: 'http://google.com', | ||
method: 'POST' | ||
}); | ||
let result = yield request({ | ||
uri: "http://google.com", | ||
method: "POST" | ||
}); | ||
})(); | ||
@@ -48,0 +54,0 @@ ``` |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
8540
6.39%168
7.01%1
-50%75
8.7%