import { expectAssignable, expectError, expectType } from 'tsd' import fastify, { ContextConfigDefault, FastifyContextConfig, FastifyLogFn, FastifySchema, FastifyTypeProviderDefault, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerDefault, RequestBodyDefault, RequestGenericInterface, RouteHandler, RouteHandlerMethod, SafePromiseLike } from '../../fastify' import { FastifyInstance } from '../../types/instance' import { FastifyLoggerInstance } from '../../types/logger' import { FastifyReply } from '../../types/reply' import { FastifyRequest, RequestRouteOptions } from '../../types/request' import { FastifyRouteConfig, RouteGenericInterface } from '../../types/route' import { RequestHeadersDefault, RequestParamsDefault, RequestQuerystringDefault } from '../../types/utils' interface RequestBody { content: string; } interface RequestQuerystring { from: string; } interface RequestParams { id: number; } interface RequestHeaders { 'x-foobar': string; } interface RequestData extends RequestGenericInterface { Body: RequestBody; Querystring: RequestQuerystring; Params: RequestParams; Headers: RequestHeaders; } type Handler = RouteHandler type CustomRequest = FastifyRequest<{ Body: RequestBody | undefined; Querystring: RequestQuerystring; Params: RequestParams; Headers: RequestHeaders; }> type HTTPRequestPart = 'body' | 'query' | 'querystring' | 'params' | 'headers' type ExpectedGetValidationFunction = (input: { [key: string]: unknown }) => boolean interface CustomLoggerInterface extends FastifyLoggerInstance { foo: FastifyLogFn; // custom severity logger method } const getHandler: RouteHandler = function (request, _reply) { expectType(request.url) expectType(request.originalUrl) expectType(request.method) expectType>(request.routeOptions) expectType(request.is404) expectType(request.hostname) expectType(request.host) expectType(request.port) expectType(request.ip) expectType(request.ips) expectType(request.raw) expectType(request.body) expectType(request.params) expectType(request.routeOptions.config) expectType(request.routeOptions.schema) expectType(request.routeOptions.handler) expectType(request.routeOptions.url) expectType(request.headers) request.headers = {} expectType(request.query) expectType(request.id) expectType(request.log) expectType(request.socket) expectType(request.validationError) expectType(request.server) expectAssignable<(httpPart: HTTPRequestPart) => ExpectedGetValidationFunction>(request.getValidationFunction) expectAssignable<(schema: { [key: string]: unknown }) => ExpectedGetValidationFunction>(request.getValidationFunction) expectAssignable<(input: { [key: string]: unknown }, schema: { [key: string]: unknown }, httpPart?: HTTPRequestPart) => boolean>(request.validateInput) expectAssignable<(input: { [key: string]: unknown }, httpPart?: HTTPRequestPart) => boolean>(request.validateInput) } const getHandlerWithCustomLogger: RouteHandlerMethod = function (request, _reply) { expectType(request.log) } const postHandler: Handler = function (request) { expectType(request.body) expectType(request.params) expectType(request.headers) expectType(request.query) expectType(request.body.content) expectType(request.query.from) expectType(request.params.id) expectType(request.headers['x-foobar']) expectType(request.server) expectType(request.routeOptions.config) } function putHandler (request: CustomRequest, reply: FastifyReply) { expectType(request.body) expectType(request.params) expectType(request.headers) expectType(request.query) if (request.body === undefined) { expectType(request.body) } else { expectType(request.body.content) } expectType(request.query.from) expectType(request.params.id) expectType(request.headers['x-foobar']) expectType(request.server) expectType(request.routeOptions.config) } const server = fastify() server.get('/get', getHandler) server.post('/post', postHandler) server.put('/put', putHandler) const customLogger: CustomLoggerInterface = { level: 'info', silent: () => { }, info: () => { }, warn: () => { }, error: () => { }, fatal: () => { }, trace: () => { }, debug: () => { }, foo: () => { }, // custom severity logger method child: () => customLogger } const serverWithCustomLogger = fastify({ loggerInstance: customLogger }) expectError< FastifyInstance & Promise> >(serverWithCustomLogger) expectAssignable< FastifyInstance & PromiseLike> >(serverWithCustomLogger) expectType< FastifyInstance & SafePromiseLike> >(serverWithCustomLogger) serverWithCustomLogger.get('/get', getHandlerWithCustomLogger)