Skip to content

Commit d03f101

Browse files
authored
Merge pull request #176 from fastify/generic-route-options
Generic route options
2 parents a0ec207 + 33c1994 commit d03f101

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

index.d.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ import * as fastify from 'fastify';
55
import * as WebSocket from 'ws';
66
import { Duplex } from 'stream';
77
import { FastifyReply } from 'fastify/types/reply';
8+
import { RouteGenericInterface } from 'fastify/types/route';
89

9-
interface WebsocketRouteOptions {
10-
wsHandler?: WebsocketHandler
10+
interface WebsocketRouteOptions<RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>, RequestGeneric extends RequestGenericInterface = RequestGenericInterface> {
11+
wsHandler?: WebsocketHandler<RawServer, RawRequest, RequestGeneric>;
1112
}
13+
1214
declare module 'fastify' {
1315
interface RouteShorthandOptions<
1416
RawServer extends RawServerBase = RawServerDefault
@@ -33,13 +35,12 @@ declare module 'fastify' {
3335
): FastifyInstance<RawServer, RawRequest, RawReply>;
3436
}
3537

36-
interface RouteOptions extends WebsocketRouteOptions {}
38+
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> {}
3739
}
3840

3941
declare const websocketPlugin: FastifyPluginCallback<WebsocketPluginOptions>;
4042

41-
interface WebSocketServerOptions extends Omit<WebSocket.ServerOptions, 'path'> {}
42-
43+
interface WebSocketServerOptions extends Omit<WebSocket.ServerOptions, "path"> {}
4344

4445
export type WebsocketHandler<
4546
RawServer extends RawServerBase = RawServerDefault,
@@ -60,6 +61,6 @@ export interface WebsocketPluginOptions {
6061
options?: WebSocketServerOptions;
6162
}
6263

63-
export interface RouteOptions extends fastify.RouteOptions, WebsocketRouteOptions {}
64+
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> {}
6465

6566
export default websocketPlugin;

test/types/index.test-d.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
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';
6+
import { RouteGenericInterface } from 'fastify/types/route';
57

68
const app: FastifyInstance = fastify();
79
app.register(wsPlugin);
@@ -18,7 +20,7 @@ app.register(wsPlugin, {
1820
});
1921
app.register(wsPlugin, { options: { perMessageDeflate: true } });
2022

21-
app.get('/websockets-via-inferrence', { websocket: true }, async function(connection, request) {
23+
app.get('/websockets-via-inferrence', { websocket: true }, async function (connection, request) {
2224
expectType<FastifyInstance>(this);
2325
expectType<SocketStream>(connection);
2426
expectType<Server>(app.websocketServer);
@@ -52,7 +54,7 @@ app.route({
5254
},
5355
wsHandler: (connection, request) => {
5456
expectType<SocketStream>(connection);
55-
expectType<FastifyRequest<RequestGenericInterface>>(request);
57+
expectType<FastifyRequest<RouteGenericInterface>>(request);
5658
},
5759
});
5860

@@ -65,7 +67,37 @@ const augmentedRouteOptions: RouteOptions = {
6567
},
6668
wsHandler: (connection, request) => {
6769
expectType<SocketStream>(connection);
68-
expectType<FastifyRequest<RequestGenericInterface>>(request)
70+
expectType<FastifyRequest<RouteGenericInterface>>(request)
6971
},
7072
};
7173
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)