forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstance.d.ts
251 lines (218 loc) · 12.1 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
import { Chain as LightMyRequestChain, InjectOptions, Response as LightMyRequestResponse, CallbackFunc as LightMyRequestCallback } from 'light-my-request'
import { RouteOptions, RouteShorthandMethod } from './route'
import { FastifySchema, FastifySchemaCompiler } from './schema'
import { RawServerBase, RawRequestDefaultExpression, RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
import { FastifyLoggerOptions } from './logger'
import { FastifyRegister } from './register'
import { onRequestHookHandler, preParsingHookHandler, onSendHookHandler, preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onResponseHookHandler, onErrorHookHandler, onRouteHookHandler, onRegisterHookHandler, onCloseHookHandler, onReadyHookHandler } from './hooks'
import { FastifyRequest, RequestGenericInterface } from './request'
import { FastifyReply } from './reply'
import { FastifyError } from './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 = FastifyLoggerOptions<RawServer>
> {
server: RawServer;
prefix: string;
log: Logger;
addSchema(schema: FastifySchema): FastifyInstance<RawServer, RawRequest, RawReply>;
after(): FastifyInstance<RawServer, RawRequest, RawReply> & PromiseLike<undefined>;
after(afterListener: (err: Error) => void): FastifyInstance<RawServer, RawRequest, RawReply>;
close(): FastifyInstance<RawServer, RawRequest, RawReply> & PromiseLike<undefined>;
close(closeListener: () => void): FastifyInstance<RawServer, RawRequest, RawReply>;
// 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>;
decorateRequest(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance<RawServer, RawRequest, RawReply>;
decorateReply(property: string | symbol, value: any, dependencies?: string[]): FastifyInstance<RawServer, RawRequest, RawReply>;
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>;
ready(): FastifyInstance<RawServer, RawRequest, RawReply> & PromiseLike<undefined>;
ready(readyListener: (err: Error) => void): FastifyInstance<RawServer, RawRequest, RawReply>;
register: FastifyRegister<FastifyInstance<RawServer, RawRequest, RawReply> & PromiseLike<undefined>>;
/**
* This method is now deprecated and will throw a `FST_ERR_MISSING_MIDDLEWARE` error.
* Visit fastify.io/docs/latest/Middleware/ for more info.
*/
use: void;
route<
RequestGeneric extends RequestGenericInterface = RequestGenericInterface,
ContextConfig = ContextConfigDefault
>(opts: RouteOptions<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig>): FastifyInstance<RawServer, RawRequest, RawReply>;
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<
RequestGeneric extends RequestGenericInterface = RequestGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'onRequest',
hook: onRequestHookHandler<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply>;
/**
* `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<
RequestGeneric extends RequestGenericInterface = RequestGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'preParsing',
hook: preParsingHookHandler<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply>;
/**
* `preValidation` is the third hook to be executed in the request lifecycle. The previous hook was `preParsing`, the next hook will be `preHandler`.
*/
addHook<
RequestGeneric extends RequestGenericInterface = RequestGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'preValidation',
hook: preValidationHookHandler<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply>;
/**
* `preHandler` is the fourth hook to be executed in the request lifecycle. The previous hook was `preValidation`, the next hook will be `preSerialization`.
*/
addHook<
RequestGeneric extends RequestGenericInterface = RequestGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'preHandler',
hook: preHandlerHookHandler<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply>;
/**
* `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,
RequestGeneric extends RequestGenericInterface = RequestGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'preSerialization',
hook: preSerializationHookHandler<PreSerializationPayload, RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply>;
/**
* 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,
RequestGeneric extends RequestGenericInterface = RequestGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'onSend',
hook: onSendHookHandler<OnSendPayload, RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply>;
/**
* `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<
RequestGeneric extends RequestGenericInterface = RequestGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'onResponse',
hook: onResponseHookHandler<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply>;
/**
* 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<
RequestGeneric extends RequestGenericInterface = RequestGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'onError',
hook: onErrorHookHandler<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply>;
// 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<
RequestGeneric extends RequestGenericInterface = RequestGenericInterface,
ContextConfig = ContextConfigDefault
>(
name: 'onRoute',
hook: onRouteHookHandler<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig>
): FastifyInstance<RawServer, RawRequest, RawReply>;
/**
* 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>;
/**
* 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>;
/**
* 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>;
/**
* Set the 404 handler
*/
setNotFoundHandler(
handler: (request: FastifyRequest<RawServer, RawRequest>, reply: FastifyReply<RawServer, RawReply>) => void
): void;
/**
* Set a function that will be called whenever an error happens
*/
setErrorHandler(
handler: (error: FastifyError, request: FastifyRequest<RawServer, RawRequest>, reply: FastifyReply<RawServer, RawReply>) => void
): void;
/**
* Set the schema validator for all routes.
*/
setValidatorCompiler(schemaCompiler: FastifySchemaCompiler): FastifyInstance<RawServer, RawRequest, RawReply>;
/**
* Set the schema serializer for all routes.
*/
setSerializerCompiler(schemaCompiler: FastifySchemaCompiler): FastifyInstance<RawServer, RawRequest, RawReply>;
/**
* 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;
}