Skip to content

Commit

Permalink
formatting updated + version bump
Browse files Browse the repository at this point in the history
  • Loading branch information
dgkanatsios committed Jul 24, 2017
1 parent d6a840f commit 6ad6f09
Show file tree
Hide file tree
Showing 4 changed files with 281 additions and 308 deletions.
312 changes: 156 additions & 156 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ var RetryStrategies = require('./strategies');
var _ = require('lodash');

var DEFAULTS = {
maxAttempts: 5, // try 5 times
retryDelay: 5000, // wait for 5s before trying again
fullResponse: true, // resolve promise with the full response object
promiseFactory: defaultPromiseFactory // Function to use a different promise implementation library
maxAttempts: 5, // try 5 times
retryDelay: 5000, // wait for 5s before trying again
fullResponse: true, // resolve promise with the full response object
promiseFactory: defaultPromiseFactory // Function to use a different promise implementation library
};

// Default promise factory which use bluebird
function defaultPromiseFactory(resolver) {
return when.promise(resolver);
return when.promise(resolver);
}

/**
Expand All @@ -34,193 +34,193 @@ function defaultPromiseFactory(resolver) {
*/
function makePromise(requestInstance, promiseFactoryFn) {

// Resolver function wich assigns the promise (resolve, reject) functions
// to the requestInstance
function Resolver(resolve, reject) {
this._resolve = resolve;
this._reject = reject;
}
// Resolver function wich assigns the promise (resolve, reject) functions
// to the requestInstance
function Resolver(resolve, reject) {
this._resolve = resolve;
this._reject = reject;
}

return promiseFactoryFn(Resolver.bind(requestInstance));
return promiseFactoryFn(Resolver.bind(requestInstance));
}

function Request(url, options, f, retryConfig) {
// ('url')
if (_.isString(url)) {
// ('url', f)
if (_.isFunction(options)) {
f = options;
}

if (!_.isObject(options)) {
options = {};
}

// ('url', {object})
options.url = url;
// ('url')
if(_.isString(url)){
// ('url', f)
if(_.isFunction(options)){
f = options;
}

if (_.isObject(url)) {
if (_.isFunction(options)) {
f = options;
}
options = url;
if(!_.isObject(options)){
options = {};
}

this.maxAttempts = retryConfig.maxAttempts;
this.retryDelay = retryConfig.retryDelay;
this.fullResponse = retryConfig.fullResponse;
this.attempts = 0;

/**
* Option object
* @type {Object}
*/
this.options = options;

/**
* Return true if the request should be retried
* @type {Function} (err, response) -> Boolean
*/
this.retryStrategy = _.isFunction(options.retryStrategy) ? options.retryStrategy : RetryStrategies.HTTPOrNetworkError;

/**
* Return a number representing how long request-retry should wait before trying again the request
* @type {Boolean} (err, response, body) -> Number
*/
this.delayStrategy = _.isFunction(options.delayStrategy) ? options.delayStrategy : function () {
return this.retryDelay;
};

this._timeout = null;
this._req = null;

this._callback = _.isFunction(f) ? _.once(f) : null;

// create the promise only when no callback was provided
if (!this._callback) {
this._promise = makePromise(this, retryConfig.promiseFactory);
}
// ('url', {object})
options.url = url;
}

this.reply = function requestRetryReply(err, response, body) {
if (this._callback) {
return this._callback(err, response, body);
}
if(_.isObject(url)){
if(_.isFunction(options)){
f = options;
}
options = url;
}

this.maxAttempts = retryConfig.maxAttempts;
this.retryDelay = retryConfig.retryDelay;
this.fullResponse = retryConfig.fullResponse;
this.attempts = 0;

/**
* Option object
* @type {Object}
*/
this.options = options;

/**
* Return true if the request should be retried
* @type {Function} (err, response) -> Boolean
*/
this.retryStrategy = _.isFunction(options.retryStrategy) ? options.retryStrategy : RetryStrategies.HTTPOrNetworkError;

/**
* Return a number representing how long request-retry should wait before trying again the request
* @type {Boolean} (err, response, body) -> Number
*/
this.delayStrategy = _.isFunction(options.delayStrategy) ? options.delayStrategy : function() { return this.retryDelay; };

this._timeout = null;
this._req = null;

this._callback = _.isFunction(f) ? _.once(f) : null;

// create the promise only when no callback was provided
if (!this._callback) {
this._promise = makePromise(this, retryConfig.promiseFactory);
}

this.reply = function requestRetryReply(err, response, body) {
if (this._callback) {
return this._callback(err, response, body);
}

if (err) {
return this._reject(err);
}
if (err) {
return this._reject(err);
}

// resolve with the full response or just the body
response = this.fullResponse ? response : body;
this._resolve(response);
};
// resolve with the full response or just the body
response = this.fullResponse ? response : body;
this._resolve(response);
};
}

Request.request = request;

Request.prototype._tryUntilFail = function () {
this.maxAttempts--;
this.attempts++;

this._req = Request.request(this.options, function (err, response, body) {
if (response) {
response.attempts = this.attempts;
}
if (this.retryStrategy(err, response, body) && this.maxAttempts > 0) {
this._timeout = setTimeout(this._tryUntilFail.bind(this), this.delayStrategy.call(this, err, response, body));
return;
}

this.reply(err, response, body);
}.bind(this));
this.maxAttempts--;
this.attempts++;

this._req = Request.request(this.options, function (err, response, body) {
if (response) {
response.attempts = this.attempts;
}
if (this.retryStrategy(err, response, body) && this.maxAttempts > 0) {
this._timeout = setTimeout(this._tryUntilFail.bind(this), this.delayStrategy.call(this, err, response, body));
return;
}

this.reply(err, response, body);
}.bind(this));
};

Request.prototype.abort = function () {
if (this._req) {
this._req.abort();
}
clearTimeout(this._timeout);
this.reply(new Error('Aborted'));
if (this._req) {
this._req.abort();
}
clearTimeout(this._timeout);
this.reply(new Error('Aborted'));
};

// expose request methods from RequestRetry
['end', 'on', 'emit', 'once', 'setMaxListeners', 'start', 'removeListener', 'pipe', 'write', 'auth', ].forEach(function (requestMethod) {
Request.prototype[requestMethod] = function exposedRequestMethod() {
return this._req[requestMethod].apply(this._req, arguments);
};
['end', 'on', 'emit', 'once', 'setMaxListeners', 'start', 'removeListener', 'pipe', 'write', 'auth'].forEach(function (requestMethod) {
Request.prototype[requestMethod] = function exposedRequestMethod () {
return this._req[requestMethod].apply(this._req, arguments);
};
});

// expose promise methods
['then', 'catch', 'finally', 'fail', 'done'].forEach(function (promiseMethod) {
Request.prototype[promiseMethod] = function exposedPromiseMethod() {
if (this._callback) {
throw new Error('A callback was provided but waiting a promise, use only one pattern');
}
return this._promise[promiseMethod].apply(this._promise, arguments);
};
Request.prototype[promiseMethod] = function exposedPromiseMethod () {
if (this._callback) {
throw new Error('A callback was provided but waiting a promise, use only one pattern');
}
return this._promise[promiseMethod].apply(this._promise, arguments);
};
});

function Factory(url, options, f) {
var retryConfig = _.chain(_.isObject(url) ? url : options || {}).defaults(DEFAULTS).pick(Object.keys(DEFAULTS)).value();
var req = new Request(url, options, f, retryConfig);
req._tryUntilFail();
return req;
var retryConfig = _.chain(_.isObject(url) ? url : options || {}).defaults(DEFAULTS).pick(Object.keys(DEFAULTS)).value();
var req = new Request(url, options, f, retryConfig);
req._tryUntilFail();
return req;
}

// adds a helper for HTTP method `verb` to object `obj`
function makeHelper(obj, verb) {
obj[verb] = function helper(url, options, f) {
// ('url')
if (_.isString(url)) {
// ('url', f)
if (_.isFunction(options)) {
f = options;
}

if (!_.isObject(options)) {
options = {};
}

// ('url', {object})
options.url = url;
}

if (_.isObject(url)) {
if (_.isFunction(options)) {
f = options;
}
options = url;
}

options.method = verb.toUpperCase();
return obj(options, f);
};
obj[verb] = function helper(url, options, f) {
// ('url')
if(_.isString(url)){
// ('url', f)
if(_.isFunction(options)){
f = options;
}

if(!_.isObject(options)){
options = {};
}

// ('url', {object})
options.url = url;
}

if(_.isObject(url)){
if(_.isFunction(options)){
f = options;
}
options = url;
}

options.method = verb.toUpperCase();
return obj(options, f);
};
}

function defaults(defaultOptions, defaultF) {
var factory = function (options, f) {
if (typeof options === "string") {
options = {
uri: options
};
}
return Factory.apply(null, [extend(true, {}, defaultOptions, options), f || defaultF]);
};

factory.defaults = function (newDefaultOptions, newDefaultF) {
return defaults.apply(null, [extend(true, {}, defaultOptions, newDefaultOptions), newDefaultF || defaultF]);
};

factory.Request = Request;
factory.RetryStrategies = RetryStrategies;
var factory = function (options, f) {
if (typeof options === "string") {
options = { uri: options };
}
return Factory.apply(null, [ extend(true, {}, defaultOptions, options), f || defaultF ]);
};

factory.defaults = function (newDefaultOptions, newDefaultF) {
return defaults.apply(null, [ extend(true, {}, defaultOptions, newDefaultOptions), newDefaultF || defaultF ]);
};

factory.Request = Request;
factory.RetryStrategies = RetryStrategies;

['get', 'head', 'post', 'put', 'patch', 'delete'].forEach(function (verb) {
makeHelper(factory, verb);
});
factory.del = factory['delete'];
makeHelper(factory, verb);
});
factory.del = factory['delete'];

return factory;
['jar', 'cookie'].forEach(function (method) {
factory[method] = Factory.Request.request[method];
});

return factory;
}

module.exports = Factory;
Expand All @@ -231,10 +231,10 @@ Factory.RetryStrategies = RetryStrategies;

// define .get/.post/... helpers
['get', 'head', 'post', 'put', 'patch', 'delete'].forEach(function (verb) {
makeHelper(Factory, verb);
makeHelper(Factory, verb);
});
Factory.del = Factory['delete'];

['jar', 'cookie'].forEach(function (method) {
Factory[method] = Factory.Request.request[method];
['jar', 'cookie'].forEach(function (method) {
Factory[method] = Factory.Request.request[method];
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "requestretry",
"description": "request-retry wrap nodejs request to retry http(s) requests in case of error",
"version": "1.12.0",
"version": "1.12.1",
"author": {
"name": "Francois-Guillaume Ribreau",
"email": "[email protected]",
Expand Down
Loading

0 comments on commit 6ad6f09

Please sign in to comment.