import { Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse, CallbackFunc as LightMyRequestCallback } from 'light-my-request' import { RouteOptions, RouteShorthandMethod, RouteGenericInterface } from './route' import { FastifySchemaCompiler, FastifySchemaValidationError } from './schema' import { RawServerBase, RawRequestDefaultExpression, RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } from './utils' import { FastifyLoggerInstance } from './logger' import { FastifyRegister } from './register' import { onRequestHookHandler, preParsingHookHandler, onSendHookHandler, preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onResponseHookHandler, onErrorHookHandler, onRouteHookHandler, onRegisterHookHandler, onCloseHookHandler, onReadyHookHandler, onTimeoutHookHandler } from './hooks' import { FastifyRequest } from './request' import { FastifyReply } from './reply' import { FastifyError } from 'fastify-error' import { AddContentTypeParser, hasContentTypeParser } from './content-type-parser' /** * Fastify server instance. Returned by the core `fastify()` method. */ export interface FastifyInstance< RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, Logger = FastifyLoggerInstance > { server: RawServer; prefix: string; version: string | undefined; log: Logger; addSchema(schema: unknown): FastifyInstance; getSchema(schemaId: string): unknown; getSchemas(): Record; after(): FastifyInstance & PromiseLike; after(afterListener: (err: Error) => void): FastifyInstance; close(): FastifyInstance & PromiseLike; close(closeListener: () => void): FastifyInstance; // should be able to define something useful with the decorator getter/setter pattern using Generics to enfore the users function returns what they expect it to decorate(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance; decorateRequest(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance; decorateReply(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance; hasDecorator(decorator: string | symbol): boolean; hasRequestDecorator(decorator: string | symbol): boolean; hasReplyDecorator(decorator: string | symbol): boolean; inject(opts: InjectOptions | string, cb: LightMyRequestCallback): void; inject(opts: InjectOptions | string): Promise; inject(): LightMyRequestChain; listen(port: number | string, address: string, backlog: number, callback: (err: Error, address: string) => void): void; listen(port: number | string, address: string, callback: (err: Error, address: string) => void): void; listen(port: number | string, callback: (err: Error, address: string) => void): void; listen(port: number | string, address?: string, backlog?: number): Promise; listen(opts: { port: number; host?: string; backlog?: number }, callback: (err: Error, address: string) => void): void; listen(opts: { port: number; host?: string; backlog?: number }): Promise; ready(): FastifyInstance & PromiseLike; ready(readyListener: (err: Error) => void): FastifyInstance; register: FastifyRegister & PromiseLike>; /** * This method will throw a `FST_ERR_MISSING_MIDDLEWARE` error unless support * for Express-style middlewares is first enabled. Visit * https://fastify.io/docs/latest/Middleware/ for more info. */ use(...args: unknown[]): unknown; route< RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault >(opts: RouteOptions): FastifyInstance; get: RouteShorthandMethod; head: RouteShorthandMethod; post: RouteShorthandMethod; put: RouteShorthandMethod; delete: RouteShorthandMethod; options: RouteShorthandMethod; patch: RouteShorthandMethod; all: RouteShorthandMethod; // addHook: overloads // Lifecycle addHooks /** * `onRequest` is the first hook to be executed in the request lifecycle. There was no previous hook, the next hook will be `preParsing`. * Notice: in the `onRequest` hook, request.body will always be null, because the body parsing happens before the `preHandler` hook. */ addHook< RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault >( name: 'onRequest', hook: onRequestHookHandler ): FastifyInstance; /** * `preParsing` is the second hook to be executed in the request lifecycle. The previous hook was `onRequest`, the next hook will be `preValidation`. * Notice: in the `preParsing` hook, request.body will always be null, because the body parsing happens before the `preHandler` hook. */ addHook< RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault >( name: 'preParsing', hook: preParsingHookHandler ): FastifyInstance; /** * `preValidation` is the third hook to be executed in the request lifecycle. The previous hook was `preParsing`, the next hook will be `preHandler`. */ addHook< RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault >( name: 'preValidation', hook: preValidationHookHandler ): FastifyInstance; /** * `preHandler` is the fourth hook to be executed in the request lifecycle. The previous hook was `preValidation`, the next hook will be `preSerialization`. */ addHook< RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault >( name: 'preHandler', hook: preHandlerHookHandler ): FastifyInstance; /** * `preSerialization` is the fifth hook to be executed in the request lifecycle. The previous hook was `preHandler`, the next hook will be `onSend`. * Note: the hook is NOT called if the payload is a string, a Buffer, a stream or null. */ addHook< PreSerializationPayload = unknown, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault >( name: 'preSerialization', hook: preSerializationHookHandler ): FastifyInstance; /** * You can change the payload with the `onSend` hook. It is the sixth hook to be executed in the request lifecycle. The previous hook was `preSerialization`, the next hook will be `onResponse`. * Note: If you change the payload, you may only change it to a string, a Buffer, a stream, or null. */ addHook< OnSendPayload = unknown, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault >( name: 'onSend', hook: onSendHookHandler ): FastifyInstance; /** * `onResponse` is the seventh and last hook in the request hook lifecycle. The previous hook was `onSend`, there is no next hook. * The onResponse hook is executed when a response has been sent, so you will not be able to send more data to the client. It can however be useful for sending data to external services, for example to gather statistics. */ addHook< RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault >( name: 'onResponse', hook: onResponseHookHandler ): FastifyInstance; /** * `onTimeout` is useful if you need to monitor the request timed out in your service. (if the `connectionTimeout` property is set on the fastify instance) * The onTimeout hook is executed when a request is timed out and the http socket has been hanged up. Therefore you will not be able to send data to the client. */ addHook< RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault >( name: 'onTimeout', hook: onTimeoutHookHandler ): FastifyInstance; /** * This hook is useful if you need to do some custom error logging or add some specific header in case of error. * It is not intended for changing the error, and calling reply.send will throw an exception. * This hook will be executed only after the customErrorHandler has been executed, and only if the customErrorHandler sends an error back to the user (Note that the default customErrorHandler always sends the error back to the user). * Notice: unlike the other hooks, pass an error to the done function is not supported. */ addHook< RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault >( name: 'onError', hook: onErrorHookHandler ): FastifyInstance; // Application addHooks /** * Triggered when a new route is registered. Listeners are passed a routeOptions object as the sole parameter. The interface is synchronous, and, as such, the listener does not get passed a callback */ addHook< RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault >( name: 'onRoute', hook: onRouteHookHandler ): FastifyInstance; /** * Triggered when a new plugin is registered and a new encapsulation context is created. The hook will be executed before the registered code. * This hook can be useful if you are developing a plugin that needs to know when a plugin context is formed, and you want to operate in that specific context. * Note: This hook will not be called if a plugin is wrapped inside fastify-plugin. */ addHook( name: 'onRegister', hook: onRegisterHookHandler ): FastifyInstance; /** * Triggered when fastify.listen() or fastify.ready() is invoked to start the server. It is useful when plugins need a "ready" event, for example to load data before the server start listening for requests. */ addHook( name: 'onReady', hook: onReadyHookHandler ): FastifyInstance; /** * Triggered when fastify.close() is invoked to stop the server. It is useful when plugins need a "shutdown" event, for example to close an open connection to a database. */ addHook( name: 'onClose', hook: onCloseHookHandler ): FastifyInstance; /** * Set the 404 handler */ setNotFoundHandler( handler: (request: FastifyRequest, reply: FastifyReply) => void ): FastifyInstance; /** * Set a function that will be called whenever an error happens */ setErrorHandler( handler: (this: FastifyInstance, error: TError, request: FastifyRequest, reply: FastifyReply) => void ): FastifyInstance; /** * Set the schema validator for all routes. */ setValidatorCompiler(schemaCompiler: FastifySchemaCompiler): FastifyInstance; /** * Set the schema serializer for all routes. */ setSerializerCompiler(schemaCompiler: FastifySchemaCompiler): FastifyInstance; /** * Set the reply serializer for all routes. */ setReplySerializer(replySerializer: (payload: unknown, statusCode: number) => string): FastifyInstance; /* * Set the schema error formatter for all routes. */ setSchemaErrorFormatter(errorFormatter: (errors: FastifySchemaValidationError[], dataVar: string) => Error): FastifyInstance; /** * Add a content type parser */ addContentTypeParser: AddContentTypeParser; hasContentTypeParser: hasContentTypeParser; /** * Prints the representation of the internal radix tree used by the router */ printRoutes(): string; }