diff --git a/README.md b/README.md index 5172b92..830b4ab 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ npm install --save @root/request ```js var request = require('@root/request'); -request('http://www.google.com', function(error, response, body) { +request('http://www.google.com', function (error, response, body) { console.log('error:', error); // Print the error if one occurred console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received console.log('body:', body); // Print the HTML for the Google homepage. @@ -31,11 +31,11 @@ request('http://www.google.com', function(error, response, body) { var request = require('@root/request'); request('http://www.google.com') - .then(function(response) { + .then(function (response) { console.log('statusCode:', response.statusCode); // Print the response status code if a response was received console.log('body:', response.body); // Print the HTML for the Google homepage. }) - .catch(function(error) { + .catch(function (error) { console.log('error:', error); // Print the error if one occurred }); ``` @@ -63,7 +63,7 @@ request.post('http://service.com/upload', { form: { key: 'value' } }); // or request.post( { url: 'http://service.com/upload', form: { key: 'value' } }, - function(err, httpResponse, body) { + function (err, httpResponse, body) { /* ... */ } ); @@ -190,7 +190,7 @@ var username = 'username', password = 'password', url = 'http://' + username + ':' + password + '@some.server.com'; -request({ url: url }, function(error, response, body) { +request({ url: url }, function (error, response, body) { // Do more stuff with 'body' here }); ``` diff --git a/examples/follow-redirect.js b/examples/follow-redirect.js index b863f27..d3b7051 100644 --- a/examples/follow-redirect.js +++ b/examples/follow-redirect.js @@ -5,16 +5,15 @@ var request = require('../'); // will redirect to https://www.github.com and then https://github.com //request('http://www.github.com', function (error, response, body) { -request({ uri: { protocol: 'http:', hostname: 'www.github.com' } }, function( - error, - response, - body -) { - if (error) { - console.log('error:', error); // Print the error if one occurred - return; +request( + { uri: { protocol: 'http:', hostname: 'www.github.com' } }, + function (error, response, body) { + if (error) { + console.log('error:', error); // Print the error if one occurred + return; + } + console.log('statusCode:', response.statusCode); // The final statusCode + console.log('Final href:', response.request.uri.href); // The final URI + console.log('Body Length:', body.length); // body length } - console.log('statusCode:', response.statusCode); // The final statusCode - console.log('Final href:', response.request.uri.href); // The final URI - console.log('Body Length:', body.length); // body length -}); +); diff --git a/examples/form-data.js b/examples/form-data.js index 92bfa52..0014850 100644 --- a/examples/form-data.js +++ b/examples/form-data.js @@ -20,7 +20,7 @@ request( ) } }, - function(error, response, body) { + function (error, response, body) { if (error) { console.log('error:', error); // Print the error if one occurred return; diff --git a/examples/get-to-json.js b/examples/get-to-json.js index 59785ef..1a4b107 100644 --- a/examples/get-to-json.js +++ b/examples/get-to-json.js @@ -2,7 +2,7 @@ //var request = require('urequest'); var request = require('../'); -request('https://www.google.com', function(error, response, body) { +request('https://www.google.com', function (error, response, body) { if (error) { console.log('error:', error); // Print the error if one occurred return; diff --git a/examples/http-string.js b/examples/http-string.js index d1f7c2a..eb5e1cf 100644 --- a/examples/http-string.js +++ b/examples/http-string.js @@ -2,7 +2,7 @@ //var request = require('urequest'); var request = require('../'); -request('http://www.google.com', function(error, response, body) { +request('http://www.google.com', function (error, response, body) { console.log('error:', error); // Print the error if one occurred console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received console.log('body:', body); // Print the HTML for the Google homepage. diff --git a/examples/https-string.js b/examples/https-string.js index 701a42b..67625ed 100644 --- a/examples/https-string.js +++ b/examples/https-string.js @@ -2,7 +2,7 @@ //var request = require('urequest'); var request = require('../'); -request('https://www.google.com', function(error, response, body) { +request('https://www.google.com', function (error, response, body) { console.log('error:', error); // Print the error if one occurred console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received console.log('body:', body); // Print the HTML for the Google homepage. diff --git a/examples/no-follow-redirect.js b/examples/no-follow-redirect.js index cc5a47d..35bd9ba 100644 --- a/examples/no-follow-redirect.js +++ b/examples/no-follow-redirect.js @@ -4,17 +4,16 @@ var request = require('../'); // would normally redirect to https://www.github.com and then https://github.com -request({ uri: 'https://www.github.com', followRedirect: false }, function( - error, - response, - body -) { - if (error) { - console.log('error:', error); // Print the error if one occurred - return; +request( + { uri: 'https://www.github.com', followRedirect: false }, + function (error, response, body) { + if (error) { + console.log('error:', error); // Print the error if one occurred + return; + } + console.log('href:', response.request.uri.href); // The final URI + console.log('statusCode:', response.statusCode); // Should be 301 or 302 + console.log('Location:', response.headers.location); // The redirect + console.log('Body:', body || JSON.stringify(body)); } - console.log('href:', response.request.uri.href); // The final URI - console.log('statusCode:', response.statusCode); // Should be 301 or 302 - console.log('Location:', response.headers.location); // The redirect - console.log('Body:', body || JSON.stringify(body)); -}); +); diff --git a/examples/postbin.js b/examples/postbin.js index dacabae..4280bd1 100644 --- a/examples/postbin.js +++ b/examples/postbin.js @@ -7,9 +7,9 @@ request({ //headers: { 'user-agent': 'test/1.0' } // overwrite //userAgent: 'test/1.1' // add to the default }) - .then(function(resp) { + .then(function (resp) { console.log(resp.body); }) - .catch(function(err) { + .catch(function (err) { console.error(err); }); diff --git a/examples/www-form.js b/examples/www-form.js index ac4fead..6bc8195 100644 --- a/examples/www-form.js +++ b/examples/www-form.js @@ -14,7 +14,7 @@ request( headers: { 'X-Foo': 'Bar' }, form: { foo: 'bar', baz: 'qux' } }, - function(error, response, body) { + function (error, response, body) { if (error) { console.log('error:', error); // Print the error if one occurred return; diff --git a/index.js b/index.js index ec8f9c6..d2ec7be 100644 --- a/index.js +++ b/index.js @@ -13,7 +13,7 @@ function debug() { } function mergeOrDelete(defaults, updates) { - Object.keys(defaults).forEach(function(key) { + Object.keys(defaults).forEach(function (key) { if (!(key in updates)) { updates[key] = defaults[key]; return; @@ -37,7 +37,7 @@ function mergeOrDelete(defaults, updates) { // retrieves an existing header, case-sensitive function getHeaderName(reqOpts, header) { var headerNames = {}; - Object.keys(reqOpts.headers).forEach(function(casedName) { + Object.keys(reqOpts.headers).forEach(function (casedName) { headerNames[casedName.toLowerCase()] = casedName; }); // returns the key, which in erroneous cases could be an empty string @@ -49,11 +49,11 @@ function hasHeader(reqOpts, header) { } function toJSONifier(keys) { - return function() { + return function () { var obj = {}; var me = this; - keys.forEach(function(key) { + keys.forEach(function (key) { if (me[key] && 'function' === typeof me[key].toJSON) { obj[key] = me[key].toJSON(); } else { @@ -78,7 +78,7 @@ function setDefaults(defs) { function onResponse(resp) { var followRedirect; - Object.keys(defs).forEach(function(key) { + Object.keys(defs).forEach(function (key) { if (key in opts && 'undefined' !== typeof opts[key]) { return; } @@ -146,7 +146,7 @@ function setDefaults(defs) { resp.body = ''; } resp._bodyLength = 0; - resp.on('data', function(chunk) { + resp.on('data', function (chunk) { if ('string' === typeof resp.body) { resp.body += chunk.toString(opts.encoding); } else { @@ -154,7 +154,7 @@ function setDefaults(defs) { resp._bodyLength += chunk.length; } }); - resp.on('end', function() { + resp.on('end', function () { if ('string' !== typeof resp.body) { if (1 === resp._body.length) { resp.body = resp._body[0]; @@ -199,12 +199,12 @@ function setDefaults(defs) { _body = JSON.stringify(opts.json); } else if (opts.form) { _body = Object.keys(opts.form) - .filter(function(key) { + .filter(function (key) { if ('undefined' !== typeof opts.form[key]) { return true; } }) - .map(function(key) { + .map(function (key) { return ( encodeURIComponent(key) + '=' + @@ -218,7 +218,7 @@ function setDefaults(defs) { _body = Buffer.from(_body); } - Object.keys(opts.uri).forEach(function(key) { + Object.keys(opts.uri).forEach(function (key) { finalOpts[key] = opts.uri[key]; }); @@ -233,7 +233,7 @@ function setDefaults(defs) { 'createConnection', 'timeout', 'setHost' - ].forEach(function(key) { + ].forEach(function (key) { finalOpts[key] = opts.uri[key]; }); @@ -299,7 +299,7 @@ function setDefaults(defs) { } try { form = new MyFormData(); - Object.keys(opts.formData).forEach(function(key) { + Object.keys(opts.formData).forEach(function (key) { function add(key, data, opts) { if (data.value) { opts = data.options; @@ -308,7 +308,7 @@ function setDefaults(defs) { form.append(key, data, opts); } if (Array.isArray(opts.formData[key])) { - opts.formData[key].forEach(function(data) { + opts.formData[key].forEach(function (data) { add(key, data); }); } else { @@ -320,7 +320,7 @@ function setDefaults(defs) { return; } formHeaders = form.getHeaders(); - Object.keys(formHeaders).forEach(function(header) { + Object.keys(formHeaders).forEach(function (header) { finalOpts.headers[header] = formHeaders[header]; }); } @@ -346,7 +346,7 @@ function setDefaults(defs) { debug(formHeaders); // generally uploads don't use Chunked Encoding (some systems have issues with it) // and I don't want to do the work to calculate the content-lengths. This seems to work. - req = form.submit(finalOpts, function(err, resp) { + req = form.submit(finalOpts, function (err, resp) { if (err) { cb(err); return; @@ -380,7 +380,7 @@ function setDefaults(defs) { if ('function' === typeof _body.pipe) { // used for chunked encoding _body.pipe(req); - _body.on('error', function(err) { + _body.on('error', function (err) { // https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options // if the Readable stream emits an error during processing, // the Writable destination is not closed automatically @@ -427,7 +427,7 @@ function setDefaults(defs) { opts = { url: opts }; } - module.exports._keys.forEach(function(key) { + module.exports._keys.forEach(function (key) { if (key in opts && 'undefined' !== typeof opts[key]) { reqOpts[key] = opts[key]; } else if (key in defs) { @@ -489,14 +489,14 @@ function setDefaults(defs) { } function smartPromisify(fn) { - return function(opts) { + return function (opts) { var cb; if ('function' === typeof arguments[1]) { cb = Array.prototype.slice.call(arguments)[1]; return fn(opts, cb); } - return new Promise(function(resolve, reject) { - fn(opts, function(err, resp) { + return new Promise(function (resolve, reject) { + fn(opts, function (err, resp) { if (err) { err._response = resp; reject(err); @@ -510,13 +510,13 @@ function setDefaults(defs) { var smartUrequest = smartPromisify(urequest); - smartUrequest.defaults = function(_defs) { + smartUrequest.defaults = function (_defs) { _defs = mergeOrDelete(defs, _defs); return setDefaults(_defs); }; ['get', 'put', 'post', 'patch', 'delete', 'head', 'options'].forEach( - function(method) { - smartUrequest[method] = smartPromisify(function(obj, cb) { + function (method) { + smartUrequest[method] = smartPromisify(function (obj, cb) { if ('string' === typeof obj) { obj = { url: obj }; } diff --git a/tests/tcp-listener.js b/tests/tcp-listener.js index e9028ee..1401a69 100644 --- a/tests/tcp-listener.js +++ b/tests/tcp-listener.js @@ -1,11 +1,11 @@ 'use strict'; var net = require('net'); -var server = net.createServer(function(socket) { - socket.on('data', function(chunk) { +var server = net.createServer(function (socket) { + socket.on('data', function (chunk) { console.info(chunk.toString('utf8')); }); }); -server.listen(3007, function() { +server.listen(3007, function () { console.info('Listening on', this.address()); });