UNPKG

11.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 <TError extends Error = FastifyError>(err: TError): void;
94 (err: null, res: unknown): void;
95}
96
97/**
98 * `preSerialization` is the fifth hook to be executed in the request lifecycle. The previous hook was `preHandler`, the next hook will be `onSend`.
99 * Note: the hook is NOT called if the payload is a string, a Buffer, a stream or null.
100 */
101export interface preSerializationHookHandler<
102 PreSerializationPayload,
103 RawServer extends RawServerBase = RawServerDefault,
104 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
105 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
106 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
107 ContextConfig = ContextConfigDefault
108> {
109 (
110 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
111 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
112 payload: PreSerializationPayload,
113 done: DoneFuncWithErrOrRes
114 ): Promise<unknown> | void;
115}
116
117/**
118 * 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`.
119 * Note: If you change the payload, you may only change it to a string, a Buffer, a stream, or null.
120 */
121export interface onSendHookHandler<
122 OnSendPayload,
123 RawServer extends RawServerBase = RawServerDefault,
124 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
125 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
126 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
127 ContextConfig = ContextConfigDefault
128> {
129 (
130 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
131 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
132 payload: OnSendPayload,
133 done: DoneFuncWithErrOrRes
134 ): Promise<unknown> | void;
135}
136
137/**
138 * `onResponse` is the seventh and last hook in the request hook lifecycle. The previous hook was `onSend`, there is no next hook.
139 * 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.
140 */
141export interface onResponseHookHandler<
142 RawServer extends RawServerBase = RawServerDefault,
143 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
144 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
145 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
146 ContextConfig = ContextConfigDefault
147> {
148 (
149 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
150 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
151 done: HookHandlerDoneFunction
152 ): Promise<unknown> | void;
153}
154
155/**
156 * This hook is useful if you need to do some custom error logging or add some specific header in case of error.
157 * It is not intended for changing the error, and calling reply.send will throw an exception.
158 * 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).
159 * Notice: unlike the other hooks, pass an error to the done function is not supported.
160 */
161export interface onErrorHookHandler<
162 RawServer extends RawServerBase = RawServerDefault,
163 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
164 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
165 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
166 ContextConfig = ContextConfigDefault,
167 TError extends Error = FastifyError
168> {
169 (
170 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
171 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
172 error: TError,
173 done: () => void
174 ): Promise<unknown> | void;
175}
176
177// Application Hooks
178
179/**
180 * 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
181 */
182export interface onRouteHookHandler<
183 RawServer extends RawServerBase = RawServerDefault,
184 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
185 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
186 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
187 ContextConfig = ContextConfigDefault
188> {
189 (
190 opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> & { path: string; prefix: string }
191 ): Promise<unknown> | void;
192}
193
194/**
195 * Triggered when a new plugin is registered and a new encapsulation context is created. The hook will be executed before the registered code.
196 * 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.
197 * Note: This hook will not be called if a plugin is wrapped inside fastify-plugin.
198 */
199export interface onRegisterHookHandler<
200 RawServer extends RawServerBase = RawServerDefault,
201 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
202 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
203 Logger = FastifyLoggerInstance
204> {
205 (
206 instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger>,
207 done: HookHandlerDoneFunction
208 ): Promise<unknown> | void; // documentation is missing the `done` method
209}
210
211/**
212 * 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.
213 */
214export interface onReadyHookHandler<
215 RawServer extends RawServerBase = RawServerDefault,
216 Logger = FastifyLoggerInstance
217> {
218 (
219 done: HookHandlerDoneFunction
220 ): Promise<unknown> | void;
221}
222
223/**
224 * 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.
225 */
226export interface onCloseHookHandler<
227 RawServer extends RawServerBase = RawServerDefault,
228 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
229 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
230 Logger = FastifyLoggerInstance
231> {
232 (
233 instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger>,
234 done: HookHandlerDoneFunction
235 ): Promise<unknown> | void;
236}