diff --git a/index.js b/index.js index 1868dca..7286240 100644 --- a/index.js +++ b/index.js @@ -194,3 +194,5 @@ module.exports = fp(fastifyWebsocket, { fastify: '>= 4.0.0', name: '@fastify/websocket' }) +module.exports.default = fastifyWebsocket +module.exports.fastifyWebsocket = fastifyWebsocket diff --git a/types/index.d.ts b/types/index.d.ts index 71086cf..b04fc49 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -16,7 +16,7 @@ interface WebsocketRouteOptions< TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, Logger extends FastifyBaseLogger = FastifyBaseLogger > { - wsHandler?: WebsocketHandler; + wsHandler?: fastifyWebsocket.WebsocketHandler; } declare module 'fastify' { @@ -44,7 +44,7 @@ declare module 'fastify' { ( path: string, opts: RouteShorthandOptions & { websocket: true }, // this creates an overload that only applies these different types if the handler is for websockets - handler?: WebsocketHandler + handler?: fastifyWebsocket.WebsocketHandler ): FastifyInstance; } @@ -60,43 +60,50 @@ declare module 'fastify' { > extends WebsocketRouteOptions { } } -declare const websocketPlugin: FastifyPluginCallback; +type FastifyWebsocket = FastifyPluginCallback; -interface WebSocketServerOptions extends Omit { } +declare namespace fastifyWebsocket { -export type WebsocketHandler< - RawServer extends RawServerBase = RawServerDefault, - RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, - RequestGeneric extends RequestGenericInterface = RequestGenericInterface, - ContextConfig = ContextConfigDefault, - SchemaCompiler extends FastifySchema = FastifySchema, - TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, - Logger extends FastifyBaseLogger = FastifyBaseLogger -> = ( - this: FastifyInstance, - connection: SocketStream, - request: FastifyRequest -) => void | Promise; + interface WebSocketServerOptions extends Omit { } + + export type WebsocketHandler< + RawServer extends RawServerBase = RawServerDefault, + RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, + RequestGeneric extends RequestGenericInterface = RequestGenericInterface, + ContextConfig = ContextConfigDefault, + SchemaCompiler extends FastifySchema = FastifySchema, + TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, + Logger extends FastifyBaseLogger = FastifyBaseLogger + > = ( + this: FastifyInstance, + connection: SocketStream, + request: FastifyRequest + ) => void | Promise; + + export interface SocketStream extends Duplex { + socket: WebSocket; + } + + export interface WebsocketPluginOptions { + errorHandler?: (this: FastifyInstance, error: Error, connection: SocketStream, request: FastifyRequest, reply: FastifyReply) => void; + options?: WebSocketServerOptions; + connectionOptions?: DuplexOptions; + } -export interface SocketStream extends Duplex { - socket: WebSocket; -} + export interface RouteOptions< + RawServer extends RawServerBase = RawServerDefault, + RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, + RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, + RouteGeneric extends RouteGenericInterface = RouteGenericInterface, + ContextConfig = ContextConfigDefault, + SchemaCompiler extends fastify.FastifySchema = fastify.FastifySchema, + TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, + Logger extends FastifyBaseLogger = FastifyBaseLogger + > extends fastify.RouteOptions, WebsocketRouteOptions { } -export interface WebsocketPluginOptions { - errorHandler?: (this: FastifyInstance, error: Error, connection: SocketStream, request: FastifyRequest, reply: FastifyReply) => void; - options?: WebSocketServerOptions; - connectionOptions?: DuplexOptions; + export const websocketPlugin: FastifyWebsocket + export { websocketPlugin as default } } -export interface RouteOptions< - RawServer extends RawServerBase = RawServerDefault, - RawRequest extends RawRequestDefaultExpression = RawRequestDefaultExpression, - RawReply extends RawReplyDefaultExpression = RawReplyDefaultExpression, - RouteGeneric extends RouteGenericInterface = RouteGenericInterface, - ContextConfig = ContextConfigDefault, - SchemaCompiler extends fastify.FastifySchema = fastify.FastifySchema, - TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, - Logger extends FastifyBaseLogger = FastifyBaseLogger -> extends fastify.RouteOptions, WebsocketRouteOptions { } - -export default websocketPlugin; +declare function fastifyWebsocket(...params: Parameters): ReturnType +export = fastifyWebsocket \ No newline at end of file