Skip to content

Commit 2b5cef7

Browse files
airhornsmcollina
andauthored
Ensure websocket handler types are only applied to websocket handlers (#68)
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! Co-authored-by: Matteo Collina <[email protected]>
1 parent f8ad6c6 commit 2b5cef7

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
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
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"homepage": "https://github.com/fastify/fastify-websocket#readme",
3131
"devDependencies": {
3232
"@types/ws": "^7.2.4",
33-
"fastify": "^3.0.0",
33+
"fastify": "^3.0.2",
3434
"pre-commit": "^1.2.2",
3535
"snazzy": "^8.0.0",
3636
"standard": "^14.3.3",

test/types/index.test-d.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
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';
66

77
const app: FastifyInstance = fastify();
88
app.register(wsPlugin);
99
app.register(wsPlugin, {});
10+
app.register(wsPlugin, { options: { maxPayload: 123 } });
1011
app.register(wsPlugin, {
1112
handle: function globalHandler(connection: SocketStream): void {
1213
expectType<FastifyInstance>(this);
@@ -15,14 +16,29 @@ app.register(wsPlugin, {
1516
});
1617
app.register(wsPlugin, { options: { perMessageDeflate: true } });
1718

18-
app.get('/', { websocket: true }, function perRouteHandler(
19-
connection: SocketStream,
20-
req: IncomingMessage,
21-
params
22-
) {
19+
app.get('/websockets-via-inferrence', { websocket: true }, async function(connection, req, params) {
2320
expectType<FastifyInstance>(this);
2421
expectType<SocketStream>(connection);
2522
expectType<Server>(app.websocketServer);
2623
expectType<IncomingMessage>(req)
2724
expectType<{ [key: string]: any } | undefined>(params);
2825
});
26+
27+
const handler: WebsocketHandler = async (connection, req, params) => {
28+
expectType<SocketStream>(connection);
29+
expectType<Server>(app.websocketServer);
30+
expectType<IncomingMessage>(req)
31+
expectType<{ [key: string]: any } | undefined>(params);
32+
}
33+
34+
app.get('/websockets-via-annotated-const', { websocket: true }, handler);
35+
36+
app.get('/not-specifed', async (request, reply) => {
37+
expectType<FastifyRequest>(request);
38+
expectType<FastifyReply>(reply)
39+
});
40+
41+
app.get('/not-websockets', { websocket: false }, async (request, reply) => {
42+
expectType<FastifyRequest>(request);
43+
expectType<FastifyReply>(reply);
44+
});

0 commit comments

Comments
 (0)