Skip to content

Commit

Permalink
Merge pull request #54 from luanmuniz/master
Browse files Browse the repository at this point in the history
Require just used functions & use natives
  • Loading branch information
analog-nico committed Jul 27, 2015
2 parents d2bb057 + 34a3995 commit 225f027
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
27 changes: 15 additions & 12 deletions lib/rp.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

var Bluebird = require('./bluebird-fresh.js'),
CapturedTrace = require('./bluebird-captured-trace-fresh.js'),
_ = require('lodash'),
assign = require('lodash/object/assign'),
isFunction = require('lodash/lang/isFunction'),
isPlainObject = require('lodash/lang/isPlainObject'),
isString = require('lodash/lang/isString'),
chalk = require('chalk'),
errors = require('./errors.js');

Expand All @@ -11,18 +14,18 @@ var Bluebird = require('./bluebird-fresh.js'),
var request = (function () {

function clearCache() {
_.forEach(_.keys(require.cache), function (key) {
Object.keys(require.cache).forEach(function (key) {
delete require.cache[key];
});
}

var temp = _.assign({}, require.cache);
var temp = assign({}, require.cache);
clearCache();

var freshRequest = require('request');

clearCache();
_.assign(require.cache, temp);
assign(require.cache, temp);

return freshRequest;

Expand All @@ -36,7 +39,7 @@ function RP$callback(err, response, body) {

var origCallbackThrewException = false, thrownException;

if (_.isFunction(self._rp_callbackOrig)) {
if (isFunction(self._rp_callbackOrig)) {
try {
self._rp_callbackOrig.apply(self, arguments);
} catch (e) {
Expand All @@ -47,22 +50,22 @@ function RP$callback(err, response, body) {

if (err) {

self._rp_reject(_.assign(new errors.RequestError(err), {
self._rp_reject(assign(new errors.RequestError(err), {
error: err,
options: self._rp_options,
response: response
}));

} else if (self._rp_options.simple && !(/^2/.test('' + response.statusCode))) {

self._rp_reject(_.assign(new errors.StatusCodeError(response.statusCode, body), {
self._rp_reject(assign(new errors.StatusCodeError(response.statusCode, body), {
error: body,
options: self._rp_options,
response: response
}));

} else {
if (_.isFunction(self._rp_options.transform)) {
if (isFunction(self._rp_options.transform)) {
try {
self._rp_resolve(self._rp_options.transform(body, response));
} catch (e) {
Expand All @@ -80,7 +83,7 @@ function RP$callback(err, response, body) {
}

// Mimic original behavior of errors emitted by request with no error listener registered
if (err && _.isFunction(self._rp_callbackOrig) === false && self._rp_promise_in_use !== true && self.listeners('error').length === 1) {
if (err && isFunction(self._rp_callbackOrig) === false && self._rp_promise_in_use !== true && self.listeners('error').length === 1) {
throw err;
}

Expand All @@ -93,7 +96,7 @@ request.Request.prototype.init = function RP$initInterceptor(options) {
var self = this;

// Init may be called again - currently in case of redirects
if (_.isPlainObject(options) && self._callback === undefined && self._rp_promise === undefined) {
if (isPlainObject(options) && self._callback === undefined && self._rp_promise === undefined) {

self._rp_promise = new Bluebird(function (resolve, reject) {
self._rp_resolve = resolve;
Expand All @@ -104,7 +107,7 @@ request.Request.prototype.init = function RP$initInterceptor(options) {
self._rp_callbackOrig = self.callback;
self.callback = RP$callback;

if (_.isString(options.method)) {
if (isString(options.method)) {
options.method = options.method.toUpperCase();
}

Expand All @@ -128,7 +131,7 @@ function expose(methodToExpose, exposeAs) {
exposeAs = exposeAs || methodToExpose;

/* istanbul ignore if */
if (!_.isUndefined(request.Request.prototype[exposeAs])) {
if (request.Request.prototype[exposeAs] !== undefined) {
console.error(chalk.bold.bgRed('[Request-Promise] Unable to expose method "' + exposeAs + '". It is already implemented by Request. Please visit https://github.com/request/request-promise/wiki/Troubleshooting'));
return;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"dependencies": {
"bluebird": "^2.3",
"chalk": "1.0.x",
"lodash": "3.6.x",
"lodash": "^3.6.0",
"request": "^2.34"
},
"devDependencies": {
Expand Down

0 comments on commit 225f027

Please sign in to comment.