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

WebSocket beforeHandle unable to access use'd plugins / state / derive's #59

Closed
bigbeno37 opened this issue Jun 22, 2023 · 1 comment · Fixed by #92
Closed

WebSocket beforeHandle unable to access use'd plugins / state / derive's #59

bigbeno37 opened this issue Jun 22, 2023 · 1 comment · Fixed by #92

Comments

@bigbeno37
Copy link

bigbeno37 commented Jun 22, 2023

I'd like to have the ability to check for certain cookies before allowing a WebSocket connection, as well as have access to the Elysia context. Here's some demo code:

import { Elysia, ws } from 'elysia'
import cookie from '@elysiajs/cookie';

new Elysia()
	.use(cookie())
	.use(ws())
	.state('count', 0)
	.ws('/ws', {
		beforeHandle({ cookie, store }) {
			if (!cookie['authToken']) return new Response(null, { status: 401 });

			console.log(store.count);
		}
	})
	.listen(8080)

When I run tsc, I get these errors for cookie in the params and store.count:

index.ts:9:18 - error TS2339: Property 'cookie' does not exist on type '{ request: Request; headers: Record<string, string | null>; query: Record<string, unknown>; params: Record<never, string>; body: unknown; store: {}; set: { headers: Record<...>; status?: number | undefined; redirect?: string | undefined; }; } & Record<...>'.

9   beforeHandle({ cookie, store }) {
                   ~~~~~~

index.ts:12:22 - error TS2339: Property 'count' does not exist on type '{}'.

12    console.log(store.count);
                        ~~~~~


Found 2 errors in the same file, starting at: index.ts:9

Ideally, I should be able to access anything available on the context in these WebSocket handlers as I would in regular HTTP endpoints.

I'm currently using Bun 0.6.8, and bun pm ls outputs

├── @elysiajs/cookie@0.5.2
├── bun-types@0.6.9
├── elysia@0.5.19
└── typescript@5.1.3

EDIT: This might not be relevant for right now, but I'd like to see the notion of a derive in the context of WebSockets, e.g. some way to add data to the "ws" objects passed into open / message / close. This'd be real handy if I wanted to attach certain data, such as user IDs.

@hesxenon
Copy link
Contributor

@bigbeno37 can you check if the derivations you'd like to see aren't already added by e.g. logging ws.data? Since other derivations are indeed added (but haven't been exposed by the types) I think your request has already been fulfilled?

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 a pull request may close this issue.

2 participants