mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-12 21:10:23 +00:00
[express-winston] Adding types for express-winston (#22742)
This commit is contained in:
committed by
Ryan Cavanaugh
parent
d7590b6d0a
commit
5e0afb0d2e
@@ -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' ];
|
||||
});
|
||||
Vendored
+99
@@ -0,0 +1,99 @@
|
||||
// Type definitions for express-winston 2.4
|
||||
// Project: https://github.com/bithavoc/express-winston#readme
|
||||
// Definitions by: Alex Brick <https://github.com/bricka>
|
||||
// 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[];
|
||||
};
|
||||
}
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user