Merge pull request #24005 from bengry/master

Added types for 'tryer' (1.0.0)
This commit is contained in:
Bowden Kelly
2018-03-01 10:35:55 -08:00
committed by GitHub
4 changed files with 163 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
// Type definitions for tryer 1.0
// Project: https://github.com/philbooth/tryer
// Definitions by: Ben Grynhaus <https://github.com/bengry>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
declare const tryer: tryer.TryerStatic;
export = tryer;
export as namespace tryer;
declare namespace tryer {
interface Options {
/**
* The function that you want to invoke.
* If action returns a promise, iterations will not end until the promise is resolved or rejected.
* Alternatively, action may take a callback argument, done, to signal that it is asynchronous. In that case, you are responsible for calling done when the action is finished.
*
* If action is not set, it defaults to an empty function.
*/
action?: (() => Promise<any>) | ((done: () => void) => any);
/**
* when: A predicate that tests the pre-condition for invoking action.
* Until when returns true (or a truthy value), action will not be called.
* Defaults to a function that immediately returns true.
*/
when?: () => boolean;
/**
* until: A predicate that tests the post-condition for invoking action.
* After until returns true (or a truthy value), action will no longer be called.
* Defaults to a function that immediately returns true.
*/
until?: () => boolean;
/**
* fail: The error handler.
* A function that will be called if limit falsey values are returned by when or until.
* Defaults to an empty function.
*/
fail?: (err: Error) => void;
/**
* pass: Success handler.
* A function that will be called after until has returned truthily.
* Defaults to an empty function.
*/
pass?: () => void;
/**
* limit: Failure limit, representing the maximum number of falsey returns from when or until that will be permitted before invocation is deemed to have failed.
* A negative number indicates that the attempt should never fail, instead continuing for as long as when and until have returned truthy values.
* Defaults to -1.
*/
limit?: number;
/**
* interval: The retry interval, in milliseconds.
* A negative number indicates that each subsequent retry should wait for twice the interval from the preceding iteration (i.e. exponential backoff).
* The default value is -1000, signifying that the initial retry interval should be one second and that each subsequent attempt should wait for double the length of the previous interval.
*/
interval?: number;
}
type TryerStatic = (options: Readonly<Options>) => void;
}
+80
View File
@@ -0,0 +1,80 @@
import * as tryer from "tryer";
tryer(); // $ExpectError
// $ExpectError
tryer({
action: null
});
// $ExpectError
tryer({
fail: null
});
// $ExpectError
tryer({
fail: null
});
// $ExpectError
tryer({
interval: null
});
// $ExpectError
tryer({
limit: null
});
// $ExpectError
tryer({
pass: null
});
// $ExpectError
tryer({
until: null
});
// $ExpectError
tryer({
when: null
});
// $ExpectError
tryer({
when: () => 1
});
// $ExpectError
tryer({
until: () => 1
});
// $ExpectError
tryer({
limit: "notNumber"
});
// $ExpectError
tryer({
interval: "notNumber"
});
tryer({});
tryer({
action: done => {
done();
},
fail: error => {
error.message;
error.name;
},
interval: 1,
until: () => true,
when: () => false,
limit: 1,
pass: () => {}
});
+16
View File
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es6"],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": ["../"],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": ["index.d.ts", "tryer-tests.ts"]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }