UNPKG

10.8 kBTypeScriptView Raw
1// Type definitions for morgan 1.9
2// Project: https://github.com/expressjs/morgan
3// Definitions by: James Roland Cabresos <https://github.com/staticfunction>
4// Paolo Scanferla <https://github.com/pscanf>
5// Piotr Błażejewicz <https://github.com/peterblazejewicz>
6// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
7
8/// <reference types="node" />
9
10import http = require('http');
11
12type Handler<Request extends http.IncomingMessage, Response extends http.ServerResponse> = (req: Request, res: Response, callback: (err?: Error) => void) => void;
13
14declare namespace morgan {
15 type FormatFn<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse> = (
16 tokens: TokenIndexer<Request, Response>,
17 req: Request,
18 res: Response,
19 ) => string | undefined | null;
20
21 type TokenCallbackFn<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse> = (
22 req: Request,
23 res: Response,
24 arg?: string | number | boolean,
25 ) => string | undefined;
26
27 interface TokenIndexer<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse> {
28 [tokenName: string]: TokenCallbackFn<Request, Response>;
29 }
30
31 /**
32 * Public interface of morgan logger.
33 */
34 interface Morgan<Request extends http.IncomingMessage, Response extends http.ServerResponse> {
35 /***
36 * Create a new morgan logger middleware function using the given format
37 * and options. The format argument may be a string of a predefined name
38 * (see below for the names), or a string of a format string containing
39 * defined tokens.
40 * @param format
41 * @param options
42 */
43 (format: string, options?: Options<Request, Response>): Handler<Request, Response>;
44 /***
45 * Standard Apache combined log output.
46 * :remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
47 * @param format
48 * @param options
49 */
50 (format: 'combined', options?: Options<Request, Response>): Handler<Request, Response>;
51 /***
52 * Standard Apache common log output.
53 * :remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length]
54 * @param format
55 * @param options
56 */
57 (format: 'common', options?: Options<Request, Response>): Handler<Request, Response>;
58 /**
59 * Concise output colored by response status for development use. The
60 * :status token will be colored red for server error codes, yellow for
61 * client error codes, cyan for redirection codes, and uncolored for
62 * all other codes.
63 * :method :url :status :response-time ms - :res[content-length]
64 */
65 (format: 'dev', options?: Options<Request, Response>): Handler<Request, Response>;
66
67 /***
68 * Shorter than default, also including response time.
69 * :remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms
70 * @param format
71 * @param options
72 */
73 (format: 'short', options?: Options<Request, Response>): Handler<Request, Response>;
74
75 /***
76 * The minimal output.
77 * :method :url :status :res[content-length] - :response-time ms
78 * @param format
79 * @param options
80 */
81 (format: 'tiny', options?: Options<Request, Response>): Handler<Request, Response>;
82
83 /***
84 * Create a new morgan logger middleware function using the given format
85 * and options. The format argument may be a custom format function
86 * which adheres to the signature.
87 * @param format
88 * @param options
89 */
90 (format: FormatFn<Request, Response>, options?: Options<Request, Response>): Handler<Request, Response>;
91
92 /**
93 * Define a custom token which can be used in custom morgan logging
94 * formats.
95 */
96 token(name: string, callback: TokenCallbackFn<Request, Response>): Morgan<Request, Response>;
97 /**
98 * Define a named custom format by specifying a format string in token
99 * notation.
100 */
101 format(name: string, fmt: string): Morgan<Request, Response>;
102
103 /**
104 * Define a named custom format by specifying a format function.
105 */
106 format(name: string, fmt: FormatFn<Request, Response>): Morgan<Request, Response>;
107
108 /**
109 * Compile a format string in token notation into a format function.
110 */
111 compile(format: string): FormatFn<Request, Response>;
112 }
113
114 /**
115 * Define a custom token which can be used in custom morgan logging formats.
116 */
117 function token<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse>(
118 name: string,
119 callback: TokenCallbackFn<Request, Response>,
120 ): Morgan<Request, Response>;
121
122 /**
123 * Define a named custom format by specifying a format string in token
124 * notation.
125 */
126 // eslint-disable-next-line no-unnecessary-generics
127 function format<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse>(name: string, fmt: string): Morgan<Request, Response>;
128
129 /**
130 * Define a named custom format by specifying a format function.
131 */
132 function format<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse>(
133 name: string,
134 fmt: FormatFn<Request, Response>,
135 ): Morgan<Request, Response>;
136
137 /**
138 * Compile a format string in token notation into a format function.
139 */
140 // eslint-disable-next-line no-unnecessary-generics
141 function compile<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse>(format: string): FormatFn<Request, Response>;
142
143 interface StreamOptions {
144 /**
145 * Output stream for writing log lines.
146 */
147 write(str: string): void;
148 }
149
150 /***
151 * Morgan accepts these properties in the options object.
152 */
153 interface Options<Request extends http.IncomingMessage, Response extends http.ServerResponse> {
154 /***
155 * Buffer duration before writing logs to the stream, defaults to false.
156 * When set to true, defaults to 1000 ms.
157 * @deprecated
158 */
159 buffer?: boolean | undefined;
160
161 /***
162 * Write log line on request instead of response. This means that a
163 * requests will be logged even if the server crashes, but data from the
164 * response cannot be logged (like the response code).
165 */
166 immediate?: boolean | undefined;
167
168 /***
169 * Function to determine if logging is skipped, defaults to false. This
170 * function will be called as skip(req, res).
171 */
172 skip?(req: Request, res: Response): boolean;
173
174 /***
175 * Output stream for writing log lines, defaults to process.stdout.
176 * @param str
177 */
178 stream?: StreamOptions | undefined;
179 }
180}
181
182/***
183 * Create a new morgan logger middleware function using the given format and
184 * options. The format argument may be a string of a predefined name (see below
185 * for the names), or a string of a format string containing defined tokens.
186 * @param format
187 * @param options
188 */
189declare function morgan<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse>(
190 format: string,
191 options?: morgan.Options<Request, Response>,
192): Handler<Request, Response>;
193
194/***
195 * Standard Apache combined log output.
196 * :remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
197 * @param format
198 * @param options
199 */
200declare function morgan<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse>(
201 format: 'combined',
202 options?: morgan.Options<Request, Response>,
203): Handler<Request, Response>;
204
205/***
206 * Standard Apache common log output.
207 * :remote-addr - :remote-user [:date] ":method :url HTTP/:http-version" :status :res[content-length]
208 * @param format
209 * @param options
210 */
211declare function morgan<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse>(
212 format: 'common',
213 options?: morgan.Options<Request, Response>,
214): Handler<Request, Response>;
215
216/***
217 * Concise output colored by response status for development use. The :status
218 * token will be colored red for server error codes, yellow for client error
219 * codes, cyan for redirection codes, and uncolored for all other codes.
220 * :method :url :status :response-time ms - :res[content-length]
221 * @param format
222 * @param options
223 */
224declare function morgan<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse>(
225 format: 'dev',
226 options?: morgan.Options<Request, Response>,
227): Handler<Request, Response>;
228
229/***
230 * Shorter than default, also including response time.
231 * :remote-addr :remote-user :method :url HTTP/:http-version :status :res[content-length] - :response-time ms
232 * @param format
233 * @param options
234 */
235declare function morgan<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse>(
236 format: 'short',
237 options?: morgan.Options<Request, Response>,
238): Handler<Request, Response>;
239
240/***
241 * The minimal output.
242 * :method :url :status :res[content-length] - :response-time ms
243 * @param format
244 * @param options
245 */
246declare function morgan<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse>(
247 format: 'tiny',
248 options?: morgan.Options<Request, Response>,
249): Handler<Request, Response>;
250
251/***
252 * Create a new morgan logger middleware function using the given format and
253 * options. The format argument may be a custom format function which adheres to
254 * the signature.
255 * @param format
256 * @param options
257 */
258declare function morgan<Request extends http.IncomingMessage = http.IncomingMessage, Response extends http.ServerResponse = http.ServerResponse>(
259 format: morgan.FormatFn<Request, Response>,
260 options?: morgan.Options<Request, Response>,
261): Handler<Request, Response>;
262
263export = morgan;