UNPKG

13 kBTypeScriptView Raw
1import { Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse, CallbackFunc as LightMyRequestCallback } from 'light-my-request'
2import { RouteOptions, RouteShorthandMethod, RouteGenericInterface } from './route'
3import { FastifySchemaCompiler } from './schema'
4import { RawServerBase, RawRequestDefaultExpression, RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
5import { FastifyLoggerInstance } from './logger'
6import { FastifyRegister } from './register'
7import { onRequestHookHandler, preParsingHookHandler, onSendHookHandler, preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onResponseHookHandler, onErrorHookHandler, onRouteHookHandler, onRegisterHookHandler, onCloseHookHandler, onReadyHookHandler } from './hooks'
8import { FastifyRequest } from './request'
9import { FastifyReply } from './reply'
10import { FastifyError } from 'fastify-error'
11import { AddContentTypeParser, hasContentTypeParser } from './content-type-parser'
12
13/**
14 * Fastify server instance. Returned by the core `fastify()` method.
15 */
16export interface FastifyInstance<
17 RawServer extends RawServerBase = RawServerDefault,
18 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
19 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
20 Logger = FastifyLoggerInstance
21> {
22 server: RawServer;
23 prefix: string;
24 log: Logger;
25
26 addSchema(schema: unknown): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
27
28 after(): FastifyInstance<RawServer, RawRequest, RawReply, Logger> & PromiseLike<undefined>;
29 after(afterListener: (err: Error) => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
30
31 close(): FastifyInstance<RawServer, RawRequest, RawReply, Logger> & PromiseLike<undefined>;
32 close(closeListener: () => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
33
34 // should be able to define something useful with the decorator getter/setter pattern using Generics to enfore the users function returns what they expect it to
35 decorate(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
36 decorateRequest(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
37 decorateReply(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
38
39 hasDecorator(decorator: string | symbol): boolean;
40 hasRequestDecorator(decorator: string | symbol): boolean;
41 hasReplyDecorator(decorator: string | symbol): boolean;
42
43 inject(opts: InjectOptions | string, cb: LightMyRequestCallback): void;
44 inject(opts: InjectOptions | string): Promise<LightMyRequestResponse>;
45 inject(): LightMyRequestChain;
46
47 listen(port: number, address: string, backlog: number, callback: (err: Error, address: string) => void): void;
48 listen(port: number, address: string, callback: (err: Error, address: string) => void): void;
49 listen(port: number, callback: (err: Error, address: string) => void): void;
50 listen(port: number, address?: string, backlog?: number): Promise<string>;
51
52 ready(): FastifyInstance<RawServer, RawRequest, RawReply> & PromiseLike<undefined>;
53 ready(readyListener: (err: Error) => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
54
55 register: FastifyRegister<FastifyInstance<RawServer, RawRequest, RawReply, Logger> & PromiseLike<undefined>>;
56
57 /**
58 * This method will throw a `FST_ERR_MISSING_MIDDLEWARE` error unless support
59 * for Express-style middlewares is first enabled. Visit
60 * https://fastify.io/docs/latest/Middleware/ for more info.
61 */
62 use(...args: unknown[]): unknown;
63
64 route<
65 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
66 ContextConfig = ContextConfigDefault
67 >(opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
68
69 get: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
70 head: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
71 post: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
72 put: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
73 delete: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
74 options: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
75 patch: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
76 all: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
77
78 // addHook: overloads
79
80 // Lifecycle addHooks
81
82 /**
83 * `onRequest` is the first hook to be executed in the request lifecycle. There was no previous hook, the next hook will be `preParsing`.
84 * Notice: in the `onRequest` hook, request.body will always be null, because the body parsing happens before the `preHandler` hook.
85 */
86 addHook<
87 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
88 ContextConfig = ContextConfigDefault
89 >(
90 name: 'onRequest',
91 hook: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
92 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
93
94 /**
95 * `preParsing` is the second hook to be executed in the request lifecycle. The previous hook was `onRequest`, the next hook will be `preValidation`.
96 * Notice: in the `preParsing` hook, request.body will always be null, because the body parsing happens before the `preHandler` hook.
97 */
98 addHook<
99 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
100 ContextConfig = ContextConfigDefault
101 >(
102 name: 'preParsing',
103 hook: preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
104 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
105
106 /**
107 * `preValidation` is the third hook to be executed in the request lifecycle. The previous hook was `preParsing`, the next hook will be `preHandler`.
108 */
109 addHook<
110 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
111 ContextConfig = ContextConfigDefault
112 >(
113 name: 'preValidation',
114 hook: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
115 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
116
117 /**
118 * `preHandler` is the fourth hook to be executed in the request lifecycle. The previous hook was `preValidation`, the next hook will be `preSerialization`.
119 */
120 addHook<
121 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
122 ContextConfig = ContextConfigDefault
123 >(
124 name: 'preHandler',
125 hook: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
126 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
127
128 /**
129 * `preSerialization` is the fifth hook to be executed in the request lifecycle. The previous hook was `preHandler`, the next hook will be `onSend`.
130 * Note: the hook is NOT called if the payload is a string, a Buffer, a stream or null.
131 */
132 addHook<
133 PreSerializationPayload = unknown,
134 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
135 ContextConfig = ContextConfigDefault
136 >(
137 name: 'preSerialization',
138 hook: preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
139 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
140
141 /**
142 * 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`.
143 * Note: If you change the payload, you may only change it to a string, a Buffer, a stream, or null.
144 */
145 addHook<
146 OnSendPayload = unknown,
147 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
148 ContextConfig = ContextConfigDefault
149 >(
150 name: 'onSend',
151 hook: onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
152 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
153
154 /**
155 * `onResponse` is the seventh and last hook in the request hook lifecycle. The previous hook was `onSend`, there is no next hook.
156 * 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.
157 */
158 addHook<
159 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
160 ContextConfig = ContextConfigDefault
161 >(
162 name: 'onResponse',
163 hook: onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
164 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
165
166 /**
167 * This hook is useful if you need to do some custom error logging or add some specific header in case of error.
168 * It is not intended for changing the error, and calling reply.send will throw an exception.
169 * 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).
170 * Notice: unlike the other hooks, pass an error to the done function is not supported.
171 */
172 addHook<
173 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
174 ContextConfig = ContextConfigDefault
175 >(
176 name: 'onError',
177 hook: onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
178 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
179
180 // Application addHooks
181
182 /**
183 * 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
184 */
185 addHook<
186 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
187 ContextConfig = ContextConfigDefault
188 >(
189 name: 'onRoute',
190 hook: onRouteHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
191 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
192
193 /**
194 * Triggered when a new plugin is registered and a new encapsulation context is created. The hook will be executed before the registered code.
195 * 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.
196 * Note: This hook will not be called if a plugin is wrapped inside fastify-plugin.
197 */
198 addHook(
199 name: 'onRegister',
200 hook: onRegisterHookHandler<RawServer, RawRequest, RawReply, Logger>
201 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
202
203 /**
204 * 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.
205 */
206 addHook(
207 name: 'onReady',
208 hook: onReadyHookHandler<RawServer, Logger>
209 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
210
211 /**
212 * 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.
213 */
214 addHook(
215 name: 'onClose',
216 hook: onCloseHookHandler<RawServer, RawRequest, RawReply, Logger>
217 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
218
219 /**
220 * Set the 404 handler
221 */
222 setNotFoundHandler<RouteGeneric extends RouteGenericInterface = RouteGenericInterface>(
223 handler: (request: FastifyRequest<RouteGeneric, RawServer, RawRequest>, reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric>) => void
224 ): void;
225
226 /**
227 * Set a function that will be called whenever an error happens
228 */
229 setErrorHandler<TError extends Error = FastifyError, RouteGeneric extends RouteGenericInterface = RouteGenericInterface>(
230 handler: (this: FastifyInstance<RawServer, RawRequest, RawReply, Logger>, error: TError, request: FastifyRequest<RouteGeneric, RawServer, RawRequest>, reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric>) => void
231 ): void;
232
233 /**
234 * Set the schema validator for all routes.
235 */
236 setValidatorCompiler(schemaCompiler: FastifySchemaCompiler): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
237
238 /**
239 * Set the schema serializer for all routes.
240 */
241 setSerializerCompiler(schemaCompiler: FastifySchemaCompiler): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
242
243 /**
244 * Set the reply serializer for all routes.
245 */
246 setReplySerializer(replySerializer: (payload: unknown, statusCode: number) => string): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
247
248 /**
249 * Add a content type parser
250 */
251 addContentTypeParser: AddContentTypeParser<RawServer, RawRequest>;
252 hasContentTypeParser: hasContentTypeParser;
253
254 /**
255 * Prints the representation of the internal radix tree used by the router
256 */
257 printRoutes(): string;
258}