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