Skip to content

Types: WebsocketRouteOptions is not generic and results in handler not being typed properly #127

@VottusCode

Description

@VottusCode

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure it has not already been reported

Fastify version

3.15.1

Node.js version

14.5.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

20.04

Description

The WebsocketRouteOptions doesn't accept generics, so WebsocketHandler is left with the defaults, not with the appropriate types.

Steps to Reproduce

import fastify from "fastify"
import fastifyWs from "fastify-websocket"

const fastify = fastify()
fastify.register(fastifyWs)

Let's say we have this interface declared:

interface AuthenticatedEndpoint extends RequestGenericInterface {
  Body: {
    auth: string
  }
}

When using it with app.route:

fastify.route<AuthenticatedEndpoint>({
  method: 'GET',
  url: '/ws',
  wsHandler(conn, req)  {
    req.body // typed as `unknown`
    req.body.auth // TypeError
  }
})

Expected Behavior

(assuming we have the same starting code as we had in Steps to Reproduce)

fastify.route<AuthenticatedEndpoint>({
  method: 'GET',
  url: '/ws',
  wsHandler(conn, req)  {
    req.body // typed as { auth: string }
    req.body.auth // string
  }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions