Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic route options #176

Merged
merged 2 commits into from
Feb 27, 2022
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
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;
38 changes: 35 additions & 3 deletions test/types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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<FastifyInstance>(this);
expectType<SocketStream>(connection);
expectType<Server>(app.websocketServer);
Expand Down Expand Up @@ -52,7 +54,7 @@ app.route({
},
wsHandler: (connection, request) => {
expectType<SocketStream>(connection);
expectType<FastifyRequest<RequestGenericInterface>>(request);
expectType<FastifyRequest<RouteGenericInterface>>(request);
},
});

Expand All @@ -65,7 +67,37 @@ const augmentedRouteOptions: RouteOptions = {
},
wsHandler: (connection, request) => {
expectType<SocketStream>(connection);
expectType<FastifyRequest<RequestGenericInterface>>(request)
expectType<FastifyRequest<RouteGenericInterface>>(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<SocketStream>(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<IncomingMessage['headers'] & { auth: string }>(request.headers);
},
wsHandler: (connection, request) => {
expectType<SocketStream>(connection);
expectType<{ foo: string }>(request.params);
expectType<{ bar: string }>(request.body);
expectType<{ search: string }>(request.query);
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers);
},
});