UNPKG

7.81 kBTypeScriptView Raw
1import { FastifyInstance } from './instance'
2import { FastifyRequest, RequestGenericInterface } from './request'
3import { FastifyReply, ReplyGenericInterface } from './reply'
4import { FastifySchema, FastifySchemaCompiler } from './schema'
5import { HTTPMethods, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, ContextConfigDefault } from './utils'
6import { LogLevel } from './logger'
7import { preValidationHookHandler, preHandlerHookHandler, preSerializationHookHandler, onRequestHookHandler, preParsingHookHandler, onResponseHookHandler, onSendHookHandler, onErrorHookHandler } from './hooks'
8
9export interface RouteGenericInterface extends RequestGenericInterface, ReplyGenericInterface {}
10
11/**
12 * Fastify Router Shorthand method type that is similar to the Express/Restify approach
13 */
14export interface RouteShorthandMethod<
15 RawServer extends RawServerBase = RawServerDefault,
16 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
17 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
18> {
19 <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault>(
20 path: string,
21 opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>,
22 handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
23 ): FastifyInstance<RawServer, RawRequest, RawReply>;
24}
25
26/**
27 * Fastify Router Shorthand method type that is similar to the Express/Restify approach
28 */
29export interface RouteShorthandMethod<
30 RawServer extends RawServerBase = RawServerDefault,
31 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
32 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
33> {
34 <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault>(
35 path: string,
36 handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
37 ): FastifyInstance<RawServer, RawRequest, RawReply>;
38}
39
40/**
41 * Fastify Router Shorthand method type that is similar to the Express/Restify approach
42 */
43export interface RouteShorthandMethod<
44 RawServer extends RawServerBase = RawServerDefault,
45 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
46 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
47> {
48 <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault>(
49 path: string,
50 opts: RouteShorthandOptionsWithHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
51 ): FastifyInstance<RawServer, RawRequest, RawReply>;
52}
53
54/**
55 * Route shorthand options for the various shorthand methods
56 */
57export interface RouteShorthandOptions<
58 RawServer extends RawServerBase = RawServerDefault,
59 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
60 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
61 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
62 ContextConfig = ContextConfigDefault
63> {
64 schema?: FastifySchema;
65 attachValidation?: boolean;
66 validatorCompiler?: FastifySchemaCompiler;
67 serializerCompiler?: FastifySchemaCompiler;
68 bodyLimit?: number;
69 logLevel?: LogLevel;
70 config?: ContextConfig;
71 version?: string;
72 prefixTrailingSlash?: boolean;
73
74 // hooks
75 onRequest?: onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | onRequestHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
76 preParsing?: preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | preParsingHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
77 preValidation?: preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | preValidationHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
78 preHandler?: preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | preHandlerHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
79 preSerialization?: preSerializationHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | preSerializationHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
80 onSend?: onSendHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | onSendHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
81 onResponse?: onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | onResponseHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
82 onError?: onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> | onErrorHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>[];
83}
84
85/**
86 * Fastify route method options.
87 */
88export interface RouteOptions<
89 RawServer extends RawServerBase = RawServerDefault,
90 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
91 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
92 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
93 ContextConfig = ContextConfigDefault
94> extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> {
95 method: HTTPMethods | HTTPMethods[];
96 url: string;
97 handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
98}
99
100/**
101 * Shorthand options including the handler function property
102 */
103export interface RouteShorthandOptionsWithHandler<
104 RawServer extends RawServerBase = RawServerDefault,
105 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
106 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
107 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
108 ContextConfig = ContextConfigDefault
109> extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig> {
110 handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>;
111}
112
113/**
114 * Route handler method declaration.
115 */
116export type RouteHandlerMethod<
117 RawServer extends RawServerBase = RawServerDefault,
118 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
119 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
120 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
121 ContextConfig = ContextConfigDefault
122> = (
123 this: FastifyInstance<RawServer, RawRequest, RawReply>,
124 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
125 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
126) => void | Promise<any>
127
128export type RouteHandler<
129 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
130 RawServer extends RawServerBase = RawServerDefault,
131 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
132 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
133 ContextConfig = ContextConfigDefault
134> = (
135 this: FastifyInstance<RawServer, RawRequest, RawReply>,
136 request: FastifyRequest<RouteGeneric, RawServer, RawRequest>,
137 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig>
138) => void | Promise<any>