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' ;
@@ -19,12 +20,12 @@ app.register(wsPlugin, {
1920} ) ;
2021app . register ( wsPlugin , { options : { perMessageDeflate : true } } ) ;
2122
22- app . get ( '/websockets-via-inferrence' , { websocket : true } , async function ( connection , request ) {
23- expectType < FastifyInstance > ( this ) ;
24- expectType < SocketStream > ( connection ) ;
25- expectType < Server > ( app . websocketServer ) ;
26- expectType < FastifyRequest < RequestGenericInterface > > ( request )
27- } ) ;
23+ app . get ( '/websockets-via-inferrence' , { websocket : true } , async function ( connection , request ) {
24+ expectType < FastifyInstance > ( this ) ;
25+ expectType < SocketStream > ( connection ) ;
26+ expectType < Server > ( app . websocketServer ) ;
27+ expectType < FastifyRequest < RequestGenericInterface > > ( request )
28+ } ) ;
2829
2930const handler : WebsocketHandler = async ( connection , request ) => {
3031 expectType < SocketStream > ( connection ) ;
@@ -70,3 +71,33 @@ const augmentedRouteOptions: RouteOptions = {
7071 } ,
7172} ;
7273app . 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