Skip to content

Commit

Permalink
fix(types): websocketPlugin is not an existing variable (#269)
Browse files Browse the repository at this point in the history
* fix: websocketPlugin is not an existing variable

* test: add named-import.test-d.ts

* fix typings DRY

---------

Co-authored-by: uzlopak <aras.abbasi@googlemail.com>
  • Loading branch information
Yovach and Uzlopak authored Oct 16, 2023
1 parent d7adb45 commit 2af90ac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
4 changes: 2 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ declare namespace fastifyWebsocket {
Logger extends FastifyBaseLogger = FastifyBaseLogger
> extends fastify.RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>, WebsocketRouteOptions<RawServer, RawRequest, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> { }

export const websocketPlugin: FastifyWebsocket
export { websocketPlugin as default }
export const fastifyWebsocket: FastifyWebsocket
export { fastifyWebsocket as default }
}

declare function fastifyWebsocket(...params: Parameters<FastifyWebsocket>): ReturnType<FastifyWebsocket>
Expand Down
22 changes: 13 additions & 9 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import wsPlugin, { WebsocketHandler, SocketStream } from '..';
import fastifyWebsocket, { WebsocketHandler, SocketStream, fastifyWebsocket as namedFastifyWebsocket, default as defaultFastifyWebsocket } from '..';
import type { IncomingMessage } from "http";
import fastify, { RouteOptions, FastifyRequest, FastifyInstance, FastifyReply, RequestGenericInterface, FastifyBaseLogger, RawServerDefault, FastifySchema, RawRequestDefaultExpression, RawServerBase, ContextConfigDefault, RawReplyDefaultExpression } from 'fastify';
import { expectType } from 'tsd';
Expand All @@ -9,10 +9,10 @@ import { Static, Type } from '@sinclair/typebox'
import { ResolveFastifyRequestType } from 'fastify/types/type-provider';

const app: FastifyInstance = fastify();
app.register(wsPlugin);
app.register(wsPlugin, {});
app.register(wsPlugin, { options: { maxPayload: 123 } });
app.register(wsPlugin, {
app.register(fastifyWebsocket);
app.register(fastifyWebsocket, {});
app.register(fastifyWebsocket, { options: { maxPayload: 123 } });
app.register(fastifyWebsocket, {
errorHandler: function errorHandler(error: Error, connection: SocketStream, request: FastifyRequest, reply: FastifyReply): void {
expectType<FastifyInstance>(this);
expectType<Error>(error)
Expand All @@ -21,9 +21,9 @@ app.register(wsPlugin, {
expectType<FastifyReply>(reply)
}
});
app.register(wsPlugin, { options: { perMessageDeflate: true } });
app.register(wsPlugin, { preClose: function syncPreclose() {} });
app.register(wsPlugin, { preClose: async function asyncPreclose(){} });
app.register(fastifyWebsocket, { options: { perMessageDeflate: true } });
app.register(fastifyWebsocket, { preClose: function syncPreclose() {} });
app.register(fastifyWebsocket, { preClose: async function asyncPreclose(){} });

app.get('/websockets-via-inferrence', { websocket: true }, async function (connection, request) {
expectType<FastifyInstance>(this);
Expand Down Expand Up @@ -197,4 +197,8 @@ server.get('/websockets-no-type-inference',
expectType<unknown>(request.body);
expectType<unknown>(request.query);
expectType<IncomingMessage['headers']>(request.headers);
});
});

expectType<typeof fastifyWebsocket>(namedFastifyWebsocket);
expectType<typeof fastifyWebsocket>(defaultFastifyWebsocket);

0 comments on commit 2af90ac

Please sign in to comment.