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 | <TError extends Error = FastifyError>(err: TError): void;
|
94 | (err: null, res: unknown): void;
|
95 | }
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | export 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 |
|
119 |
|
120 |
|
121 | export 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 |
|
139 |
|
140 |
|
141 | export 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 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 | export 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 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 | export 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 |
|
196 |
|
197 |
|
198 |
|
199 | export 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;
|
209 | }
|
210 |
|
211 |
|
212 |
|
213 |
|
214 | export interface onReadyHookHandler<
|
215 | RawServer extends RawServerBase = RawServerDefault,
|
216 | Logger = FastifyLoggerInstance
|
217 | > {
|
218 | (
|
219 | done: HookHandlerDoneFunction
|
220 | ): Promise<unknown> | void;
|
221 | }
|
222 |
|
223 |
|
224 |
|
225 |
|
226 | export 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 | }
|