forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks.d.ts
377 lines (350 loc) · 17.1 KB
/
hooks.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { Readable } from 'stream'
import { FastifyInstance } from './instance'
import { RouteOptions, RouteGenericInterface } from './route'
import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
import { FastifyRequest } from './request'
import { FastifyReply } from './reply'
import { FastifyError } from 'fastify-error'
import { FastifyLoggerInstance } from './logger'
type HookHandlerDoneFunction = <TError extends Error = FastifyError>(err?: TError) => void
interface RequestPayload extends Readable {
receivedEncodedLength?: number;
}
// Lifecycle Hooks
/**
* `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.
*/
export interface onRequestHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
}
export interface onRequestAsyncHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
): Promise<unknown>;
}
/**
* `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.
*/
export interface preParsingHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: RequestPayload,
done: <TError extends Error = FastifyError>(err?: TError | null, res?: RequestPayload) => void
): void;
}
export interface preParsingAsyncHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: RequestPayload,
): Promise<RequestPayload | unknown>;
}
/**
* `preValidation` is the third hook to be executed in the request lifecycle. The previous hook was `preParsing`, the next hook will be `preHandler`.
*/
export interface preValidationHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
}
export interface preValidationAsyncHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
): Promise<unknown>;
}
/**
* `preHandler` is the fourth hook to be executed in the request lifecycle. The previous hook was `preValidation`, the next hook will be `preSerialization`.
*/
export interface preHandlerHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
}
export interface preHandlerAsyncHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
): Promise<unknown>;
}
// This is used within the `preSerialization` and `onSend` hook handlers
interface DoneFuncWithErrOrRes {
(): void;
<TError extends Error = FastifyError>(err: TError): void;
(err: null, res: unknown): void;
}
/**
* `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.
*/
export interface preSerializationHookHandler<
PreSerializationPayload,
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: PreSerializationPayload,
done: DoneFuncWithErrOrRes
): void;
}
export interface preSerializationAsyncHookHandler<
PreSerializationPayload,
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: PreSerializationPayload
): Promise<unknown>;
}
/**
* 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.
*/
export interface onSendHookHandler<
OnSendPayload,
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: OnSendPayload,
done: DoneFuncWithErrOrRes
): void;
}
export interface onSendAsyncHookHandler<
OnSendPayload,
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
payload: OnSendPayload,
): Promise<unknown>;
}
/**
* `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.
*/
export interface onResponseHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
}
export interface onResponseAsyncHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): Promise<unknown>;
}
/**
* `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.
*/
export interface onTimeoutHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
done: HookHandlerDoneFunction
): void;
}
export interface onTimeoutAsyncHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): Promise<unknown>;
}
/**
* 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.
*/
export interface onErrorHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault,
TError extends Error = FastifyError
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
error: TError,
done: () => void
): void;
}
export interface onErrorAsyncHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault,
TError extends Error = FastifyError
> {
(
request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
error: TError
): Promise<unknown>;
}
// Application Hooks
/**
* 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
*/
export interface onRouteHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
> {
(
opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> & { routePath: string; path: string; prefix: string }
): Promise<unknown> | void;
}
/**
* 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.
*/
export interface onRegisterHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
Logger = FastifyLoggerInstance
> {
(
instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger>,
done: HookHandlerDoneFunction
): Promise<unknown> | void; // documentation is missing the `done` method
}
/**
* 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.
*/
export interface onReadyHookHandler {
(
done: HookHandlerDoneFunction
): void;
}
export interface onReadyAsyncHookHandler {
(): Promise<unknown>;
}
/**
* 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.
*/
export interface onCloseHookHandler<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
Logger = FastifyLoggerInstance
> {
(
instance: FastifyInstance<RawServer, RawRequest, RawReply, Logger>,
done: HookHandlerDoneFunction
): Promise<unknown> | void;
}