forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
instance.d.ts
279 lines (243 loc) · 14 KB
/
instance.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
import { Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse, CallbackFunc as LightMyRequestCallback } from 'light-my-request'
import { RouteOptions, RouteShorthandMethod, RouteGenericInterface } from './route'
import { FastifySchemaCompiler, FastifySchemaValidationError } from './schema'
import { RawServerBase, RawRequestDefaultExpression, RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
import { FastifyLoggerInstance } from './logger'
import { FastifyRegister } from './register'
import { onRequestHookHandler, preParsingHookHandler, onSendHookHandler, preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onResponseHookHandler, onErrorHookHandler, onRouteHookHandler, onRegisterHookHandler, onCloseHookHandler, onReadyHookHandler, onTimeoutHookHandler } from './hooks'
import { FastifyRequest } from './request'
import { FastifyReply } from './reply'
import { FastifyError } from 'fastify-error'
import { AddContentTypeParser, hasContentTypeParser } from './content-type-parser'
/**
* Fastify server instance. Returned by the core `fastify()` method.
*/
export interface FastifyInstance<
RawServer extends RawServerBase = RawServerDefault,
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
Logger = FastifyLoggerInstance
> {
server: RawServer;
prefix: string;
version: string;
log: Logger;
addSchema(schema: unknown): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
getSchema(schemaId: string): unknown;
getSchemas(): Record<string, unknown>;
after(): FastifyInstance<RawServer, RawRequest, RawReply, Logger> & PromiseLike<undefined>;
after(afterListener: (err: Error) => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
close(): FastifyInstance<RawServer, RawRequest, RawReply, Logger> & PromiseLike<undefined>;
close(closeListener: () => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
// 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
decorate(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
decorateRequest(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
decorateReply(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
hasDecorator(decorator: string | symbol): boolean;
hasRequestDecorator(decorator: string | symbol): boolean;
hasReplyDecorator(decorator: string | symbol): boolean;
inject(opts: InjectOptions | string, cb: LightMyRequestCallback): void;
inject(opts: InjectOptions | string): Promise<LightMyRequestResponse>;
inject(): LightMyRequestChain;
listen(port: number, address: string, backlog: number, callback: (err: Error, address: string) => void): void;
listen(port: number, address: string, callback: (err: Error, address: string) => void): void;
listen(port: number, callback: (err: Error, address: string) => void): void;
listen(port: number, address?: string, backlog?: number): Promise<string>;
listen(opts: { port: number; host?: string; backlog?: number }, callback: (err: Error, address: string) => void): void;
listen(opts: { port: number; host?: string; backlog?: number }): Promise<string>;
ready(): FastifyInstance<RawServer, RawRequest, RawReply> & PromiseLike<undefined>;
ready(readyListener: (err: Error) => void): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
register: FastifyRegister<FastifyInstance<RawServer, RawRequest, RawReply, Logger> & PromiseLike<undefined>>;
/**
* This method will throw a `FST_ERR_MISSING_MIDDLEWARE` error unless support
* for Express-style middlewares is first enabled. Visit
* https://fastify.io/docs/latest/Middleware/ for more info.
*/
use(...args: unknown[]): unknown;
route<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
>(opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
get: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
head: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
post: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
put: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
delete: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
options: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
patch: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
all: RouteShorthandMethod<RawServer, RawRequest, RawReply>;
// addHook: overloads
// Lifecycle addHooks
/**
* `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.
*/
addHook<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'onRequest',
hook: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* `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.
*/
addHook<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'preParsing',
hook: preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* `preValidation` is the third hook to be executed in the request lifecycle. The previous hook was `preParsing`, the next hook will be `preHandler`.
*/
addHook<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'preValidation',
hook: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* `preHandler` is the fourth hook to be executed in the request lifecycle. The previous hook was `preValidation`, the next hook will be `preSerialization`.
*/
addHook<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'preHandler',
hook: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* `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.
*/
addHook<
PreSerializationPayload = unknown,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'preSerialization',
hook: preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* 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.
*/
addHook<
OnSendPayload = unknown,
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'onSend',
hook: onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* `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.
*/
addHook<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'onResponse',
hook: onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* `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.
*/
addHook<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'onTimeout',
hook: onTimeoutHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* 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.
*/
addHook<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'onError',
hook: onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
// Application addHooks
/**
* 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
*/
addHook<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'onRoute',
hook: onRouteHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* 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.
*/
addHook(
name: 'onRegister',
hook: onRegisterHookHandler<RawServer, RawRequest, RawReply, Logger>
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* 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.
*/
addHook(
name: 'onReady',
hook: onReadyHookHandler<RawServer, Logger>
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* 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.
*/
addHook(
name: 'onClose',
hook: onCloseHookHandler<RawServer, RawRequest, RawReply, Logger>
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* Set the 404 handler
*/
setNotFoundHandler<RouteGeneric extends RouteGenericInterface = RouteGenericInterface>(
handler: (request: FastifyRequest<RouteGeneric, RawServer, RawRequest>, reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric>) => void
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* Set a function that will be called whenever an error happens
*/
setErrorHandler<TError extends Error = FastifyError, RouteGeneric extends RouteGenericInterface = RouteGenericInterface>(
handler: (this: FastifyInstance<RawServer, RawRequest, RawReply, Logger>, error: TError, request: FastifyRequest<RouteGeneric, RawServer, RawRequest>, reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric>) => void
): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* Set the schema validator for all routes.
*/
setValidatorCompiler(schemaCompiler: FastifySchemaCompiler): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* Set the schema serializer for all routes.
*/
setSerializerCompiler(schemaCompiler: FastifySchemaCompiler): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* Set the reply serializer for all routes.
*/
setReplySerializer(replySerializer: (payload: unknown, statusCode: number) => string): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/*
* Set the schema error formatter for all routes.
*/
setSchemaErrorFormatter(errorFormatter: (errors: FastifySchemaValidationError[], dataVar: string) => Error): FastifyInstance<RawServer, RawRequest, RawReply, Logger>;
/**
* Add a content type parser
*/
addContentTypeParser: AddContentTypeParser<RawServer, RawRequest>;
hasContentTypeParser: hasContentTypeParser;
/**
* Prints the representation of the internal radix tree used by the router
*/
printRoutes(): string;
}