Skip to content

Commit

Permalink
Fix PluginOptions incorrect typings (#53)
Browse files Browse the repository at this point in the history
* fix(types): indicate PluginOptions properties as optional

addtionally, tests were added to cover this change

* style(types): clean up typescript declaration file

remove unused imports & clean up dangling whitespaces
  • Loading branch information
felixputera authored Mar 28, 2020
1 parent 300a478 commit 30c3ae3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
7 changes: 3 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/// <reference types="node" />
import * as fastify from 'fastify';
import { IncomingMessage, ServerResponse, Server } from 'http';
import { Plugin } from 'fastify';
import * as WebSocket from 'ws';
Expand Down Expand Up @@ -40,7 +39,7 @@ declare module 'fastify' {
>,
handler?: WebsocketHandler<HttpRequest, HttpResponse>
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>;

websocketServer: WebSocket.Server;
}

Expand Down Expand Up @@ -84,8 +83,8 @@ declare namespace websocketPlugin {
}

export interface PluginOptions {
handle: (connection: SocketStream) => void;
options: WebSocket.ServerOptions;
handle?: (connection: SocketStream) => void;
options?: WebSocket.ServerOptions;
}
}

Expand Down
9 changes: 6 additions & 3 deletions test/types/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import { WebsocketHandler, FastifyRequest, FastifyInstance } from 'fastify';
import { expectType } from 'tsd';
import { Server } from 'ws';

const app: FastifyInstance = fastify();
app.register(wsPlugin);

const handler: WebsocketHandler = (
connection: SocketStream,
req: FastifyRequest,
Expand All @@ -18,4 +15,10 @@ const handler: WebsocketHandler = (
expectType<{ [key: string]: any } | undefined>(params);
};

const app: FastifyInstance = fastify();
app.register(wsPlugin);
app.register(wsPlugin, {});
app.register(wsPlugin, { handler });
app.register(wsPlugin, { options: { perMessageDeflate: true } });

app.get('/', { websocket: true }, handler);

0 comments on commit 30c3ae3

Please sign in to comment.