diff --git a/types/p-retry/index.d.ts b/types/p-retry/index.d.ts new file mode 100644 index 0000000000..30a3b23542 --- /dev/null +++ b/types/p-retry/index.d.ts @@ -0,0 +1,18 @@ +// Type definitions for p-retry 1.0 +// Project: https://github.com/sindresorhus/p-retry#readme +// Definitions by: BendingBender +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import { OperationOptions } from 'retry'; + +export = pRetry; + +declare function pRetry(input: (attemptCount: number) => PromiseLike | T, options?: OperationOptions): Promise; + +declare namespace pRetry { + class AbortError extends Error { + readonly name: 'AbortError'; + readonly originalError: Error; + constructor(message: string | Error); + } +} diff --git a/types/p-retry/p-retry-tests.ts b/types/p-retry/p-retry-tests.ts new file mode 100644 index 0000000000..72962d553e --- /dev/null +++ b/types/p-retry/p-retry-tests.ts @@ -0,0 +1,16 @@ +import pRetry = require('p-retry'); +import fetch from 'node-fetch'; + +const run = () => fetch('https://sindresorhus.com/unicorn') + .then(response => { + // abort retrying if the resource doesn't exist + if (response.status === 404) { + throw new pRetry.AbortError(response.statusText); + } + + return response.text(); + }); + +pRetry(run, {retries: 5}).then(result => { + const str: string = result; +}); diff --git a/types/p-retry/tsconfig.json b/types/p-retry/tsconfig.json new file mode 100644 index 0000000000..68f27fc4b2 --- /dev/null +++ b/types/p-retry/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "p-retry-tests.ts" + ] +} diff --git a/types/p-retry/tslint.json b/types/p-retry/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/p-retry/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/retry/index.d.ts b/types/retry/index.d.ts index 09dc603dd7..cc5e8b57ee 100644 --- a/types/retry/index.d.ts +++ b/types/retry/index.d.ts @@ -15,10 +15,7 @@ export interface RetryOperation { * * @return {void} */ - attempt(callback: (current: number) => void, options?: { - timeout?: number - callback?: () => void - }): void + attempt(callback: (current: number) => void, options?: AttemptOptions): void; /** * Returns false when no error value is given, or the maximum amount of retries has been reached. @@ -28,14 +25,14 @@ export interface RetryOperation { * * @return {boolean} */ - retry(err?: Error): boolean + retry(err?: Error): boolean; /** * The number of attempts it took to call the retrying function before it was successful. * * @return {number} */ - attempts(): number + attempts(): number; /** * A reference to the error object that occured most frequently. @@ -43,20 +40,26 @@ export interface RetryOperation { * * @return {(Error|null)} If no errors occured so far the value will be null. */ - mainError(): Error | null + mainError(): Error | null; /** * Returns an array of all errors that have been passed to RetryOperation.retry() so far. * * @return {Error[]} */ - errors(): Error[] + errors(): Error[]; /** * Stops the operation being retried. Useful for aborting the operation on a fatal error etc. */ - stop(): void + stop(): void; } + +export interface AttemptOptions { + timeout?: number; + callback?(): void; +} + /** * Create a new RetryOperation object. * @@ -70,15 +73,17 @@ export interface RetryOperation { * * @return {RetryOperation} */ -export function operation(options?: { - retries?: number - factor?: number - minTimeout?: number - maxTimeout?: number - randomize?: boolean - forever?: boolean - unref?: boolean -}): RetryOperation +export function operation(options?: OperationOptions): RetryOperation; + +export interface OperationOptions { + retries?: number; + factor?: number; + minTimeout?: number; + maxTimeout?: number; + randomize?: boolean; + forever?: boolean; + unref?: boolean; +} /** * Get an array with timeouts and their return values in milliseconds. @@ -91,13 +96,15 @@ export function operation(options?: { * * @return {number[]} */ -export function timeouts(options?: { - retries?: number - factor?: number - minTimeout?: number - maxTimeout?: number - randomize?: boolean -}): number[] +export function timeouts(options?: TimeoutsOptions): number[]; + +export interface TimeoutsOptions { + retries?: number; + factor?: number; + minTimeout?: number; + maxTimeout?: number; + randomize?: boolean; +} /** * Create a new timeout (in milliseconds) based on the given parameters. @@ -110,12 +117,14 @@ export function timeouts(options?: { * * @return {number} timeout */ -export function createTimeout(attempt: number, options?: { - factor?: number - minTimeout?: number - maxTimeout?: number - randomize?: boolean -}): number +export function createTimeout(attempt: number, options?: CreateTimeoutOptions): number; + +export interface CreateTimeoutOptions { + factor?: number; + minTimeout?: number; + maxTimeout?: number; + randomize?: boolean; +} /** * Wrap all functions of the object with retry. @@ -132,12 +141,14 @@ export function createTimeout(attempt: number, options?: { * * @return {void} */ -export function wrap(object: object, options?: { - retries?: number - factor?: number - minTimeout?: number - maxTimeout?: number - randomize?: boolean - forever?: boolean - unref?: boolean // tslint:disable-next-line:align -}, methods?: string[]): void +export function wrap(object: object, options?: WrapOptions, methods?: string[]): void; + +export interface WrapOptions { + retries?: number; + factor?: number; + minTimeout?: number; + maxTimeout?: number; + randomize?: boolean; + forever?: boolean; + unref?: boolean; +} diff --git a/types/retry/retry-tests.ts b/types/retry/retry-tests.ts index 1754d1ca62..bb5eb35976 100644 --- a/types/retry/retry-tests.ts +++ b/types/retry/retry-tests.ts @@ -1,14 +1,14 @@ -import retry = require('retry') +import retry = require('retry'); // Option values -const att = 4 -const ret = 2 -const fac = 1.5 -const min = 2000 -const max = 4000 -const rnd = false -const evr = false -const unr = false +const att = 4; +const ret = 2; +const fac = 1.5; +const min = 2000; +const max = 4000; +const rnd = false; +const evr = false; +const unr = false; // Options themselves const operationOptions = { @@ -19,14 +19,14 @@ const operationOptions = { randomize: rnd, forever: evr, unref: unr, -} +}; const timeoutOptions = { factor: fac, minTimeout: min, maxTimeout: max, randomize: rnd, -} +}; const timeoutsOptions = { retries: ret, @@ -34,40 +34,39 @@ const timeoutsOptions = { minTimeout: min, maxTimeout: max, randomize: rnd, -} +}; // Class to be wrapped later on class Foo { - public bar() { + bar() { // } - public baz() { + baz() { // } } -const operation = retry.operation(operationOptions) +const operation = retry.operation(operationOptions); operation.attempt((current) => { - const err = Math.random() >= 0.5 ? new Error('Happens to the best of us') : undefined + const err = Math.random() >= 0.5 ? new Error('Happens to the best of us') : undefined; - const retry = operation.retry(err) + const retry = operation.retry(err); if (retry) { - const after = operation.attempts() - } - else { - const errors = operation.errors() + const after = operation.attempts(); + } else { + const errors = operation.errors(); - const main = operation.mainError() + const main = operation.mainError(); } - operation.stop() -}) + operation.stop(); +}); -const timeout = retry.createTimeout(att, timeoutOptions) +const timeout = retry.createTimeout(att, timeoutOptions); -const timeouts = retry.timeouts(timeoutsOptions) +const timeouts = retry.timeouts(timeoutsOptions); -const wrap = retry.wrap(new Foo(), operationOptions, ['bar']) +const wrap = retry.wrap(new Foo(), operationOptions, ['bar']); diff --git a/types/retry/tslint.json b/types/retry/tslint.json index d2a13a49cb..3db14f85ea 100644 --- a/types/retry/tslint.json +++ b/types/retry/tslint.json @@ -1,14 +1 @@ -{ - "extends": "dtslint/dt.json", - "rules": { - "member-access": true, - "prefer-method-signature": false, - "semicolon": false, - "one-line": { - "options": [ - "check-open-brace", - "check-whitespace" - ] - } - } -} +{ "extends": "dtslint/dt.json" }