UNPKG

28.1 kBTypeScriptView Raw
1/* eslint no-unused-vars: 0 */
2/* eslint no-undef: 0 */
3/* eslint space-infix-ops: 0 */
4
5/// <reference types="node" />
6
7import * as http from 'http'
8import * as http2 from 'http2'
9import * as https from 'https'
10
11declare function fastify<
12 HttpServer extends (http.Server | http2.Http2Server) = http.Server,
13 HttpRequest extends (http.IncomingMessage | http2.Http2ServerRequest) = http.IncomingMessage,
14 HttpResponse extends (http.ServerResponse | http2.Http2ServerResponse) = http.ServerResponse
15>(opts?: fastify.ServerOptions): fastify.FastifyInstance<HttpServer, HttpRequest, HttpResponse>;
16declare function fastify(opts?: fastify.ServerOptionsAsHttp): fastify.FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>;
17declare function fastify(opts?: fastify.ServerOptionsAsSecureHttp): fastify.FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>;
18declare function fastify(opts?: fastify.ServerOptionsAsHttp2): fastify.FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>;
19declare function fastify(opts?: fastify.ServerOptionsAsSecureHttp2): fastify.FastifyInstance<http2.Http2SecureServer, http2.Http2ServerRequest, http2.Http2ServerResponse>;
20
21// eslint-disable-next-line no-redeclare
22declare namespace fastify {
23
24 type Plugin < HttpServer, HttpRequest, HttpResponse, T > = (instance: FastifyInstance< HttpServer, HttpRequest, HttpResponse >, opts: T, callback: (err?: FastifyError) => void) => void
25
26 type Middleware < HttpServer, HttpRequest, HttpResponse > = (this: FastifyInstance<HttpServer, HttpRequest, HttpResponse>, req: HttpRequest, res: HttpResponse, callback: (err?: FastifyError) => void) => void
27
28 type DefaultQuery = { [k: string]: any }
29 type DefaultParams = { [k: string]: any }
30 type DefaultHeaders = { [k: string]: any }
31 type DefaultBody = any
32
33 type HTTPMethod = 'DELETE' | 'GET' | 'HEAD' | 'PATCH' | 'POST' | 'PUT' | 'OPTIONS'
34
35 interface ValidationResult {
36 keyword: string;
37 dataPath: string;
38 schemaPath: string;
39 params: {
40 [type: string]: string;
41 },
42 message: string;
43 }
44
45 /**
46 * Fastify custom error
47 */
48 interface FastifyError extends Error {
49 statusCode?: number;
50 /**
51 * Validation errors
52 */
53 validation?: Array<ValidationResult>;
54 }
55
56 interface Logger {
57 fatal(msg: string, ...args: any[]): void;
58 fatal(obj: {}, msg?: string, ...args: any[]): void;
59 error(msg: string, ...args: any[]): void;
60 error(obj: {}, msg?: string, ...args: any[]): void;
61 warn(msg: string, ...args: any[]): void;
62 warn(obj: {}, msg?: string, ...args: any[]): void;
63 info(msg: string, ...args: any[]): void;
64 info(obj: {}, msg?: string, ...args: any[]): void;
65 debug(msg: string, ...args: any[]): void;
66 debug(obj: {}, msg?: string, ...args: any[]): void;
67 trace(msg: string, ...args: any[]): void;
68 trace(obj: {}, msg?: string, ...args: any[]): void;
69 }
70
71 type FastifyMiddleware<
72 HttpServer = http.Server,
73 HttpRequest = http.IncomingMessage,
74 HttpResponse = http.ServerResponse,
75 Query = DefaultQuery,
76 Params = DefaultParams,
77 Headers = DefaultHeaders,
78 Body = DefaultBody
79 > = (
80 this: FastifyInstance<HttpServer, HttpRequest, HttpResponse>,
81 req: FastifyRequest<HttpRequest, Query, Params, Headers, Body>,
82 reply: FastifyReply<HttpResponse>,
83 done: (err?: Error) => void,
84 ) => void
85
86 type FastifyMiddlewareWithPayload<
87 HttpServer = http.Server,
88 HttpRequest = http.IncomingMessage,
89 HttpResponse = http.ServerResponse,
90 Query = DefaultQuery,
91 Params = DefaultParams,
92 Headers = DefaultHeaders,
93 Body = DefaultBody
94 > = (
95 this: FastifyInstance<HttpServer, HttpRequest, HttpResponse>,
96 req: FastifyRequest<HttpRequest, Query, Params, Headers, Body>,
97 reply: FastifyReply<HttpResponse>,
98 payload: any,
99 done: (err?: Error, value?: any) => void,
100 ) => void
101
102 type RequestHandler<
103 HttpRequest = http.IncomingMessage,
104 HttpResponse = http.ServerResponse,
105 Query = DefaultQuery,
106 Params = DefaultParams,
107 Headers = DefaultHeaders,
108 Body = DefaultBody
109 > = (
110 this: FastifyInstance<http.Server, HttpRequest, HttpResponse>,
111 request: FastifyRequest<HttpRequest, Query, Params, Headers, Body>,
112 reply: FastifyReply<HttpResponse>,
113 ) => void | Promise<any>
114
115 type SchemaCompiler = (schema: Object) => Function
116
117 type BodyParser<HttpRequest, RawBody extends string | Buffer> =
118 | ((req: HttpRequest, rawBody: RawBody, done: (err: Error | null, body?: any) => void) => void)
119 | ((req: HttpRequest, rawBody: RawBody) => Promise<any>)
120
121 type ContentTypeParser<HttpRequest> =
122 | ((req: HttpRequest, done: (err: Error | null, body?: any) => void) => void)
123 | ((req: HttpRequest) => Promise<any>)
124
125 interface FastifyContext {
126 config: any
127 }
128
129 /**
130 * fastify's wrapped version of node.js IncomingMessage
131 */
132 interface FastifyRequest<
133 HttpRequest = http.IncomingMessage,
134 Query = DefaultQuery,
135 Params = DefaultParams,
136 Headers = DefaultHeaders,
137 Body = DefaultBody
138 > {
139 query: Query
140
141 params: Params
142
143 headers: Headers
144
145 body: Body
146
147 id: any
148
149 ip: string
150 ips: string[]
151 hostname: string
152
153 raw: HttpRequest
154 req: HttpRequest
155 log: Logger
156 }
157
158 /**
159 * Response object that is used to build and send a http response
160 */
161 interface FastifyReply<HttpResponse> {
162 code(statusCode: number): FastifyReply<HttpResponse>
163 status(statusCode: number): FastifyReply<HttpResponse>
164 header(name: string, value: any): FastifyReply<HttpResponse>
165 headers(headers: { [key: string]: any }): FastifyReply<HttpResponse>
166 getHeader(name: string): string | undefined
167 hasHeader(name: string): boolean
168 callNotFound(): void
169 getResponseTime(): number
170 type(contentType: string): FastifyReply<HttpResponse>
171 redirect(url: string): FastifyReply<HttpResponse>
172 redirect(statusCode: number, url: string): FastifyReply<HttpResponse>
173 serialize(payload: any): string
174 serializer(fn: Function): FastifyReply<HttpResponse>
175 send(payload?: any): FastifyReply<HttpResponse>
176 sent: boolean
177 res: HttpResponse
178 context: FastifyContext
179 request: FastifyRequest
180 }
181 type TrustProxyFunction = (addr: string, index: number) => boolean
182 interface ServerOptions {
183 caseSensitive?: boolean,
184 ignoreTrailingSlash?: boolean,
185 bodyLimit?: number,
186 pluginTimeout?: number,
187 disableRequestLogging?: boolean,
188 onProtoPoisoning?: 'error' | 'remove' | 'ignore',
189 logger?: any,
190 trustProxy?: string | number | boolean | Array<string> | TrustProxyFunction,
191 maxParamLength?: number,
192 querystringParser?: (str: string) => { [key: string]: string | string[] },
193 versioning? : {
194 storage() : {
195 get(version: String) : Function | null,
196 set(version: String, store: Function) : void,
197 del(version: String) : void,
198 empty() : void
199 },
200 deriveVersion<Context>(req: Object, ctx?: Context) : String,
201 },
202 modifyCoreObjects?: boolean,
203 return503OnClosing?: boolean
204 }
205 interface ServerOptionsAsSecure extends ServerOptions {
206 https: http2.SecureServerOptions
207 }
208 interface ServerOptionsAsHttp extends ServerOptions {
209 http2?: false
210 }
211 interface ServerOptionsAsSecureHttp extends ServerOptionsAsHttp, ServerOptionsAsSecure {}
212 interface ServerOptionsAsHttp2 extends ServerOptions {
213 http2: true
214 }
215 interface ServerOptionsAsSecureHttp2 extends ServerOptionsAsHttp2, ServerOptionsAsSecure {}
216
217 // TODO - define/import JSONSchema types
218 type JSONSchema = Object
219
220 interface RouteSchema {
221 body?: JSONSchema
222 querystring?: JSONSchema
223 params?: JSONSchema
224 headers?: JSONSchema
225 response?: {
226 [code: number]: JSONSchema,
227 [code: string]: JSONSchema
228 }
229 }
230
231 /**
232 * Optional configuration parameters for the route being created
233 */
234 interface RouteShorthandOptions<
235 HttpServer = http.Server,
236 HttpRequest = http.IncomingMessage,
237 HttpResponse = http.ServerResponse,
238 Query = DefaultQuery,
239 Params = DefaultParams,
240 Headers = DefaultHeaders,
241 Body = DefaultBody
242 > {
243 schema?: RouteSchema
244 attachValidation?: boolean
245 onRequest?:
246 | FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>
247 | Array<FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>>
248 preParsing?:
249 | FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>
250 | Array<FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>>
251 preValidation?:
252 | FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>
253 | Array<FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>>
254 preHandler?:
255 | FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>
256 | Array<FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>>
257 preSerialization?:
258 FastifyMiddlewareWithPayload<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>
259 | Array<FastifyMiddlewareWithPayload<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>>
260 handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>
261 schemaCompiler?: SchemaCompiler
262 bodyLimit?: number
263 logLevel?: string
264 config?: any
265 prefixTrailingSlash?: 'slash' | 'no-slash' | 'both'
266 }
267
268 /**
269 * Route configuration options such as "url" and "method"
270 */
271 interface RouteOptions<
272 HttpServer = http.Server,
273 HttpRequest = http.IncomingMessage,
274 HttpResponse = http.ServerResponse,
275 Query = DefaultQuery,
276 Params = DefaultParams,
277 Headers = DefaultHeaders,
278 Body = DefaultBody
279 > extends RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body> {
280 method: HTTPMethod | HTTPMethod[]
281 url: string
282 handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>
283 }
284
285 /**
286 * Register options
287 */
288 interface RegisterOptions<HttpServer, HttpRequest, HttpResponse> {
289 [key: string]: any,
290 prefix?: string,
291 }
292
293 /**
294 * Fake http inject options
295 */
296 interface HTTPInjectOptions {
297 url: string,
298 method?: HTTPMethod,
299 authority?: string,
300 headers?: DefaultHeaders,
301 query?: DefaultQuery,
302 remoteAddress?: string,
303 payload?: string | object | Buffer | NodeJS.ReadableStream
304 simulate?: {
305 end?: boolean,
306 split?: boolean,
307 error?: boolean,
308 close?: boolean
309 },
310 validate?: boolean
311 }
312
313 /**
314 * Fake http inject response
315 */
316 interface HTTPInjectResponse {
317 raw: {
318 req: NodeJS.ReadableStream,
319 res: http.ServerResponse
320 },
321 headers: Record<string, string>,
322 statusCode: number,
323 statusMessage: string,
324 payload: string,
325 rawPayload: Buffer,
326 trailers: object
327 }
328
329 /**
330 * Server listen options
331 */
332 interface ListenOptions {
333 port?: number;
334 host?: string;
335 backlog?: number;
336 path?: string;
337 exclusive?: boolean;
338 readableAll?: boolean;
339 writableAll?: boolean;
340 /**
341 * @default false
342 */
343 ipv6Only?: boolean;
344 }
345
346 /**
347 * Represents the fastify instance created by the factory function the module exports.
348 */
349 interface FastifyInstance<HttpServer = http.Server, HttpRequest = http.IncomingMessage, HttpResponse = http.ServerResponse> {
350 server: HttpServer
351 log: Logger
352 schemaCompiler: SchemaCompiler
353
354 /**
355 * Adds a route to the server
356 */
357 route<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
358 opts: RouteOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
359 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
360
361 /**
362 * Defines a GET route with the given mount path, options, and handler
363 */
364 get<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
365 url: string,
366 opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
367 handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
368 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
369
370 /**
371 * Defines a GET route with the given mount path and handler
372 */
373 get<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
374 url: string,
375 handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
376 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
377
378 /**
379 * Defines a PUT route with the given mount path, options, and handler
380 */
381 put<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
382 url: string,
383 opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
384 handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
385 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
386
387 /**
388 * Defines a PUT route with the given mount path and handler
389 */
390 put<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
391 url: string,
392 handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
393 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
394
395 /**
396 * Defines a PATCH route with the given mount path, options, and handler
397 */
398 patch<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
399 url: string,
400 opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
401 handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
402 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
403
404 /**
405 * Defines a PATCH route with the given mount path and handler
406 */
407 patch<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
408 url: string,
409 handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
410 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
411
412 /**
413 * Defines a POST route with the given mount path, options, and handler
414 */
415 post<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
416 url: string,
417 opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
418 handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
419 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
420
421 /**
422 * Defines a POST route with the given mount path and handler
423 */
424 post<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
425 url: string,
426 handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
427 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
428
429 /**
430 * Defines a HEAD route with the given mount path, options, and handler
431 */
432 head<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
433 url: string,
434 opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
435 handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
436 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
437
438 /**
439 * Defines a HEAD route with the given mount path and handler
440 */
441 head<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
442 url: string,
443 handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
444 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
445
446 /**
447 * Defines a DELETE route with the given mount path, options, and handler
448 */
449 delete<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
450 url: string,
451 opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
452 handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
453 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
454
455 /**
456 * Defines a DELETE route with the given mount path and handler
457 */
458 delete<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
459 url: string,
460 handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
461 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
462
463 /**
464 * Defines a OPTIONS route with the given mount path, options, and handler
465 */
466 options<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
467 url: string,
468 opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
469 handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
470 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
471
472 /**
473 * Defines a OPTIONS route with the given mount path and handler
474 */
475 options<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
476 url: string,
477 handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
478 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
479
480 /**
481 * Defines a route for all the supported methods with the given mount path, options, and handler
482 */
483 all<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
484 url: string,
485 opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
486 handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
487 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
488
489 /**
490 * Defines a route for all the supported methods with the given mount path and handler
491 */
492 all<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
493 url: string,
494 handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
495 ): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
496
497 /**
498 * Starts the server on the given port after all the plugins are loaded,
499 * internally waits for the .ready() event. The callback is the same as the
500 * Node core.
501 */
502 listen(callback: (err: Error, address: string) => void): void
503 listen(port: number, callback: (err: Error, address: string) => void): void
504 listen(port: number, address: string, callback: (err: Error, address: string) => void): void
505 listen(port: number, address: string, backlog: number, callback: (err: Error, address: string) => void): void
506 listen(options: ListenOptions, callback: (err: Error, address: string) => void): void
507 listen(sockFile: string, callback: (err: Error, address: string) => void): void
508 listen(port: number, address?: string, backlog?: number): Promise<string>
509 listen(sockFile: string): Promise<string>
510 listen(options: ListenOptions): Promise<string>
511
512 /**
513 * Registers a listener function that is invoked when all the plugins have
514 * been loaded. It receives an error parameter if something went wrong.
515 */
516 ready(): Promise<FastifyInstance<HttpServer, HttpRequest, HttpResponse>>
517 ready(readyListener: (err: Error) => void): void
518 ready(readyListener: (err: Error, done: Function) => void): void
519 ready(readyListener: (err: Error, context: FastifyInstance<HttpServer, HttpRequest, HttpResponse>, done: Function) => void): void
520
521 /**
522 * Call this function to close the server instance and run the "onClose" callback
523 */
524 close(closeListener: () => void): void
525 close<T = any>(): Promise<T>
526
527 /**
528 * Apply the given middleware to all incoming requests
529 */
530 use(middleware: Middleware<HttpServer, HttpRequest, HttpResponse>): void
531
532 /**
533 * Apply the given middleware to routes matching the given path
534 */
535 use(path: string, middleware: Middleware<HttpServer, HttpRequest, HttpResponse>): void
536
537 /**
538 * Registers a plugin
539 */
540 register<T extends RegisterOptions<HttpServer, HttpRequest, HttpResponse>>(plugin: Plugin<HttpServer, HttpRequest, HttpResponse, T>, opts?: T): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
541
542 /**
543 * `Register a callback that will be executed just after a register.
544 * It can take up to three parameters
545 */
546 after(afterListener: (err: Error) => void): void
547 after(afterListener: (err: Error, done: Function) => void): void
548 after(afterListener: (err: Error, context: FastifyInstance<HttpServer, HttpRequest, HttpResponse>, done: Function) => void): void
549
550 /**
551 * Decorate this fastify instance with new properties. Throws an execption if
552 * you attempt to add the same decorator name twice
553 */
554 decorate(name: string, decoration: any, dependencies?: Array<string>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
555
556 /**
557 * Decorate reply objects with new properties. Throws an execption if
558 * you attempt to add the same decorator name twice
559 */
560 decorateReply(name: string, decoration: any, dependencies?: Array<string>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
561
562 /**
563 * Decorate request objects with new properties. Throws an execption if
564 * you attempt to add the same decorator name twice
565 */
566 decorateRequest(name: string, decoration: any, dependencies?: Array<string>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
567
568 /**
569 * Determines if the given named decorator is available
570 */
571 hasDecorator(name: string): boolean
572
573 /**
574 * Determines if the given named request decorator is available
575 */
576 hasRequestDecorator(name: string): boolean
577
578 /**
579 * Determines if the given named reply decorator is available
580 */
581 hasReplyDecorator(name: string): boolean
582
583 /**
584 * Add a hook that is triggered when a request is initially received
585 */
586 addHook(name: 'onRequest', hook: FastifyMiddleware<HttpServer, HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
587
588 /**
589 * Add a hook that is triggered after the onRequest hook and middlewares, but before body parsing
590 */
591 addHook(name: 'preParsing', hook: FastifyMiddleware<HttpServer, HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
592
593 /**
594 * Add a hook that is triggered after the onRequest, middlewares, and body parsing, but before the validation
595 */
596 addHook(name: 'preValidation', hook: FastifyMiddleware<HttpServer, HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
597
598 /**
599 * Hook that is fired after a request is processed, but before the response is serialized
600 * hook
601 */
602 addHook(name: 'preSerialization', hook: FastifyMiddlewareWithPayload<HttpServer, HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
603
604 /**
605 * Hook that is fired before a request is processed, but after the "preValidation"
606 * hook
607 */
608 addHook(name: 'preHandler', hook: FastifyMiddleware<HttpServer, HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
609
610 /**
611 * Hook that is fired after a request is processed, but before the "onResponse"
612 * hook
613 */
614 addHook(name: 'onSend', hook: (this: FastifyInstance<HttpServer, HttpRequest, HttpResponse>, req: FastifyRequest<HttpRequest>, reply: FastifyReply<HttpResponse>, payload: any, done: (err?: Error, value?: any) => void) => void): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
615
616 /**
617 * Hook that is fired if `reply.send` is invoked with an Error
618 */
619 addHook(name: 'onError', hook: (this: FastifyInstance<HttpServer, HttpRequest, HttpResponse>, req: FastifyRequest<HttpRequest>, reply: FastifyReply<HttpResponse>, error: FastifyError, done: () => void) => void): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
620
621 /**
622 * Hook that is called when a response is about to be sent to a client
623 */
624 addHook(name: 'onResponse', hook: FastifyMiddleware<HttpServer, HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
625
626 /**
627 * Adds a hook that is triggered when server.close is called. Useful for closing connections
628 * and performing cleanup tasks
629 */
630 addHook(name: 'onClose', hook: (instance: FastifyInstance<HttpServer, HttpRequest, HttpResponse>, done: () => void) => void): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
631
632 /**
633 * Adds a hook that is triggered when a new route is registered. Listeners are passed a
634 * routeOptions object as the sole parameter.
635 * The interface is synchronous, and, as such, the listeners do not get passed a callback.
636 */
637 addHook(name: 'onRoute', hook: (opts: RouteOptions<HttpServer, HttpRequest, HttpResponse> & { path: string, prefix: string }) => void): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
638
639 /**
640 * Adds a hook that is triggered when Fastify a new plugin is being registered.
641 * This hook can be useful if you are developing a plugin that needs to use the encapsulation functionality of Fastify.
642 * The interface is synchronous, and, as such, the listeners do not get passed a callback.
643 */
644 addHook(name: 'onRegister', hook: (instance: FastifyInstance) => void): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
645
646 /**
647 * Useful for testing http requests without running a sever
648 */
649 inject(opts: HTTPInjectOptions | string, cb: (err: Error, res: HTTPInjectResponse) => void): void
650
651 /**
652 * Useful for testing http requests without running a sever
653 */
654 inject(opts: HTTPInjectOptions | string): Promise<HTTPInjectResponse>
655
656 /**
657 * Set the 404 handler
658 */
659 setNotFoundHandler(handler: (request: FastifyRequest<HttpRequest>, reply: FastifyReply<HttpResponse>) => void): void
660
661 /**
662 * Set a function that will be called whenever an error happens
663 */
664 setErrorHandler(handler: (error: FastifyError, request: FastifyRequest<HttpRequest>, reply: FastifyReply<HttpResponse>) => void): void
665
666 /**
667 * Set a function that will be called whenever an error happens
668 */
669 setReplySerializer(handler: (payload: string | object | Buffer | NodeJS.ReadableStream, statusCode: number) => string): void
670
671 /**
672 * Set the schema compiler for all routes.
673 */
674 setSchemaCompiler(schemaCompiler: SchemaCompiler): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
675
676 /**
677 * Create a shared schema
678 */
679 addSchema(schema: object): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
680
681 /**
682 * Get all shared schemas
683 */
684 getSchemas(): {[shemaId: string]: Object}
685
686 /**
687 * Add a content type parser
688 */
689 addContentTypeParser(contentType: string | string[], opts: { bodyLimit?: number }, parser: ContentTypeParser<HttpRequest>): void
690 addContentTypeParser(contentType: string | string[], opts: { parseAs: 'string'; bodyLimit?: number }, parser: BodyParser<HttpRequest, string>): void
691 addContentTypeParser(contentType: string | string[], opts: { parseAs: 'buffer'; bodyLimit?: number }, parser: BodyParser<HttpRequest, Buffer>): void
692 addContentTypeParser(contentType: string | string[], parser: ContentTypeParser<HttpRequest>): void
693
694 /**
695 * Check if a parser for the specified content type exists
696 */
697 hasContentTypeParser(contentType: string): boolean;
698
699 /**
700 * Prints the representation of the internal radix tree used by the router
701 */
702 printRoutes(): string
703 }
704}
705
706export = fastify;