diff --git a/types/morgan/index.d.ts b/types/morgan/index.d.ts index 4a5afe4df5..09a9f21f57 100644 --- a/types/morgan/index.d.ts +++ b/types/morgan/index.d.ts @@ -24,7 +24,7 @@ declare namespace morgan { */ export interface Morgan { /*** - * Create a new morgan logger middleware function using the given format and options. The format argument may be a string of a predefined name (see below for the names), + * Create a new morgan logger middleware function using the given format and options. The format argument may be a string of a predefined name (see below for the names), * or a string of a format string containing defined tokens. * @param format * @param options @@ -69,7 +69,7 @@ declare namespace morgan { (format: 'tiny', options?: Options): express.RequestHandler; /*** - * Create a new morgan logger middleware function using the given format and options. The format argument may be a + * Create a new morgan logger middleware function using the given format and options. The format argument may be a * custom format function which adheres to the signature. * @param format * @param options @@ -152,7 +152,7 @@ declare namespace morgan { } /*** - * Create a new morgan logger middleware function using the given format and options. The format argument may be a string of a predefined name (see below for the names), + * Create a new morgan logger middleware function using the given format and options. The format argument may be a string of a predefined name (see below for the names), * or a string of a format string containing defined tokens. * @param format * @param options @@ -200,11 +200,11 @@ declare function morgan(format: 'short', options?: morgan.Options): express.Requ declare function morgan(format: 'tiny', options?: morgan.Options): express.RequestHandler; /*** - * Create a new morgan logger middleware function using the given format and options. The format argument may be a + * Create a new morgan logger middleware function using the given format and options. The format argument may be a * custom format function which adheres to the signature. * @param format * @param options */ -declare function morgan(custom: (req: express.Request, res: express.Response) => string): express.RequestHandler +declare function morgan(format: morgan.FormatFn, options?: morgan.Options): express.RequestHandler export = morgan; diff --git a/types/morgan/morgan-tests.ts b/types/morgan/morgan-tests.ts index 3bbd7c33ee..50a185fdd4 100644 --- a/types/morgan/morgan-tests.ts +++ b/types/morgan/morgan-tests.ts @@ -15,16 +15,37 @@ morgan('tiny') morgan(':remote-addr :method :url') morgan('combined', { - buffer: true, - immediate: true, - skip: function (req, res) { return res.statusCode < 400 }, - stream: { - write: (str: string) => { - console.log(str); - } + buffer: true, + immediate: true, + skip: function (req, res) { return res.statusCode < 400 }, + stream: { + write: (str: string) => { + console.log(str); } + } }); +// a custom format function +morgan((tokens, req, res) => { + return [ + tokens.method(req, res), + tokens.url(req, res), + tokens.status(req, res), + tokens.res(req, res, 'content-length'), '-', + tokens['response-time'](req, res), 'ms' + ].join(' '); +}); +// a custom format function with options +morgan((_tokens, _req, _res) => "", { + buffer: true, + immediate: true, + skip: function (req, res) { return res.statusCode < 400 }, + stream: { + write: (str: string) => { + console.log(str); + } + } +}); // test interface definition for morgan @@ -46,7 +67,7 @@ interface IExtendedFormatFn extends morgan.FormatFn { var developmentExtendedFormatLine : IExtendedFormatFn = function(tokens, req, res):string { - + // get the status code if response written var status = res.statusCode ? res.statusCode