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..ca8b93a 100644 --- a/test/types/index.test-d.ts +++ b/test/types/index.test-d.ts @@ -1,7 +1,9 @@ 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'; +import { RouteGenericInterface } from 'fastify/types/route'; const app: FastifyInstance = fastify(); app.register(wsPlugin); @@ -18,7 +20,7 @@ app.register(wsPlugin, { }); app.register(wsPlugin, { options: { perMessageDeflate: true } }); -app.get('/websockets-via-inferrence', { websocket: true }, async function(connection, request) { +app.get('/websockets-via-inferrence', { websocket: true }, async function (connection, request) { expectType(this); expectType(connection); expectType(app.websocketServer); @@ -52,7 +54,7 @@ app.route({ }, wsHandler: (connection, request) => { expectType(connection); - expectType>(request); + expectType>(request); }, }); @@ -65,7 +67,37 @@ const augmentedRouteOptions: RouteOptions = { }, wsHandler: (connection, request) => { expectType(connection); - expectType>(request) + expectType>(request) }, }; 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); + }, +});