question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebsocketHandler Request generics don't work the same as http Handlers

See original GitHub issue

🐛 Bug Report

In a TypeScript project, I am using the new 3.0.0 WebsocketHandler interface that provides FastifyRequest. I want to declare the request Header type so I can access them without casting or using //@ts-ignore as I do with HTTP request handlers.

Here is an example

  server.get("/ws",  {
      websocket: true,
      schema: {
        headers: TenantHeaderSchema
      }
    },
    // compiler error, see below
    createWSHandler())

...

function createWSHandler() {
  return (conn:     SocketStream,
          request:  FastifyRequest<{Headers: TenantHTTPHeader}>) => {
    const {tenant} = request.headers
...

Here is the compiler error:

 Overload 1 of 4, '(path: string, opts: RouteShorthandOptions<Server, IncomingMessage, ServerResponse, RequestGenericInterface, unknown> & { ...; }, handler?: WebsocketHandler | undefined): FastifyInstance<...>', gave the following error.
    Argument of type '(conn: SocketStream, request: FastifyRequest<{    Headers: TenantHTTPHeader;}>) => void' is not assignable to parameter of type 'WebsocketHandler'.
      Types of parameters 'request' and 'request' are incompatible.
        Type 'FastifyRequest<RouteGenericInterface, Server, IncomingMessage>' is not assignable to type 'FastifyRequest<{ Headers: TenantHTTPHeader; }, Server, IncomingMessage>'.
          Type 'RouteGenericInterface' is not assignable to type '{ Headers: TenantHTTPHeader; }'.
            Types of property 'Headers' are incompatible.
              Type 'unknown' is not assignable to type 'TenantHTTPHeader'.

Expected behavior

I expect the FastifyRequest declaration to accept Headers declarations as they do in other get() handlers. An example that works with the same schema and types is below:

  server.get("/data", {
      schema: {
        headers: TenantHeaderSchema
      }
    },
    createGetDataHandler())

...

function createGetDataHandler() {
  return async (request:  FastifyRequest<{Headers: TenantHTTPHeader}>,
                reply:    FastifyReply) => {
    const {tenant} = request.headers

Your Environment

  • node version: 12.20.0
  • fastify version: >=3.11.0
  • fastify-websocket version: 3.0.0
  • os: Mac

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
mattbishopcommented, May 4, 2021

This issue has been fixed by https://github.com/fastify/fastify-websocket/pull/112

Thanks!

0reactions
Ethan-Arrowoodcommented, Apr 1, 2021

Could you merge over the RouteOptions type instead?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shared state between WebSocketHandler and RequestHandler
I am writing chat app and I need session data in my websocket handler. Problem is that login and logout is done by...
Read more >
Revisiting HTTP Handlers - Learn Go with tests - GitBook
Testing HTTP handlers seems to be a recurring question in the Go ... so long as the interface remains the same you don't...
Read more >
I Don't Like Go's Default HTTP Handlers : r/golang - Reddit
Honestly I think gRPC/http-gateway got it right. Having concrete types for the request and response as well as an error return value.
Read more >
Consumers — Channels 4.0.0 documentation
The AsyncConsumer is laid out very similarly, but all the handler methods must be coroutines, and self.send is a coroutine: from channels.consumer import ......
Read more >
Allow generics in route handlers #408 - SergioBenitez/Rocket
Seems like a reasonable request, though I'm not sure how this would work. Before I start, I want to note that if we...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found