Skip to content

Commit bee40b9

Browse files
committed
usable zod enum for event names
1 parent 184442b commit bee40b9

File tree

4 files changed

+24
-20
lines changed

4 files changed

+24
-20
lines changed

src/client/pages/chat.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ServerToClientEvents,
88
ClientToServerEvents,
99
KickUser,
10+
JoinRoom,
1011
} from '../../shared/interfaces/chat.interface';
1112
import { Header } from '../components/header';
1213
import { 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,

src/server/chat/guards/chat.guard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
import { RoomService } from '../../room/room.service';
1313
import { PolicyHandler } from '../../casl/interfaces/policy.interface';
1414
import {
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
{
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { z } from 'zod';
22
import {
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

1516
export type UserId = z.infer<typeof UserIdSchema>;
@@ -19,22 +20,10 @@ export type User = z.infer<typeof UserSchema>;
1920

2021
export type RoomName = z.infer<typeof RoomNameSchema>;
2122
export type Room = z.infer<typeof RoomSchema>;
22-
export type EventName = z.infer<typeof EventNameSchema>;
2323
export type Message = z.infer<typeof ChatMessageSchema>;
2424

2525
export type JoinRoom = z.infer<typeof JoinRoomSchema>;
2626
export 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>;

src/shared/schemas/chat.schema.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff 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

2929
export 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+
});

0 commit comments

Comments
 (0)