Skip to content

Commit 33c1994

Browse files
committed
Add typescript tests for overriding the route option generics for websocket routes
1 parent 65bacd9 commit 33c1994

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

test/types/index.test-d.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import wsPlugin, { WebsocketHandler, SocketStream } from '../..';
2+
import type {IncomingMessage} from "http";
23
import fastify, { RouteOptions, FastifyRequest, FastifyInstance, FastifyReply, RequestGenericInterface } from 'fastify';
34
import { expectType } from 'tsd';
45
import { Server } from 'ws';
@@ -19,12 +20,12 @@ app.register(wsPlugin, {
1920
});
2021
app.register(wsPlugin, { options: { perMessageDeflate: true } });
2122

22-
app.get('/websockets-via-inferrence', { websocket: true }, async function(connection, request) {
23-
expectType<FastifyInstance>(this);
24-
expectType<SocketStream>(connection);
25-
expectType<Server>(app.websocketServer);
26-
expectType<FastifyRequest<RequestGenericInterface>>(request)
27-
});
23+
app.get('/websockets-via-inferrence', { websocket: true }, async function (connection, request) {
24+
expectType<FastifyInstance>(this);
25+
expectType<SocketStream>(connection);
26+
expectType<Server>(app.websocketServer);
27+
expectType<FastifyRequest<RequestGenericInterface>>(request)
28+
});
2829

2930
const handler: WebsocketHandler = async (connection, request) => {
3031
expectType<SocketStream>(connection);
@@ -70,3 +71,33 @@ const augmentedRouteOptions: RouteOptions = {
7071
},
7172
};
7273
app.route(augmentedRouteOptions);
74+
75+
76+
app.get<{ Params: { foo: string }, Body: { bar: string }, Querystring: { search: string }, Headers: { auth: string } }>('/shorthand-explicit-types', {
77+
websocket: true
78+
}, async (connection, request) => {
79+
expectType<SocketStream>(connection);
80+
expectType<{ foo: string }>(request.params);
81+
expectType<{ bar: string }>(request.body);
82+
expectType<{ search: string }>(request.query);
83+
expectType< IncomingMessage['headers'] & { auth: string }>(request.headers);
84+
});
85+
86+
87+
app.route<{ Params: { foo: string }, Body: { bar: string }, Querystring: { search: string }, Headers: { auth: string } }>({
88+
method: 'GET',
89+
url: '/longhand-explicit-types',
90+
handler: (request, _reply) => {
91+
expectType<{ foo: string }>(request.params);
92+
expectType<{ bar: string }>(request.body);
93+
expectType<{ search: string }>(request.query);
94+
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers);
95+
},
96+
wsHandler: (connection, request) => {
97+
expectType<SocketStream>(connection);
98+
expectType<{ foo: string }>(request.params);
99+
expectType<{ bar: string }>(request.body);
100+
expectType<{ search: string }>(request.query);
101+
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers);
102+
},
103+
});

0 commit comments

Comments
 (0)