@fastify/websocket
Advanced tools
Comparing version 7.0.1 to 7.1.0
/// <reference types="node" /> | ||
import { IncomingMessage, ServerResponse, Server } from 'http'; | ||
import { FastifyRequest, FastifyPluginCallback, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, RequestGenericInterface, ContextConfigDefault, FastifyInstance} from 'fastify'; | ||
import { FastifyRequest, FastifyPluginCallback, RawServerBase, RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, RequestGenericInterface, ContextConfigDefault, FastifyInstance, FastifySchema, FastifyTypeProvider, FastifyTypeProviderDefault, FastifyBaseLogger } from 'fastify'; | ||
import * as fastify from 'fastify'; | ||
@@ -10,4 +10,12 @@ import * as WebSocket from 'ws'; | ||
interface WebsocketRouteOptions<RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>, RequestGeneric extends RequestGenericInterface = RequestGenericInterface> { | ||
wsHandler?: WebsocketHandler<RawServer, RawRequest, RequestGeneric>; | ||
interface WebsocketRouteOptions< | ||
RawServer extends RawServerBase = RawServerDefault, | ||
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>, | ||
RequestGeneric extends RequestGenericInterface = RequestGenericInterface, | ||
ContextConfig = ContextConfigDefault, | ||
SchemaCompiler extends FastifySchema = FastifySchema, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, | ||
Logger extends FastifyBaseLogger = FastifyBaseLogger | ||
> { | ||
wsHandler?: WebsocketHandler<RawServer, RawRequest, RequestGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>; | ||
} | ||
@@ -35,11 +43,21 @@ | ||
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, | ||
> { | ||
<RequestGeneric extends RequestGenericInterface = RequestGenericInterface, ContextConfig = ContextConfigDefault>( | ||
<RequestGeneric extends RequestGenericInterface = RequestGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler extends FastifySchema = FastifySchema, Logger extends FastifyBaseLogger = FastifyBaseLogger>( | ||
path: string, | ||
opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig> & { websocket: true }, // this creates an overload that only applies these different types if the handler is for websockets | ||
handler?: WebsocketHandler<RawServer, RawRequest, RequestGeneric> | ||
): FastifyInstance<RawServer, RawRequest, RawReply>; | ||
opts: RouteShorthandOptions<RawServer, RawRequest, RawReply, RequestGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> & { websocket: true }, // this creates an overload that only applies these different types if the handler is for websockets | ||
handler?: WebsocketHandler<RawServer, RawRequest, RequestGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> | ||
): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>; | ||
} | ||
interface RouteOptions<RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>, RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault,SchemaCompiler = fastify.FastifySchema> extends WebsocketRouteOptions<RawServer, RawRequest, RouteGeneric> {} | ||
interface RouteOptions< | ||
RawServer extends RawServerBase = RawServerDefault, | ||
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>, | ||
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>, | ||
RouteGeneric extends RouteGenericInterface = RouteGenericInterface, | ||
ContextConfig = ContextConfigDefault, | ||
SchemaCompiler = fastify.FastifySchema, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, | ||
Logger extends FastifyBaseLogger = FastifyBaseLogger | ||
> extends WebsocketRouteOptions<RawServer, RawRequest, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> { } | ||
} | ||
@@ -49,3 +67,3 @@ | ||
interface WebSocketServerOptions extends Omit<WebSocket.ServerOptions, "path"> {} | ||
interface WebSocketServerOptions extends Omit<WebSocket.ServerOptions, "path"> { } | ||
@@ -55,7 +73,11 @@ export type WebsocketHandler< | ||
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>, | ||
RequestGeneric extends RequestGenericInterface = RequestGenericInterface | ||
RequestGeneric extends RequestGenericInterface = RequestGenericInterface, | ||
ContextConfig = ContextConfigDefault, | ||
SchemaCompiler extends FastifySchema = FastifySchema, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, | ||
Logger extends FastifyBaseLogger = FastifyBaseLogger | ||
> = ( | ||
this: FastifyInstance<Server, IncomingMessage, ServerResponse>, | ||
connection: SocketStream, | ||
request: FastifyRequest<RequestGeneric, RawServer, RawRequest>, | ||
request: FastifyRequest<RequestGeneric, RawServer, RawRequest, SchemaCompiler, TypeProvider, ContextConfig, Logger> | ||
) => void | Promise<any>; | ||
@@ -73,4 +95,13 @@ | ||
export interface RouteOptions<RawServer extends RawServerBase = RawServerDefault, RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>, RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>, RouteGeneric extends RouteGenericInterface = RouteGenericInterface, ContextConfig = ContextConfigDefault, SchemaCompiler extends fastify.FastifySchema = fastify.FastifySchema> extends fastify.RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler>, WebsocketRouteOptions<RawServer, RawRequest, RouteGeneric> {} | ||
export interface RouteOptions< | ||
RawServer extends RawServerBase = RawServerDefault, | ||
RawRequest extends RawRequestDefaultExpression<RawServer> = RawRequestDefaultExpression<RawServer>, | ||
RawReply extends RawReplyDefaultExpression<RawServer> = RawReplyDefaultExpression<RawServer>, | ||
RouteGeneric extends RouteGenericInterface = RouteGenericInterface, | ||
ContextConfig = ContextConfigDefault, | ||
SchemaCompiler extends fastify.FastifySchema = fastify.FastifySchema, | ||
TypeProvider extends FastifyTypeProvider = FastifyTypeProviderDefault, | ||
Logger extends FastifyBaseLogger = FastifyBaseLogger | ||
> extends fastify.RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>, WebsocketRouteOptions<RawServer, RawRequest, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger> { } | ||
export default websocketPlugin; |
{ | ||
"name": "@fastify/websocket", | ||
"version": "7.0.1", | ||
"version": "7.1.0", | ||
"description": "basic websocket support for fastify", | ||
@@ -33,2 +33,4 @@ "main": "index.js", | ||
"@fastify/pre-commit": "^2.0.2", | ||
"@fastify/type-provider-typebox": "^2.3.0", | ||
"@sinclair/typebox": "^0.24.44", | ||
"@types/ws": "^8.2.2", | ||
@@ -40,3 +42,3 @@ "fastify": "^4.0.0-rc.2", | ||
"tap": "^16.0.0", | ||
"tsd": "^0.22.0" | ||
"tsd": "^0.24.1" | ||
}, | ||
@@ -43,0 +45,0 @@ "dependencies": { |
import wsPlugin, { WebsocketHandler, SocketStream } from '../..'; | ||
import type {IncomingMessage} from "http"; | ||
import fastify, { RouteOptions, FastifyRequest, FastifyInstance, FastifyReply, RequestGenericInterface } from 'fastify'; | ||
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'; | ||
import { Server } from 'ws'; | ||
import { RouteGenericInterface } from 'fastify/types/route'; | ||
import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'; | ||
import { Static, Type } from '@sinclair/typebox' | ||
import { ResolveFastifyRequestType } from 'fastify/types/type-provider'; | ||
@@ -108,1 +111,89 @@ const app: FastifyInstance = fastify(); | ||
}); | ||
const schema = { | ||
params: Type.Object({ | ||
foo: Type.String() | ||
}), | ||
querystring: Type.Object({ | ||
search: Type.String() | ||
}), | ||
body: Type.Object({ | ||
bar: Type.String() | ||
}), | ||
headers: Type.Object({ | ||
auth: Type.String() | ||
}) | ||
}; | ||
type SchemaType = { | ||
params: Static<typeof schema.params>; | ||
querystring: Static<typeof schema.querystring>; | ||
body: Static<typeof schema.body>; | ||
headers: Static<typeof schema.headers>; | ||
}; | ||
const server = app.withTypeProvider<TypeBoxTypeProvider>(); | ||
server.route({ | ||
method: 'GET', | ||
url: '/longhand-type-inference', | ||
schema, | ||
handler: (request, _reply) => { | ||
expectType<{ foo: string }>(request.params); | ||
expectType<{ bar: string }>(request.body); | ||
expectType<{ search: string }>(request.query); | ||
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers); | ||
}, | ||
wsHandler: (connection, request) => { | ||
expectType<SocketStream>(connection); | ||
expectType<{ foo: string }>(request.params); | ||
expectType<{ bar: string }>(request.body); | ||
expectType<{ search: string }>(request.query); | ||
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers); | ||
}, | ||
}); | ||
server.get('/websockets-type-inference', | ||
{ | ||
websocket: true, | ||
schema | ||
}, | ||
async function (connection, request) { | ||
expectType<FastifyInstance>(this); | ||
expectType<SocketStream>(connection); | ||
expectType<Server>(app.websocketServer); | ||
expectType<FastifyRequest<RequestGenericInterface, RawServerDefault, IncomingMessage, SchemaType, TypeBoxTypeProvider, unknown, FastifyBaseLogger>>(request); | ||
expectType<boolean>(request.ws); | ||
expectType<{ foo: string }>(request.params); | ||
expectType<{ bar: string }>(request.body); | ||
expectType<{ search: string }>(request.query); | ||
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers); | ||
}); | ||
server.get('/not-websockets-type-inference', | ||
{ | ||
websocket: false, | ||
schema | ||
}, | ||
async (request, reply) => { | ||
expectType<FastifyRequest<RouteGenericInterface, RawServerDefault, IncomingMessage, SchemaType, TypeBoxTypeProvider, unknown, FastifyBaseLogger, ResolveFastifyRequestType<TypeBoxTypeProvider, FastifySchema, RouteGenericInterface>>>(request); | ||
expectType<FastifyReply<RawServerDefault, RawRequestDefaultExpression, RawReplyDefaultExpression, RouteGenericInterface, ContextConfigDefault, SchemaType, TypeBoxTypeProvider>>(reply); | ||
expectType<{ foo: string }>(request.params); | ||
expectType<{ bar: string }>(request.body); | ||
expectType<{ search: string }>(request.query); | ||
expectType<IncomingMessage['headers'] & { auth: string }>(request.headers); | ||
}); | ||
server.get('/websockets-no-type-inference', | ||
{ websocket: true }, | ||
async function (connection, request) { | ||
expectType<FastifyInstance>(this); | ||
expectType<SocketStream>(connection); | ||
expectType<Server>(app.websocketServer); | ||
expectType<FastifyRequest<RequestGenericInterface, RawServerDefault, RawRequestDefaultExpression, FastifySchema, TypeBoxTypeProvider, unknown, FastifyBaseLogger>>(request); | ||
expectType<boolean>(request.ws); | ||
expectType<unknown>(request.params); | ||
expectType<unknown>(request.body); | ||
expectType<unknown>(request.query); | ||
expectType<IncomingMessage['headers']>(request.headers); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
80954
1828
10