async-retry-wrapper
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -11,4 +11,5 @@ type AsyncFunction = Function; | ||
rule?: Function; | ||
logging?: boolean; | ||
} | ||
export default function retryWrapper(functions: Functions, options?: Options): Functions; |
101
index.js
@@ -16,3 +16,2 @@ 'use strict'; | ||
const _isObject = obj => typeof obj === 'object' && !Array.isArray(obj); | ||
const _isNotObject = obj => !_isObject(obj); | ||
@@ -25,5 +24,11 @@ /** | ||
const _isFunction = func => typeof func === 'function'; | ||
const _isNotFunction = func => !_isFunction(func); | ||
/** | ||
* @private check boolean | ||
* | ||
* @param {Boolean} bool boolean | ||
*/ | ||
const _isBoolean = bool => typeof bool === 'boolean'; | ||
/** | ||
* @private make logger when error occur | ||
@@ -57,14 +62,25 @@ * | ||
/** | ||
* @private make retry option with validation | ||
* | ||
* @param {Object} options | ||
* @param {Number} options.count retry count | ||
* @param {Number} options.interval retry interval | ||
* @param {Function} options.rule retry skip rule | ||
* @param {Function} options.errorLogger retry error logger | ||
* @private make valid option | ||
* | ||
* @param {Object} [options] | ||
* @param {Number} [options.count] retry count | ||
* @param {Number} [options.interval] retry interval | ||
* @param {Function} [options.rule] retry skip rule | ||
* @param {Function} [options.errorLogger] retry logger | ||
* @param {Boolean} [options.logging] retry logging option, default: false | ||
*/ | ||
const _makeValidRetryOption = (options) => { | ||
const { rule, count, errorLogger, interval } = options; | ||
const _makeValidOptions = (options) => { | ||
if (!!options && !_isObject(options)) { | ||
throw new Error('options is must be object'); | ||
} | ||
const setOptions = { | ||
count: options?.count ?? 1, | ||
interval: options?.interval ?? 0, | ||
rule: options?.rule ?? _makeDefaultRetrySkipRule, | ||
logging: options?.logging ?? false, | ||
} | ||
if (_isNotFunction(rule)) { | ||
const { count, rule, interval, logging } = setOptions; | ||
if (!_isFunction(rule)) { | ||
throw new Error('rule is must be function'); | ||
@@ -75,38 +91,13 @@ } | ||
} | ||
if (_isNotFunction(errorLogger)) { | ||
throw new Error('errorLogger is must be function'); | ||
} | ||
if (!Number.isInteger(interval)) { | ||
throw new Error('interval is must be integer'); | ||
} | ||
if (!_isBoolean(logging)) { | ||
throw new Error('logging is must be boolean'); | ||
} | ||
return { | ||
rule, | ||
count, | ||
interval, | ||
errorLogger, | ||
}; | ||
}; | ||
return setOptions; | ||
} | ||
/** | ||
* set default option | ||
* | ||
* @param {Object} [options] | ||
* @param {Number} [options.count] retry count | ||
* @param {Number} [options.interval] retry interval | ||
* @param {Function} [options.rule] retry skip rule | ||
* @param {Function} [options.errorLogger] retry logger | ||
* @param {String} name function name | ||
*/ | ||
const _makeDefaultOptions = (options, name) => { | ||
const count = options?.count ?? 1; | ||
return { | ||
count, | ||
interval: options?.interval ?? 0, | ||
rule: options?.rule ?? _makeDefaultRetrySkipRule, | ||
errorLogger: _makeRetryLogger(name, count), | ||
}; | ||
}; | ||
/** | ||
* @private functions validator | ||
@@ -117,3 +108,3 @@ * | ||
const _validFunctions = (functions) => { | ||
if (_isNotObject(functions)) { | ||
if (!_isObject(functions)) { | ||
throw new Error('functions is must be object'); | ||
@@ -132,13 +123,2 @@ } | ||
/** | ||
* @private functions validator | ||
* | ||
* @param {Object} options | ||
*/ | ||
const _validOptions = (options) => { | ||
if (!!options && _isNotObject(options)) { | ||
throw new Error('options is must be object'); | ||
} | ||
}; | ||
/** | ||
* @private function wrapping | ||
@@ -157,6 +137,7 @@ * | ||
throw err; | ||
} | ||
}; | ||
if (option.logging) { | ||
option.errorLogger(currentCount, err); | ||
}; | ||
option.errorLogger(currentCount, err); | ||
return _delay(option.interval) | ||
@@ -175,6 +156,6 @@ .then(() => _call(func, currentCount, option)); | ||
* @param {Function} [options.rule] retry skip rule, default: occur error | ||
* @param {Boolean} [options.logging] retry logging option, default: false | ||
*/ | ||
const retryWrapper = (functions, options) => { | ||
_validFunctions(functions); | ||
_validOptions(options); | ||
@@ -184,4 +165,6 @@ return Object.keys(functions) | ||
const func = functions[name]; | ||
const setOptions = _makeDefaultOptions(options, name); | ||
const option = _makeValidRetryOption(setOptions); | ||
const option = _makeValidOptions(options); | ||
if (option.logging) { | ||
option.errorLogger = _makeRetryLogger(name, option.count); | ||
} | ||
@@ -188,0 +171,0 @@ result[name] = async (...args) => { |
@@ -47,9 +47,9 @@ const { it, describe } = require('@jest/globals'); | ||
describe('options', () => { | ||
it('If the option exists and is not an object, it fails', () => { | ||
const options = []; | ||
it('If the option exists and is not an object, it fails', () => { | ||
const options = []; | ||
expect(() => { | ||
retryWrapper(DEFAULT_FUNCTIONS, options); | ||
}).toThrowError(); | ||
}); | ||
expect(() => { | ||
retryWrapper(DEFAULT_FUNCTIONS, options); | ||
}).toThrowError(); | ||
}); | ||
@@ -95,2 +95,11 @@ it('If count is not a number, it fails', () => { | ||
}); | ||
it('if logging is not a boolean, it fails', () => { | ||
const options = { | ||
logging: 'test', | ||
} | ||
expect(() => { | ||
retryWrapper(DEFAULT_FUNCTIONS, options); | ||
}).toThrowError(); | ||
}); | ||
}); | ||
@@ -97,0 +106,0 @@ }); |
{ | ||
"name": "async-retry-wrapper", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Wrap the async function", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -48,2 +48,4 @@ # async-retry-wrapper | ||
* default: Retry if an error occurs | ||
* logging: Do I need a log for retries? | ||
* default: false | ||
@@ -59,2 +61,3 @@ The example is an option to retry twice more at 100 ms intervals when the response status is not 500. | ||
rule: err => err.status !== 500, | ||
logging: true, | ||
}; | ||
@@ -61,0 +64,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
13489
93
303