Skip to content

Commit 30c3ae3

Browse files
authored
Fix PluginOptions incorrect typings (#53)
* 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
1 parent 300a478 commit 30c3ae3

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

index.d.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/// <reference types="node" />
2-
import * as fastify from 'fastify';
32
import { IncomingMessage, ServerResponse, Server } from 'http';
43
import { Plugin } from 'fastify';
54
import * as WebSocket from 'ws';
@@ -40,7 +39,7 @@ declare module 'fastify' {
4039
>,
4140
handler?: WebsocketHandler<HttpRequest, HttpResponse>
4241
): FastifyInstance<HttpServer, HttpRequest, HttpResponse>;
43-
42+
4443
websocketServer: WebSocket.Server;
4544
}
4645

@@ -84,8 +83,8 @@ declare namespace websocketPlugin {
8483
}
8584

8685
export interface PluginOptions {
87-
handle: (connection: SocketStream) => void;
88-
options: WebSocket.ServerOptions;
86+
handle?: (connection: SocketStream) => void;
87+
options?: WebSocket.ServerOptions;
8988
}
9089
}
9190

test/types/index.test-d.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ import { WebsocketHandler, FastifyRequest, FastifyInstance } from 'fastify';
55
import { expectType } from 'tsd';
66
import { Server } from 'ws';
77

8-
const app: FastifyInstance = fastify();
9-
app.register(wsPlugin);
10-
118
const handler: WebsocketHandler = (
129
connection: SocketStream,
1310
req: FastifyRequest,
@@ -18,4 +15,10 @@ const handler: WebsocketHandler = (
1815
expectType<{ [key: string]: any } | undefined>(params);
1916
};
2017

18+
const app: FastifyInstance = fastify();
19+
app.register(wsPlugin);
20+
app.register(wsPlugin, {});
21+
app.register(wsPlugin, { handler });
22+
app.register(wsPlugin, { options: { perMessageDeflate: true } });
23+
2124
app.get('/', { websocket: true }, handler);

0 commit comments

Comments
 (0)