-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessages.ts
More file actions
57 lines (49 loc) · 1.47 KB
/
Copy pathmessages.ts
File metadata and controls
57 lines (49 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* SPDX-FileCopyrightText: 2014-present Kriasoft */
/* SPDX-License-Identifier: MIT */
import { z } from "zod";
import { messageSchema } from "./schema";
/**
* Ping message for connection health checks.
*/
export const PingSchema = messageSchema("PING");
export type PingMessage = z.infer<typeof PingSchema>;
/**
* Pong message sent in response to PING.
*/
export const PongSchema = messageSchema("PONG");
export type PongMessage = z.infer<typeof PongSchema>;
/**
* Echo request - simple example of request/response pattern.
*/
export const EchoSchema = messageSchema("ECHO", {
text: z.string(),
});
export type EchoMessage = z.infer<typeof EchoSchema>;
/**
* Notification - simple example of server-to-client broadcast.
*/
export const NotificationSchema = messageSchema("NOTIFICATION", {
level: z.enum(["info", "warning", "error"]),
message: z.string(),
});
export type NotificationMessage = z.infer<typeof NotificationSchema>;
/**
* Error message for communicating errors.
*/
export const ErrorSchema = messageSchema("ERROR", {
code: z.enum(["INVALID_MESSAGE", "UNAUTHORIZED", "SERVER_ERROR"]),
message: z.string(),
});
export type ErrorMessage = z.infer<typeof ErrorSchema>;
/**
* All possible message types for easy discrimination.
*/
export const MessageSchema = z.discriminatedUnion("type", [
PingSchema,
PongSchema,
EchoSchema,
NotificationSchema,
ErrorSchema,
]);
export type Message = z.infer<typeof MessageSchema>;
export type MessageType = Message["type"];