Skip to content

Commit

Permalink
fix: add type checking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sf3ris committed Dec 27, 2023
1 parent 19ec132 commit a06ad26
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion fastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function fastify (options) {
hasRoute: function _route (options) {
return router.hasRoute.call(this, { options })
},
findRoute: function _route (options) {
findRoute: function _findRoute (options) {
return router.findRoute(options)
},
// expose logger instance
Expand Down
11 changes: 10 additions & 1 deletion test/types/route.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ 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 { HTTPMethods } from '../../types/utils'
import { HTTPMethods, RawServerBase, RawServerDefault } from '../../types/utils'
import { FindResult, HTTPVersion } from 'find-my-way'
import { FindMyWayFindResult, FindMyWayVersion } from '../../types/instance'

/*
* Testing Fastify HTTP Routes and Route Shorthands.
Expand Down Expand Up @@ -453,6 +455,13 @@ expectType<boolean>(fastify().hasRoute({
}
}))

expectType<FindMyWayFindResult<RawServerDefault>>(
fastify().findRoute({
url: '/',
method: 'get'
})
)

expectType<FastifyInstance>(fastify().route({
url: '/',
method: 'get',
Expand Down
3 changes: 2 additions & 1 deletion types/instance.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export interface FastifyListenOptions {

type NotInInterface<Key, _Interface> = Key extends keyof _Interface ? never : Key
type FindMyWayVersion<RawServer extends RawServerBase> = RawServer extends http.Server ? HTTPVersion.V1 : HTTPVersion.V2
type FindMyWayFindResult<RawServer extends RawServerBase> = FindResult<FindMyWayVersion<RawServer>>

type GetterSetter<This, T> = T | {
getter: (this: This) => T,
Expand Down Expand Up @@ -223,7 +224,7 @@ export interface FastifyInstance<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault,
SchemaCompiler extends FastifySchema = FastifySchema,
>(opts: Pick<RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>, 'method' | 'url' | 'constraints'>): FindResult<FindMyWayVersion<RawServer>>;
>(opts: Pick<RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>, 'method' | 'url' | 'constraints'>): FindMyWayFindResult<RawServer>;

// addHook: overloads

Expand Down

0 comments on commit a06ad26

Please sign in to comment.