Skip to content
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
22 changes: 12 additions & 10 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@ declare module 'fastify' {

export type WebsocketHandler = (
this: FastifyInstance<Server, IncomingMessage, ServerResponse>,
connection: SocketStream,
connection: websocketPlugin.SocketStream,
request: IncomingMessage,
params?: { [key: string]: any }
) => void | Promise<any>;
}

export interface SocketStream extends Duplex {
socket: WebSocket;
}
declare const websocketPlugin: FastifyPlugin<websocketPlugin.WebsocketPluginOptions>;

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<WebsocketPluginOptions>;
interface WebsocketPluginOptions {
handle?: (this: FastifyInstance, connection: SocketStream) => void;
options?: WebSocket.ServerOptions;
}
}

export default websocketPlugin;
export = websocketPlugin;
10 changes: 5 additions & 5 deletions test/types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import wsPlugin, { SocketStream } from '../..';
import * as wsPlugin from '../..';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ethan-Arrowood @mfpopa @fox1t can you please explain this to me?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll defer to @fox1t as he has a better understanding of this than I do

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked out this branch and tested with both esModuleInterop set to true and false (it's false by default). I see find no issues.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To clarify, when using "esModuleInterop": false (the default), import needs to be done like this import * as wsPlugin from '../..';. When using "esModuleInterop": true, import needs to be done like this import wsPlugin from '../..';.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It works because export = syntax exports a so called namespace that is then imported by import *. Export default has to be used only in presence of .default prop on the exported object.

import fastify, { WebsocketHandler, FastifyRequest, FastifyInstance, RequestGenericInterface, FastifyReply } from 'fastify';
import { expectType } from 'tsd';
import { Server as HttpServer, IncomingMessage } from 'http'
Expand All @@ -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<FastifyInstance>(this);
expectType<SocketStream>(connection)
expectType<wsPlugin.SocketStream>(connection)
}
});
app.register(wsPlugin, { options: { perMessageDeflate: true } });

app.get('/websockets-via-inferrence', { websocket: true }, async function(connection, req, params) {
expectType<FastifyInstance>(this);
expectType<SocketStream>(connection);
expectType<wsPlugin.SocketStream>(connection);
expectType<Server>(app.websocketServer);
expectType<IncomingMessage>(req)
expectType<{ [key: string]: any } | undefined>(params);
});

const handler: WebsocketHandler = async (connection, req, params) => {
expectType<SocketStream>(connection);
expectType<wsPlugin.SocketStream>(connection);
expectType<Server>(app.websocketServer);
expectType<IncomingMessage>(req)
expectType<{ [key: string]: any } | undefined>(params);
Expand Down