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'
8
9
import { Type } from '@sinclair/typebox'
9
10
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 } } )
14
15
app . 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 )
17
18
expectType < Error > ( error )
18
19
expectType < WebSocket > ( socket )
19
20
expectType < FastifyRequest > ( request )
20
21
expectType < FastifyReply > ( reply )
21
22
}
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 ( ) { } } )
26
27
27
28
app . 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 )
31
32
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
+ } )
35
36
36
37
const handler : WebsocketHandler = async ( socket , request ) => {
37
- expectType < WebSocket > ( socket ) ;
38
- expectType < Server > ( app . websocketServer ) ;
38
+ expectType < WebSocket > ( socket )
39
+ expectType < Server > ( app . websocketServer )
39
40
expectType < FastifyRequest < RequestGenericInterface > > ( request )
40
41
}
41
42
42
- app . get ( '/websockets-via-annotated-const' , { websocket : true } , handler ) ;
43
+ app . get ( '/websockets-via-annotated-const' , { websocket : true } , handler )
43
44
44
45
app . get ( '/not-specifed' , async ( request , reply ) => {
45
- expectType < FastifyRequest > ( request ) ;
46
+ expectType < FastifyRequest > ( request )
46
47
expectType < FastifyReply > ( reply )
47
- expectType < boolean > ( request . ws ) ;
48
- } ) ;
48
+ expectType < boolean > ( request . ws )
49
+ } )
49
50
50
51
app . 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
+ } )
54
55
55
56
app . route ( {
56
57
method : 'GET' ,
57
58
url : '/route-full-declaration-syntax' ,
58
59
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 )
62
63
} ,
63
64
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 )
67
68
} ,
68
- } ) ;
69
+ } )
69
70
70
71
const augmentedRouteOptions : RouteOptions = {
71
72
method : 'GET' ,
72
73
url : '/route-with-exported-augmented-route-options' ,
73
74
handler : ( request , reply ) => {
74
- expectType < FastifyRequest > ( request ) ;
75
- expectType < FastifyReply > ( reply ) ;
75
+ expectType < FastifyRequest > ( request )
76
+ expectType < FastifyReply > ( reply )
76
77
} ,
77
78
wsHandler : ( socket , request ) => {
78
- expectType < WebSocket > ( socket ) ;
79
+ expectType < WebSocket > ( socket )
79
80
expectType < FastifyRequest < RouteGenericInterface > > ( request )
80
81
} ,
81
- } ;
82
- app . route ( augmentedRouteOptions ) ;
83
-
82
+ }
83
+ app . route ( augmentedRouteOptions )
84
84
85
85
app . get < { Params : { foo : string } , Body : { bar : string } , Querystring : { search : string } , Headers : { auth : string } } > ( '/shorthand-explicit-types' , {
86
86
websocket : true
87
87
} , 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
+ } )
95
94
96
95
app . route < { Params : { foo : string } , Body : { bar : string } , Querystring : { search : string } , Headers : { auth : string } } > ( {
97
96
method : 'GET' ,
98
97
url : '/longhand-explicit-types' ,
99
98
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 )
104
103
} ,
105
104
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 )
111
110
} ,
112
- } ) ;
113
-
111
+ } )
114
112
115
113
const schema = {
116
114
params : Type . Object ( {
@@ -125,43 +123,42 @@ const schema = {
125
123
headers : Type . Object ( {
126
124
auth : Type . String ( )
127
125
} )
128
- } ;
126
+ }
129
127
130
- const server = app . withTypeProvider < TypeBoxTypeProvider > ( ) ;
128
+ const server = app . withTypeProvider < TypeBoxTypeProvider > ( )
131
129
132
130
server . route ( {
133
131
method : 'GET' ,
134
132
url : '/longhand-type-inference' ,
135
133
schema,
136
134
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 )
141
139
} ,
142
140
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 )
148
146
} ,
149
- } ) ;
147
+ } )
150
148
151
149
server . get ( '/websockets-no-type-inference' ,
152
150
{ websocket : true } ,
153
151
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
+ } )
167
162
163
+ expectType < typeof fastifyWebsocket > ( namedFastifyWebsocket )
164
+ expectType < typeof fastifyWebsocket > ( defaultFastifyWebsocket )
0 commit comments