From 695b694eb95c6c056b88f152e0949edf2ea09bcc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Vatle?= Date: Thu, 16 May 2019 02:16:18 +0300 Subject: [PATCH] [log-process-errors] Add type definitions for 'log-process-errors' (#35464) * Add base type definitions for log-process-errors. * Correct return type of LogProcessErrors function. * Add log-process-errors tests. * Add tsconfig and tslint for log-process-errors. * Correct import path for log-process-errors-tests.ts. * Define minimal compatible TypeScript version for log-process-errors * Correct placement of log-process-errors type definitions. * Make options in LogProcessErrors function optional * Define `testing` and `colors` properties of LogProcessErrorsOptions as optional. * Use camelCase for logProcessErrors function to be more inline with log-process-errors' documentation. * Correct log-process-errors exports. * Remove redundant exports from logProcessErrors namespace. --- types/log-process-errors/index.d.ts | 68 +++++++++++++++++++ .../log-process-errors-tests.ts | 24 +++++++ types/log-process-errors/tsconfig.json | 23 +++++++ types/log-process-errors/tslint.json | 3 + 4 files changed, 118 insertions(+) create mode 100644 types/log-process-errors/index.d.ts create mode 100644 types/log-process-errors/log-process-errors-tests.ts create mode 100644 types/log-process-errors/tsconfig.json create mode 100644 types/log-process-errors/tslint.json diff --git a/types/log-process-errors/index.d.ts b/types/log-process-errors/index.d.ts new file mode 100644 index 0000000000..8a9e494374 --- /dev/null +++ b/types/log-process-errors/index.d.ts @@ -0,0 +1,68 @@ +// Type definitions for log-process-errors 4.1 +// Project: https://git.io/fhSGY +// Definitions by: Jørgen Vatle +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.1 + +declare namespace logProcessErrors { + /** + * Log level + */ + type LogLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent' | 'default'; + + /** + * Exception type. + */ + type ExceptionType = 'uncaughtException' | 'unhandledRejection' | 'rejectionHandled' | 'multipleResolves' | 'warning'; + + /** + * log-process-errors constructor options. + */ + interface Options { + /** + * Override logger + * + * @link https://github.com/ehmicky/log-process-errors/blob/master/docs/API.md#log + */ + log?(error: Error, level: LogLevel): Promise | void; + + /** + * Which log level to use for different exceptions. + * + * https://github.com/ehmicky/log-process-errors/blob/master/docs/API.md#log + */ + level?: Partial> & { + default?(error: Error): 'silent' | 'default'; + }; + + /** + * Which process should trigger `process.exit(1)` + * + * @link https://github.com/ehmicky/log-process-errors/blob/master/docs/API.md#exiton + */ + exitOn?: ExceptionType[]; + + /** + * When running tests, makes them fail if there are any process errors. + * + * @link https://github.com/ehmicky/log-process-errors/blob/master/docs/API.md#testing + */ + testing?: 'ava' | 'mocha' | 'jasmine' | 'tape' | 'node-tap'; + + /** + * Whether or not to colorize messages. + * + * @link https://github.com/ehmicky/log-process-errors/blob/master/docs/API.md#colors + */ + colors?: boolean; + } +} + +/** + * Log Process Errors + * + * @link https://github.com/ehmicky/log-process-errors/blob/master/docs/API.md#logprocesserrorsoptions + */ +declare function logProcessErrors(options?: logProcessErrors.Options): () => void; + +export = logProcessErrors; diff --git a/types/log-process-errors/log-process-errors-tests.ts b/types/log-process-errors/log-process-errors-tests.ts new file mode 100644 index 0000000000..5d8b88862c --- /dev/null +++ b/types/log-process-errors/log-process-errors-tests.ts @@ -0,0 +1,24 @@ +import * as LogProcessErrors from 'log-process-errors'; + +/** + * Initialize log-process-errors with example options. + */ +const reset = LogProcessErrors({ + log(error, level) { + const someErr: Error = error; + const someLogLevel: LogProcessErrors.LogLevel = level; + }, + + level: { multipleResolves: 'debug' }, + + exitOn: ['uncaughtException', 'unhandledRejection'], + + testing: 'ava', + + colors: false, +}); + +/** + * Restore default Node.js error handling functionality. + */ +reset(); diff --git a/types/log-process-errors/tsconfig.json b/types/log-process-errors/tsconfig.json new file mode 100644 index 0000000000..08dae10399 --- /dev/null +++ b/types/log-process-errors/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "log-process-errors-tests.ts" + ] +} diff --git a/types/log-process-errors/tslint.json b/types/log-process-errors/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/log-process-errors/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}