samchon / nestia

NestJS Helper Libraries + TypeScript OpenAPI generator
https://nestia.io/
MIT License
1.79k stars 93 forks source link

Add a decorator to handle @MessageBody of NestJS gateways #557

Open kevpug opened 1 year ago

kevpug commented 1 year ago

A decorator for NestJS Gateways

I was about to pull the trigger to replace my entire validation with Nestia/Typia but didn't because I saw there was no gateway @MessageBody decorator replacement.

Would it be possible to add one ?

samchon commented 1 year ago

I don't have insight about this subject. Can you explain me more detaily?

In my case, when websocket being required, I used this library instead.

https://tgrid.com/en

kevpug commented 1 year ago

I'm referring to this: NestJS Gateway In brief, WebSockets in Nest.js utilize Gateways, marked with @WebSocketGateway(), to facilitate real-time communication. Gateways manage incoming messages using decorators such as @SubscribeMessage(). The @MessageBody() decorator extracts data from incoming messages. These gateways can respond in sync or asynchronously, while lifecycle hooks oversee initialization, connections, and disconnections.

Considering that Nestia Core provides access to essential decorators for safeguarding and validating data within controller routes, could it also potentially introduce a new decorator dedicated to NestJS Gateways? Specifically, a decorator that replaces the existing @MessageBody()? @MessageBody

The logic might resemble the TypedBody decorator, but adjusted to throw a WSException instead of the standard HTTP bad request error. Moreover, it would require utilizing something like context.switchToWs().getData() to retrieve and validate the client's message.