Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typescript): route config should not pass url and method #4872

Merged
merged 2 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
129 changes: 65 additions & 64 deletions test/types/hooks.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { FastifyError } from '@fastify/error'
import { expectAssignable, expectError, expectType } from 'tsd'
import fastify, {
ContextConfigDefault, FastifyContextConfig,
FastifyInstance,
FastifyPluginOptions,
FastifyReply,
FastifyRequest,
FastifySchema,
FastifyTypeProviderDefault,
RawReplyDefaultExpression,
RawRequestDefaultExpression,
RouteOptions,
RawServerDefault,
RegisterOptions,
FastifyPluginOptions,
FastifySchema,
FastifyTypeProviderDefault,
ContextConfigDefault, FastifyContextConfig, RawServerDefault
RouteOptions
} from '../../fastify'
import { preHandlerAsyncHookHandler, RequestPayload } from '../../types/hooks'
import { RouteGenericInterface } from '../../types/route'
import { ResolveFastifyRequestType } from '../../types/type-provider'
import { RequestPayload, preHandlerAsyncHookHandler } from '../../types/hooks'
import { FastifyRouteConfig, RouteGenericInterface } from '../../types/route'

const server = fastify()

Expand Down Expand Up @@ -252,89 +252,90 @@ type CustomContextConfig = FastifyContextConfig & {
foo: string;
bar: number;
}
type CustomContextConfigWithDefault = CustomContextConfig & FastifyRouteConfig

server.route<RouteGenericInterface, CustomContextConfig>({
method: 'GET',
url: '/',
handler: () => { },
onRequest: (request, reply, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
preParsing: (request, reply, payload, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
preValidation: (request, reply, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
preHandler: (request, reply, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
preSerialization: (request, reply, payload, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
onSend: (request, reply, payload, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
onResponse: (request, reply, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
onTimeout: (request, reply, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
onError: (request, reply, error, done) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
}
})

server.get<RouteGenericInterface, CustomContextConfig>('/', {
onRequest: async (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
preParsing: async (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
preValidation: async (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
preHandler: async (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
preSerialization: async (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
onSend: async (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
onResponse: async (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
onTimeout: async (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
onError: async (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
}
}, async (request, reply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
})

type CustomContextRequest = FastifyRequest<any, any, any, any, any, CustomContextConfig, any>
Expand All @@ -344,40 +345,40 @@ server.route<RouteGenericInterface, CustomContextConfig>({
url: '/',
handler: () => { },
onRequest: async (request: CustomContextRequest, reply: CustomContextReply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
preParsing: async (request: CustomContextRequest, reply: CustomContextReply, payload: RequestPayload) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
preValidation: async (request: CustomContextRequest, reply: CustomContextReply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
preHandler: async (request: CustomContextRequest, reply: CustomContextReply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
preSerialization: async (request: CustomContextRequest, reply: CustomContextReply, payload: any) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
onSend: async (request: CustomContextRequest, reply: CustomContextReply, payload: any) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
onResponse: async (request: CustomContextRequest, reply: CustomContextReply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
onTimeout: async (request: CustomContextRequest, reply: CustomContextReply) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
},
onError: async (request: CustomContextRequest, reply: CustomContextReply, error: FastifyError) => {
expectType<CustomContextConfig>(request.context.config)
expectType<CustomContextConfig>(reply.context.config)
expectType<CustomContextConfigWithDefault>(request.context.config)
expectType<CustomContextConfigWithDefault>(reply.context.config)
}
})

Expand Down
14 changes: 7 additions & 7 deletions test/types/reply.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { expectType, expectError, expectAssignable } from 'tsd'
import fastify, { RouteHandlerMethod, RouteHandler, RawRequestDefaultExpression, FastifyContext, FastifyContextConfig, FastifyRequest, FastifyReply, FastifySchema, FastifyTypeProviderDefault } from '../../fastify'
import { RawServerDefault, RawReplyDefaultExpression, ContextConfigDefault } from '../../types/utils'
import { FastifyLoggerInstance } from '../../types/logger'
import { RouteGenericInterface } from '../../types/route'
import { FastifyInstance } from '../../types/instance'
import { Buffer } from 'buffer'
import { expectAssignable, expectError, expectType } from 'tsd'
import fastify, { FastifyContext, FastifyReply, FastifyRequest, FastifySchema, FastifyTypeProviderDefault, RawRequestDefaultExpression, RouteHandler, RouteHandlerMethod } from '../../fastify'
import { FastifyInstance } from '../../types/instance'
import { FastifyLoggerInstance } from '../../types/logger'
import { ReplyTypeConstrainer } from '../../types/reply'
import { RouteGenericInterface } from '../../types/route'
import { ContextConfigDefault, RawReplyDefaultExpression, RawServerDefault } from '../../types/utils'

type DefaultSerializationFunction = (payload: { [key: string]: unknown }) => string
type DefaultFastifyReplyWithCode<Code extends number> = FastifyReply<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, RouteGenericInterface, ContextConfigDefault, FastifySchema, FastifyTypeProviderDefault, ReplyTypeConstrainer<RouteGenericInterface['Reply'], Code>>

const getHandler: RouteHandlerMethod = function (_request, reply) {
expectType<RawReplyDefaultExpression>(reply.raw)
expectType<FastifyContext<ContextConfigDefault>>(reply.context)
expectType<FastifyContextConfig>(reply.context.config)
expectType<FastifyContext<ContextConfigDefault>['config']>(reply.context.config)
expectType<FastifyLoggerInstance>(reply.log)
expectType<FastifyRequest<RouteGenericInterface, RawServerDefault, RawRequestDefaultExpression>>(reply.request)
expectType<<Code extends number>(statusCode: Code) => DefaultFastifyReplyWithCode<Code>>(reply.code)
Expand Down
35 changes: 17 additions & 18 deletions test/types/request.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import { expectAssignable, expectType } from 'tsd'
import pino from 'pino'
import { expectAssignable, expectType } from 'tsd'
import fastify, {
RouteHandler,
RawRequestDefaultExpression,
RequestBodyDefault,
RequestGenericInterface,
FastifyContext,
ContextConfigDefault,
FastifyContextConfig,
FastifyContext,
FastifyLogFn,
RouteHandlerMethod,
RawServerDefault,
RawReplyDefaultExpression,
FastifySchema,
FastifyTypeProviderDefault
FastifyTypeProviderDefault,
RawReplyDefaultExpression,
RawRequestDefaultExpression,
RawServerDefault,
RequestBodyDefault,
RequestGenericInterface,
RouteHandler,
RouteHandlerMethod
} from '../../fastify'
import { RequestParamsDefault, RequestHeadersDefault, RequestQuerystringDefault } from '../../types/utils'
import { FastifyInstance } from '../../types/instance'
import { FastifyLoggerInstance } from '../../types/logger'
import { FastifyRequest, RequestRouteOptions } from '../../types/request'
import { FastifyReply } from '../../types/reply'
import { FastifyInstance } from '../../types/instance'
import { FastifyRequest, RequestRouteOptions } from '../../types/request'
import { RouteGenericInterface } from '../../types/route'
import { RequestHeadersDefault, RequestParamsDefault, RequestQuerystringDefault } from '../../types/utils'

interface RequestBody {
content: string;
Expand Down Expand Up @@ -76,8 +75,8 @@ const getHandler: RouteHandler = function (request, _reply) {
expectType<RequestBodyDefault>(request.body)
expectType<RequestParamsDefault>(request.params)
expectType<FastifyContext<ContextConfigDefault>>(request.context)
expectType<FastifyContextConfig>(request.context.config)
expectType<FastifyContextConfig>(request.routeConfig)
expectType<FastifyContext<ContextConfigDefault>['config']>(request.context.config)
expectType<FastifyContext<ContextConfigDefault>['config']>(request.routeConfig)
expectType<FastifySchema>(request.routeSchema)

expectType<RequestHeadersDefault & RawRequestDefaultExpression['headers']>(request.headers)
Expand Down Expand Up @@ -110,7 +109,7 @@ const postHandler: Handler = function (request) {
expectType<string>(request.headers['x-foobar'])
expectType<FastifyInstance>(request.server)
expectType<FastifyContext<ContextConfigDefault>>(request.context)
expectType<FastifyContextConfig>(request.context.config)
expectType<FastifyContext<ContextConfigDefault>['config']>(request.context.config)
}

function putHandler (request: CustomRequest, reply: FastifyReply) {
Expand All @@ -128,7 +127,7 @@ function putHandler (request: CustomRequest, reply: FastifyReply) {
expectType<string>(request.headers['x-foobar'])
expectType<FastifyInstance>(request.server)
expectType<FastifyContext<ContextConfigDefault>>(request.context)
expectType<FastifyContextConfig>(request.context.config)
expectType<FastifyContext<ContextConfigDefault>['config']>(request.context.config)
}

const server = fastify()
Expand Down
14 changes: 7 additions & 7 deletions test/types/route.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import fastify, { FastifyInstance, FastifyRequest, FastifyReply, RouteHandlerMethod } from '../../fastify'
import { expectType, expectError, expectAssignable, printType } from 'tsd'
import { HTTPMethods } from '../../types/utils'
import { FastifyError } from '@fastify/error'
import * as http from 'http'
import { expectAssignable, expectError, expectType } from 'tsd'
import fastify, { FastifyInstance, FastifyReply, FastifyRequest, RouteHandlerMethod } from '../../fastify'
import { RequestPayload } from '../../types/hooks'
import { FastifyError } from '@fastify/error'
import { HTTPMethods } from '../../types/utils'

/*
* Testing Fastify HTTP Routes and Route Shorthands.
Expand Down Expand Up @@ -74,7 +74,7 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
Headers: HeadersInterface;
}

fastify()[lowerCaseMethod]<RouteGeneric, RouteSpecificContextConfigType>('/', { config: { foo: 'bar', bar: 100, extra: true, url: '/', method: lowerCaseMethod } }, (req, res) => {
fastify()[lowerCaseMethod]<RouteGeneric, RouteSpecificContextConfigType>('/', { config: { foo: 'bar', bar: 100, extra: true } }, (req, res) => {
expectType<BodyInterface>(req.body)
expectType<QuerystringInterface>(req.query)
expectType<ParamsInterface>(req.params)
Expand All @@ -94,7 +94,7 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
fastify().route<RouteGeneric>({
url: '/',
method: method as HTTPMethods,
config: { foo: 'bar', bar: 100, url: '/', method: method as HTTPMethods },
config: { foo: 'bar', bar: 100 },
prefixTrailingSlash: 'slash',
onRequest: (req, res, done) => { // these handlers are tested in `hooks.test-d.ts`
expectType<BodyInterface>(req.body)
Expand Down Expand Up @@ -231,7 +231,7 @@ type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete'
fastify().route<RouteGeneric>({
url: '/',
method: method as HTTPMethods,
config: { foo: 'bar', bar: 100, url: '/', method: method as HTTPMethods },
config: { foo: 'bar', bar: 100 },
prefixTrailingSlash: 'slash',
onRequest: async (req, res, done) => { // these handlers are tested in `hooks.test-d.ts`
expectType<BodyInterface>(req.body)
Expand Down
6 changes: 3 additions & 3 deletions types/context.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ContextConfigDefault } from './utils'
import { FastifyRouteConfig } from './route'
import { ContextConfigDefault } from './utils'

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface FastifyContextConfig extends FastifyRouteConfig {
export interface FastifyContextConfig {
}

/**
Expand All @@ -12,5 +12,5 @@ export interface FastifyContext<ContextConfig = ContextConfigDefault> {
/**
* @deprecated Use Request#routeConfig or Request#routeSchema instead
*/
config: FastifyContextConfig & ContextConfig;
config: FastifyContextConfig & FastifyRouteConfig & ContextConfig;
}
2 changes: 1 addition & 1 deletion types/reply.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FastifyRequest } from './request'
import { RouteGenericInterface } from './route'
import { FastifySchema } from './schema'
import { FastifyReplyType, FastifyTypeProvider, FastifyTypeProviderDefault, ResolveFastifyReplyType } from './type-provider'
import { CodeToReplyKey, ContextConfigDefault, ReplyKeysToCodes, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault, ReplyDefault } from './utils'
import { CodeToReplyKey, ContextConfigDefault, RawReplyDefaultExpression, RawRequestDefaultExpression, RawServerBase, RawServerDefault, ReplyDefault, ReplyKeysToCodes } from './utils'

export interface ReplyGenericInterface {
Reply?: ReplyDefault;
Expand Down