11import wsPlugin , { WebsocketHandler , SocketStream } from '../..' ;
2+ import type { IncomingMessage } from "http" ;
23import fastify , { RouteOptions , FastifyRequest , FastifyInstance , FastifyReply , RequestGenericInterface } from 'fastify' ;
34import { expectType } from 'tsd' ;
45import { Server } from 'ws' ;
6+ import { RouteGenericInterface } from 'fastify/types/route' ;
57
68const app : FastifyInstance = fastify ( ) ;
79app . register ( wsPlugin ) ;
@@ -18,7 +20,7 @@ app.register(wsPlugin, {
1820} ) ;
1921app . register ( wsPlugin , { options : { perMessageDeflate : true } } ) ;
2022
21- app . get ( '/websockets-via-inferrence' , { websocket : true } , async function ( connection , request ) {
23+ app . get ( '/websockets-via-inferrence' , { websocket : true } , async function ( connection , request ) {
2224 expectType < FastifyInstance > ( this ) ;
2325 expectType < SocketStream > ( connection ) ;
2426 expectType < Server > ( app . websocketServer ) ;
@@ -52,7 +54,7 @@ app.route({
5254 } ,
5355 wsHandler : ( connection , request ) => {
5456 expectType < SocketStream > ( connection ) ;
55- expectType < FastifyRequest < RequestGenericInterface > > ( request ) ;
57+ expectType < FastifyRequest < RouteGenericInterface > > ( request ) ;
5658 } ,
5759} ) ;
5860
@@ -65,7 +67,37 @@ const augmentedRouteOptions: RouteOptions = {
6567 } ,
6668 wsHandler : ( connection , request ) => {
6769 expectType < SocketStream > ( connection ) ;
68- expectType < FastifyRequest < RequestGenericInterface > > ( request )
70+ expectType < FastifyRequest < RouteGenericInterface > > ( request )
6971 } ,
7072} ;
7173app . route ( augmentedRouteOptions ) ;
74+
75+
76+ app . get < { Params : { foo : string } , Body : { bar : string } , Querystring : { search : string } , Headers : { auth : string } } > ( '/shorthand-explicit-types' , {
77+ websocket : true
78+ } , async ( connection , request ) => {
79+ expectType < SocketStream > ( connection ) ;
80+ expectType < { foo : string } > ( request . params ) ;
81+ expectType < { bar : string } > ( request . body ) ;
82+ expectType < { search : string } > ( request . query ) ;
83+ expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers ) ;
84+ } ) ;
85+
86+
87+ app . route < { Params : { foo : string } , Body : { bar : string } , Querystring : { search : string } , Headers : { auth : string } } > ( {
88+ method : 'GET' ,
89+ url : '/longhand-explicit-types' ,
90+ handler : ( request , _reply ) => {
91+ expectType < { foo : string } > ( request . params ) ;
92+ expectType < { bar : string } > ( request . body ) ;
93+ expectType < { search : string } > ( request . query ) ;
94+ expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers ) ;
95+ } ,
96+ wsHandler : ( connection , request ) => {
97+ expectType < SocketStream > ( connection ) ;
98+ expectType < { foo : string } > ( request . params ) ;
99+ expectType < { bar : string } > ( request . body ) ;
100+ expectType < { search : string } > ( request . query ) ;
101+ expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers ) ;
102+ } ,
103+ } ) ;
0 commit comments