Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>, RequestGeneric extends RequestGenericInterface = RequestGenericInterface> {
wsHandler?: WebsocketHandler<RawServer, RawRequest, RequestGeneric>;
}

declare module 'fastify' {
interface RouteShorthandOptions<
RawServer extends RawServerBase = RawServerDefault
Expand All @@ -33,13 +35,12 @@ declare module 'fastify' {
): FastifyInstance<RawServer, RawRequest, RawReply>;
}

interface RouteOptions extends WebsocketRouteOptions {}
interface RouteOptions<RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>, RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault,SchemaCompiler = fastify.FastifySchema> extends WebsocketRouteOptions<RawServer, RawRequest, RouteGeneric> {}
}

declare const websocketPlugin: FastifyPluginCallback<WebsocketPluginOptions>;

interface WebSocketServerOptions extends Omit<WebSocket.ServerOptions, 'path'> {}

interface WebSocketServerOptions extends Omit<WebSocket.ServerOptions, "path"> {}

export type WebsocketHandler<
RawServer extends RawServerBase = RawServerDefault,
Expand All @@ -60,6 +61,6 @@ export interface WebsocketPluginOptions {
options?: WebSocketServerOptions;
}

export interface RouteOptions extends fastify.RouteOptions, WebsocketRouteOptions {}
export interface RouteOptions<RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>, RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler = fastify.FastifySchema> extends fastify.RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler>, WebsocketRouteOptions<RawServer, RawRequest, RouteGeneric> {}

export default websocketPlugin;
15 changes: 8 additions & 7 deletions test/types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<FastifyInstance>(this);
expectType<SocketStream>(connection);
expectType<Server>(app.websocketServer);
expectType<FastifyRequest<RequestGenericInterface>>(request)
});
expectType<FastifyInstance>(this);
expectType<SocketStream>(connection);
expectType<Server>(app.websocketServer);
expectType<FastifyRequest<RequestGenericInterface>>(request)
});

const handler: WebsocketHandler = async (connection, request) => {
expectType<SocketStream>(connection);
Expand Down Expand Up @@ -52,7 +53,7 @@ app.route({
},
wsHandler: (connection, request) => {
expectType<SocketStream>(connection);
expectType<FastifyRequest<RequestGenericInterface>>(request);
expectType<FastifyRequest<RouteGenericInterface>>(request);
},
});

Expand All @@ -65,7 +66,7 @@ const augmentedRouteOptions: RouteOptions = {
},
wsHandler: (connection, request) => {
expectType<SocketStream>(connection);
expectType<FastifyRequest<RequestGenericInterface>>(request)
expectType<FastifyRequest<RouteGenericInterface>>(request)
},
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Want to add a test for the new functionality based on your original issue? Thanks for the contribution!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never really tested "types", didn't know that was a thing until now :D

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... what next then? :D

Copy link
Member

@airhorns airhorns May 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add a test case you'd need to copy one of these stanzas and exercise specifically the new types you added! I think you could do that by adding a route that uses a specific type for the params or whatever, and then asserting that thats the type of the params you get in the handler. This file is using tsd which is a typescript testing tool and you can find more info in its docs.

app.route(augmentedRouteOptions);