1 |
|
2 |
|
3 | import { Readable } from 'stream'
|
4 | import { FastifyInstance } from './instance'
|
5 | import { RouteOptions, RouteGenericInterface } from './route'
|
6 | import { RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
|
7 | import { FastifyRequest } from './request'
|
8 | import { FastifyReply } from './reply'
|
9 | import { FastifyError } from 'fastify-error'
|
10 | import { FastifyLoggerInstance } from './logger'
|
11 |
|
12 | type HookHandlerDoneFunction = <TError extends Error = FastifyError>(err?: TError) => void
|
13 |
|
14 | interface RequestPayload extends Readable {
|
15 | receivedEncodedLength?: number;
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export 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 |
|
40 |
|
41 |
|
42 | export 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 |
|
59 |
|
60 | export 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 |
|
76 |
|
77 | export 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 |
|
92 | interface DoneFuncWithErrOrRes {
|
93 | (): void;
|
94 | <TError extends Error = FastifyError>(err: TError): void;
|
95 | (err: null, res: unknown): void;
|
96 | }
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | export 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 |
|
120 |
|
121 |
|
122 | export 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 |
|
140 |
|
141 |
|
142 | export 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 |
|
158 |
|
159 |
|
160 | export 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 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | export 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 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | export 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 |
|
215 |
|
216 |
|
217 |
|
218 | export 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;
|
228 | }
|
229 |
|
230 |
|
231 |
|
232 |
|
233 | export interface onReadyHookHandler<
|
234 | RawServer extends RawServerBase = RawServerDefault,
|
235 | Logger = FastifyLoggerInstance
|
236 | > {
|
237 | (
|
238 | done: HookHandlerDoneFunction
|
239 | ): Promise<unknown> | void;
|
240 | }
|
241 |
|
242 |
|
243 |
|
244 |
|
245 | export 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 | }
|