diff --git a/types/p-catch-if/index.d.ts b/types/p-catch-if/index.d.ts new file mode 100644 index 0000000000..a832b88534 --- /dev/null +++ b/types/p-catch-if/index.d.ts @@ -0,0 +1,12 @@ +// Type definitions for p-catch-if 1.0 +// Project: https://github.com/sindresorhus/p-catch-if#readme +// Definitions by: Linus Unnebäck +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace pCatchIf { + type Predicate = ErrorConstructor | ReadonlyArray | boolean | ((err: Error) => boolean | PromiseLike); +} + +declare function pCatchIf(predicate: pCatchIf.Predicate, fn: (err: Error) => T): (err: Error) => T; + +export = pCatchIf; diff --git a/types/p-catch-if/p-catch-if-tests.ts b/types/p-catch-if/p-catch-if-tests.ts new file mode 100644 index 0000000000..f42679cce6 --- /dev/null +++ b/types/p-catch-if/p-catch-if-tests.ts @@ -0,0 +1,39 @@ +import pCatchIf = require('p-catch-if'); + +/**** Setup "fake" environemt ****/ + +declare function getData(): Promise<{}>; +declare function retry(fn: () => T): T; +declare function recordError(err: Error): undefined; +declare function isProduction(): boolean; +declare function sendError(err: Error): Promise; +declare function checkResults(): Promise; + +declare const TimeoutError: ErrorConstructor; +declare const NetworkError: ErrorConstructor; +declare const UnicornError: ErrorConstructor; + +declare const console: { error(): undefined }; + +/**** Use examples from the official library's readme.md ****/ + +// error constructor +getData().catch(pCatchIf(TimeoutError, () => retry(getData))); + +// multiple error constructors +getData().catch(pCatchIf([NetworkError, TimeoutError], () => retry(getData))); + +// boolean +getData().catch(pCatchIf(isProduction, recordError)); + +// function +const hasUnicornMessage = (err: Error) => err.message.includes('unicorn'); +getData().catch(pCatchIf(hasUnicornMessage, console.error)); + +// promise-returning function +const handler = (err: Error) => sendError(err).then(checkResults); +getData().catch(pCatchIf(handler, console.error)); + +// can also be nested +const validateMessage = (err: Error) => err.message === 'Too many rainbows'; +getData().catch(pCatchIf(UnicornError, pCatchIf(validateMessage, console.error))); diff --git a/types/p-catch-if/tsconfig.json b/types/p-catch-if/tsconfig.json new file mode 100644 index 0000000000..bdabef2511 --- /dev/null +++ b/types/p-catch-if/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "p-catch-if-tests.ts" + ] +} diff --git a/types/p-catch-if/tslint.json b/types/p-catch-if/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/p-catch-if/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }