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
13 changes: 12 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,21 @@ declare module 'fastify' {
type InjectWSFn<RawRequest> =
((path?: string, upgradeContext?: Partial<RawRequest>, options?: InjectWSOption) => Promise<WebSocket>)

// eslint-disable-next-line @typescript-eslint/no-unused-vars
interface FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider> {
websocketServer: WebSocket.Server,
injectWS: InjectWSFn<RawRequest>

// When `websocket: true` is set, `handler` is the websocket handler, so type it as such
route<
RouteGeneric extends RouteGenericInterface = RouteGenericInterface,
ContextConfig = ContextConfigDefault,
const SchemaCompiler extends FastifySchema = FastifySchema
>(
opts: Omit<RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>, 'handler' | 'websocket'> & {
websocket: true,
handler: fastifyWebsocket.WebsocketHandler<RawServer, RawRequest, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider, Logger>
}
): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;
}

interface FastifyRequest {
Expand Down
58 changes: 58 additions & 0 deletions types/index.tst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,48 @@ app.route<{
}
})

app.route({
method: 'GET',
url: '/route-full-declaration-syntax-websocket',
websocket: true,
handler: async function (socket, request) {
expect(this).type.toBe<FastifyInstance>()
expect(socket).type.toBe<WebSocket>()
expect(request).type.toBe<FastifyRequest<RouteGenericInterface>>()
expect(request.ws).type.toBe<boolean>()
}
})

app.route({
method: 'GET',
url: '/route-full-declaration-syntax-not-websocket',
websocket: false,
handler: (request, reply) => {
expect(request).type.toBe<FastifyRequest>()
expect(reply).type.toBe<FastifyReply>()
}
})

app.route<{
Params: { foo: string };
Body: { bar: string };
Querystring: { search: string };
Headers: { auth: string };
}>({
method: 'GET',
url: '/longhand-explicit-types-websocket',
websocket: true,
handler: (socket, request) => {
expect(socket).type.toBe<WebSocket>()
expect(request.params).type.toBe<{ foo: string }>()
expect(request.body).type.toBe<{ bar: string }>()
expect(request.query).type.toBe<{ search: string }>()
expect(request.headers).type.toBe<
IncomingMessage['headers'] & { auth: string }
>()
}
})

const schema = {
params: Type.Object({ foo: Type.String() }, { required: ['foo'] }),
querystring: Type.Object({ search: Type.String() }, { required: ['search'] }),
Expand Down Expand Up @@ -187,6 +229,22 @@ server.get(
}
)

server.route({
method: 'GET',
url: '/longhand-type-inference-websocket',
schema,
websocket: true,
handler: (socket, request) => {
expect(socket).type.toBe<WebSocket>()
expect(request.params).type.toBe<{ foo: string }>()
expect(request.body).type.toBe<{ bar: string }>()
expect(request.query).type.toBe<{ search: string }>()
expect(request.headers).type.toBe<
IncomingMessage['headers'] & { auth: string }
>()
}
})

expect(namedFastifyWebsocket).type.toBe(fastifyWebsocket)
expect(fastifyWebsocket).type.toBe(fastifyWebsocket)

Expand Down
Loading