Skip to content

Commit c075f38

Browse files
committed
Ensure websocket handler types are only applied to websocket handlers
This corrects an issue introduced in #64 where upon adding `fastify-websocket` to a project all route handlers were (accidentally) assumed to be websocket handlers getting the different (and decidedly less useful) types. My bad! This corrects the issue by using a type-land overload of the RouteShorthand function definition to change the type of the handler only if the handler is in fact `{ websocket: true }`. Also adds tests, `tsd` is handy!
1 parent 61276c6 commit c075f38

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ declare module 'fastify' {
2323
> {
2424
<RequestGeneric extends RequestGenericInterface = RequestGenericInterface, ContextConfig = ContextConfigDefault>(
2525
path: string,
26-
opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig>,
26+
opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig> & { websocket: true }, // this creates an overload that only applies these different types if the handler is for websockets
2727
handler?: WebsocketHandler
2828
): FastifyInstance<RawServer, RawRequest, RawReply>;
2929
}

test/types/index.test-d.ts

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import wsPlugin, { SocketStream } from '../..';
2-
import fastify, { WebsocketHandler, FastifyRequest, FastifyInstance, RequestGenericInterface } from 'fastify';
2+
import fastify, { WebsocketHandler, FastifyRequest, FastifyInstance, RequestGenericInterface, FastifyReply } from 'fastify';
33
import { expectType } from 'tsd';
44
import { Server as HttpServer, IncomingMessage } from 'http'
55
import { Server } from 'ws';
@@ -17,12 +17,27 @@ const handler: WebsocketHandler = (
1717

1818
const handle = (connection: SocketStream): void => {
1919
expectType<SocketStream>(connection)
20-
}
20+
}
2121

2222
const app: FastifyInstance = fastify();
2323
app.register(wsPlugin);
2424
app.register(wsPlugin, {});
2525
app.register(wsPlugin, { handle } );
2626
app.register(wsPlugin, { options: { perMessageDeflate: true } });
2727

28-
app.get('/', { websocket: true }, handler);
28+
app.get('/websockets', { websocket: true }, async (connection, req, params) => {
29+
expectType<SocketStream>(connection);
30+
expectType<Server>(app.websocketServer);
31+
expectType<FastifyRequest<HttpServer, IncomingMessage, RequestGenericInterface>>(req)
32+
expectType<{ [key: string]: any } | undefined>(params);
33+
});
34+
35+
app.get('/not-specifed', async (request, reply) => {
36+
expectType<FastifyRequest>(request);
37+
expectType<FastifyReply>(reply)
38+
});
39+
40+
app.get('/not-websockets', { websocket: false }, async (request, reply) => {
41+
expectType<FastifyRequest>(request);
42+
expectType<FastifyReply>(reply);
43+
});

0 commit comments

Comments
 (0)