mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-04 14:57:10 +00:00
Merge pull request #26444 from LinusU/p-catch-if
p-catch-if: add typings
This commit is contained in:
Vendored
+12
@@ -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;
|
||||
@@ -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)));
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user