UNPKG

12.3 kBTypeScriptView Raw
1/* eslint-disable @typescript-eslint/class-name-casing */
2
3import { Readable } from 'stream'
4import { FastifyInstance } from './instance'
5import { RouteOptions, RouteGenericInterface } from './route'
6import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
7import { FastifyRequest } from './request'
8import { FastifyReply } from './reply'
9import { FastifyError } from 'fastify-error'
10import { FastifyLoggerInstance } from './logger'
11
12type HookHandlerDoneFunction = <TError extends Error = FastifyError>(err?: TError) => void
13
14interface RequestPayload extends Readable {
15 receivedEncodedLength?: number;
16}
17
18// Lifecycle Hooks
19
20/**
21 * `onRequest` is the first hook to be executed in the request lifecycle. There was no previous hook, the next hook will be `preParsing`.
22 * Notice: in the `onRequest` hook, request.body will always be null, because the body parsing happens before the `preHandler` hook.
23 */
24export interface onRequestHookHandler<
25 RawServer extends RawServerBase = RawServerDefault,
26 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
27 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
28 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
29 ContextConfig = ContextConfigDefault
30> {
31 (
32 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
33 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
34 done: HookHandlerDoneFunction
35 ): Promise<unknown> | void;
36}
37
38/**
39 * `preParsing` is the second hook to be executed in the request lifecycle. The previous hook was `onRequest`, the next hook will be `preValidation`.
40 * Notice: in the `preParsing` hook, request.body will always be null, because the body parsing happens before the `preHandler` hook.
41 */
42export interface preParsingHookHandler<
43 RawServer extends RawServerBase = RawServerDefault,
44 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
45 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
46 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
47 ContextConfig = ContextConfigDefault
48> {
49 (
50 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
51 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
52 payload: RequestPayload,
53 done: <TError extends Error = FastifyError>(err?: TError | null, res?: RequestPayload) => void
54 ): Promise<RequestPayload | unknown> | void;
55}
56
57/**
58 * `preValidation` is the third hook to be executed in the request lifecycle. The previous hook was `preParsing`, the next hook will be `preHandler`.
59 */
60export interface preValidationHookHandler<
61 RawServer extends RawServerBase = RawServerDefault,
62 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
63 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
64 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
65 ContextConfig = ContextConfigDefault
66> {
67 (
68 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
69 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
70 done: HookHandlerDoneFunction
71 ): Promise<unknown> | void;
72}
73
74/**
75 * `preHandler` is the fourth hook to be executed in the request lifecycle. The previous hook was `preValidation`, the next hook will be `preSerialization`.
76 */
77export interface preHandlerHookHandler<
78 RawServer extends RawServerBase = RawServerDefault,
79 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
80 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
81 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
82 ContextConfig = ContextConfigDefault
83> {
84 (
85 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
86 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
87 done: HookHandlerDoneFunction
88 ): Promise<unknown> | void;
89}
90
91// This is used within the `preSerialization` and `onSend` hook handlers
92interface DoneFuncWithErrOrRes {
93 (): void;
94 <TError extends Error = FastifyError>(err: TError): void;
95 (err: null, res: unknown): void;
96}
97
98/**
99 * `preSerialization` is the fifth hook to be executed in the request lifecycle. The previous hook was `preHandler`, the next hook will be `onSend`.
100 * Note: the hook is NOT called if the payload is a string, a Buffer, a stream or null.
101 */
102export interface preSerializationHookHandler<
103 PreSerializationPayload,
104 RawServer extends RawServerBase = RawServerDefault,
105 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
106 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
107 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
108 ContextConfig = ContextConfigDefault
109> {
110 (
111 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
112 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
113 payload: PreSerializationPayload,
114 done: DoneFuncWithErrOrRes
115 ): Promise<unknown> | void;
116}
117
118/**
119 * 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`.
120 * Note: If you change the payload, you may only change it to a string, a Buffer, a stream, or null.
121 */
122export interface onSendHookHandler<
123 OnSendPayload,
124 RawServer extends RawServerBase = RawServerDefault,
125 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
126 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
127 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
128 ContextConfig = ContextConfigDefault
129> {
130 (
131 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
132 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
133 payload: OnSendPayload,
134 done: DoneFuncWithErrOrRes
135 ): Promise<unknown> | void;
136}
137
138/**
139 * `onResponse` is the seventh and last hook in the request hook lifecycle. The previous hook was `onSend`, there is no next hook.
140 * 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.
141 */
142export interface onResponseHookHandler<
143 RawServer extends RawServerBase = RawServerDefault,
144 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
145 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
146 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
147 ContextConfig = ContextConfigDefault
148> {
149 (
150 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
151 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
152 done: HookHandlerDoneFunction
153 ): Promise<unknown> | void;
154}
155
156/**
157 * `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)
158 * 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.
159 */
160export interface onTimeoutHookHandler<
161 RawServer extends RawServerBase = RawServerDefault,
162 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
163 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
164 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
165 ContextConfig = ContextConfigDefault
166> {
167 (
168 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
169 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
170 done: HookHandlerDoneFunction
171 ): Promise<unknown> | void;
172}
173
174/**
175 * This hook is useful if you need to do some custom error logging or add some specific header in case of error.
176 * It is not intended for changing the error, and calling reply.send will throw an exception.
177 * 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).
178 * Notice: unlike the other hooks, pass an error to the done function is not supported.
179 */
180export interface onErrorHookHandler<
181 RawServer extends RawServerBase = RawServerDefault,
182 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
183 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
184 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
185 ContextConfig = ContextConfigDefault,
186 TError extends Error = FastifyError
187> {
188 (
189 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
190 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
191 error: TError,
192 done: () => void
193 ): Promise<unknown> | void;
194}
195
196// Application Hooks
197
198/**
199 * 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
200 */
201export interface onRouteHookHandler<
202 RawServer extends RawServerBase = RawServerDefault,
203 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
204 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
205 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
206 ContextConfig = ContextConfigDefault
207> {
208 (
209 opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> & { routePath: string; path: string; prefix: string }
210 ): Promise<unknown> | void;
211}
212
213/**
214 * Triggered when a new plugin is registered and a new encapsulation context is created. The hook will be executed before the registered code.
215 * 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.
216 * Note: This hook will not be called if a plugin is wrapped inside fastify-plugin.
217 */
218export interface onRegisterHookHandler<
219 RawServer extends RawServerBase = RawServerDefault,
220 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
221 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
222 Logger = FastifyLoggerInstance
223> {
224 (
225 instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger>,
226 done: HookHandlerDoneFunction
227 ): Promise<unknown> | void; // documentation is missing the `done` method
228}
229
230/**
231 * 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.
232 */
233export interface onReadyHookHandler<
234 RawServer extends RawServerBase = RawServerDefault,
235 Logger = FastifyLoggerInstance
236> {
237 (
238 done: HookHandlerDoneFunction
239 ): Promise<unknown> | void;
240}
241
242/**
243 * 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.
244 */
245export interface onCloseHookHandler<
246 RawServer extends RawServerBase = RawServerDefault,
247 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
248 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
249 Logger = FastifyLoggerInstance
250> {
251 (
252 instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger>,
253 done: HookHandlerDoneFunction
254 ): Promise<unknown> | void;
255}