diff --git a/index.d.ts b/index.d.ts index e69a042..e08c84a 100644 --- a/index.d.ts +++ b/index.d.ts @@ -30,21 +30,23 @@ declare module 'fastify' { export type WebsocketHandler = ( this: FastifyInstance, - connection: SocketStream, + connection: websocketPlugin.SocketStream, request: IncomingMessage, params?: { [key: string]: any } ) => void | Promise; } -export interface SocketStream extends Duplex { - socket: WebSocket; -} +declare const websocketPlugin: FastifyPlugin; -export interface WebsocketPluginOptions { - handle?: (this: FastifyInstance, connection: SocketStream) => void; - options?: WebSocket.ServerOptions; -} +declare module websocketPlugin { + interface SocketStream extends Duplex { + socket: WebSocket; + } -declare const websocketPlugin: FastifyPlugin; + interface WebsocketPluginOptions { + handle?: (this: FastifyInstance, connection: SocketStream) => void; + options?: WebSocket.ServerOptions; + } +} -export default websocketPlugin; +export = websocketPlugin; diff --git a/test/types/index.test-d.ts b/test/types/index.test-d.ts index 7227720..5cbc185 100644 --- a/test/types/index.test-d.ts +++ b/test/types/index.test-d.ts @@ -1,4 +1,4 @@ -import wsPlugin, { SocketStream } from '../..'; +import * as wsPlugin from '../..'; import fastify, { WebsocketHandler, FastifyRequest, FastifyInstance, RequestGenericInterface, FastifyReply } from 'fastify'; import { expectType } from 'tsd'; import { Server as HttpServer, IncomingMessage } from 'http' @@ -9,23 +9,23 @@ app.register(wsPlugin); app.register(wsPlugin, {}); app.register(wsPlugin, { options: { maxPayload: 123 } }); app.register(wsPlugin, { - handle: function globalHandler(connection: SocketStream): void { + handle: function globalHandler(connection: wsPlugin.SocketStream): void { expectType(this); - expectType(connection) + expectType(connection) } }); app.register(wsPlugin, { options: { perMessageDeflate: true } }); app.get('/websockets-via-inferrence', { websocket: true }, async function(connection, req, params) { expectType(this); - expectType(connection); + expectType(connection); expectType(app.websocketServer); expectType(req) expectType<{ [key: string]: any } | undefined>(params); }); const handler: WebsocketHandler = async (connection, req, params) => { - expectType(connection); + expectType(connection); expectType(app.websocketServer); expectType(req) expectType<{ [key: string]: any } | undefined>(params);