From 51b7608b3eaa1cfe11b4ac2b5668421d3c478589 Mon Sep 17 00:00:00 2001 From: Ben Grynhaus Date: Thu, 1 Mar 2018 18:20:21 +0200 Subject: [PATCH] Added types for 'tryer' (1.0.0) --- types/tryer/index.d.ts | 66 +++++++++++++++++++++++++++++++ types/tryer/tryer-tests.ts | 80 ++++++++++++++++++++++++++++++++++++++ types/tryer/tsconfig.json | 16 ++++++++ types/tryer/tslint.json | 1 + 4 files changed, 163 insertions(+) create mode 100644 types/tryer/index.d.ts create mode 100644 types/tryer/tryer-tests.ts create mode 100644 types/tryer/tsconfig.json create mode 100644 types/tryer/tslint.json diff --git a/types/tryer/index.d.ts b/types/tryer/index.d.ts new file mode 100644 index 0000000000..44835dc2b2 --- /dev/null +++ b/types/tryer/index.d.ts @@ -0,0 +1,66 @@ +// Type definitions for tryer 1.0 +// Project: https://github.com/philbooth/tryer +// Definitions by: Ben Grynhaus +// 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) | ((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) => void; +} diff --git a/types/tryer/tryer-tests.ts b/types/tryer/tryer-tests.ts new file mode 100644 index 0000000000..4be432d3c7 --- /dev/null +++ b/types/tryer/tryer-tests.ts @@ -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: () => {} +}); diff --git a/types/tryer/tsconfig.json b/types/tryer/tsconfig.json new file mode 100644 index 0000000000..260613aabf --- /dev/null +++ b/types/tryer/tsconfig.json @@ -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"] +} diff --git a/types/tryer/tslint.json b/types/tryer/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/tryer/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }