UNPKG

50.4 kBTypeScriptView Raw
1import { Readable } from 'stream'
2import { FastifyInstance } from './instance'
3import { RouteOptions, RouteGenericInterface } from './route'
4import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
5import { FastifyRequest } from './request'
6import { FastifyReply } from './reply'
7import { FastifyError } from '@fastify/error'
8import { FastifyBaseLogger } from './logger'
9import {
10 FastifyTypeProvider,
11 FastifyTypeProviderDefault
12} from './type-provider'
13import { RegisterOptions } from './register'
14import { FastifySchema } from './schema'
15import { FastifyPluginOptions } from './plugin'
16
17type HookHandlerDoneFunction = <TError extends Error = FastifyError>(err?: TError) => void
18
19interface RequestPayload extends Readable {
20 receivedEncodedLength?: number;
21}
22
23// Lifecycle Hooks
24
25/**
26 * `onRequest` is the first hook to be executed in the request lifecycle. There was no previous hook, the next hook will be `preParsing`.
27 * Notice: in the `onRequest` hook, request.body will always be null, because the body parsing happens before the `preHandler` hook.
28 */
29export interface onRequestHookHandler<
30 RawServer extends RawServerBase = RawServerDefault,
31 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
32 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
33 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
34 ContextConfig = ContextConfigDefault,
35 SchemaCompiler extends FastifySchema = FastifySchema,
36 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
37 Logger extends FastifyBaseLogger = FastifyBaseLogger
38> {
39 (
40 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
41 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
42 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
43 done: HookHandlerDoneFunction
44 ): void;
45}
46
47export interface onRequestAsyncHookHandler<
48 RawServer extends RawServerBase = RawServerDefault,
49 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
50 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
51 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
52 ContextConfig = ContextConfigDefault,
53 SchemaCompiler extends FastifySchema = FastifySchema,
54 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
55 Logger extends FastifyBaseLogger = FastifyBaseLogger
56> {
57 (
58 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
59 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
60 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
61 ): Promise<unknown>;
62}
63
64// helper type which infers whether onRequestHookHandler or onRequestAsyncHookHandler are
65// applicable based on the specified return type.
66export type onRequestMetaHookHandler<
67 RawServer extends RawServerBase = RawServerDefault,
68 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
69 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
70 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
71 ContextConfig = ContextConfigDefault,
72 SchemaCompiler extends FastifySchema = FastifySchema,
73 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
74 Logger extends FastifyBaseLogger = FastifyBaseLogger,
75 Return extends ReturnType<onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
76 | ReturnType<onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
77 = ReturnType<onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
78> = Return extends ReturnType<onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
79 ? onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
80 : onRequestAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
81
82/**
83 * `preParsing` is the second hook to be executed in the request lifecycle. The previous hook was `onRequest`, the next hook will be `preValidation`.
84 * Notice: in the `preParsing` hook, request.body will always be null, because the body parsing happens before the `preHandler` hook.
85 */
86export interface preParsingHookHandler<
87 RawServer extends RawServerBase = RawServerDefault,
88 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
89 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
90 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
91 ContextConfig = ContextConfigDefault,
92 SchemaCompiler extends FastifySchema = FastifySchema,
93 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
94 Logger extends FastifyBaseLogger = FastifyBaseLogger
95> {
96 (
97 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
98 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
99 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
100 payload: RequestPayload,
101 done: <TError extends Error = FastifyError>(err?: TError | null, res?: RequestPayload) => void
102 ): void;
103}
104
105export interface preParsingAsyncHookHandler<
106 RawServer extends RawServerBase = RawServerDefault,
107 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
108 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
109 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
110 ContextConfig = ContextConfigDefault,
111 SchemaCompiler extends FastifySchema = FastifySchema,
112 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
113 Logger extends FastifyBaseLogger = FastifyBaseLogger
114> {
115 (
116 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
117 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
118 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
119 payload: RequestPayload,
120 ): Promise<RequestPayload | unknown>;
121}
122
123// helper type which infers whether preParsingHookHandler or preParsingAsyncHookHandler are
124// applicable based on the specified return type.
125export type preParsingMetaHookHandler<
126 RawServer extends RawServerBase = RawServerDefault,
127 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
128 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
129 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
130 ContextConfig = ContextConfigDefault,
131 SchemaCompiler extends FastifySchema = FastifySchema,
132 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
133 Logger extends FastifyBaseLogger = FastifyBaseLogger,
134 Return extends ReturnType<preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
135 | ReturnType<preParsingAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
136 = ReturnType<preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
137> = Return extends ReturnType<preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
138 ? preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
139 : preParsingAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
140
141/**
142 * `preValidation` is the third hook to be executed in the request lifecycle. The previous hook was `preParsing`, the next hook will be `preHandler`.
143 */
144export interface preValidationHookHandler<
145 RawServer extends RawServerBase = RawServerDefault,
146 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
147 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
148 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
149 ContextConfig = ContextConfigDefault,
150 SchemaCompiler extends FastifySchema = FastifySchema,
151 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
152 Logger extends FastifyBaseLogger = FastifyBaseLogger
153> {
154 (
155 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
156 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
157 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
158 done: HookHandlerDoneFunction
159 ): void;
160}
161
162export interface preValidationAsyncHookHandler<
163 RawServer extends RawServerBase = RawServerDefault,
164 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
165 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
166 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
167 ContextConfig = ContextConfigDefault,
168 SchemaCompiler extends FastifySchema = FastifySchema,
169 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
170 Logger extends FastifyBaseLogger = FastifyBaseLogger
171> {
172 (
173 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
174 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
175 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
176 ): Promise<unknown>;
177}
178
179// helper type which infers whether preValidationHookHandler or preValidationAsyncHookHandler are
180// applicable based on the specified return type.
181export type preValidationMetaHookHandler<
182 RawServer extends RawServerBase = RawServerDefault,
183 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
184 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
185 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
186 ContextConfig = ContextConfigDefault,
187 SchemaCompiler extends FastifySchema = FastifySchema,
188 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
189 Logger extends FastifyBaseLogger = FastifyBaseLogger,
190 Return extends ReturnType<preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
191 | ReturnType<preValidationAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
192 = ReturnType<preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
193> = Return extends ReturnType<preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
194 ? preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
195 : preValidationAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
196
197/**
198 * `preHandler` is the fourth hook to be executed in the request lifecycle. The previous hook was `preValidation`, the next hook will be `preSerialization`.
199 */
200export interface preHandlerHookHandler<
201 RawServer extends RawServerBase = RawServerDefault,
202 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
203 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
204 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
205 ContextConfig = ContextConfigDefault,
206 SchemaCompiler extends FastifySchema = FastifySchema,
207 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
208 Logger extends FastifyBaseLogger = FastifyBaseLogger
209> {
210 (
211 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
212 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
213 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
214 done: HookHandlerDoneFunction
215 ): void;
216}
217
218export interface preHandlerAsyncHookHandler<
219 RawServer extends RawServerBase = RawServerDefault,
220 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
221 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
222 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
223 ContextConfig = ContextConfigDefault,
224 SchemaCompiler extends FastifySchema = FastifySchema,
225 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
226 Logger extends FastifyBaseLogger = FastifyBaseLogger
227> {
228 (
229 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
230 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
231 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
232 ): Promise<unknown>;
233}
234
235// helper type which infers whether preHandlerHookHandler or preHandlerAsyncHookHandler are
236// applicable based on the specified return type.
237export type preHandlerMetaHookHandler<
238 RawServer extends RawServerBase = RawServerDefault,
239 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
240 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
241 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
242 ContextConfig = ContextConfigDefault,
243 SchemaCompiler extends FastifySchema = FastifySchema,
244 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
245 Logger extends FastifyBaseLogger = FastifyBaseLogger,
246 Return extends ReturnType<preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
247 | ReturnType<preHandlerAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
248 = ReturnType<preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
249> = Return extends ReturnType<preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
250 ? preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
251 : preHandlerAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
252
253// This is used within the `preSerialization` and `onSend` hook handlers
254interface DoneFuncWithErrOrRes {
255 (): void;
256 <TError extends Error = FastifyError>(err: TError): void;
257 (err: null, res: unknown): void;
258}
259
260/**
261 * `preSerialization` is the fifth hook to be executed in the request lifecycle. The previous hook was `preHandler`, the next hook will be `onSend`.
262 * Note: the hook is NOT called if the payload is a string, a Buffer, a stream or null.
263 */
264export interface preSerializationHookHandler<
265 PreSerializationPayload = unknown,
266 RawServer extends RawServerBase = RawServerDefault,
267 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
268 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
269 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
270 ContextConfig = ContextConfigDefault,
271 SchemaCompiler extends FastifySchema = FastifySchema,
272 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
273 Logger extends FastifyBaseLogger = FastifyBaseLogger
274> {
275 (
276 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
277 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
278 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
279 payload: PreSerializationPayload,
280 done: DoneFuncWithErrOrRes
281 ): void;
282}
283
284export interface preSerializationAsyncHookHandler<
285 PreSerializationPayload = unknown,
286 RawServer extends RawServerBase = RawServerDefault,
287 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
288 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
289 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
290 ContextConfig = ContextConfigDefault,
291 SchemaCompiler extends FastifySchema = FastifySchema,
292 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
293 Logger extends FastifyBaseLogger = FastifyBaseLogger
294> {
295 (
296 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
297 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
298 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
299 payload: PreSerializationPayload
300 ): Promise<unknown>;
301}
302
303// helper type which infers whether preSerializationHookHandler or preSerializationAsyncHookHandler are
304// applicable based on the specified return type.
305export type preSerializationMetaHookHandler<
306 PreSerializationPayload = unknown,
307 RawServer extends RawServerBase = RawServerDefault,
308 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
309 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
310 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
311 ContextConfig = ContextConfigDefault,
312 SchemaCompiler extends FastifySchema = FastifySchema,
313 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
314 Logger extends FastifyBaseLogger = FastifyBaseLogger,
315 Return extends ReturnType<preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
316 | ReturnType<preSerializationAsyncHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
317 = ReturnType<preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
318> = Return extends ReturnType<preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
319 ? preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
320 : preSerializationAsyncHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
321
322/**
323 * 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`.
324 * Note: If you change the payload, you may only change it to a string, a Buffer, a stream, or null.
325 */
326export interface onSendHookHandler<
327 OnSendPayload = unknown,
328 RawServer extends RawServerBase = RawServerDefault,
329 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
330 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
331 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
332 ContextConfig = ContextConfigDefault,
333 SchemaCompiler extends FastifySchema = FastifySchema,
334 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
335 Logger extends FastifyBaseLogger = FastifyBaseLogger
336> {
337 (
338 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
339 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
340 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
341 payload: OnSendPayload,
342 done: DoneFuncWithErrOrRes
343 ): void;
344}
345
346export interface onSendAsyncHookHandler<
347 OnSendPayload = unknown,
348 RawServer extends RawServerBase = RawServerDefault,
349 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
350 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
351 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
352 ContextConfig = ContextConfigDefault,
353 SchemaCompiler extends FastifySchema = FastifySchema,
354 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
355 Logger extends FastifyBaseLogger = FastifyBaseLogger
356> {
357 (
358 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
359 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
360 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
361 payload: OnSendPayload,
362 ): Promise<unknown>;
363}
364
365// helper type which infers whether onSendHookHandler or onSendAsyncHookHandler are
366// applicable based on the specified return type.
367export type onSendMetaHookHandler<
368 OnSendPayload = unknown,
369 RawServer extends RawServerBase = RawServerDefault,
370 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
371 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
372 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
373 ContextConfig = ContextConfigDefault,
374 SchemaCompiler extends FastifySchema = FastifySchema,
375 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
376 Logger extends FastifyBaseLogger = FastifyBaseLogger,
377 Return extends ReturnType<onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
378 | ReturnType<onSendAsyncHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
379 = ReturnType<onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
380> = Return extends ReturnType<onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
381 ? onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
382 : onSendAsyncHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
383
384/**
385 * `onResponse` is the seventh and last hook in the request hook lifecycle. The previous hook was `onSend`, there is no next hook.
386 * 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.
387 */
388export interface onResponseHookHandler<
389 RawServer extends RawServerBase = RawServerDefault,
390 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
391 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
392 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
393 ContextConfig = ContextConfigDefault,
394 SchemaCompiler extends FastifySchema = FastifySchema,
395 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
396 Logger extends FastifyBaseLogger = FastifyBaseLogger
397> {
398 (
399 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
400 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
401 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
402 done: HookHandlerDoneFunction
403 ): void;
404}
405
406export interface onResponseAsyncHookHandler<
407 RawServer extends RawServerBase = RawServerDefault,
408 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
409 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
410 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
411 ContextConfig = ContextConfigDefault,
412 SchemaCompiler extends FastifySchema = FastifySchema,
413 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
414 Logger extends FastifyBaseLogger = FastifyBaseLogger
415> {
416 (
417 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
418 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
419 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
420 ): Promise<unknown>;
421}
422
423// helper type which infers whether onResponseHookHandler or onResponseAsyncHookHandler are
424// applicable based on the specified return type.
425export type onResponseMetaHookHandler<
426 RawServer extends RawServerBase = RawServerDefault,
427 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
428 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
429 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
430 ContextConfig = ContextConfigDefault,
431 SchemaCompiler extends FastifySchema = FastifySchema,
432 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
433 Logger extends FastifyBaseLogger = FastifyBaseLogger,
434 Return extends ReturnType<onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
435 | ReturnType<onResponseAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
436 = ReturnType<onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
437> = Return extends ReturnType<onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
438 ? onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
439 : onResponseAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
440
441/**
442 * `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)
443 * 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.
444 */
445export interface onTimeoutHookHandler<
446 RawServer extends RawServerBase = RawServerDefault,
447 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
448 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
449 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
450 ContextConfig = ContextConfigDefault,
451 SchemaCompiler extends FastifySchema = FastifySchema,
452 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
453 Logger extends FastifyBaseLogger = FastifyBaseLogger
454> {
455 (
456 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
457 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
458 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
459 done: HookHandlerDoneFunction
460 ): void;
461}
462
463export interface onTimeoutAsyncHookHandler<
464 RawServer extends RawServerBase = RawServerDefault,
465 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
466 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
467 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
468 ContextConfig = ContextConfigDefault,
469 SchemaCompiler extends FastifySchema = FastifySchema,
470 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
471 Logger extends FastifyBaseLogger = FastifyBaseLogger
472> {
473 (
474 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
475 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
476 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
477 ): Promise<unknown>;
478}
479
480// helper type which infers whether onTimeoutHookHandler or onTimeoutAsyncHookHandler are
481// applicable based on the specified return type.
482export type onTimeoutMetaHookHandler<
483 RawServer extends RawServerBase = RawServerDefault,
484 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
485 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
486 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
487 ContextConfig = ContextConfigDefault,
488 SchemaCompiler extends FastifySchema = FastifySchema,
489 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
490 Logger extends FastifyBaseLogger = FastifyBaseLogger,
491 Return extends ReturnType<onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
492 | ReturnType<onTimeoutAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
493 = ReturnType<onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
494> = Return extends ReturnType<onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
495 ? onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
496 : onTimeoutAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
497
498/**
499 * This hook is useful if you need to do some custom error logging or add some specific header in case of error.
500 * It is not intended for changing the error, and calling reply.send will throw an exception.
501 * 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).
502 * Notice: unlike the other hooks, pass an error to the done function is not supported.
503 */
504export interface onErrorHookHandler<
505 RawServer extends RawServerBase = RawServerDefault,
506 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
507 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
508 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
509 ContextConfig = ContextConfigDefault,
510 TError extends Error = FastifyError,
511 SchemaCompiler extends FastifySchema = FastifySchema,
512 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
513 Logger extends FastifyBaseLogger = FastifyBaseLogger
514> {
515 (
516 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
517 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
518 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
519 error: TError,
520 done: () => void
521 ): void;
522}
523
524export interface onErrorAsyncHookHandler<
525 RawServer extends RawServerBase = RawServerDefault,
526 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
527 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
528 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
529 ContextConfig = ContextConfigDefault,
530 TError extends Error = FastifyError,
531 SchemaCompiler extends FastifySchema = FastifySchema,
532 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
533 Logger extends FastifyBaseLogger = FastifyBaseLogger
534> {
535 (
536 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
537 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
538 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>,
539 error: TError
540 ): Promise<unknown>;
541}
542
543// helper type which infers whether onErrorHookHandler or onErrorAsyncHookHandler are
544// applicable based on the specified return type.
545export type onErrorMetaHookHandler<
546 RawServer extends RawServerBase = RawServerDefault,
547 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
548 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
549 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
550 ContextConfig = ContextConfigDefault,
551 TError extends Error = FastifyError,
552 SchemaCompiler extends FastifySchema = FastifySchema,
553 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
554 Logger extends FastifyBaseLogger = FastifyBaseLogger,
555 Return extends ReturnType<onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, TError, SchemaCompiler, TypeProvider, Logger>>
556 | ReturnType<onErrorAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, TError, SchemaCompiler, TypeProvider, Logger>>
557 = ReturnType<onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, TError, SchemaCompiler, TypeProvider, Logger>>
558> = Return extends ReturnType<onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, TError, SchemaCompiler, TypeProvider, Logger>>
559 ? onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, TError, SchemaCompiler, TypeProvider, Logger>
560 : onErrorAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, TError, SchemaCompiler, TypeProvider, Logger>
561
562/**
563 * `onRequestAbort` is useful if you need to monitor the if the client aborts the request (if the `request.raw.aborted` property is set to `true`).
564 * The `onRequestAbort` hook is executed when a client closes the connection before the entire request has been received. Therefore, you will not be able to send data to the client.
565 * Notice: client abort detection is not completely reliable. See: https://github.com/fastify/fastify/blob/main/docs/Guides/Detecting-When-Clients-Abort.md
566 */
567export interface onRequestAbortHookHandler<
568 RawServer extends RawServerBase = RawServerDefault,
569 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
570 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
571 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
572 ContextConfig = ContextConfigDefault,
573 SchemaCompiler extends FastifySchema = FastifySchema,
574 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
575 Logger extends FastifyBaseLogger = FastifyBaseLogger
576> {
577 (
578 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
579 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
580 done: HookHandlerDoneFunction
581 ): void;
582}
583
584export interface onRequestAbortAsyncHookHandler<
585 RawServer extends RawServerBase = RawServerDefault,
586 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
587 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
588 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
589 ContextConfig = ContextConfigDefault,
590 SchemaCompiler extends FastifySchema = FastifySchema,
591 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
592 Logger extends FastifyBaseLogger = FastifyBaseLogger
593> {
594 (
595 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
596 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
597 ): Promise<unknown>;
598}
599
600// helper type which infers whether onRequestAbortHookHandler or onRequestAbortHookHandler are
601// applicable based on the specified return type.
602export type onRequestAbortMetaHookHandler<
603 RawServer extends RawServerBase = RawServerDefault,
604 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
605 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
606 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
607 ContextConfig = ContextConfigDefault,
608 SchemaCompiler extends FastifySchema = FastifySchema,
609 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
610 Logger extends FastifyBaseLogger = FastifyBaseLogger,
611 Return extends ReturnType<onRequestAbortHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
612 | ReturnType<onRequestAbortAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
613 = ReturnType<onRequestAbortHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
614> = Return extends ReturnType<onRequestAbortHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>>
615 ? onRequestAbortHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
616 : onRequestAbortAsyncHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
617
618export type LifecycleHook = 'onRequest'
619| 'preParsing'
620| 'preValidation'
621| 'preHandler'
622| 'preSerialization'
623| 'onSend'
624| 'onResponse'
625| 'onRequest'
626| 'onError'
627| 'onTimeout'
628| 'onRequestAbort'
629
630export type LifecycleHookLookup<K extends LifecycleHook> = K extends 'onRequest'
631 ? onRequestHookHandler
632 : K extends 'preParsing'
633 ? preParsingHookHandler
634 : K extends 'preValidation'
635 ? preValidationHookHandler
636 : K extends 'preHandler'
637 ? preHandlerHookHandler
638 : K extends 'preSerialization'
639 ? preSerializationHookHandler
640 : K extends 'onSend'
641 ? onSendHookHandler
642 : K extends 'onResponse'
643 ? onResponseHookHandler
644 : K extends 'onRequest'
645 ? onRequestHookHandler
646 : K extends 'onError'
647 ? onErrorHookHandler
648 : K extends 'onTimeout'
649 ? onTimeoutHookHandler
650 : K extends 'onRequestAbort'
651 ? onRequestAbortHookHandler
652 : never
653
654export type LifecycleHookAsyncLookup<K extends LifecycleHook> = K extends 'onRequest'
655 ? onRequestAsyncHookHandler
656 : K extends 'preParsing'
657 ? preParsingAsyncHookHandler
658 : K extends 'preValidation'
659 ? preValidationAsyncHookHandler
660 : K extends 'preHandler'
661 ? preHandlerAsyncHookHandler
662 : K extends 'preSerialization'
663 ? preSerializationAsyncHookHandler
664 : K extends 'onSend'
665 ? onSendAsyncHookHandler
666 : K extends 'onResponse'
667 ? onResponseAsyncHookHandler
668 : K extends 'onRequest'
669 ? onRequestAsyncHookHandler
670 : K extends 'onError'
671 ? onErrorAsyncHookHandler
672 : K extends 'onTimeout'
673 ? onTimeoutAsyncHookHandler
674 : K extends 'onRequestAbort'
675 ? onRequestAbortAsyncHookHandler
676 : never
677
678// Application Hooks
679
680/**
681 * 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
682 */
683export interface onRouteHookHandler<
684 RawServer extends RawServerBase = RawServerDefault,
685 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
686 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
687 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
688 ContextConfig = ContextConfigDefault,
689 SchemaCompiler extends FastifySchema = FastifySchema,
690 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
691 Logger extends FastifyBaseLogger = FastifyBaseLogger
692> {
693 (
694 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
695 opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider> & { routePath: string; path: string; prefix: string }
696 ): Promise<unknown> | void;
697}
698
699/**
700 * Triggered when a new plugin is registered and a new encapsulation context is created. The hook will be executed before the registered code.
701 * 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.
702 * Note: This hook will not be called if a plugin is wrapped inside fastify-plugin.
703 */
704export interface onRegisterHookHandler<
705 RawServer extends RawServerBase = RawServerDefault,
706 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
707 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
708 Logger extends FastifyBaseLogger = FastifyBaseLogger,
709 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
710 Options extends FastifyPluginOptions = FastifyPluginOptions
711> {
712 (
713 instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
714 opts: RegisterOptions & Options,
715 done: HookHandlerDoneFunction
716 ): Promise<unknown> | void; // documentation is missing the `done` method
717}
718
719/**
720 * 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.
721 */
722export interface onReadyHookHandler<
723 RawServer extends RawServerBase = RawServerDefault,
724 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
725 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
726 Logger extends FastifyBaseLogger = FastifyBaseLogger,
727 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
728> {
729 (
730 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
731 done: HookHandlerDoneFunction
732 ): void;
733}
734
735export interface onReadyAsyncHookHandler<
736 RawServer extends RawServerBase = RawServerDefault,
737 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
738 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
739 Logger extends FastifyBaseLogger = FastifyBaseLogger,
740 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
741> {
742 (
743 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
744 ): Promise<unknown>;
745}
746
747/**
748 * Triggered when fastify.listen() is invoked to start the server. It is useful when plugins need a "onListen" event, for example to run logics after the server start listening for requests.
749 */
750export interface onListenHookHandler<
751 RawServer extends RawServerBase = RawServerDefault,
752 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
753 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
754 Logger extends FastifyBaseLogger = FastifyBaseLogger,
755 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
756> {
757 (
758 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
759 done: HookHandlerDoneFunction
760 ): void;
761}
762
763export interface onListenAsyncHookHandler<
764 RawServer extends RawServerBase = RawServerDefault,
765 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
766 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
767 Logger extends FastifyBaseLogger = FastifyBaseLogger,
768 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
769> {
770 (
771 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
772 ): Promise<unknown>;
773}
774/**
775 * 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.
776 */
777export interface onCloseHookHandler<
778 RawServer extends RawServerBase = RawServerDefault,
779 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
780 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
781 Logger extends FastifyBaseLogger = FastifyBaseLogger,
782 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
783> {
784 (
785 instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
786 done: HookHandlerDoneFunction
787 ): void;
788}
789
790export interface onCloseAsyncHookHandler<
791 RawServer extends RawServerBase = RawServerDefault,
792 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
793 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
794 Logger extends FastifyBaseLogger = FastifyBaseLogger,
795 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
796> {
797 (
798 instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>
799 ): Promise<unknown>;
800}
801
802/**
803 * Triggered when fastify.close() is invoked to stop the server. It is useful when plugins need to cancel some state to allow the server to close successfully.
804 */
805export interface preCloseHookHandler<
806 RawServer extends RawServerBase = RawServerDefault,
807 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
808 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
809 Logger extends FastifyBaseLogger = FastifyBaseLogger,
810 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
811> {
812 (
813 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
814 done: HookHandlerDoneFunction
815 ): void;
816}
817
818export interface preCloseAsyncHookHandler<
819 RawServer extends RawServerBase = RawServerDefault,
820 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
821 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
822 Logger extends FastifyBaseLogger = FastifyBaseLogger,
823 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault
824> {
825 (
826 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
827 ): Promise<unknown>;
828}
829
830export type ApplicationHook = 'onRoute'
831| 'onRegister'
832| 'onReady'
833| 'onListen'
834| 'onClose'
835| 'preClose'
836
837export type ApplicationHookLookup<K extends ApplicationHook> = K extends 'onRegister'
838 ? onRegisterHookHandler
839 : K extends 'onReady'
840 ? onReadyHookHandler
841 : K extends 'onListen'
842 ? onListenHookHandler
843 : K extends 'onClose'
844 ? onCloseHookHandler
845 : K extends 'preClose'
846 ? preCloseHookHandler
847 : K extends 'onRoute'
848 ? onRouteHookHandler
849 : never
850
851export type ApplicationHookAsyncLookup<K extends ApplicationHook> = K extends 'onRegister'
852 ? onRegisterHookHandler
853 : K extends 'onReady'
854 ? onReadyAsyncHookHandler
855 : K extends 'onListen'
856 ? onListenAsyncHookHandler
857 : K extends 'onClose'
858 ? onCloseAsyncHookHandler
859 : K extends 'preClose'
860 ? preCloseAsyncHookHandler
861 : never
862
863export type HookLookup <K extends ApplicationHook | LifecycleHook> = K extends ApplicationHook
864 ? ApplicationHookLookup<K>
865 : K extends LifecycleHook
866 ? LifecycleHookLookup<K>
867 : never
868
869export type HookAsyncLookup <K extends ApplicationHook | LifecycleHook> = K extends ApplicationHook
870 ? ApplicationHookAsyncLookup<K>
871 : K extends LifecycleHook
872 ? LifecycleHookAsyncLookup<K>
873 : never