import { FastifyError } from '@fastify/error' import { ConstraintStrategy } from 'find-my-way' import { FastifyContextConfig } from './context' import { onErrorHookHandler, onRequestAbortHookHandler, onRequestHookHandler, onResponseHookHandler, onSendHookHandler, onTimeoutHookHandler, preHandlerHookHandler, preParsingHookHandler, preSerializationHookHandler, preValidationHookHandler } from './hooks' import { FastifyInstance } from './instance' import { FastifyBaseLogger, FastifyChildLoggerFactory, LogLevel } from './logger' import { FastifyReply, ReplyGenericInterface } from './reply' import { FastifyRequest, RequestGenericInterface } from './request' import { FastifySchema, FastifySchemaCompiler, FastifySerializerCompiler, SchemaErrorFormatter } from './schema' import { FastifyTypeProvider, FastifyTypeProviderDefault, ResolveFastifyReplyReturnType } from './type-provider' import { ContextConfigDefault, HTTPMethods, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault } from './utils' export interface FastifyRouteConfig { url: string; method: HTTPMethods | HTTPMethods[]; } export interface RouteGenericInterface extends RequestGenericInterface, ReplyGenericInterface { } export type RouteConstraintType = Omit, 'deriveConstraint'> & { deriveConstraint(req: RawRequestDefaultExpression, ctx?: Context, done?: (err: Error, ...args: any) => any): any, } export interface RouteConstraint { version?: string host?: RegExp | string [name: string]: unknown } /** * Route shorthand options for the various shorthand methods */ type RouteShorthandHook any> = (...args: Parameters) => void | Promise export interface RouteShorthandOptions< RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler extends FastifySchema = FastifySchema, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, Logger extends FastifyBaseLogger = FastifyBaseLogger > { schema?: SchemaCompiler, // originally FastifySchema attachValidation?: boolean; exposeHeadRoute?: boolean; validatorCompiler?: FastifySchemaCompiler>; serializerCompiler?: FastifySerializerCompiler>; bodyLimit?: number; handlerTimeout?: number; logLevel?: LogLevel; config?: FastifyContextConfig & ContextConfig; constraints?: RouteConstraint, prefixTrailingSlash?: 'slash' | 'no-slash' | 'both'; errorHandler?: ( this: FastifyInstance, error: FastifyError, request: FastifyRequest, TypeProvider, ContextConfig, Logger>, reply: FastifyReply, TypeProvider> ) => void; childLoggerFactory?: FastifyChildLoggerFactory; schemaErrorFormatter?: SchemaErrorFormatter; // hooks onRequest?: RouteShorthandHook, TypeProvider, Logger>> | RouteShorthandHook, TypeProvider, Logger>>[]; preParsing?: RouteShorthandHook, TypeProvider, Logger>> | RouteShorthandHook, TypeProvider, Logger>>[]; preValidation?: RouteShorthandHook, TypeProvider, Logger>> | RouteShorthandHook, TypeProvider, Logger>>[]; preHandler?: RouteShorthandHook, TypeProvider, Logger>> | RouteShorthandHook, TypeProvider, Logger>>[]; preSerialization?: RouteShorthandHook, TypeProvider, Logger>> | RouteShorthandHook, TypeProvider, Logger>>[]; onSend?: RouteShorthandHook, TypeProvider, Logger>> | RouteShorthandHook, TypeProvider, Logger>>[]; onResponse?: RouteShorthandHook, TypeProvider, Logger>> | RouteShorthandHook, TypeProvider, Logger>>[]; onTimeout?: RouteShorthandHook, TypeProvider, Logger>> | RouteShorthandHook, TypeProvider, Logger>>[]; onError?: RouteShorthandHook, TypeProvider, Logger>> | RouteShorthandHook, TypeProvider, Logger>>[]; onRequestAbort?: RouteShorthandHook, TypeProvider, Logger>> | RouteShorthandHook, TypeProvider, Logger>>[]; } /** * Route handler method declaration. */ export type RouteHandlerMethod< RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler extends FastifySchema = FastifySchema, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, Logger extends FastifyBaseLogger = FastifyBaseLogger > = ( this: FastifyInstance, request: FastifyRequest, reply: FastifyReply // This return type used to be a generic type argument. Due to TypeScript's inference of return types, this rendered returns unchecked. ) => ResolveFastifyReplyReturnType /** * Shorthand options including the handler function property */ export interface RouteShorthandOptionsWithHandler< RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler extends FastifySchema = FastifySchema, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, Logger extends FastifyBaseLogger = FastifyBaseLogger > extends RouteShorthandOptions { handler: RouteHandlerMethod, TypeProvider, Logger>; } /** * Fastify Router Shorthand method type that is similar to the Express/Restify approach */ export interface RouteShorthandMethod< RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, Logger extends FastifyBaseLogger = FastifyBaseLogger > { ( path: string, opts: RouteShorthandOptions, handler: RouteHandlerMethod ): FastifyInstance; ( path: string, handler: RouteHandlerMethod ): FastifyInstance; ( path: string, opts: RouteShorthandOptionsWithHandler ): FastifyInstance; } /** * Fastify route method options. */ export interface RouteOptions< RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler extends FastifySchema = FastifySchema, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, Logger extends FastifyBaseLogger = FastifyBaseLogger > extends RouteShorthandOptions { method: HTTPMethods | HTTPMethods[]; url: string; handler: RouteHandlerMethod; } export type RouteHandler< RouteGeneric extends RouteGenericInterface = RouteGenericInterface, RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, ContextConfig = ContextConfigDefault, SchemaCompiler extends FastifySchema = FastifySchema, TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, Logger extends FastifyBaseLogger = FastifyBaseLogger > = ( this: FastifyInstance, request: FastifyRequest, reply: FastifyReply ) => RouteGeneric['Reply'] | void | Promise export type DefaultRoute = ( req: Request, res: Reply, ) => void