Skip to content

Commit 80c2434

Browse files
authored
Merge pull request #188 from fastify/ws-typedefs
Add `request.ws` to type definitions
2 parents 0e7646f + ab470f7 commit 80c2434

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ declare module 'fastify' {
2323
websocketServer: WebSocket.Server,
2424
}
2525

26+
interface FastifyRequest {
27+
ws: boolean
28+
}
29+
2630
interface RouteShorthandMethod<
2731
RawServer extends RawServerBase = RawServerDefault,
2832
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>,

test/types/index.test-d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ app.get('/websockets-via-inferrence', { websocket: true }, async function (conne
2525
expectType<SocketStream>(connection);
2626
expectType<Server>(app.websocketServer);
2727
expectType<FastifyRequest<RequestGenericInterface>>(request)
28+
expectType<boolean>(request.ws);
2829
});
2930

3031
const handler: WebsocketHandler = async (connection, request) => {
@@ -38,6 +39,7 @@ app.get('/websockets-via-annotated-const', { websocket: true }, handler);
3839
app.get('/not-specifed', async (request, reply) => {
3940
expectType<FastifyRequest>(request);
4041
expectType<FastifyReply>(reply)
42+
expectType<boolean>(request.ws);
4143
});
4244

4345
app.get('/not-websockets', { websocket: false }, async (request, reply) => {
@@ -51,10 +53,12 @@ app.route({
5153
handler: (request, reply) => {
5254
expectType<FastifyRequest>(request);
5355
expectType<FastifyReply>(reply);
56+
expectType<boolean>(request.ws);
5457
},
5558
wsHandler: (connection, request) => {
5659
expectType<SocketStream>(connection);
5760
expectType<FastifyRequest<RouteGenericInterface>>(request);
61+
expectType<boolean>(request.ws);
5862
},
5963
});
6064

0 commit comments

Comments
 (0)