Merge pull request #26444 from LinusU/p-catch-if

p-catch-if: add typings
This commit is contained in:
Nathan Shively-Sanders
2018-06-19 15:16:41 -07:00
committed by GitHub
4 changed files with 75 additions and 0 deletions
+12
View File
@@ -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 <https://github.com/LinusU>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare namespace pCatchIf {
type Predicate = ErrorConstructor | ReadonlyArray<ErrorConstructor> | boolean | ((err: Error) => boolean | PromiseLike<boolean>);
}
declare function pCatchIf<T>(predicate: pCatchIf.Predicate, fn: (err: Error) => T): (err: Error) => T;
export = pCatchIf;
+39
View File
@@ -0,0 +1,39 @@
import pCatchIf = require('p-catch-if');
/**** Setup "fake" environemt ****/
declare function getData(): Promise<{}>;
declare function retry<T>(fn: () => T): T;
declare function recordError(err: Error): undefined;
declare function isProduction(): boolean;
declare function sendError(err: Error): Promise<undefined>;
declare function checkResults(): Promise<boolean>;
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)));
+23
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }