Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(treaty2): ws response data type #122

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

ap0nia
Copy link

@ap0nia ap0nia commented Aug 18, 2024

Summary

EdenWS has the wrong event.data type for all subscription-related listeners.

Expected

When defining a response for the message, Eden should show that same type in the event.data for the callback argument in EdenWS.on, EdenWS.subscribe, and EdenWS.addEventListener.

Solution

  • Expects to receive the actual data type as the second generic here
  • Currently, it is being provided the entire response here
  • To fix this, the EdenWS class should provided Schema['response'][200] as the data type for the second generic argument in Treaty.WSEvent
type Response = {
  // 200 is the HTTP code, and string is the actual response data.
  200: string
}

type EdenWSEvent_WRONG = Treaty.WSEvent<keyof WebSocketEventMap, Response>

type EdenWSEvent_CORRECT = Treaty.WSEvent<keyof WebSocketEventMap, Response[200]>

Minimal Reproduction

import { Elysia, t } from 'elysia'
import { treaty } from '@elysiajs/eden'

const app = new Elysia().ws('/chat', {
    body: t.Object({ message: t.String() }),
    response: t.String(),
    message: async (_ws, body) => {
        body.message
    }
})

const api = treaty<typeof app>()

const subscription = api.chat.subscribe()

subscription.on('message', (event) => {
    event.data
    // ^? is { 200: { message: string } } instead of { message: string }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants