From 5e0afb0d2e74712bcedf5fab525e15bd6eebcf38 Mon Sep 17 00:00:00 2001 From: Alex Brick Date: Mon, 8 Jan 2018 19:23:29 +0100 Subject: [PATCH] [express-winston] Adding types for express-winston (#22742) --- .../express-winston/express-winston-tests.ts | 94 ++++++++++++++++++ types/express-winston/index.d.ts | 99 +++++++++++++++++++ types/express-winston/tsconfig.json | 23 +++++ types/express-winston/tslint.json | 1 + 4 files changed, 217 insertions(+) create mode 100644 types/express-winston/express-winston-tests.ts create mode 100644 types/express-winston/index.d.ts create mode 100644 types/express-winston/tsconfig.json create mode 100644 types/express-winston/tslint.json diff --git a/types/express-winston/express-winston-tests.ts b/types/express-winston/express-winston-tests.ts new file mode 100644 index 0000000000..7ce00c479e --- /dev/null +++ b/types/express-winston/express-winston-tests.ts @@ -0,0 +1,94 @@ +import expressWinston = require('express-winston'); +import * as winston from 'winston'; +import * as express from 'express'; + +const app = express(); + +// Logger with all options +app.use(expressWinston.logger({ + baseMeta: { foo: 'foo' }, + bodyBlacklist: [ 'foo' ], + bodyWhitelist: [ 'bar' ], + colorize: true, + dynamicMeta: (req, res, err) => ({ foo: 'bar' }), + expressFormat: true, + ignoreRoute: (req, res) => true, + ignoredRoutes: [ 'foo' ], + level: 'level', + metaField: 'metaField', + msg: 'msg', + requestFilter: (req, prop) => true, + requestWhitelist: [ 'foo', 'bar' ], + skip: (req, res) => false, + statusLevels: ({ error: 'error', success: 'success', warn: 'warn' }), + transports: [ + new winston.transports.Console({ + json: true, + colorize: true + }) + ] +})); + +// Logger with minimum options (transport) +app.use(expressWinston.logger({ + transports: [ + new winston.transports.Console({ + json: true, + colorize: true + }) + ], +})); + +// Logger with minimum options (winstonInstance) +app.use(expressWinston.logger({ + winstonInstance: winston, +})); + +// Error Logger with all options +app.use(expressWinston.errorLogger({ + baseMeta: { foo: 'foo' }, + dynamicMeta: (req, res, err) => ({ foo: 'bar' }), + level: 'level', + metaField: 'metaField', + msg: 'msg', + requestFilter: (req, prop) => true, + requestWhitelist: [ 'foo', 'bar' ], + transports: [ + new winston.transports.Console({ + json: true, + colorize: true + }) + ] +})); + +// Error Logger with min options (transports) +app.use(expressWinston.errorLogger({ + transports: [ + new winston.transports.Console({ + json: true, + colorize: true + }) + ], +})); + +// Error Logger with min options (winstonInstance) +app.use(expressWinston.errorLogger({ + winstonInstance: winston, +})); + +expressWinston.bodyBlacklist.push('potato'); +expressWinston.bodyWhitelist.push('apple'); +expressWinston.defaultRequestFilter = (req: express.Request, prop: string) => true; +expressWinston.defaultResponseFilter = (res: express.Response, prop: string) => true; +expressWinston.defaultSkip = () => true; +expressWinston.ignoredRoutes.push('/ignored'); +expressWinston.responseWhitelist.push('body'); + +const router = express.Router(); + +router.post('/user/register', (req, res, next) => { + const expressWinstonReq = req as expressWinston.ExpressWinstonRequest; + expressWinstonReq._routeWhitelists.body = [ 'username', 'email', 'age' ]; + expressWinstonReq._routeWhitelists.req = [ 'userId' ]; + expressWinstonReq._routeWhitelists.res = [ '_headers' ]; +}); diff --git a/types/express-winston/index.d.ts b/types/express-winston/index.d.ts new file mode 100644 index 0000000000..21e7ead653 --- /dev/null +++ b/types/express-winston/index.d.ts @@ -0,0 +1,99 @@ +// Type definitions for express-winston 2.4 +// Project: https://github.com/bithavoc/express-winston#readme +// Definitions by: Alex Brick +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +import { ErrorRequestHandler, Handler, Request, Response } from 'express'; +import { TransportInstance, Winston } from 'winston'; + +export interface MetaObject { + [field: string]: string; +} + +export type DynamicMetaFunction = (req: Request, res: Response, err: Error) => MetaObject | undefined; +export type RequestFilter = (req: Request, propName: string) => boolean; +export type ResponseFilter = (res: Response, propName: string) => boolean; +export type RouteFilter = (req: Request, res: Response) => boolean; + +export interface BaseLoggerOptions { + baseMeta?: MetaObject; + bodyBlacklist?: string[]; + bodyWhitelist?: string[]; + colorize?: boolean; + dynamicMeta?: DynamicMetaFunction; + expressFormat?: boolean; + ignoreRoute?: RouteFilter; + ignoredRoutes?: string[]; + level?: string; + metaField?: string; + msg?: string; + requestFilter?: RequestFilter; + requestWhitelist?: string[]; + responseFilter?: ResponseFilter; + responseWhitelist?: string[]; + skip?: RouteFilter; + statusLevels?: { + error?: string; + success?: string; + warn?: string; + }; +} + +export interface LoggerOptionsWithTransports extends BaseLoggerOptions { + transports: TransportInstance[]; +} + +export interface LoggerOptionsWithWinstonInstance extends BaseLoggerOptions { + winstonInstance: Winston; +} + +export type LoggerOptions = LoggerOptionsWithTransports | LoggerOptionsWithWinstonInstance; + +export function logger(options: LoggerOptions): Handler; + +export interface BaseErrorLoggerOptions { + baseMeta?: MetaObject; + dynamicMeta?: DynamicMetaFunction; + level?: string; + metaField?: string; + msg?: string; + requestFilter?: RequestFilter; + requestWhitelist?: string[]; +} + +export interface ErrorLoggerOptionsWithTransports extends BaseErrorLoggerOptions { + transports: TransportInstance[]; +} + +export interface ErrorLoggerOptionsWithWinstonInstance extends BaseErrorLoggerOptions { + winstonInstance: Winston; +} + +export type ErrorLoggerOptions = ErrorLoggerOptionsWithTransports | ErrorLoggerOptionsWithWinstonInstance; + +export function errorLogger(options: ErrorLoggerOptions): ErrorRequestHandler; + +export let requestWhitelist: string[]; + +export let bodyWhitelist: string[]; + +export let bodyBlacklist: string[]; + +export let responseWhitelist: string[]; + +export let ignoredRoutes: string[]; + +export let defaultRequestFilter: RequestFilter; + +export let defaultResponseFilter: ResponseFilter; + +export function defaultSkip(): boolean; + +export interface ExpressWinstonRequest extends Request { + _routeWhitelists: { + body: string[]; + req: string[]; + res: string[]; + }; +} diff --git a/types/express-winston/tsconfig.json b/types/express-winston/tsconfig.json new file mode 100644 index 0000000000..dcebd7fafb --- /dev/null +++ b/types/express-winston/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", + "express-winston-tests.ts" + ] +} diff --git a/types/express-winston/tslint.json b/types/express-winston/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/express-winston/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }