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+ // eslint-disable-next-line import-x/no-named-default -- Testing default export
2+ import fastifyWebsocket , { WebsocketHandler , fastifyWebsocket as namedFastifyWebsocket , default as defaultFastifyWebsocket , WebSocket } from '..'
3+ import type { IncomingMessage } from 'http'
4+ import fastify , { RouteOptions , FastifyRequest , FastifyInstance , FastifyReply , RequestGenericInterface , FastifyBaseLogger , RawServerDefault , FastifySchema , RawRequestDefaultExpression } from 'fastify'
5+ import { expectType } from 'tsd'
6+ import { Server } from 'ws'
7+ import { RouteGenericInterface } from 'fastify/types/route'
8+ import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
89import { Type } from '@sinclair/typebox'
910
10- const app : FastifyInstance = fastify ( ) ;
11- app . register ( fastifyWebsocket ) ;
12- app . register ( fastifyWebsocket , { } ) ;
13- app . register ( fastifyWebsocket , { options : { maxPayload : 123 } } ) ;
11+ const app : FastifyInstance = fastify ( )
12+ app . register ( fastifyWebsocket )
13+ app . register ( fastifyWebsocket , { } )
14+ app . register ( fastifyWebsocket , { options : { maxPayload : 123 } } )
1415app . register ( fastifyWebsocket , {
15- errorHandler : function errorHandler ( error : Error , socket : WebSocket , request : FastifyRequest , reply : FastifyReply ) : void {
16- expectType < FastifyInstance > ( this ) ;
16+ errorHandler : function errorHandler ( error : Error , socket : WebSocket , request : FastifyRequest , reply : FastifyReply ) : void {
17+ expectType < FastifyInstance > ( this )
1718 expectType < Error > ( error )
1819 expectType < WebSocket > ( socket )
1920 expectType < FastifyRequest > ( request )
2021 expectType < FastifyReply > ( reply )
2122 }
22- } ) ;
23- app . register ( fastifyWebsocket , { options : { perMessageDeflate : true } } ) ;
24- app . register ( fastifyWebsocket , { preClose : function syncPreclose ( ) { } } ) ;
25- app . register ( fastifyWebsocket , { preClose : async function asyncPreclose ( ) { } } ) ;
23+ } )
24+ app . register ( fastifyWebsocket , { options : { perMessageDeflate : true } } )
25+ app . register ( fastifyWebsocket , { preClose : function syncPreclose ( ) { } } )
26+ app . register ( fastifyWebsocket , { preClose : async function asyncPreclose ( ) { } } )
2627
2728app . get ( '/websockets-via-inferrence' , { websocket : true } , async function ( socket , request ) {
28- expectType < FastifyInstance > ( this ) ;
29- expectType < WebSocket > ( socket ) ;
30- expectType < Server > ( app . websocketServer ) ;
29+ expectType < FastifyInstance > ( this )
30+ expectType < WebSocket > ( socket )
31+ expectType < Server > ( app . websocketServer )
3132 expectType < FastifyRequest < RequestGenericInterface > > ( request )
32- expectType < boolean > ( request . ws ) ;
33- expectType < FastifyBaseLogger > ( request . log ) ;
34- } ) ;
33+ expectType < boolean > ( request . ws )
34+ expectType < FastifyBaseLogger > ( request . log )
35+ } )
3536
3637const handler : WebsocketHandler = async ( socket , request ) => {
37- expectType < WebSocket > ( socket ) ;
38- expectType < Server > ( app . websocketServer ) ;
38+ expectType < WebSocket > ( socket )
39+ expectType < Server > ( app . websocketServer )
3940 expectType < FastifyRequest < RequestGenericInterface > > ( request )
4041}
4142
42- app . get ( '/websockets-via-annotated-const' , { websocket : true } , handler ) ;
43+ app . get ( '/websockets-via-annotated-const' , { websocket : true } , handler )
4344
4445app . get ( '/not-specifed' , async ( request , reply ) => {
45- expectType < FastifyRequest > ( request ) ;
46+ expectType < FastifyRequest > ( request )
4647 expectType < FastifyReply > ( reply )
47- expectType < boolean > ( request . ws ) ;
48- } ) ;
48+ expectType < boolean > ( request . ws )
49+ } )
4950
5051app . get ( '/not-websockets' , { websocket : false } , async ( request , reply ) => {
51- expectType < FastifyRequest > ( request ) ;
52- expectType < FastifyReply > ( reply ) ;
53- } ) ;
52+ expectType < FastifyRequest > ( request )
53+ expectType < FastifyReply > ( reply )
54+ } )
5455
5556app . route ( {
5657 method : 'GET' ,
5758 url : '/route-full-declaration-syntax' ,
5859 handler : ( request , reply ) => {
59- expectType < FastifyRequest > ( request ) ;
60- expectType < FastifyReply > ( reply ) ;
61- expectType < boolean > ( request . ws ) ;
60+ expectType < FastifyRequest > ( request )
61+ expectType < FastifyReply > ( reply )
62+ expectType < boolean > ( request . ws )
6263 } ,
6364 wsHandler : ( socket , request ) => {
64- expectType < WebSocket > ( socket ) ;
65- expectType < FastifyRequest < RouteGenericInterface > > ( request ) ;
66- expectType < boolean > ( request . ws ) ;
65+ expectType < WebSocket > ( socket )
66+ expectType < FastifyRequest < RouteGenericInterface > > ( request )
67+ expectType < boolean > ( request . ws )
6768 } ,
68- } ) ;
69+ } )
6970
7071const augmentedRouteOptions : RouteOptions = {
7172 method : 'GET' ,
7273 url : '/route-with-exported-augmented-route-options' ,
7374 handler : ( request , reply ) => {
74- expectType < FastifyRequest > ( request ) ;
75- expectType < FastifyReply > ( reply ) ;
75+ expectType < FastifyRequest > ( request )
76+ expectType < FastifyReply > ( reply )
7677 } ,
7778 wsHandler : ( socket , request ) => {
78- expectType < WebSocket > ( socket ) ;
79+ expectType < WebSocket > ( socket )
7980 expectType < FastifyRequest < RouteGenericInterface > > ( request )
8081 } ,
81- } ;
82- app . route ( augmentedRouteOptions ) ;
83-
82+ }
83+ app . route ( augmentedRouteOptions )
8484
8585app . get < { Params : { foo : string } , Body : { bar : string } , Querystring : { search : string } , Headers : { auth : string } } > ( '/shorthand-explicit-types' , {
8686 websocket : true
8787} , 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-
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+ } )
9594
9695app . route < { Params : { foo : string } , Body : { bar : string } , Querystring : { search : string } , Headers : { auth : string } } > ( {
9796 method : 'GET' ,
9897 url : '/longhand-explicit-types' ,
9998 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 ) ;
99+ expectType < { foo : string } > ( request . params )
100+ expectType < { bar : string } > ( request . body )
101+ expectType < { search : string } > ( request . query )
102+ expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers )
104103 } ,
105104 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 ) ;
105+ expectType < WebSocket > ( socket )
106+ expectType < { foo : string } > ( request . params )
107+ expectType < { bar : string } > ( request . body )
108+ expectType < { search : string } > ( request . query )
109+ expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers )
111110 } ,
112- } ) ;
113-
111+ } )
114112
115113const schema = {
116114 params : Type . Object ( {
@@ -125,43 +123,42 @@ const schema = {
125123 headers : Type . Object ( {
126124 auth : Type . String ( )
127125 } )
128- } ;
126+ }
129127
130- const server = app . withTypeProvider < TypeBoxTypeProvider > ( ) ;
128+ const server = app . withTypeProvider < TypeBoxTypeProvider > ( )
131129
132130server . route ( {
133131 method : 'GET' ,
134132 url : '/longhand-type-inference' ,
135133 schema,
136134 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 ) ;
135+ expectType < { foo : string } > ( request . params )
136+ expectType < { bar : string } > ( request . body )
137+ expectType < { search : string } > ( request . query )
138+ expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers )
141139 } ,
142140 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 ) ;
141+ expectType < WebSocket > ( socket )
142+ expectType < { foo : string } > ( request . params )
143+ expectType < { bar : string } > ( request . body )
144+ expectType < { search : string } > ( request . query )
145+ expectType < IncomingMessage [ 'headers' ] & { auth : string } > ( request . headers )
148146 } ,
149- } ) ;
147+ } )
150148
151149server . get ( '/websockets-no-type-inference' ,
152150 { websocket : true } ,
153151 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 ) ;
152+ expectType < FastifyInstance > ( this )
153+ expectType < WebSocket > ( socket )
154+ expectType < Server > ( app . websocketServer )
155+ expectType < FastifyRequest < RequestGenericInterface , RawServerDefault , RawRequestDefaultExpression , FastifySchema , TypeBoxTypeProvider , unknown , FastifyBaseLogger > > ( request )
156+ expectType < boolean > ( request . ws )
157+ expectType < unknown > ( request . params )
158+ expectType < unknown > ( request . body )
159+ expectType < unknown > ( request . query )
160+ expectType < IncomingMessage [ 'headers' ] > ( request . headers )
161+ } )
167162
163+ expectType < typeof fastifyWebsocket > ( namedFastifyWebsocket )
164+ expectType < typeof fastifyWebsocket > ( defaultFastifyWebsocket )
0 commit comments