From 65bacd9a7d1c767bb616c92513a0e13e3d683eeb Mon Sep 17 00:00:00 2001 From: Mia Date: Fri, 14 May 2021 16:23:17 +0200 Subject: [PATCH 1/2] fix: generify WebsocketRouteOptions --- index.d.ts | 13 +++++++------ test/types/index.test-d.ts | 15 ++++++++------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/index.d.ts b/index.d.ts index a450933..145aa6c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -5,10 +5,12 @@ import * as fastify from 'fastify'; import * as WebSocket from 'ws'; import { Duplex } from 'stream'; import { FastifyReply } from 'fastify/types/reply'; +import { RouteGenericInterface } from 'fastify/types/route'; -interface WebsocketRouteOptions { - wsHandler?: WebsocketHandler +interface WebsocketRouteOptions = RawRequestDefaultExpression, RequestGeneric extends RequestGenericInterface = RequestGenericInterface> { + wsHandler?: WebsocketHandler; } + declare module 'fastify' { interface RouteShorthandOptions< RawServer extends RawServerBase = RawServerDefault @@ -33,13 +35,12 @@ declare module 'fastify' { ): FastifyInstance; } - interface RouteOptions extends WebsocketRouteOptions {} + interface RouteOptions = RawRequestDefaultExpression, RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault,SchemaCompiler = fastify.FastifySchema> extends WebsocketRouteOptions {} } declare const websocketPlugin: FastifyPluginCallback; -interface WebSocketServerOptions extends Omit {} - +interface WebSocketServerOptions extends Omit {} export type WebsocketHandler< RawServer extends RawServerBase = RawServerDefault, @@ -60,6 +61,6 @@ export interface WebsocketPluginOptions { options?: WebSocketServerOptions; } -export interface RouteOptions extends fastify.RouteOptions, WebsocketRouteOptions {} +export interface RouteOptions = RawRequestDefaultExpression, RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler = fastify.FastifySchema> extends fastify.RouteOptions, WebsocketRouteOptions {} export default websocketPlugin; diff --git a/test/types/index.test-d.ts b/test/types/index.test-d.ts index b3e4b38..9f96ec3 100644 --- a/test/types/index.test-d.ts +++ b/test/types/index.test-d.ts @@ -2,6 +2,7 @@ import wsPlugin, { WebsocketHandler, SocketStream } from '../..'; import fastify, { RouteOptions, FastifyRequest, FastifyInstance, FastifyReply, RequestGenericInterface } from 'fastify'; import { expectType } from 'tsd'; import { Server } from 'ws'; +import { RouteGenericInterface } from 'fastify/types/route'; const app: FastifyInstance = fastify(); app.register(wsPlugin); @@ -19,11 +20,11 @@ app.register(wsPlugin, { app.register(wsPlugin, { options: { perMessageDeflate: true } }); app.get('/websockets-via-inferrence', { websocket: true }, async function(connection, request) { - expectType(this); - expectType(connection); - expectType(app.websocketServer); - expectType>(request) -}); + expectType(this); + expectType(connection); + expectType(app.websocketServer); + expectType>(request) + }); const handler: WebsocketHandler = async (connection, request) => { expectType(connection); @@ -52,7 +53,7 @@ app.route({ }, wsHandler: (connection, request) => { expectType(connection); - expectType>(request); + expectType>(request); }, }); @@ -65,7 +66,7 @@ const augmentedRouteOptions: RouteOptions = { }, wsHandler: (connection, request) => { expectType(connection); - expectType>(request) + expectType>(request) }, }; app.route(augmentedRouteOptions); From 33c199445e2ca0d60afe64c4c639e2cb8684963a Mon Sep 17 00:00:00 2001 From: Harry Brundage Date: Sun, 27 Feb 2022 10:26:23 -0500 Subject: [PATCH 2/2] Add typescript tests for overriding the route option generics for websocket routes --- test/types/index.test-d.ts | 43 ++++++++++++++++++++++++++++++++------ 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/test/types/index.test-d.ts b/test/types/index.test-d.ts index 9f96ec3..ca8b93a 100644 --- a/test/types/index.test-d.ts +++ b/test/types/index.test-d.ts @@ -1,4 +1,5 @@ import wsPlugin, { WebsocketHandler, SocketStream } from '../..'; +import type {IncomingMessage} from "http"; import fastify, { RouteOptions, FastifyRequest, FastifyInstance, FastifyReply, RequestGenericInterface } from 'fastify'; import { expectType } from 'tsd'; import { Server } from 'ws'; @@ -19,12 +20,12 @@ app.register(wsPlugin, { }); app.register(wsPlugin, { options: { perMessageDeflate: true } }); -app.get('/websockets-via-inferrence', { websocket: true }, async function(connection, request) { - expectType(this); - expectType(connection); - expectType(app.websocketServer); - expectType>(request) - }); +app.get('/websockets-via-inferrence', { websocket: true }, async function (connection, request) { + expectType(this); + expectType(connection); + expectType(app.websocketServer); + expectType>(request) +}); const handler: WebsocketHandler = async (connection, request) => { expectType(connection); @@ -70,3 +71,33 @@ const augmentedRouteOptions: RouteOptions = { }, }; app.route(augmentedRouteOptions); + + +app.get<{ Params: { foo: string }, Body: { bar: string }, Querystring: { search: string }, Headers: { auth: string } }>('/shorthand-explicit-types', { + websocket: true +}, async (connection, request) => { + expectType(connection); + expectType<{ foo: string }>(request.params); + expectType<{ bar: string }>(request.body); + expectType<{ search: string }>(request.query); + expectType< IncomingMessage['headers'] & { auth: string }>(request.headers); +}); + + +app.route<{ Params: { foo: string }, Body: { bar: string }, Querystring: { search: string }, Headers: { auth: string } }>({ + method: 'GET', + url: '/longhand-explicit-types', + handler: (request, _reply) => { + expectType<{ foo: string }>(request.params); + expectType<{ bar: string }>(request.body); + expectType<{ search: string }>(request.query); + expectType(request.headers); + }, + wsHandler: (connection, request) => { + expectType(connection); + expectType<{ foo: string }>(request.params); + expectType<{ bar: string }>(request.body); + expectType<{ search: string }>(request.query); + expectType(request.headers); + }, +});