dialogue-ts 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,273 @@
1
+ import { Socket } from 'socket.io-client';
2
+
3
+ /**
4
+ * Client-side type definitions for Dialogue
5
+ * These types mirror the backend types for consistent API
6
+ */
7
+ /**
8
+ * Configuration for DialogueClient
9
+ */
10
+ interface ClientConfig {
11
+ /** WebSocket server URL (e.g., 'ws://localhost:3000' or 'wss://example.com') */
12
+ url: string;
13
+ /** Authentication data sent with connection */
14
+ auth?: {
15
+ /** User ID for identification */
16
+ userId?: string;
17
+ /** JWT or other auth token */
18
+ token?: string;
19
+ /** Additional auth data */
20
+ [key: string]: unknown;
21
+ };
22
+ /** Auto-connect on instantiation (default: true) */
23
+ autoConnect?: boolean;
24
+ /** Reconnection options */
25
+ reconnection?: boolean;
26
+ /** Number of reconnection attempts */
27
+ reconnectionAttempts?: number;
28
+ }
29
+ /**
30
+ * Room info returned by listRooms
31
+ */
32
+ interface RoomInfo {
33
+ id: string;
34
+ name: string;
35
+ description?: string;
36
+ size: number;
37
+ maxSize?: number;
38
+ createdById?: string;
39
+ }
40
+ /**
41
+ * Options for creating a room
42
+ */
43
+ interface CreateRoomOptions {
44
+ /** Unique room identifier */
45
+ id: string;
46
+ /** Human-readable room name */
47
+ name: string;
48
+ /** Room description */
49
+ description?: string;
50
+ /** Maximum number of participants */
51
+ maxSize?: number;
52
+ }
53
+ /**
54
+ * Event message received from server
55
+ * Matches EventMessage from backend
56
+ */
57
+ interface EventMessage<T = unknown> {
58
+ event: string;
59
+ roomId: string;
60
+ data: T;
61
+ from: string;
62
+ timestamp: number;
63
+ meta?: Record<string, unknown>;
64
+ }
65
+ /**
66
+ * Error response from server
67
+ */
68
+ interface DialogueError {
69
+ code: string;
70
+ message: string;
71
+ }
72
+ /**
73
+ * Connection result emitted on successful connect
74
+ */
75
+ interface ConnectionResult {
76
+ clientId: string;
77
+ userId: string;
78
+ }
79
+ /**
80
+ * Room join result
81
+ */
82
+ interface JoinResult {
83
+ roomId: string;
84
+ roomName: string;
85
+ }
86
+ /**
87
+ * Room context - represents a joined room with methods
88
+ */
89
+ interface RoomContext {
90
+ /** Room ID */
91
+ readonly roomId: string;
92
+ /** Room name */
93
+ readonly roomName: string;
94
+ /**
95
+ * Trigger an event in this room
96
+ * @param eventName - Event name to trigger
97
+ * @param data - Event payload data
98
+ * @param meta - Optional metadata for flexible additional context
99
+ */
100
+ trigger<T>(eventName: string, data: T, meta?: Record<string, unknown>): void;
101
+ /**
102
+ * Listen for a specific event
103
+ * @param eventName - Event name to listen for
104
+ * @param handler - Callback when event received
105
+ * @returns Unsubscribe function
106
+ */
107
+ on<T>(eventName: string, handler: (msg: EventMessage<T>) => void): () => void;
108
+ /**
109
+ * Listen for all events in this room
110
+ * @param handler - Callback for any event
111
+ * @returns Unsubscribe function
112
+ */
113
+ onAny(handler: (eventName: string, msg: EventMessage<unknown>) => void): () => void;
114
+ /**
115
+ * Subscribe to additional event type
116
+ * @param eventName - Event name to subscribe to
117
+ */
118
+ subscribe(eventName: string): void;
119
+ /**
120
+ * Subscribe to all events in this room
121
+ * Tells the server to send all events from this room to this client
122
+ */
123
+ subscribeAll(): void;
124
+ /**
125
+ * Unsubscribe from event type
126
+ * @param eventName - Event name to unsubscribe from
127
+ */
128
+ unsubscribe(eventName: string): void;
129
+ /**
130
+ * Get historical events for this room (paginated, newest first)
131
+ * @param eventName - Event type to get history for
132
+ * @param start - Starting index (0 = most recent)
133
+ * @param end - Ending index (exclusive)
134
+ * @returns Promise resolving to array of events
135
+ */
136
+ getHistory<T = unknown>(eventName: string, start?: number, end?: number): Promise<EventMessage<T>[]>;
137
+ /**
138
+ * Leave this room
139
+ */
140
+ leave(): void;
141
+ }
142
+ /**
143
+ * Event handler type
144
+ */
145
+ type EventHandler<T = unknown> = (msg: EventMessage<T>) => void;
146
+ /**
147
+ * Wildcard event handler type
148
+ */
149
+ type WildcardHandler = (eventName: string, msg: EventMessage<unknown>) => void;
150
+ /**
151
+ * Connection state
152
+ */
153
+ type ConnectionState = "disconnected" | "connecting" | "connected";
154
+
155
+ /**
156
+ * Dialogue client instance returned by createDialogueClient
157
+ */
158
+ interface DialogueClientInstance {
159
+ /** User ID assigned by the server */
160
+ readonly userId: string;
161
+ /** Whether the client is connected to the server */
162
+ readonly connected: boolean;
163
+ /** Current connection state */
164
+ readonly state: ConnectionState;
165
+ /**
166
+ * Connect to the server
167
+ * @returns Promise that resolves when connected
168
+ */
169
+ connect(): Promise<void>;
170
+ /**
171
+ * Disconnect from the server
172
+ */
173
+ disconnect(): void;
174
+ /**
175
+ * Join a room
176
+ * @param roomId - Room ID to join
177
+ * @returns Promise resolving to RoomContext
178
+ */
179
+ join(roomId: string): Promise<RoomContext>;
180
+ /**
181
+ * Get a joined room context
182
+ * @param roomId - Room ID
183
+ * @returns RoomContext or undefined if not joined
184
+ */
185
+ getRoom(roomId: string): RoomContext | undefined;
186
+ /**
187
+ * List all available rooms on the server
188
+ * @returns Promise resolving to array of room info
189
+ */
190
+ listRooms(): Promise<RoomInfo[]>;
191
+ /**
192
+ * Create a new room on the server
193
+ * @param options - Room creation options
194
+ * @returns Promise resolving to created room info
195
+ */
196
+ createRoom(options: CreateRoomOptions): Promise<RoomInfo>;
197
+ /**
198
+ * Delete a room on the server (only creator can delete)
199
+ * @param roomId - Room ID to delete
200
+ * @returns Promise resolving when deleted
201
+ */
202
+ deleteRoom(roomId: string): Promise<void>;
203
+ /**
204
+ * Register a handler for when a new room is created
205
+ * @param handler - Called when a room is created
206
+ * @returns Unsubscribe function
207
+ */
208
+ onRoomCreated(handler: (room: RoomInfo) => void): () => void;
209
+ /**
210
+ * Register a handler for when a room is deleted
211
+ * @param handler - Called when a room is deleted
212
+ * @returns Unsubscribe function
213
+ */
214
+ onRoomDeleted(handler: (roomId: string) => void): () => void;
215
+ /**
216
+ * Register a connection handler
217
+ * @param handler - Called when connected
218
+ * @returns Unsubscribe function
219
+ */
220
+ onConnect(handler: () => void): () => void;
221
+ /**
222
+ * Register a disconnection handler
223
+ * @param handler - Called when disconnected with reason
224
+ * @returns Unsubscribe function
225
+ */
226
+ onDisconnect(handler: (reason: string) => void): () => void;
227
+ /**
228
+ * Register an error handler
229
+ * @param handler - Called on errors
230
+ * @returns Unsubscribe function
231
+ */
232
+ onError(handler: (error: Error) => void): () => void;
233
+ /**
234
+ * Register a handler for history events (sent when joining a room with syncHistoryOnJoin)
235
+ * @param handler - Called when history is received
236
+ * @returns Unsubscribe function
237
+ */
238
+ onHistory(handler: (roomId: string, events: EventMessage[]) => void): () => void;
239
+ }
240
+ /**
241
+ * Creates a Dialogue client for connecting to a Dialogue server from the frontend.
242
+ * Manages WebSocket connection, room membership, and event handling.
243
+ *
244
+ * @param config - Client configuration
245
+ * @returns DialogueClientInstance
246
+ *
247
+ * @example
248
+ * const client = createDialogueClient({
249
+ * url: 'ws://localhost:3000',
250
+ * auth: { userId: 'user-123', token: 'jwt-token' }
251
+ * })
252
+ *
253
+ * await client.connect()
254
+ *
255
+ * const chat = await client.join('chat')
256
+ * chat.on('message', (msg) => console.log(msg.data))
257
+ * chat.trigger('message', { text: 'Hello!' })
258
+ */
259
+ declare function createDialogueClient(config: ClientConfig): DialogueClientInstance;
260
+
261
+ /**
262
+ * Creates a room context for a joined room.
263
+ * Manages event subscriptions and triggers for a specific room.
264
+ *
265
+ * @param socket - Socket.IO client socket
266
+ * @param roomId - Room identifier
267
+ * @param roomName - Human-readable room name
268
+ * @param onLeave - Callback when leaving room
269
+ * @returns RoomContext with methods for interacting with the room
270
+ */
271
+ declare function createRoomContext(socket: Socket, roomId: string, roomName: string, onLeave: () => void): RoomContext;
272
+
273
+ export { type ClientConfig, type ConnectionResult, type ConnectionState, type CreateRoomOptions, type DialogueClientInstance, type DialogueError, type EventHandler, type EventMessage, type JoinResult, type RoomContext, type RoomInfo, type WildcardHandler, createDialogueClient, createRoomContext };
@@ -0,0 +1,273 @@
1
+ import { Socket } from 'socket.io-client';
2
+
3
+ /**
4
+ * Client-side type definitions for Dialogue
5
+ * These types mirror the backend types for consistent API
6
+ */
7
+ /**
8
+ * Configuration for DialogueClient
9
+ */
10
+ interface ClientConfig {
11
+ /** WebSocket server URL (e.g., 'ws://localhost:3000' or 'wss://example.com') */
12
+ url: string;
13
+ /** Authentication data sent with connection */
14
+ auth?: {
15
+ /** User ID for identification */
16
+ userId?: string;
17
+ /** JWT or other auth token */
18
+ token?: string;
19
+ /** Additional auth data */
20
+ [key: string]: unknown;
21
+ };
22
+ /** Auto-connect on instantiation (default: true) */
23
+ autoConnect?: boolean;
24
+ /** Reconnection options */
25
+ reconnection?: boolean;
26
+ /** Number of reconnection attempts */
27
+ reconnectionAttempts?: number;
28
+ }
29
+ /**
30
+ * Room info returned by listRooms
31
+ */
32
+ interface RoomInfo {
33
+ id: string;
34
+ name: string;
35
+ description?: string;
36
+ size: number;
37
+ maxSize?: number;
38
+ createdById?: string;
39
+ }
40
+ /**
41
+ * Options for creating a room
42
+ */
43
+ interface CreateRoomOptions {
44
+ /** Unique room identifier */
45
+ id: string;
46
+ /** Human-readable room name */
47
+ name: string;
48
+ /** Room description */
49
+ description?: string;
50
+ /** Maximum number of participants */
51
+ maxSize?: number;
52
+ }
53
+ /**
54
+ * Event message received from server
55
+ * Matches EventMessage from backend
56
+ */
57
+ interface EventMessage<T = unknown> {
58
+ event: string;
59
+ roomId: string;
60
+ data: T;
61
+ from: string;
62
+ timestamp: number;
63
+ meta?: Record<string, unknown>;
64
+ }
65
+ /**
66
+ * Error response from server
67
+ */
68
+ interface DialogueError {
69
+ code: string;
70
+ message: string;
71
+ }
72
+ /**
73
+ * Connection result emitted on successful connect
74
+ */
75
+ interface ConnectionResult {
76
+ clientId: string;
77
+ userId: string;
78
+ }
79
+ /**
80
+ * Room join result
81
+ */
82
+ interface JoinResult {
83
+ roomId: string;
84
+ roomName: string;
85
+ }
86
+ /**
87
+ * Room context - represents a joined room with methods
88
+ */
89
+ interface RoomContext {
90
+ /** Room ID */
91
+ readonly roomId: string;
92
+ /** Room name */
93
+ readonly roomName: string;
94
+ /**
95
+ * Trigger an event in this room
96
+ * @param eventName - Event name to trigger
97
+ * @param data - Event payload data
98
+ * @param meta - Optional metadata for flexible additional context
99
+ */
100
+ trigger<T>(eventName: string, data: T, meta?: Record<string, unknown>): void;
101
+ /**
102
+ * Listen for a specific event
103
+ * @param eventName - Event name to listen for
104
+ * @param handler - Callback when event received
105
+ * @returns Unsubscribe function
106
+ */
107
+ on<T>(eventName: string, handler: (msg: EventMessage<T>) => void): () => void;
108
+ /**
109
+ * Listen for all events in this room
110
+ * @param handler - Callback for any event
111
+ * @returns Unsubscribe function
112
+ */
113
+ onAny(handler: (eventName: string, msg: EventMessage<unknown>) => void): () => void;
114
+ /**
115
+ * Subscribe to additional event type
116
+ * @param eventName - Event name to subscribe to
117
+ */
118
+ subscribe(eventName: string): void;
119
+ /**
120
+ * Subscribe to all events in this room
121
+ * Tells the server to send all events from this room to this client
122
+ */
123
+ subscribeAll(): void;
124
+ /**
125
+ * Unsubscribe from event type
126
+ * @param eventName - Event name to unsubscribe from
127
+ */
128
+ unsubscribe(eventName: string): void;
129
+ /**
130
+ * Get historical events for this room (paginated, newest first)
131
+ * @param eventName - Event type to get history for
132
+ * @param start - Starting index (0 = most recent)
133
+ * @param end - Ending index (exclusive)
134
+ * @returns Promise resolving to array of events
135
+ */
136
+ getHistory<T = unknown>(eventName: string, start?: number, end?: number): Promise<EventMessage<T>[]>;
137
+ /**
138
+ * Leave this room
139
+ */
140
+ leave(): void;
141
+ }
142
+ /**
143
+ * Event handler type
144
+ */
145
+ type EventHandler<T = unknown> = (msg: EventMessage<T>) => void;
146
+ /**
147
+ * Wildcard event handler type
148
+ */
149
+ type WildcardHandler = (eventName: string, msg: EventMessage<unknown>) => void;
150
+ /**
151
+ * Connection state
152
+ */
153
+ type ConnectionState = "disconnected" | "connecting" | "connected";
154
+
155
+ /**
156
+ * Dialogue client instance returned by createDialogueClient
157
+ */
158
+ interface DialogueClientInstance {
159
+ /** User ID assigned by the server */
160
+ readonly userId: string;
161
+ /** Whether the client is connected to the server */
162
+ readonly connected: boolean;
163
+ /** Current connection state */
164
+ readonly state: ConnectionState;
165
+ /**
166
+ * Connect to the server
167
+ * @returns Promise that resolves when connected
168
+ */
169
+ connect(): Promise<void>;
170
+ /**
171
+ * Disconnect from the server
172
+ */
173
+ disconnect(): void;
174
+ /**
175
+ * Join a room
176
+ * @param roomId - Room ID to join
177
+ * @returns Promise resolving to RoomContext
178
+ */
179
+ join(roomId: string): Promise<RoomContext>;
180
+ /**
181
+ * Get a joined room context
182
+ * @param roomId - Room ID
183
+ * @returns RoomContext or undefined if not joined
184
+ */
185
+ getRoom(roomId: string): RoomContext | undefined;
186
+ /**
187
+ * List all available rooms on the server
188
+ * @returns Promise resolving to array of room info
189
+ */
190
+ listRooms(): Promise<RoomInfo[]>;
191
+ /**
192
+ * Create a new room on the server
193
+ * @param options - Room creation options
194
+ * @returns Promise resolving to created room info
195
+ */
196
+ createRoom(options: CreateRoomOptions): Promise<RoomInfo>;
197
+ /**
198
+ * Delete a room on the server (only creator can delete)
199
+ * @param roomId - Room ID to delete
200
+ * @returns Promise resolving when deleted
201
+ */
202
+ deleteRoom(roomId: string): Promise<void>;
203
+ /**
204
+ * Register a handler for when a new room is created
205
+ * @param handler - Called when a room is created
206
+ * @returns Unsubscribe function
207
+ */
208
+ onRoomCreated(handler: (room: RoomInfo) => void): () => void;
209
+ /**
210
+ * Register a handler for when a room is deleted
211
+ * @param handler - Called when a room is deleted
212
+ * @returns Unsubscribe function
213
+ */
214
+ onRoomDeleted(handler: (roomId: string) => void): () => void;
215
+ /**
216
+ * Register a connection handler
217
+ * @param handler - Called when connected
218
+ * @returns Unsubscribe function
219
+ */
220
+ onConnect(handler: () => void): () => void;
221
+ /**
222
+ * Register a disconnection handler
223
+ * @param handler - Called when disconnected with reason
224
+ * @returns Unsubscribe function
225
+ */
226
+ onDisconnect(handler: (reason: string) => void): () => void;
227
+ /**
228
+ * Register an error handler
229
+ * @param handler - Called on errors
230
+ * @returns Unsubscribe function
231
+ */
232
+ onError(handler: (error: Error) => void): () => void;
233
+ /**
234
+ * Register a handler for history events (sent when joining a room with syncHistoryOnJoin)
235
+ * @param handler - Called when history is received
236
+ * @returns Unsubscribe function
237
+ */
238
+ onHistory(handler: (roomId: string, events: EventMessage[]) => void): () => void;
239
+ }
240
+ /**
241
+ * Creates a Dialogue client for connecting to a Dialogue server from the frontend.
242
+ * Manages WebSocket connection, room membership, and event handling.
243
+ *
244
+ * @param config - Client configuration
245
+ * @returns DialogueClientInstance
246
+ *
247
+ * @example
248
+ * const client = createDialogueClient({
249
+ * url: 'ws://localhost:3000',
250
+ * auth: { userId: 'user-123', token: 'jwt-token' }
251
+ * })
252
+ *
253
+ * await client.connect()
254
+ *
255
+ * const chat = await client.join('chat')
256
+ * chat.on('message', (msg) => console.log(msg.data))
257
+ * chat.trigger('message', { text: 'Hello!' })
258
+ */
259
+ declare function createDialogueClient(config: ClientConfig): DialogueClientInstance;
260
+
261
+ /**
262
+ * Creates a room context for a joined room.
263
+ * Manages event subscriptions and triggers for a specific room.
264
+ *
265
+ * @param socket - Socket.IO client socket
266
+ * @param roomId - Room identifier
267
+ * @param roomName - Human-readable room name
268
+ * @param onLeave - Callback when leaving room
269
+ * @returns RoomContext with methods for interacting with the room
270
+ */
271
+ declare function createRoomContext(socket: Socket, roomId: string, roomName: string, onLeave: () => void): RoomContext;
272
+
273
+ export { type ClientConfig, type ConnectionResult, type ConnectionState, type CreateRoomOptions, type DialogueClientInstance, type DialogueError, type EventHandler, type EventMessage, type JoinResult, type RoomContext, type RoomInfo, type WildcardHandler, createDialogueClient, createRoomContext };