UNPKG

12.4 kBTypeScriptView Raw
1import { FastifyError } from '@fastify/error'
2import { ConstraintStrategy } from 'find-my-way'
3import { FastifyRequestContext } from './context'
4import { onErrorMetaHookHandler, onRequestAbortMetaHookHandler, onRequestMetaHookHandler, onResponseMetaHookHandler, onSendMetaHookHandler, onTimeoutMetaHookHandler, preHandlerMetaHookHandler, preParsingMetaHookHandler, preSerializationMetaHookHandler, preValidationMetaHookHandler } from './hooks'
5import { FastifyInstance } from './instance'
6import { FastifyBaseLogger, FastifyChildLoggerFactory, LogLevel } from './logger'
7import { FastifyReply, ReplyGenericInterface } from './reply'
8import { FastifyRequest, RequestGenericInterface } from './request'
9import { FastifySchema, FastifySchemaCompiler, FastifySerializerCompiler, SchemaErrorFormatter } from './schema'
10import {
11 FastifyTypeProvider,
12 FastifyTypeProviderDefault,
13 ResolveFastifyReplyReturnType
14} from './type-provider'
15import { ContextConfigDefault, HTTPMethods, NoInferCompat, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault } from './utils'
16
17export interface FastifyRouteConfig {
18 url: string;
19 method: HTTPMethods | HTTPMethods[];
20}
21
22export interface RouteGenericInterface extends RequestGenericInterface, ReplyGenericInterface {}
23
24export type RouteConstraintType = Omit<ConstraintStrategy<any>, 'deriveConstraint'> & {
25 deriveConstraint<Context>(req: RawRequestDefaultExpression<RawServerDefault>, ctx?: Context, done?: (err: Error, ...args: any) => any): any,
26}
27
28export interface RouteConstraint {
29 version?: string
30 host?: RegExp | string
31 [name: string]: unknown
32}
33
34/**
35 * Route shorthand options for the various shorthand methods
36 */
37export interface RouteShorthandOptions<
38 RawServer extends RawServerBase = RawServerDefault,
39 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
40 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
41 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
42 ContextConfig = ContextConfigDefault,
43 SchemaCompiler extends FastifySchema = FastifySchema,
44 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
45 Logger extends FastifyBaseLogger = FastifyBaseLogger
46> {
47 schema?: SchemaCompiler, // originally FastifySchema
48 attachValidation?: boolean;
49 exposeHeadRoute?: boolean;
50
51 validatorCompiler?: FastifySchemaCompiler<NoInferCompat<SchemaCompiler>>;
52 serializerCompiler?: FastifySerializerCompiler<NoInferCompat<SchemaCompiler>>;
53 bodyLimit?: number;
54 logLevel?: LogLevel;
55 config?: Omit<FastifyRequestContext<ContextConfig>['config'], 'url' | 'method'>;
56 version?: string;
57 constraints?: RouteConstraint,
58 prefixTrailingSlash?: 'slash'|'no-slash'|'both';
59 errorHandler?: (
60 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
61 error: FastifyError,
62 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, NoInferCompat<SchemaCompiler>, TypeProvider, ContextConfig, Logger>,
63 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider>
64 ) => void;
65 childLoggerFactory?: FastifyChildLoggerFactory<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
66 schemaErrorFormatter?: SchemaErrorFormatter;
67
68 // hooks
69 onRequest?: onRequestMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>
70 | onRequestMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>[];
71 preParsing?: preParsingMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>
72 | preParsingMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>[];
73 preValidation?: preValidationMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>
74 | preValidationMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>[];
75 preHandler?: preHandlerMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>
76 | preHandlerMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>[];
77 preSerialization?: preSerializationMetaHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>
78 | preSerializationMetaHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>[];
79 onSend?: onSendMetaHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>
80 | onSendMetaHookHandler<unknown, RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>[];
81 onResponse?: onResponseMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>
82 | onResponseMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>[];
83 onTimeout?: onTimeoutMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>
84 | onTimeoutMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>[];
85 onError?: onErrorMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>
86 | onErrorMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, FastifyError, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>[];
87 onRequestAbort?: onRequestAbortMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>
88 | onRequestAbortMetaHookHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>[];
89}
90/**
91 * Route handler method declaration.
92 */
93export type RouteHandlerMethod<
94 RawServer extends RawServerBase = RawServerDefault,
95 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
96 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
97 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
98 ContextConfig = ContextConfigDefault,
99 SchemaCompiler extends FastifySchema = FastifySchema,
100 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
101 Logger extends FastifyBaseLogger = FastifyBaseLogger
102> = (
103 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
104 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
105 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
106// This return type used to be a generic type argument. Due to TypeScript's inference of return types, this rendered returns unchecked.
107) => ResolveFastifyReplyReturnType<TypeProvider, SchemaCompiler, RouteGeneric>
108
109/**
110 * Shorthand options including the handler function property
111 */
112export interface RouteShorthandOptionsWithHandler<
113 RawServer extends RawServerBase = RawServerDefault,
114 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
115 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
116 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
117 ContextConfig = ContextConfigDefault,
118 SchemaCompiler extends FastifySchema = FastifySchema,
119 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
120 Logger extends FastifyBaseLogger = FastifyBaseLogger
121> extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> {
122 handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>;
123}
124
125/**
126 * Fastify Router Shorthand method type that is similar to the Express/Restify approach
127 */
128export interface RouteShorthandMethod<
129 RawServer extends RawServerBase = RawServerDefault,
130 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
131 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
132 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
133 Logger extends FastifyBaseLogger = FastifyBaseLogger
134> {
135 <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, const SchemaCompiler extends FastifySchema = FastifySchema>(
136 path: string,
137 opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>,
138 handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
139 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
140 <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, const SchemaCompiler extends FastifySchema = FastifySchema>(
141 path: string,
142 handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
143 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
144 <RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, const SchemaCompiler extends FastifySchema = FastifySchema>(
145 path: string,
146 opts: RouteShorthandOptionsWithHandler<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
147 ): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
148}
149
150/**
151 * Fastify route method options.
152 */
153export interface RouteOptions<
154 RawServer extends RawServerBase = RawServerDefault,
155 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
156 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
157 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
158 ContextConfig = ContextConfigDefault,
159 SchemaCompiler extends FastifySchema = FastifySchema,
160 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
161 Logger extends FastifyBaseLogger = FastifyBaseLogger
162> extends RouteShorthandOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> {
163 method: HTTPMethods | HTTPMethods[];
164 url: string;
165 handler: RouteHandlerMethod<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, NoInferCompat<SchemaCompiler>, TypeProvider, Logger>;
166}
167
168export type RouteHandler<
169 RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
170 RawServer extends RawServerBase = RawServerDefault,
171 RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,
172 RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>,
173 ContextConfig = ContextConfigDefault,
174 SchemaCompiler extends FastifySchema = FastifySchema,
175 TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault,
176 Logger extends FastifyBaseLogger = FastifyBaseLogger
177> = (
178 this: FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>,
179 request: FastifyRequest<RouteGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger>,
180 reply: FastifyReply<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>
181) => RouteGeneric['Reply'] | void | Promise<RouteGeneric['Reply'] | void>
182
183export type DefaultRoute<Request, Reply> = (
184 req: Request,
185 res: Reply,
186) => void;