forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastify.d.ts
352 lines (293 loc) · 13.2 KB
/
fastify.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/// <reference types="node" />
/// <reference types="pino" />
import * as http from 'http';
import * as http2 from 'http2';
import * as https from 'https';
import * as pino from 'pino';
declare function fastify<HttpServer, HttpRequest, HttpResponse>(opts?: fastify.ServerOptions): fastify.FastifyInstance<HttpServer, HttpRequest, HttpResponse>;
declare function fastify(opts?: fastify.ServerOptionsAsHttp): fastify.FastifyInstance<http.Server, http.IncomingMessage, http.ServerResponse>;
declare function fastify(opts?: fastify.ServerOptionsAsSecureHttp): fastify.FastifyInstance<https.Server, http.IncomingMessage, http.ServerResponse>;
declare function fastify(opts?: fastify.ServerOptionsAsHttp2): fastify.FastifyInstance<http2.Http2Server, http2.Http2ServerRequest, http2.Http2ServerResponse>;
declare function fastify(opts?: fastify.ServerOptionsAsSecureHttp2): fastify.FastifyInstance<http2.Http2SecureServer, http2.Http2ServerRequest, http2.Http2ServerResponse>;
declare namespace fastify {
type Plugin<HttpServer, HttpRequest, HttpResponse, T> = (instance: FastifyInstance<HttpServer, HttpRequest, HttpResponse>, opts: T, callback?: (err?: Error) => void) => void
type Middleware<HttpRequest, HttpResponse> = (req: HttpRequest, res: HttpResponse, callback?: (err?: Error) => void) => void
type HTTPMethod = 'DELETE' | 'GET' | 'HEAD' | 'PATCH' | 'POST' | 'PUT' | 'OPTIONS';
type FastifyMiddleware<HttpRequest, HttpResponse> = (req: FastifyRequest<HttpRequest>, reply: FastifyReply<HttpResponse>, done: (err?: Error) => void) => void
type RequestHandler<HttpRequest, HttpResponse> = (req: FastifyRequest<HttpRequest>, res: FastifyReply<HttpResponse>) => void
/**
* fastify's wrapped version of node.js IncomingMessage
*/
interface FastifyRequest<HttpRequest> {
query: {
[key: string]: any
},
params: {
[key: string]: any
},
body: any,
req: HttpRequest,
log: pino.Logger
}
/**
* Response object that is used to build and send a http response
*/
interface FastifyReply<HttpResponse> {
code: (statusCode: number) => FastifyReply<HttpResponse>
header: (name: string, value: any) => FastifyReply<HttpResponse>
type: (contentType: string) => FastifyReply<HttpResponse>
redirect: (statusCode: number, url: string) => FastifyReply<HttpResponse>
serialize: (payload: any) => string
serializer: (fn: Function) => FastifyReply<HttpResponse>
send: (payload?: string|Array<any>|Object|Error|Promise<any>|NodeJS.ReadableStream) => FastifyReply<HttpResponse>
sent: boolean
res: HttpResponse
}
interface ServerOptions {
logger?: pino.LoggerOptions,
}
interface ServerOptionsAsSecure extends ServerOptions {
https: {
key: Buffer,
cert: Buffer
}
}
interface ServerOptionsAsHttp extends ServerOptions {
http2?: false
}
interface ServerOptionsAsSecureHttp extends ServerOptionsAsHttp, ServerOptionsAsSecure {}
interface ServerOptionsAsHttp2 extends ServerOptions {
http2: true
}
interface ServerOptionsAsSecureHttp2 extends ServerOptionsAsHttp2, ServerOptionsAsSecure {}
interface JSONSchema {
// TODO - define/import JSONSchema types
body?: Object
querystring?: Object
params?: Object
response?: {
[code: number]: Object,
[code: string]: Object
}
}
/**
* Optional configuration parameters for the route being created
*/
interface RouteShorthandOptions<HttpRequest, HttpResponse> {
schema?: JSONSchema
beforeHandler?: FastifyMiddleware<HttpRequest, HttpResponse>
}
/**
* Route configuration options such as "url" and "method"
*/
interface RouteOptions<HttpRequest, HttpResponse> extends RouteShorthandOptions<HttpRequest, HttpResponse> {
method: HTTPMethod|HTTPMethod[],
url: string,
handler: RequestHandler<HttpRequest, HttpResponse>
}
/**
* Register options
*/
interface RegisterOptions<HttpRequest, HttpResponse> extends RouteShorthandOptions<HttpRequest, HttpResponse> {
[key: string]: any,
prefix?: string,
}
/**
* Fake http inject options
*/
interface HTTPInjectOptions {
url: string,
method?: HTTPMethod,
authority?: string,
headers?: object,
remoteAddress?: string,
payload?: string | object | Buffer | NodeJS.ReadableStream
simulate?: {
end?: boolean,
split?: boolean,
error?: boolean,
close?: boolean
},
validate?: boolean
}
/**
* Fake http inject response
*/
interface HTTPInjectResponse {
raw: {
req: NodeJS.ReadableStream,
res: http.ServerResponse
},
headers: object,
statusCode: number,
statusMessage: string,
payload: string,
rawPayload: Buffer,
trailers: object
}
/**
* Represents the fastify instance created by the factory function the module exports.
*/
interface FastifyInstance<HttpServer, HttpRequest, HttpResponse> {
server: HttpServer
log: pino.Logger
/**
* Adds a route to the server
*/
route(opts: RouteOptions<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a GET route with the given mount path, options, and handler
*/
get(url: string, opts: RouteShorthandOptions<HttpRequest, HttpResponse>, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a GET route with the given mount path and handler
*/
get(url: string, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a PUT route with the given mount path, options, and handler
*/
put(url: string, opts: RouteShorthandOptions<HttpRequest, HttpResponse>, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a PUT route with the given mount path and handler
*/
put(url: string, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a PATCH route with the given mount path, options, and handler
*/
patch(url: string, opts: RouteShorthandOptions<HttpRequest, HttpResponse>, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a PATCH route with the given mount path and handler
*/
patch(url: string, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a POST route with the given mount path, options, and handler
*/
post(url: string, opts: RouteShorthandOptions<HttpRequest, HttpResponse>, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a POST route with the given mount path and handler
*/
post(url: string, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a HEAD route with the given mount path, options, and handler
*/
head(url: string, opts: RouteShorthandOptions<HttpRequest, HttpResponse>, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a HEAD route with the given mount path and handler
*/
head(url: string, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a DELETE route with the given mount path, options, and handler
*/
delete(url: string, opts: RouteShorthandOptions<HttpRequest, HttpResponse>, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a DELETE route with the given mount path and handler
*/
delete(url: string, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a OPTIONS route with the given mount path, options, and handler
*/
options(url: string, opts: RouteShorthandOptions<HttpRequest, HttpResponse>, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a OPTIONS route with the given mount path and handler
*/
options(url: string, handler: RequestHandler<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Starts the server on the given port after all the plugins are loaded,
* internally waits for the .ready() event. The callback is the same as the
* Node core.
*/
listen(port: number, hostname: string, callback?: (err: Error) => void): http.Server
/**
* Starts the server on the given port after all the plugins are loaded,
* internally waits for the .ready() event. The callback is the same as the
* Node core.
*/
listen(port: number, callback?: (err: Error) => void): http.Server
listen(path: string, callback?: (err: Error) => void): http.Server
/**
* Registers a listener function that is invoked when all the plugins have
* been loaded. It receives an error parameter if something went wrong.
*/
ready(readyListener?: () => void): void
/**
* Call this function to close the server instance and run the "onClose" callback
*/
close(closeListener: () => void): void
/**
* Apply the given middleware to all incoming requests
*/
use(middleware: Middleware<HttpRequest, HttpResponse>): void
/**
* Apply the given middleware to routes matching the given path
*/
use(path: string, middleware: Middleware<HttpRequest, HttpResponse>): void
/**
* Registers a plugin or array of plugins on the server
*/
register<T extends RegisterOptions<HttpRequest, HttpResponse>>(plugin: Plugin<HttpServer, HttpRequest, HttpResponse, T>|Array<Plugin<HttpServer, HttpRequest, HttpResponse, T>>, opts?: T, callback?: (err: Error) => void): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Decorate this fastify instance with new properties. Throws an execption if
* you attempt to add the same decorator name twice
*/
decorate(name: string, decoration: any, dependencies?: Array<string>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Decorate reply objects with new properties. Throws an execption if
* you attempt to add the same decorator name twice
*/
decorateReply(name: string, decoration: any, dependencies?: Array<string>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Decorate request objects with new properties. Throws an execption if
* you attempt to add the same decorator name twice
*/
decorateRequest(name: string, decoration: any, dependencies?: Array<string>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Extends the standard server error. Return an object with the properties you'd
* like added to the error
*/
extendServerError(extendFn: (error: Error) => Object): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Determines if the given named decorator is available
*/
hasDecorator(name: string): boolean
/**
* Add a hook that is triggered when a request is initially received
*/
addHook(name: 'onRequest', hook: Middleware<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Hook that is fired before a request is processed, but after the "onRequest"
* hook
*/
addHook(name: 'preHandler', hook: FastifyMiddleware<HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Hook that is fired after a request is processed, but before the "onResponse"
* hook
*/
addHook(name: 'onSend', hook: (req: FastifyRequest<HttpRequest>, reply: FastifyReply<HttpResponse>, payload: any, done: (err?: Error, value?: any) => void) => void): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Hook that is called when a response is about to be sent to a client
*/
addHook(name: 'onResponse', hook: (res: http.OutgoingMessage, next: (err?: Error) => void) => void): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Adds a hook that is triggered when server.close is called. Useful for closing connections
* and performing cleanup tasks
*/
addHook(name: 'onClose', hook: (instance: FastifyInstance<HttpServer, HttpRequest, HttpResponse>, done: () => void) => void): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Useful for testing http requests without running a sever
*/
inject(opts: HTTPInjectOptions, clb: (res: HTTPInjectResponse) => void): void
/**
* Useful for testing http requests without running a sever
*/
inject(opts: HTTPInjectOptions): Promise<HTTPInjectResponse>
/**
* Set the 404 handler
*/
setNotFoundHandler(handler: (request: FastifyRequest<HttpRequest>, reply: FastifyReply<HttpResponse>) => void): void
/**
* Set a function that will be called whenever an error happens
*/
setErrorHandler(handler: (error: Error, reply: FastifyReply<HttpResponse>) => void): void
}
}
export = fastify;