Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require just used functions & use natives #54

Merged
merged 2 commits into from
Jul 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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