1- import fastifyWebsocket , { WebsocketHandler , fastifyWebsocket as namedFastifyWebsocket , default as defaultFastifyWebsocket , WebSocket } from '..' ;
2- import type { IncomingMessage } from " http" ;
3- import fastify , { RouteOptions , FastifyRequest , FastifyInstance , FastifyReply , RequestGenericInterface , FastifyBaseLogger , RawServerDefault , FastifySchema , RawRequestDefaultExpression } from 'fastify' ;
4- import { expectType } from 'tsd' ;
5- import { Server } from 'ws' ;
6- import { RouteGenericInterface } from 'fastify/types/route' ;
7- import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox' ;
1+ import fastifyWebsocket , { WebsocketHandler , fastifyWebsocket as namedFastifyWebsocket , default as defaultFastifyWebsocket , WebSocket } from '..'
2+ import type { IncomingMessage } from ' http'
3+ import fastify , { RouteOptions , FastifyRequest , FastifyInstance , FastifyReply , RequestGenericInterface , FastifyBaseLogger , RawServerDefault , FastifySchema , RawRequestDefaultExpression } from 'fastify'
4+ import { expectType } from 'tsd'
5+ import { Server } from 'ws'
6+ import { RouteGenericInterface } from 'fastify/types/route'
7+ import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
88import { Type } from '@sinclair/typebox'
99
10- const app : FastifyInstance = fastify ( ) ;
11- app . register ( fastifyWebsocket ) ;
12- app . register ( fastifyWebsocket , { } ) ;
13- app . register ( fastifyWebsocket , { options : { maxPayload : 123 } } ) ;
10+ const app : FastifyInstance = fastify ( )
11+ app . register ( fastifyWebsocket )
12+ app . register ( fastifyWebsocket , { } )
13+ app . register ( fastifyWebsocket , { options : { maxPayload : 123 } } )
1414app . register ( fastifyWebsocket , {
15- errorHandler : function errorHandler ( error : Error , socket : WebSocket , request : FastifyRequest , reply : FastifyReply ) : void {
16- expectType < FastifyInstance > ( this ) ;
15+ errorHandler : function errorHandler ( error : Error , socket : WebSocket , request : FastifyRequest , reply : FastifyReply ) : void {
16+ expectType < FastifyInstance > ( this )
1717 expectType < Error > ( error )
1818 expectType < WebSocket > ( socket )
1919 expectType < FastifyRequest > ( request )
2020 expectType < FastifyReply > ( reply )
2121 }
22- } ) ;
23- app . register ( fastifyWebsocket , { options : { perMessageDeflate : true } } ) ;
24- app . register ( fastifyWebsocket , { preClose : function syncPreclose ( ) { } } ) ;
25- app . register ( fastifyWebsocket , { preClose : async function asyncPreclose ( ) { } } ) ;
22+ } )
23+ app . register ( fastifyWebsocket , { options : { perMessageDeflate : true } } )
24+ app . register ( fastifyWebsocket , { preClose : function syncPreclose ( ) { } } )
25+ app . register ( fastifyWebsocket , { preClose : async function asyncPreclose ( ) { } } )
2626
2727app . get ( '/websockets-via-inferrence' , { websocket : true } , async function ( socket , request ) {
28- expectType < FastifyInstance > ( this ) ;
29- expectType < WebSocket > ( socket ) ;
30- expectType < Server > ( app . websocketServer ) ;
28+ expectType < FastifyInstance > ( this )
29+ expectType < WebSocket > ( socket )
30+ expectType < Server > ( app . websocketServer )
3131 expectType < FastifyRequest < RequestGenericInterface > > ( request )
32- expectType < boolean > ( request . ws ) ;
33- expectType < FastifyBaseLogger > ( request . log ) ;
34- } ) ;
32+ expectType < boolean > ( request . ws )
33+ expectType < FastifyBaseLogger > ( request . log )
34+ } )
3535
3636const handler : WebsocketHandler = async ( socket , request ) => {
37- expectType < WebSocket > ( socket ) ;
38- expectType < Server > ( app . websocketServer ) ;
37+ expectType < WebSocket > ( socket )
38+ expectType < Server > ( app . websocketServer )
3939 expectType < FastifyRequest < RequestGenericInterface > > ( request )
4040}
4141
42- app . get ( '/websockets-via-annotated-const' , { websocket : true } , handler ) ;
42+ app . get ( '/websockets-via-annotated-const' , { websocket : true } , handler )
4343
4444app . get ( '/not-specifed' , async ( request , reply ) => {
45- expectType < FastifyRequest > ( request ) ;
45+ expectType < FastifyRequest > ( request )
4646 expectType < FastifyReply > ( reply )
47- expectType < boolean > ( request . ws ) ;
48- } ) ;
47+ expectType < boolean > ( request . ws )
48+ } )
4949
5050app . get ( '/not-websockets' , { websocket : false } , async ( request , reply ) => {
51- expectType < FastifyRequest > ( request ) ;
52- expectType < FastifyReply > ( reply ) ;
53- } ) ;
51+ expectType < FastifyRequest > ( request )
52+ expectType < FastifyReply > ( reply )
53+ } )
5454
5555app . route ( {
5656 method : 'GET' ,
5757 url : '/route-full-declaration-syntax' ,
5858 handler : ( request , reply ) => {
59- expectType < FastifyRequest > ( request ) ;
60- expectType < FastifyReply > ( reply ) ;
61- expectType < boolean > ( request . ws ) ;
59+ expectType < FastifyRequest > ( request )
60+ expectType < FastifyReply > ( reply )
61+ expectType < boolean > ( request . ws )
6262 } ,
6363 wsHandler : ( socket , request ) => {
64- expectType < WebSocket > ( socket ) ;
65- expectType < FastifyRequest < RouteGenericInterface > > ( request ) ;
66- expectType < boolean > ( request . ws ) ;
64+ expectType < WebSocket > ( socket )
65+ expectType < FastifyRequest < RouteGenericInterface > > ( request )
66+ expectType < boolean > ( request . ws )
6767 } ,
68- } ) ;
68+ } )
6969
7070const augmentedRouteOptions : RouteOptions = {
7171 method : 'GET' ,
7272 url : '/route-with-exported-augmented-route-options' ,
7373 handler : ( request , reply ) => {
74- expectType < FastifyRequest > ( request ) ;
75- expectType < FastifyReply > ( reply ) ;
74+ expectType < FastifyRequest > ( request )
75+ expectType < FastifyReply > ( reply )
7676 } ,
7777 wsHandler : ( socket , request ) => {
78- expectType < WebSocket > ( socket ) ;
78+ expectType < WebSocket > ( socket )
7979 expectType < FastifyRequest < RouteGenericInterface > > ( request )
8080 } ,
81- } ;
82- app . route ( augmentedRouteOptions ) ;
83-
81+ }
82+ app . route ( augmentedRouteOptions )
8483
8584app . get < { Params : { foo : string } , Body : { bar : string } , Querystring : { search : string } , Headers : { auth : string } } > ( '/shorthand-explicit-types' , {
8685 websocket : true
8786} , async ( socket , request ) => {
88- expectType < WebSocket > ( socket ) ;
89- expectType < { foo : string } > ( request . params ) ;
90- expectType < { bar : string } > ( request . body ) ;
91- expectType < { search : string } > ( request . query ) ;
92- expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers ) ;
93- } ) ;
94-
87+ expectType < WebSocket > ( socket )
88+ expectType < { foo : string } > ( request . params )
89+ expectType < { bar : string } > ( request . body )
90+ expectType < { search : string } > ( request . query )
91+ expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers )
92+ } )
9593
9694app . route < { Params : { foo : string } , Body : { bar : string } , Querystring : { search : string } , Headers : { auth : string } } > ( {
9795 method : 'GET' ,
9896 url : '/longhand-explicit-types' ,
9997 handler : ( request , _reply ) => {
100- expectType < { foo : string } > ( request . params ) ;
101- expectType < { bar : string } > ( request . body ) ;
102- expectType < { search : string } > ( request . query ) ;
103- expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers ) ;
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 )
104102 } ,
105103 wsHandler : ( socket , request ) => {
106- expectType < WebSocket > ( socket ) ;
107- expectType < { foo : string } > ( request . params ) ;
108- expectType < { bar : string } > ( request . body ) ;
109- expectType < { search : string } > ( request . query ) ;
110- expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers ) ;
104+ expectType < WebSocket > ( socket )
105+ expectType < { foo : string } > ( request . params )
106+ expectType < { bar : string } > ( request . body )
107+ expectType < { search : string } > ( request . query )
108+ expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers )
111109 } ,
112- } ) ;
113-
110+ } )
114111
115112const schema = {
116113 params : Type . Object ( {
@@ -125,43 +122,42 @@ const schema = {
125122 headers : Type . Object ( {
126123 auth : Type . String ( )
127124 } )
128- } ;
125+ }
129126
130- const server = app . withTypeProvider < TypeBoxTypeProvider > ( ) ;
127+ const server = app . withTypeProvider < TypeBoxTypeProvider > ( )
131128
132129server . route ( {
133130 method : 'GET' ,
134131 url : '/longhand-type-inference' ,
135132 schema,
136133 handler : ( request , _reply ) => {
137- expectType < { foo : string } > ( request . params ) ;
138- expectType < { bar : string } > ( request . body ) ;
139- expectType < { search : string } > ( request . query ) ;
140- expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers ) ;
134+ expectType < { foo : string } > ( request . params )
135+ expectType < { bar : string } > ( request . body )
136+ expectType < { search : string } > ( request . query )
137+ expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers )
141138 } ,
142139 wsHandler : ( socket , request ) => {
143- expectType < WebSocket > ( socket ) ;
144- expectType < { foo : string } > ( request . params ) ;
145- expectType < { bar : string } > ( request . body ) ;
146- expectType < { search : string } > ( request . query ) ;
147- expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers ) ;
140+ expectType < WebSocket > ( socket )
141+ expectType < { foo : string } > ( request . params )
142+ expectType < { bar : string } > ( request . body )
143+ expectType < { search : string } > ( request . query )
144+ expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers )
148145 } ,
149- } ) ;
146+ } )
150147
151148server . get ( '/websockets-no-type-inference' ,
152149 { websocket : true } ,
153150 async function ( socket , request ) {
154- expectType < FastifyInstance > ( this ) ;
155- expectType < WebSocket > ( socket ) ;
156- expectType < Server > ( app . websocketServer ) ;
157- expectType < FastifyRequest < RequestGenericInterface , RawServerDefault , RawRequestDefaultExpression , FastifySchema , TypeBoxTypeProvider , unknown , FastifyBaseLogger > > ( request ) ;
158- expectType < boolean > ( request . ws ) ;
159- expectType < unknown > ( request . params ) ;
160- expectType < unknown > ( request . body ) ;
161- expectType < unknown > ( request . query ) ;
162- expectType < IncomingMessage [ 'headers' ] > ( request . headers ) ;
163- } ) ;
164-
165- expectType < typeof fastifyWebsocket > ( namedFastifyWebsocket ) ;
166- expectType < typeof fastifyWebsocket > ( defaultFastifyWebsocket ) ;
151+ expectType < FastifyInstance > ( this )
152+ expectType < WebSocket > ( socket )
153+ expectType < Server > ( app . websocketServer )
154+ expectType < FastifyRequest < RequestGenericInterface , RawServerDefault , RawRequestDefaultExpression , FastifySchema , TypeBoxTypeProvider , unknown , FastifyBaseLogger > > ( request )
155+ expectType < boolean > ( request . ws )
156+ expectType < unknown > ( request . params )
157+ expectType < unknown > ( request . body )
158+ expectType < unknown > ( request . query )
159+ expectType < IncomingMessage [ 'headers' ] > ( request . headers )
160+ } )
167161
162+ expectType < typeof fastifyWebsocket > ( namedFastifyWebsocket )
163+ expectType < typeof fastifyWebsocket > ( defaultFastifyWebsocket )
0 commit comments