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

feat: add TS libdef #81

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .nycrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
],
"exclude": [
"test",
"node_modules"
"node_modules",
"patch-md.js"
],
"check-coverage": true,
"report-dir": ".nyc_output"
Expand Down
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ dist: focal
language: node_js

node_js:
- '20'
- '18'
- '16'
- '14'
Expand All @@ -27,7 +28,7 @@ script:
- npm run readme
- test -z "$(git diff)"

- npm test
# run tests with coverage
- npm run cover

after_success:
Expand All @@ -38,5 +39,5 @@ deploy:
skip_cleanup: true
script: semantic-release
on:
node_js: '16'
node_js: '20'
all_branches: true
19 changes: 19 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
type RetryOptions = {
retries?: number
initialDelay?: number
timeout?: number
factor?: number
log?: (msg: string) => void
shouldRetry?: (err: Error) => boolean
}

type Callable = (...args: any[]) => any

type RetryWrapper = {
<T extends Callable = Callable>(fn: T): T
<T extends Callable = Callable>(opts: RetryOptions, fn: T): T
}

declare function retryify(opts: RetryOptions): RetryWrapper

export = retryify
9 changes: 6 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ const getOpt = function(option, _default) {
* @param {Number} context.initialDelay time to wait before making attempts
* @param {Number} context.timeout time to wait between retries (in ms)
* @param {Number} context.factor the exponential scaling factor
* @param {function} options.shouldRetry - Invoked with the thrown error,
* @param {Function} context.shouldRetry - Invoked with the thrown error,
* retryify will retry if this method returns true.
* @param {Function} context.log logging function that takes a message as input
*
* @return {Promise} a Promise for whatever the wrapped function eventually
* resolves to.
Expand Down Expand Up @@ -107,9 +108,9 @@ const execute = async function(context) {
* after 200 ms, the third after 400 ms, etc.... The formula used to
* calculate the delay between each retry:
* ```timeout * Math.pow(factor, attempts)```
* @property {function} [options.shouldRetry=() => true] - Invoked with the
* @property {Function} [options.shouldRetry=() => true] - Invoked with the
* thrown error, retryify will retry if this method returns true.
* @property {Function} [options.log] Logging function that takes a message as
* @property {Function} [options.log] Logging function that takes a message as input
*/

/**
Expand Down Expand Up @@ -191,4 +192,6 @@ const retryify = function(options = {}) {
return retryWrapper;
};

retryify.retryify = retryify;

module.exports = retryify;
Loading