Skip to content
Merged
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
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ declare module 'fastify' {
> {
<RequestGeneric extends RequestGenericInterface = RequestGenericInterface, ContextConfig = ContextConfigDefault>(
path: string,
opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig>,
opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig> & { websocket: true }, // this creates an overload that only applies these different types if the handler is for websockets
handler?: WebsocketHandler
): FastifyInstance<RawServer, RawRequest, RawReply>;
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 22 additions & 6 deletions test/types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
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';

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<FastifyInstance>(this);
Expand All @@ -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<FastifyInstance>(this);
expectType<SocketStream>(connection);
expectType<Server>(app.websocketServer);
expectType<IncomingMessage>(req)
expectType<{ [key: string]: any } | undefined>(params);
});

const handler: WebsocketHandler = async (connection, req, params) => {
expectType<SocketStream>(connection);
expectType<Server>(app.websocketServer);
expectType<IncomingMessage>(req)
expectType<{ [key: string]: any } | undefined>(params);
}

app.get('/websockets-via-annotated-const', { websocket: true }, handler);

app.get('/not-specifed', async (request, reply) => {
expectType<FastifyRequest>(request);
expectType<FastifyReply>(reply)
});

app.get('/not-websockets', { websocket: false }, async (request, reply) => {
expectType<FastifyRequest>(request);
expectType<FastifyReply>(reply);
});