From b0589a64f6662134e3e510d150450de6993ee7e8 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Sat, 11 Jul 2020 11:12:13 +0200 Subject: [PATCH] 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! --- index.d.ts | 2 +- package.json | 2 +- test/types/index.test-d.ts | 28 ++++++++++++++++++++++------ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/index.d.ts b/index.d.ts index be3b0d5..e69a042 100644 --- a/index.d.ts +++ b/index.d.ts @@ -23,7 +23,7 @@ declare module 'fastify' { > { ( path: string, - opts: RouteShorthandOptions, + opts: RouteShorthandOptions & { websocket: true }, // this creates an overload that only applies these different types if the handler is for websockets handler?: WebsocketHandler ): FastifyInstance; } diff --git a/package.json b/package.json index fc5edb3..f109228 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,7 @@ "homepage": "https://github.com/fastify/fastify-websocket#readme", "devDependencies": { "@types/ws": "^7.2.4", - "fastify": "^3.0.0", + "fastify": "^3.0.2", "pre-commit": "^1.2.2", "snazzy": "^8.0.0", "standard": "^14.3.3", diff --git a/test/types/index.test-d.ts b/test/types/index.test-d.ts index e240de7..7227720 100644 --- a/test/types/index.test-d.ts +++ b/test/types/index.test-d.ts @@ -1,5 +1,5 @@ import wsPlugin, { SocketStream } from '../..'; -import fastify, { WebsocketHandler, FastifyRequest, FastifyInstance, RequestGenericInterface } from 'fastify'; +import fastify, { WebsocketHandler, FastifyRequest, FastifyInstance, RequestGenericInterface, FastifyReply } from 'fastify'; import { expectType } from 'tsd'; import { Server as HttpServer, IncomingMessage } from 'http' import { Server } from 'ws'; @@ -7,6 +7,7 @@ import { Server } from 'ws'; const app: FastifyInstance = fastify(); app.register(wsPlugin); app.register(wsPlugin, {}); +app.register(wsPlugin, { options: { maxPayload: 123 } }); app.register(wsPlugin, { handle: function globalHandler(connection: SocketStream): void { expectType(this); @@ -15,14 +16,29 @@ app.register(wsPlugin, { }); app.register(wsPlugin, { options: { perMessageDeflate: true } }); -app.get('/', { websocket: true }, function perRouteHandler( - connection: SocketStream, - req: IncomingMessage, - params -) { +app.get('/websockets-via-inferrence', { websocket: true }, async function(connection, req, params) { expectType(this); expectType(connection); expectType(app.websocketServer); expectType(req) expectType<{ [key: string]: any } | undefined>(params); }); + +const handler: WebsocketHandler = async (connection, req, params) => { + expectType(connection); + expectType(app.websocketServer); + expectType(req) + expectType<{ [key: string]: any } | undefined>(params); +} + +app.get('/websockets-via-annotated-const', { websocket: true }, handler); + +app.get('/not-specifed', async (request, reply) => { + expectType(request); + expectType(reply) +}); + +app.get('/not-websockets', { websocket: false }, async (request, reply) => { + expectType(request); + expectType(reply); +});