-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrealtime.zod.ts
More file actions
122 lines (103 loc) · 3.87 KB
/
realtime.zod.ts
File metadata and controls
122 lines (103 loc) · 3.87 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { z } from 'zod';
/**
* Transport Protocol Enum
* Defines the communication protocol for realtime data synchronization
*/
export const TransportProtocol = z.enum([
'websocket', // Full-duplex, low latency communication
'sse', // Server-Sent Events, unidirectional push
'polling', // Short polling, best compatibility
]);
export type TransportProtocol = z.infer<typeof TransportProtocol>;
/**
* Event Type Enum
* Types of realtime events that can be subscribed to
*/
export const RealtimeEventType = z.enum([
'record.created',
'record.updated',
'record.deleted',
'field.changed',
]);
export type RealtimeEventType = z.infer<typeof RealtimeEventType>;
/**
* Subscription Event Configuration
* Defines what events to subscribe to with optional filtering
*/
export const SubscriptionEventSchema = z.object({
type: RealtimeEventType.describe('Type of event to subscribe to'),
object: z.string().optional().describe('Object name to subscribe to'),
filters: z.unknown().optional().describe('Filter conditions'),
});
/**
* Subscription Schema
* Configuration for subscribing to realtime events
*/
export const SubscriptionSchema = z.object({
id: z.string().uuid().describe('Unique subscription identifier'),
events: z.array(SubscriptionEventSchema).describe('Array of events to subscribe to'),
transport: TransportProtocol.describe('Transport protocol to use'),
channel: z.string().optional().describe('Optional channel name for grouping subscriptions'),
});
export type Subscription = z.infer<typeof SubscriptionSchema>;
/**
* Presence Status Enum
* User online/offline status
*/
export const RealtimePresenceStatus = z.enum([
'online',
'away',
'busy',
'offline',
]);
export type RealtimePresenceStatus = z.infer<typeof RealtimePresenceStatus>;
/**
* Presence Schema
* Tracks user online status and metadata
*/
export const RealtimePresenceSchema = z.object({
userId: z.string().describe('User identifier'),
status: RealtimePresenceStatus.describe('Current presence status'),
lastSeen: z.string().datetime().describe('ISO 8601 datetime of last activity'),
metadata: z.record(z.string(), z.unknown()).optional().describe('Custom presence data (e.g., current page, custom status)'),
});
export type RealtimePresence = z.infer<typeof RealtimePresenceSchema>;
/**
* Realtime Action Enum
* Actions that can occur on records
*/
export const RealtimeAction = z.enum([
'created',
'updated',
'deleted',
]);
export type RealtimeAction = z.infer<typeof RealtimeAction>;
/**
* Realtime Event Schema
* Represents a realtime synchronization event
*/
export const RealtimeEventSchema = z.object({
id: z.string().uuid().describe('Unique event identifier'),
type: z.string().describe('Event type (e.g., record.created, record.updated)'),
object: z.string().optional().describe('Object name the event relates to'),
action: RealtimeAction.optional().describe('Action performed'),
payload: z.record(z.string(), z.unknown()).describe('Event payload data'),
timestamp: z.string().datetime().describe('ISO 8601 datetime when event occurred'),
userId: z.string().optional().describe('User who triggered the event'),
sessionId: z.string().optional().describe('Session identifier'),
});
export type RealtimeEvent = z.infer<typeof RealtimeEventSchema>;
/**
* Realtime Configuration Schema
*
* Configuration for enabling realtime data synchronization.
*/
export const RealtimeConfigSchema = z.object({
/** Enable realtime sync */
enabled: z.boolean().default(true).describe('Enable realtime synchronization'),
/** Transport protocol */
transport: TransportProtocol.default('websocket').describe('Transport protocol'),
/** Default subscriptions */
subscriptions: z.array(SubscriptionSchema).optional().describe('Default subscriptions'),
}).passthrough(); // Allow additional properties
export type RealtimeConfig = z.infer<typeof RealtimeConfigSchema>;