File tree Expand file tree Collapse file tree 4 files changed +24
-20
lines changed
Expand file tree Collapse file tree 4 files changed +24
-20
lines changed Original file line number Diff line number Diff line change 77 ServerToClientEvents ,
88 ClientToServerEvents ,
99 KickUser ,
10+ JoinRoom ,
1011} from '../../shared/interfaces/chat.interface' ;
1112import { Header } from '../components/header' ;
1213import { UserList } from '../components/list' ;
@@ -46,7 +47,7 @@ function Chat() {
4647 navigate ( { to : '/' , replace : true } ) ;
4748 } else {
4849 socket . on ( 'connect' , ( ) => {
49- const joinRoom = {
50+ const joinRoom : JoinRoom = {
5051 roomName,
5152 user : { socketId : socket . id , ...user } ,
5253 eventName : 'join_room' ,
@@ -88,7 +89,7 @@ function Chat() {
8889
8990 const sendMessage = ( message : string ) => {
9091 if ( user && socket && roomName ) {
91- const chatMessage = {
92+ const chatMessage : Message = {
9293 user : {
9394 userId : user . userId ,
9495 userName : user . userName ,
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ import {
1212import { RoomService } from '../../room/room.service' ;
1313import { PolicyHandler } from '../../casl/interfaces/policy.interface' ;
1414import {
15- EventName ,
15+ ClientToServerEvents ,
1616 Room as RoomType ,
1717 User ,
1818} from '../../../shared/interfaces/chat.interface' ;
@@ -23,7 +23,7 @@ export class ChatPoliciesGuard<
2323 CtxData extends {
2424 user : User ;
2525 roomName : RoomType [ 'name' ] ;
26- eventName : EventName ;
26+ eventName : keyof ClientToServerEvents ;
2727 } ,
2828> implements CanActivate
2929{
Original file line number Diff line number Diff line change 11import { z } from 'zod' ;
22import {
33 ChatMessageSchema ,
4- EventNameSchema ,
54 JoinRoomSchema ,
65 KickUserSchema ,
76 RoomNameSchema ,
@@ -10,6 +9,8 @@ import {
109 UserIdSchema ,
1110 UserNameSchema ,
1211 UserSchema ,
12+ ServerToClientEventsSchema ,
13+ ClientToServerEventsSchema ,
1314} from '../schemas/chat.schema' ;
1415
1516export type UserId = z . infer < typeof UserIdSchema > ;
@@ -19,22 +20,10 @@ export type User = z.infer<typeof UserSchema>;
1920
2021export type RoomName = z . infer < typeof RoomNameSchema > ;
2122export type Room = z . infer < typeof RoomSchema > ;
22- export type EventName = z . infer < typeof EventNameSchema > ;
2323export type Message = z . infer < typeof ChatMessageSchema > ;
2424
2525export type JoinRoom = z . infer < typeof JoinRoomSchema > ;
2626export type KickUser = z . infer < typeof KickUserSchema > ;
2727
28- export interface ServerToClientEvents {
29- chat : ( e : Message ) => void ;
30- kick_user : ( e : KickUser ) => void ;
31- }
32-
33- export interface ClientToServerEvents {
34- chat : ( e : Message ) => void ;
35- join_room : ( e : JoinRoom ) => void ;
36- kick_user : (
37- e : KickUser ,
38- completionCallback : ( complete : boolean ) => void ,
39- ) => void ;
40- }
28+ export type ServerToClientEvents = z . infer < typeof ServerToClientEventsSchema > ;
29+ export type ClientToServerEvents = z . infer < typeof ClientToServerEventsSchema > ;
Original file line number Diff line number Diff line change @@ -24,7 +24,7 @@ export const RoomNameSchema = z
2424 message : 'Must not contain spaces or special characters.' ,
2525 } ) ;
2626
27- export const EventNameSchema = z . string ( ) ;
27+ export const EventNameSchema = z . enum ( [ 'chat' , 'kick_user' , 'join_room' ] ) ;
2828
2929export const SocketIdSchema = z
3030 . string ( )
@@ -62,3 +62,17 @@ export const KickUserSchema = z.object({
6262 roomName : RoomNameSchema ,
6363 eventName : EventNameSchema ,
6464} ) ;
65+
66+ export const ClientToServerEventsSchema = z . object ( {
67+ chat : z . function ( ) . args ( ChatMessageSchema ) . returns ( z . void ( ) ) ,
68+ join_room : z . function ( ) . args ( JoinRoomSchema ) . returns ( z . void ( ) ) ,
69+ kick_user : z
70+ . function ( )
71+ . args ( KickUserSchema , z . function ( ) . args ( z . boolean ( ) ) . returns ( z . void ( ) ) )
72+ . returns ( z . void ( ) ) ,
73+ } ) ;
74+
75+ export const ServerToClientEventsSchema = z . object ( {
76+ chat : z . function ( ) . args ( ChatMessageSchema ) . returns ( z . void ( ) ) ,
77+ kick_user : z . function ( ) . args ( KickUserSchema ) . returns ( z . void ( ) ) ,
78+ } ) ;
You can’t perform that action at this time.
0 commit comments