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