forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fastify.d.ts
710 lines (622 loc) · 27.7 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/* eslint no-unused-vars: 0 */
/* eslint no-undef: 0 */
/* eslint space-infix-ops: 0 */
/// <reference types="node" />
import * as http from 'http'
import * as http2 from 'http2'
import * as https from 'https'
declare function fastify<
HttpServer extends (http.Server | http2.Http2Server) = http.Server,
HttpRequest extends (http.IncomingMessage | http2.Http2ServerRequest) = http.IncomingMessage,
HttpResponse extends (http.ServerResponse | http2.Http2ServerResponse) = http.ServerResponse
>(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>;
// eslint-disable-next-line no-redeclare
declare namespace fastify {
type Plugin < HttpServer, HttpRequest, HttpResponse, T > = (instance: FastifyInstance< HttpServer, HttpRequest, HttpResponse >, opts: T, callback: (err?: FastifyError) => void) => void
type Middleware < HttpServer, HttpRequest, HttpResponse > = (this: FastifyInstance<HttpServer, HttpRequest, HttpResponse>, req: HttpRequest, res: HttpResponse, callback: (err?: FastifyError) => void) => void
type DefaultQuery = { [k: string]: any }
type DefaultParams = { [k: string]: any }
type DefaultHeaders = { [k: string]: any }
type DefaultBody = any
type HTTPMethod = 'DELETE' | 'GET' | 'HEAD' | 'PATCH' | 'POST' | 'PUT' | 'OPTIONS'
interface ValidationResult {
keyword: string;
dataPath: string;
schemaPath: string;
params: {
[type: string]: string;
},
message: string;
}
/**
* Fastify custom error
*/
interface FastifyError extends Error {
statusCode?: number;
/**
* Validation errors
*/
validation?: Array<ValidationResult>;
}
interface Logger {
fatal(msg: string, ...args: any[]): void;
fatal(obj: {}, msg?: string, ...args: any[]): void;
error(msg: string, ...args: any[]): void;
error(obj: {}, msg?: string, ...args: any[]): void;
warn(msg: string, ...args: any[]): void;
warn(obj: {}, msg?: string, ...args: any[]): void;
info(msg: string, ...args: any[]): void;
info(obj: {}, msg?: string, ...args: any[]): void;
debug(msg: string, ...args: any[]): void;
debug(obj: {}, msg?: string, ...args: any[]): void;
trace(msg: string, ...args: any[]): void;
trace(obj: {}, msg?: string, ...args: any[]): void;
}
type FastifyMiddleware<
HttpServer = http.Server,
HttpRequest = http.IncomingMessage,
HttpResponse = http.ServerResponse,
Query = DefaultQuery,
Params = DefaultParams,
Headers = DefaultHeaders,
Body = DefaultBody
> = (
this: FastifyInstance<HttpServer, HttpRequest, HttpResponse>,
req: FastifyRequest<HttpRequest, Query, Params, Headers, Body>,
reply: FastifyReply<HttpResponse>,
done: (err?: Error) => void,
) => void | Promise<any>
type FastifyMiddlewareWithPayload<
HttpServer = http.Server,
HttpRequest = http.IncomingMessage,
HttpResponse = http.ServerResponse,
Query = DefaultQuery,
Params = DefaultParams,
Headers = DefaultHeaders,
Body = DefaultBody
> = (
this: FastifyInstance<HttpServer, HttpRequest, HttpResponse>,
req: FastifyRequest<HttpRequest, Query, Params, Headers, Body>,
reply: FastifyReply<HttpResponse>,
payload: any,
done: (err?: Error, value?: any) => void,
) => void | Promise<any>
type RequestHandler<
HttpRequest = http.IncomingMessage,
HttpResponse = http.ServerResponse,
Query = DefaultQuery,
Params = DefaultParams,
Headers = DefaultHeaders,
Body = DefaultBody
> = (
this: FastifyInstance<http.Server, HttpRequest, HttpResponse>,
request: FastifyRequest<HttpRequest, Query, Params, Headers, Body>,
reply: FastifyReply<HttpResponse>,
) => void | Promise<any>
type SchemaCompiler = (schema: Object) => Function
type BodyParser<HttpRequest, RawBody extends string | Buffer> =
| ((req: HttpRequest, rawBody: RawBody, done: (err: Error | null, body?: any) => void) => void)
| ((req: HttpRequest, rawBody: RawBody) => Promise<any>)
type ContentTypeParser<HttpRequest> =
| ((req: HttpRequest, done: (err: Error | null, body?: any) => void) => void)
| ((req: HttpRequest) => Promise<any>)
interface FastifyContext {
config: any
}
/**
* fastify's wrapped version of node.js IncomingMessage
*/
interface FastifyRequest<
HttpRequest = http.IncomingMessage,
Query = DefaultQuery,
Params = DefaultParams,
Headers = DefaultHeaders,
Body = DefaultBody
> {
query: Query
params: Params
headers: Headers
body: Body
id: any
ip: string
ips: string[]
hostname: string
raw: HttpRequest
req: HttpRequest
log: Logger
}
/**
* Response object that is used to build and send a http response
*/
interface FastifyReply<HttpResponse> {
code(statusCode: number): FastifyReply<HttpResponse>
status(statusCode: number): FastifyReply<HttpResponse>
header(name: string, value: any): FastifyReply<HttpResponse>
headers(headers: { [key: string]: any }): FastifyReply<HttpResponse>
getHeader(name: string): string | undefined
hasHeader(name: string): boolean
removeHeader(name: string): FastifyReply<HttpResponse>
callNotFound(): void
getResponseTime(): number
type(contentType: string): FastifyReply<HttpResponse>
redirect(url: string): FastifyReply<HttpResponse>
redirect(statusCode: number, url: string): FastifyReply<HttpResponse>
serialize(payload: any): string
serializer(fn: Function): FastifyReply<HttpResponse>
send(payload?: any): FastifyReply<HttpResponse>
sent: boolean
res: HttpResponse
context: FastifyContext
request: FastifyRequest
}
type TrustProxyFunction = (addr: string, index: number) => boolean
interface ServerOptions {
caseSensitive?: boolean,
ignoreTrailingSlash?: boolean,
bodyLimit?: number,
pluginTimeout?: number,
disableRequestLogging?: boolean,
onProtoPoisoning?: 'error' | 'remove' | 'ignore',
logger?: any,
trustProxy?: string | number | boolean | Array<string> | TrustProxyFunction,
maxParamLength?: number,
querystringParser?: (str: string) => { [key: string]: string | string[] },
versioning? : {
storage() : {
get(version: String) : Function | null,
set(version: String, store: Function) : void,
del(version: String) : void,
empty() : void
},
deriveVersion<Context>(req: Object, ctx?: Context) : String,
},
modifyCoreObjects?: boolean,
return503OnClosing?: boolean
}
interface ServerOptionsAsSecure extends ServerOptions {
https: http2.SecureServerOptions
}
interface ServerOptionsAsHttp extends ServerOptions {
http2?: false
}
interface ServerOptionsAsSecureHttp extends ServerOptionsAsHttp, ServerOptionsAsSecure {}
interface ServerOptionsAsHttp2 extends ServerOptions {
http2: true
}
interface ServerOptionsAsSecureHttp2 extends ServerOptionsAsHttp2, ServerOptionsAsSecure {}
// TODO - define/import JSONSchema types
type JSONSchema = Object
interface RouteSchema {
body?: JSONSchema
querystring?: JSONSchema
params?: JSONSchema
headers?: JSONSchema
response?: {
[code: number]: JSONSchema,
[code: string]: JSONSchema
}
}
/**
* Optional configuration parameters for the route being created
*/
interface RouteShorthandOptions<
HttpServer = http.Server,
HttpRequest = http.IncomingMessage,
HttpResponse = http.ServerResponse,
Query = DefaultQuery,
Params = DefaultParams,
Headers = DefaultHeaders,
Body = DefaultBody
> {
schema?: RouteSchema
attachValidation?: boolean
onRequest?:
| FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>
| Array<FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>>
onResponse?:
| FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>
| Array<FastifyMiddleware<HttpServer, HttpRequest, HttpResponse>>
preParsing?:
| FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>
| Array<FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>>
preValidation?:
| FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>
| Array<FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>>
preHandler?:
| FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>
| Array<FastifyMiddleware<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>>
preSerialization?:
FastifyMiddlewareWithPayload<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>
| Array<FastifyMiddlewareWithPayload<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>>
handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>
schemaCompiler?: SchemaCompiler
bodyLimit?: number
logLevel?: string
config?: any
prefixTrailingSlash?: 'slash' | 'no-slash' | 'both'
}
/**
* Route configuration options such as "url" and "method"
*/
interface RouteOptions<
HttpServer = http.Server,
HttpRequest = http.IncomingMessage,
HttpResponse = http.ServerResponse,
Query = DefaultQuery,
Params = DefaultParams,
Headers = DefaultHeaders,
Body = DefaultBody
> extends RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body> {
method: HTTPMethod | HTTPMethod[]
url: string
handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>
}
/**
* Register options
*/
interface RegisterOptions<HttpServer, HttpRequest, HttpResponse> {
[key: string]: any,
prefix?: string,
}
/**
* Fake http inject options
*/
interface HTTPInjectOptions {
url: string,
method?: HTTPMethod,
authority?: string,
headers?: DefaultHeaders,
query?: DefaultQuery,
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: Record<string, string>,
statusCode: number,
statusMessage: string,
payload: string,
rawPayload: Buffer,
trailers: object
}
/**
* Server listen options
*/
interface ListenOptions {
port?: number;
host?: string;
backlog?: number;
path?: string;
exclusive?: boolean;
readableAll?: boolean;
writableAll?: boolean;
/**
* @default false
*/
ipv6Only?: boolean;
}
/**
* Represents the fastify instance created by the factory function the module exports.
*/
interface FastifyInstance<HttpServer = http.Server, HttpRequest = http.IncomingMessage, HttpResponse = http.ServerResponse> {
server: HttpServer
log: Logger
schemaCompiler: SchemaCompiler
/**
* Adds a route to the server
*/
route<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
opts: RouteOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a GET route with the given mount path, options, and handler
*/
get<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a GET route with the given mount path and handler
*/
get<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a PUT route with the given mount path, options, and handler
*/
put<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a PUT route with the given mount path and handler
*/
put<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a PATCH route with the given mount path, options, and handler
*/
patch<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a PATCH route with the given mount path and handler
*/
patch<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a POST route with the given mount path, options, and handler
*/
post<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a POST route with the given mount path and handler
*/
post<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a HEAD route with the given mount path, options, and handler
*/
head<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a HEAD route with the given mount path and handler
*/
head<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a DELETE route with the given mount path, options, and handler
*/
delete<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a DELETE route with the given mount path and handler
*/
delete<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a OPTIONS route with the given mount path, options, and handler
*/
options<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a OPTIONS route with the given mount path and handler
*/
options<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a route for all the supported methods with the given mount path, options, and handler
*/
all<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
opts: RouteShorthandOptions<HttpServer, HttpRequest, HttpResponse, Query, Params, Headers, Body>,
handler?: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Defines a route for all the supported methods with the given mount path and handler
*/
all<Query = DefaultQuery, Params = DefaultParams, Headers = DefaultHeaders, Body = DefaultBody>(
url: string,
handler: RequestHandler<HttpRequest, HttpResponse, Query, Params, Headers, Body>,
): 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(callback: (err: Error, address: string) => void): void
listen(port: number, callback: (err: Error, address: string) => void): void
listen(port: number, address: string, callback: (err: Error, address: string) => void): void
listen(port: number, address: string, backlog: number, callback: (err: Error, address: string) => void): void
listen(options: ListenOptions, callback: (err: Error, address: string) => void): void
listen(sockFile: string, callback: (err: Error, address: string) => void): void
listen(port: number, address?: string, backlog?: number): Promise<string>
listen(sockFile: string): Promise<string>
listen(options: ListenOptions): Promise<string>
/**
* Registers a listener function that is invoked when all the plugins have
* been loaded. It receives an error parameter if something went wrong.
*/
ready(): Promise<FastifyInstance<HttpServer, HttpRequest, HttpResponse>>
ready(readyListener: (err: Error) => void): void
ready(readyListener: (err: Error, done: Function) => void): void
ready(readyListener: (err: Error, context: FastifyInstance<HttpServer, HttpRequest, HttpResponse>, done: Function) => void): void
/**
* Call this function to close the server instance and run the "onClose" callback
*/
close(closeListener: () => void): void
close<T = any>(): Promise<T>
/**
* Apply the given middleware to all incoming requests
*/
use(middleware: Middleware<HttpServer, HttpRequest, HttpResponse>): void
/**
* Apply the given middleware to routes matching the given path
*/
use(path: string, middleware: Middleware<HttpServer, HttpRequest, HttpResponse>): void
/**
* Registers a plugin
*/
register<T extends RegisterOptions<HttpServer, HttpRequest, HttpResponse>>(plugin: Plugin<HttpServer, HttpRequest, HttpResponse, T>, opts?: T): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* `Register a callback that will be executed just after a register.
* It can take up to three parameters
*/
after(afterListener: (err: Error) => void): void
after(afterListener: (err: Error, done: Function) => void): void
after(afterListener: (err: Error, context: FastifyInstance<HttpServer, HttpRequest, HttpResponse>, done: Function) => void): void
/**
* 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>
/**
* Determines if the given named decorator is available
*/
hasDecorator(name: string): boolean
/**
* Determines if the given named request decorator is available
*/
hasRequestDecorator(name: string): boolean
/**
* Determines if the given named reply decorator is available
*/
hasReplyDecorator(name: string): boolean
/**
* Add a hook that is triggered when a request is initially received
*/
addHook(name: 'onRequest', hook: FastifyMiddleware<HttpServer, HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Add a hook that is triggered after the onRequest hook and middlewares, but before body parsing
*/
addHook(name: 'preParsing', hook: FastifyMiddleware<HttpServer, HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Add a hook that is triggered after the onRequest, middlewares, and body parsing, but before the validation
*/
addHook(name: 'preValidation', hook: FastifyMiddleware<HttpServer, HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Hook that is fired after a request is processed, but before the response is serialized
* hook
*/
addHook(name: 'preSerialization', hook: FastifyMiddlewareWithPayload<HttpServer, HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Hook that is fired before a request is processed, but after the "preValidation"
* hook
*/
addHook(name: 'preHandler', hook: FastifyMiddleware<HttpServer, HttpRequest, HttpResponse>): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Hook that is fired after a request is processed, but before the "onResponse"
* hook
*/
addHook(name: 'onSend', hook: (this: FastifyInstance<HttpServer, HttpRequest, HttpResponse>, req: FastifyRequest<HttpRequest>, reply: FastifyReply<HttpResponse>, payload: any, done: (err?: Error, value?: any) => void) => void): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Hook that is fired if `reply.send` is invoked with an Error
*/
addHook(name: 'onError', hook: (this: FastifyInstance<HttpServer, HttpRequest, HttpResponse>, req: FastifyRequest<HttpRequest>, reply: FastifyReply<HttpResponse>, error: FastifyError, done: () => 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: FastifyMiddleware<HttpServer, HttpRequest, HttpResponse>): 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>
/**
* Adds a hook that is 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 listeners do not get passed a callback.
*/
addHook(name: 'onRoute', hook: (opts: RouteOptions<HttpServer, HttpRequest, HttpResponse> & { path: string, prefix: string }) => void): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Adds a hook that is triggered when Fastify a new plugin is being registered.
* This hook can be useful if you are developing a plugin that needs to use the encapsulation functionality of Fastify.
* The interface is synchronous, and, as such, the listeners do not get passed a callback.
*/
addHook(name: 'onRegister', hook: (instance: FastifyInstance) => void): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Useful for testing http requests without running a sever
*/
inject(opts: HTTPInjectOptions | string, cb: (err: Error, res: HTTPInjectResponse) => void): void
/**
* Useful for testing http requests without running a sever
*/
inject(opts: HTTPInjectOptions | string): 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: FastifyError, request: FastifyRequest<HttpRequest>, reply: FastifyReply<HttpResponse>) => void): void
/**
* Set a function that will be called whenever an error happens
*/
setReplySerializer(handler: (payload: string | object | Buffer | NodeJS.ReadableStream, statusCode: number) => string): void
/**
* Set the schema compiler for all routes.
*/
setSchemaCompiler(schemaCompiler: SchemaCompiler): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Create a shared schema
*/
addSchema(schema: object): FastifyInstance<HttpServer, HttpRequest, HttpResponse>
/**
* Get all shared schemas
*/
getSchemas(): {[shemaId: string]: Object}
/**
* Add a content type parser
*/
addContentTypeParser(contentType: string | string[], opts: { bodyLimit?: number }, parser: ContentTypeParser<HttpRequest>): void
addContentTypeParser(contentType: string | string[], opts: { parseAs: 'string'; bodyLimit?: number }, parser: BodyParser<HttpRequest, string>): void
addContentTypeParser(contentType: string | string[], opts: { parseAs: 'buffer'; bodyLimit?: number }, parser: BodyParser<HttpRequest, Buffer>): void
addContentTypeParser(contentType: string | string[], parser: ContentTypeParser<HttpRequest>): void
/**
* Check if a parser for the specified content type exists
*/
hasContentTypeParser(contentType: string): boolean;
/**
* Prints the representation of the internal radix tree used by the router
*/
printRoutes(): string
}
}
export = fastify;