mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-05-07 04:50:54 +08:00
morgan: enable strictNullChecks, add linting
This commit is contained in:
96
types/morgan/index.d.ts
vendored
96
types/morgan/index.d.ts
vendored
@@ -1,31 +1,29 @@
|
||||
// Type definitions for morgan 1.7.0
|
||||
// Type definitions for morgan 1.7
|
||||
// Project: https://github.com/expressjs/morgan
|
||||
// Definitions by: James Roland Cabresos <https://github.com/staticfunction>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.2
|
||||
|
||||
import express = require('express');
|
||||
|
||||
declare namespace morgan {
|
||||
type FormatFn = (tokens: TokenIndexer, req: express.Request, res: express.Response) => string;
|
||||
|
||||
export interface FormatFn extends Function {
|
||||
(tokens: TokenIndexer, req: express.Request, res: express.Response): string;
|
||||
}
|
||||
type TokenCallbackFn = (req: express.Request, res: express.Response, arg?: string | number | boolean) => string;
|
||||
|
||||
export interface TokenCallbackFn extends Function {
|
||||
(req: express.Request, res: express.Response, arg?: string | number | boolean): string;
|
||||
}
|
||||
|
||||
export interface TokenIndexer {
|
||||
interface TokenIndexer {
|
||||
[tokenName: string]: TokenCallbackFn;
|
||||
}
|
||||
|
||||
/**
|
||||
* Public interface of morgan logger
|
||||
* Public interface of morgan logger.
|
||||
*/
|
||||
export interface Morgan {
|
||||
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),
|
||||
* or a string of a format string containing defined tokens.
|
||||
* 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
|
||||
*/
|
||||
@@ -45,7 +43,10 @@ declare namespace morgan {
|
||||
*/
|
||||
(format: 'common', options?: Options): express.RequestHandler;
|
||||
/**
|
||||
* Concise output colored by response status for development use. The :status token will be colored red for server error codes, yellow for client error codes, cyan for redirection codes, and uncolored for all other codes.
|
||||
* Concise output colored by response status for development use. The
|
||||
* :status token will be colored red for server error codes, yellow for
|
||||
* client error codes, cyan for redirection codes, and uncolored for
|
||||
* all other codes.
|
||||
* :method :url :status :response-time ms - :res[content-length]
|
||||
* @param format
|
||||
* @param options
|
||||
@@ -69,29 +70,32 @@ 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
|
||||
* custom format function which adheres to the signature.
|
||||
* 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
|
||||
*/
|
||||
(format: FormatFn, options?: Options): express.RequestHandler;
|
||||
|
||||
/**
|
||||
* Define a custom token which can be used in custom morgan logging formats.
|
||||
* Define a custom token which can be used in custom morgan logging
|
||||
* formats.
|
||||
*/
|
||||
token(name: string, callback: TokenCallbackFn): Morgan;
|
||||
/**
|
||||
* Define a named custom format by specifying a format string in token notation
|
||||
* Define a named custom format by specifying a format string in token
|
||||
* notation.
|
||||
*/
|
||||
format(name: string, fmt: string): Morgan;
|
||||
|
||||
/**
|
||||
* Define a named custom format by specifying a format function
|
||||
* Define a named custom format by specifying a format function.
|
||||
*/
|
||||
format(name: string, fmt: FormatFn): Morgan;
|
||||
|
||||
/**
|
||||
* Compile a format string in token notation into a format function
|
||||
* Compile a format string in token notation into a format function.
|
||||
*/
|
||||
compile(format: string): FormatFn;
|
||||
}
|
||||
@@ -99,49 +103,53 @@ declare namespace morgan {
|
||||
/**
|
||||
* Define a custom token which can be used in custom morgan logging formats.
|
||||
*/
|
||||
export function token(name: string, callback: TokenCallbackFn): Morgan;
|
||||
function token(name: string, callback: TokenCallbackFn): Morgan;
|
||||
|
||||
/**
|
||||
* Define a named custom format by specifying a format string in token notation
|
||||
* Define a named custom format by specifying a format string in token
|
||||
* notation.
|
||||
*/
|
||||
export function format(name: string, fmt: string): Morgan;
|
||||
function format(name: string, fmt: string): Morgan;
|
||||
|
||||
/**
|
||||
* Define a named custom format by specifying a format function
|
||||
* Define a named custom format by specifying a format function.
|
||||
*/
|
||||
export function format(name: string, fmt: FormatFn): Morgan;
|
||||
function format(name: string, fmt: FormatFn): Morgan;
|
||||
|
||||
/**
|
||||
* Compile a format string in token notation into a format function
|
||||
* Compile a format string in token notation into a format function.
|
||||
*/
|
||||
export function compile(format: string): FormatFn;
|
||||
function compile(format: string): FormatFn;
|
||||
|
||||
export interface StreamOptions {
|
||||
interface StreamOptions {
|
||||
/**
|
||||
* Output stream for writing log lines
|
||||
* Output stream for writing log lines.
|
||||
*/
|
||||
write: (str: string) => void;
|
||||
write(str: string): void;
|
||||
}
|
||||
|
||||
/***
|
||||
* Morgan accepts these properties in the options object.
|
||||
*/
|
||||
export interface Options {
|
||||
|
||||
interface Options {
|
||||
/***
|
||||
* Buffer duration before writing logs to the stream, defaults to false. When set to true, defaults to 1000 ms.
|
||||
* Buffer duration before writing logs to the stream, defaults to false.
|
||||
* When set to true, defaults to 1000 ms.
|
||||
*/
|
||||
buffer?: boolean;
|
||||
|
||||
/***
|
||||
* Write log line on request instead of response. This means that a requests will be logged even if the server crashes, but data from the response cannot be logged (like the response code).
|
||||
* Write log line on request instead of response. This means that a
|
||||
* requests will be logged even if the server crashes, but data from the
|
||||
* response cannot be logged (like the response code).
|
||||
*/
|
||||
immediate?: boolean;
|
||||
|
||||
/***
|
||||
* Function to determine if logging is skipped, defaults to false. This function will be called as skip(req, res).
|
||||
* Function to determine if logging is skipped, defaults to false. This
|
||||
* function will be called as skip(req, res).
|
||||
*/
|
||||
skip?: (req: express.Request, res: express.Response) => boolean;
|
||||
skip?(req: express.Request, res: express.Response): boolean;
|
||||
|
||||
/***
|
||||
* Output stream for writing log lines, defaults to process.stdout.
|
||||
@@ -152,8 +160,9 @@ 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),
|
||||
* or a string of a format string containing defined tokens.
|
||||
* 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
|
||||
*/
|
||||
@@ -176,7 +185,9 @@ declare function morgan(format: 'combined', options?: morgan.Options): express.R
|
||||
declare function morgan(format: 'common', options?: morgan.Options): express.RequestHandler;
|
||||
|
||||
/***
|
||||
* Concise output colored by response status for development use. The :status token will be colored red for server error codes, yellow for client error codes, cyan for redirection codes, and uncolored for all other codes.
|
||||
* Concise output colored by response status for development use. The :status
|
||||
* token will be colored red for server error codes, yellow for client error
|
||||
* codes, cyan for redirection codes, and uncolored for all other codes.
|
||||
* :method :url :status :response-time ms - :res[content-length]
|
||||
* @param format
|
||||
* @param options
|
||||
@@ -200,11 +211,12 @@ 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
|
||||
* custom format function which adheres to the signature.
|
||||
* 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(format: morgan.FormatFn, options?: morgan.Options): express.RequestHandler
|
||||
declare function morgan(format: morgan.FormatFn, options?: morgan.Options): express.RequestHandler;
|
||||
|
||||
export = morgan;
|
||||
|
||||
@@ -6,18 +6,18 @@ import morgan = require('morgan');
|
||||
import express = require('express');
|
||||
|
||||
// a pre-defined name
|
||||
morgan('combined')
|
||||
morgan('common')
|
||||
morgan('short')
|
||||
morgan('tiny')
|
||||
morgan('combined');
|
||||
morgan('common');
|
||||
morgan('short');
|
||||
morgan('tiny');
|
||||
|
||||
// a format string
|
||||
morgan(':remote-addr :method :url')
|
||||
morgan(':remote-addr :method :url');
|
||||
|
||||
morgan('combined', {
|
||||
buffer: true,
|
||||
immediate: true,
|
||||
skip: function (req, res) { return res.statusCode < 400 },
|
||||
skip: (req, res) => res.statusCode < 400,
|
||||
stream: {
|
||||
write: (str: string) => {
|
||||
console.log(str);
|
||||
@@ -39,7 +39,7 @@ morgan((tokens, req, res) => {
|
||||
morgan((_tokens, _req, _res) => "", {
|
||||
buffer: true,
|
||||
immediate: true,
|
||||
skip: function (req, res) { return res.statusCode < 400 },
|
||||
skip: (req, res) => res.statusCode < 400,
|
||||
stream: {
|
||||
write: (str: string) => {
|
||||
console.log(str);
|
||||
@@ -52,47 +52,40 @@ morgan((_tokens, _req, _res) => "", {
|
||||
// a named custom format defined as string (example: extend 'tiny' format with user-agent token)
|
||||
morgan.format('tiny-extended', ':method :url :status :res[content-length] - :response-time ms :user-agent');
|
||||
|
||||
|
||||
// a named custom format defined using the Function signature (example: extend 'dev' format with user-agent token)
|
||||
|
||||
// extend morgan.FormatFn interface with memoizer property to avoid unnecessary re-compiling
|
||||
// of status-code range driven colorized format functions
|
||||
interface IFormatFnIndexer {
|
||||
interface FormatFnIndexer {
|
||||
[memoizerName: string]: morgan.FormatFn;
|
||||
}
|
||||
|
||||
interface IExtendedFormatFn extends morgan.FormatFn {
|
||||
memoizer?: IFormatFnIndexer;
|
||||
interface ExtendedFormatFn extends morgan.FormatFn {
|
||||
memoizer: FormatFnIndexer;
|
||||
}
|
||||
|
||||
|
||||
var developmentExtendedFormatLine : IExtendedFormatFn = function(tokens, req, res):string {
|
||||
|
||||
const developmentExtendedFormatLine = ((tokens, req, res): string => {
|
||||
// get the status code if response written
|
||||
var status = res.statusCode
|
||||
? res.statusCode
|
||||
: undefined;
|
||||
const status = res.statusCode;
|
||||
|
||||
// get status color
|
||||
var color = status >= 500 ? 31 // red
|
||||
const color = status >= 500 ? 31 // red
|
||||
: status >= 400 ? 33 // yellow
|
||||
: status >= 300 ? 36 // cyan
|
||||
: status >= 200 ? 32 // green
|
||||
: 0; // no color
|
||||
|
||||
// get colored format function, if previously memoized, otherwise undefined
|
||||
var fn: morgan.FormatFn = developmentExtendedFormatLine.memoizer[color];
|
||||
let fn: morgan.FormatFn | undefined = developmentExtendedFormatLine.memoizer[color];
|
||||
|
||||
if (!fn) {
|
||||
// compile
|
||||
fn = developmentExtendedFormatLine.memoizer[color] = morgan.compile('\x1b[0m:method :url \x1b['
|
||||
+ color + 'm:status \x1b[0m:response-time ms - :res[content-length]\x1b[0m :user-agent');
|
||||
fn = developmentExtendedFormatLine.memoizer[color] = morgan.compile(`\x1b[0m:method :url \x1b[${color}m:status \x1b[0m:response-time ms - :res[content-length]\x1b[0m :user-agent`);
|
||||
}
|
||||
|
||||
return fn(tokens, req, res);
|
||||
};
|
||||
}) as ExtendedFormatFn;
|
||||
|
||||
developmentExtendedFormatLine.memoizer = {};
|
||||
|
||||
|
||||
morgan.format('dev-extended', developmentExtendedFormatLine);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
@@ -19,4 +19,4 @@
|
||||
"index.d.ts",
|
||||
"morgan-tests.ts"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
6
types/morgan/tslint.json
Normal file
6
types/morgan/tslint.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json",
|
||||
"rules": {
|
||||
"unified-signatures": false
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user