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); +});