@eleven-am/pondsocket 0.1.120 → 0.1.122

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/types.d.ts +47 -55
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.120",
3
+ "version": "0.1.122",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
package/types.d.ts CHANGED
@@ -91,6 +91,20 @@ type LeaveCallback = (event: LeaveEvent) => void;
91
91
 
92
92
  type ParamDecoratorCallback<Input> = (data: Input, context: Context) => unknown | Promise<unknown>;
93
93
 
94
+ type PondEvenType = { [key: string]: PondMessage };
95
+
96
+ type NestFuncType<Event extends string, Payload extends PondMessage> = {
97
+ event?: Event;
98
+ broadcast?: Event;
99
+ assigns?: PondAssigns;
100
+ presence?: PondPresence;
101
+ updatePresence?: PondPresence;
102
+ } & Payload;
103
+
104
+ type NestReturnType<EventType extends PondEvenType, Event extends keyof EventType> = Event extends string ?
105
+ NestFuncType<Event, EventType[Event]> | Promise<NestFuncType<Event, EventType[Event]>> :
106
+ never;
107
+
94
108
  interface UserData {
95
109
  assigns: PondAssigns;
96
110
  presence: PondPresence;
@@ -210,42 +224,13 @@ declare class AbstractRequest<Path extends string> {
210
224
  presence: UserPresences;
211
225
  }
212
226
 
213
- declare abstract class PondResponse {
214
- /**
215
- * @desc Whether the server has responded to the request
216
- */
217
- public abstract hasResponded: boolean;
218
-
219
- /**
220
- * @desc Rejects the request with the given error message
221
- * @param message - the error message
222
- * @param errorCode - the error code
223
- * @param assigns - the data to assign to the client
224
- */
225
- abstract reject (message?: string, errorCode?: number, assigns?: PondAssigns): void;
226
-
227
- /**
228
- * @desc Emits a direct message to the client
229
- * @param event - the event name
230
- * @param payload - the payload to send
231
- * @param assigns - the data to assign to the client
232
- */
233
- abstract send (event: string, payload: PondMessage, assigns?: PondAssigns): void;
234
-
235
- /**
236
- * @desc Accepts the request and optionally assigns data to the client
237
- * @param assigns - the data to assign to the client
238
- */
239
- abstract accept (assigns?: PondAssigns): void;
240
- }
241
-
242
227
  declare class EventRequest<Path extends string> extends AbstractRequest<Path> {
243
228
  user: UserData;
244
229
 
245
230
  channel: Channel;
246
231
  }
247
232
 
248
- declare class EventResponse extends PondResponse {
233
+ declare class EventResponse<EventType extends PondEvenType = PondEvenType> {
249
234
  /**
250
235
  * @desc Whether the server has responded to the request
251
236
  */
@@ -273,28 +258,27 @@ declare class EventResponse extends PondResponse {
273
258
  */
274
259
  send (event: string, payload: PondMessage, assigns?: PondAssigns): void;
275
260
 
276
-
277
261
  /**
278
262
  * @desc Emits a direct message to the client without accepting the request
279
263
  * @param event - the event name
280
264
  * @param payload - the payload to send
281
265
  * @param assigns - the data to assign to the client
282
266
  */
283
- sendOnly (event: string, payload: PondMessage, assigns?: PondAssigns): void;
267
+ sendOnly <Key extends keyof EventType> (event: Key, payload: EventType[Key], assigns?: PondAssigns): void;
284
268
 
285
269
  /**
286
270
  * @desc Sends a message to all clients in the channel
287
271
  * @param event - the event to send
288
272
  * @param payload - the payload to send
289
273
  */
290
- broadcast (event: string, payload: PondMessage): EventResponse;
274
+ broadcast <Key extends keyof EventType> (event: Key, payload: EventType[Key]): EventResponse;
291
275
 
292
276
  /**
293
277
  * @desc Sends a message to all clients in the channel except the client making the request
294
278
  * @param event - the event to send
295
279
  * @param payload - the payload to send
296
280
  */
297
- broadcastFromUser (event: string, payload: PondMessage): EventResponse;
281
+ broadcastFromUser <Key extends keyof EventType> (event: Key, payload: EventType[Key]): EventResponse;
298
282
 
299
283
  /**
300
284
  * @desc Sends a message to a set of clients in the channel
@@ -302,7 +286,7 @@ declare class EventResponse extends PondResponse {
302
286
  * @param payload - the payload to send
303
287
  * @param userIds - the ids of the clients to send the message to
304
288
  */
305
- sendToUsers (event: string, payload: PondMessage, userIds: string[]): EventResponse;
289
+ sendToUsers <Key extends keyof EventType> (event: Key, payload: EventType[Key], userIds: string[]): EventResponse;
306
290
 
307
291
  /**
308
292
  * @desc Tracks a user's presence in the channel
@@ -338,7 +322,7 @@ declare class EventResponse extends PondResponse {
338
322
  closeChannel (reason: string): void;
339
323
  }
340
324
 
341
- export declare class ClientChannel {
325
+ export declare class ClientChannel<EventType extends PondEvenType = PondEvenType> {
342
326
  channelState: ChannelState;
343
327
 
344
328
  /**
@@ -355,14 +339,14 @@ export declare class ClientChannel {
355
339
  * @desc Monitors the channel for messages.
356
340
  * @param callback - The callback to call when a message is received.
357
341
  */
358
- onMessage (callback: (event: string, message: PondMessage) => void): Unsubscribe;
342
+ onMessage (callback: (event: keyof EventType, message: EventType[keyof EventType]) => void): Unsubscribe;
359
343
 
360
344
  /**
361
345
  * @desc Monitors the channel for messages.
362
346
  * @param event - The event to monitor.
363
347
  * @param callback - The callback to call when a message is received.
364
348
  */
365
- onMessageEvent (event: string, callback: (message: PondMessage) => void): Unsubscribe;
349
+ onMessageEvent <Key extends keyof EventType> (event: Key, callback: (message: EventType[Key]) => void): Unsubscribe;
366
350
 
367
351
  /**
368
352
  * @desc Monitors the channel state of the channel.
@@ -394,21 +378,21 @@ export declare class ClientChannel {
394
378
  * @param payload - The message to send.
395
379
  * @param recipient - The clients to send the message to.
396
380
  */
397
- sendMessage (event: string, payload: PondMessage, recipient: string[]): void;
381
+ sendMessage <Key extends keyof EventType> (event: Key, payload: EventType[Key], recipient: string[]): void;
398
382
 
399
383
  /**
400
384
  * @desc Broadcasts a message to every other client in the channel except yourself.
401
385
  * @param event - The event to send.
402
386
  * @param payload - The message to send.
403
387
  */
404
- broadcastFrom (event: string, payload: PondMessage): void;
388
+ broadcastFrom <Key extends keyof EventType> (event: Key, payload: EventType[Key]): void;
405
389
 
406
390
  /**
407
391
  * @desc Broadcasts a message to the channel, including yourself.
408
392
  * @param event - The event to send.
409
393
  * @param payload - The message to send.
410
394
  */
411
- broadcast (event: string, payload: PondMessage): void;
395
+ broadcast <Key extends keyof EventType> (event: Key, payload: EventType[Key]): void;
412
396
 
413
397
  /**
414
398
  * @desc Gets the current presence of the channel.
@@ -464,7 +448,7 @@ declare class Endpoint {
464
448
  closeConnection (clientIds: string | string[]): void;
465
449
  }
466
450
 
467
- export declare class Channel {
451
+ export declare class Channel<EventType extends PondEvenType = PondEvenType> {
468
452
  /**
469
453
  * The name of the channel.
470
454
  */
@@ -486,7 +470,15 @@ export declare class Channel {
486
470
  * @param event - The event to send.
487
471
  * @param payload - The message to send.
488
472
  */
489
- broadcastMessage (event: string, payload: PondMessage): void;
473
+ broadcastMessage <Key extends keyof EventType> (event: Key, payload: EventType[Key]): void;
474
+
475
+ /**
476
+ * @desc Broadcasts a message to every client in the channel except the sender,
477
+ * @param userId - The id of the user to send the message from.
478
+ * @param event - The event to send.
479
+ * @param payload - The message to send.
480
+ */
481
+ broadcastMessageFromUser <Key extends keyof EventType> (userId: string, event: Key, payload: EventType[Key]): void;
490
482
 
491
483
  /**
492
484
  * @desc Sends a message to a specific client in the channel.
@@ -494,7 +486,7 @@ export declare class Channel {
494
486
  * @param event - The event to send.
495
487
  * @param payload - The message to send.
496
488
  */
497
- sendToUser (userId: string, event: string, payload: PondMessage): void;
489
+ sendToUser <Key extends keyof EventType> (userId: string, event: Key, payload: EventType[Key]): void;
498
490
 
499
491
  /**
500
492
  * @desc Sends a message to specific clients in the channel.
@@ -502,7 +494,7 @@ export declare class Channel {
502
494
  * @param event - The event to send.
503
495
  * @param payload - The message to send.
504
496
  */
505
- sendToUsers (userIds: string[], event: string, payload: PondMessage): void;
497
+ sendToUsers <Key extends keyof EventType> (userIds: string[], event: Key, payload: EventType[Key]): void;
506
498
 
507
499
  /**
508
500
  * @desc Bans a user from the channel.
@@ -540,7 +532,7 @@ declare class JoinRequest<Path extends string> extends AbstractRequest<Path> {
540
532
  channel: Channel;
541
533
  }
542
534
 
543
- declare class JoinResponse extends PondResponse {
535
+ declare class JoinResponse<EventType extends PondEvenType = PondEvenType> {
544
536
  /**
545
537
  * @desc Whether the server has responded to the request
546
538
  */
@@ -565,21 +557,21 @@ declare class JoinResponse extends PondResponse {
565
557
  * @param payload - the payload to send
566
558
  * @param assigns - the data to assign to the client
567
559
  */
568
- send (event: string, payload: PondMessage, assigns?: PondAssigns): this;
560
+ send <Key extends keyof EventType> (event: Key, payload: EventType[Key], assigns?: PondAssigns): JoinResponse;
569
561
 
570
562
  /**
571
563
  * @desc Emits a message to all clients in the channel
572
564
  * @param event - the event name
573
565
  * @param payload - the payload to send
574
566
  */
575
- broadcast (event: string, payload: PondMessage): JoinResponse;
567
+ broadcast <Key extends keyof EventType> (event: Key, payload: EventType[Key]): JoinResponse;
576
568
 
577
569
  /**
578
570
  * @desc Emits a message to all clients in the channel except the sender
579
571
  * @param event - the event name
580
572
  * @param payload - the payload to send
581
573
  */
582
- broadcastFromUser (event: string, payload: PondMessage): JoinResponse;
574
+ broadcastFromUser <Key extends keyof EventType> (event: Key, payload: EventType[Key]): JoinResponse;
583
575
 
584
576
  /**
585
577
  * @desc Emits a message to a specific set of clients
@@ -587,7 +579,7 @@ declare class JoinResponse extends PondResponse {
587
579
  * @param payload - the payload to send
588
580
  * @param userIds - the ids of the clients to send the message to
589
581
  */
590
- sendToUsers (event: string, payload: PondMessage, userIds: string[]): JoinResponse;
582
+ sendToUsers <Key extends keyof EventType> (event: Key, payload: EventType[Key], userIds: string[]): JoinResponse;
591
583
 
592
584
  /**
593
585
  * @desc tracks the presence of a client
@@ -596,7 +588,7 @@ declare class JoinResponse extends PondResponse {
596
588
  trackPresence (presence: PondPresence): JoinResponse;
597
589
  }
598
590
 
599
- declare class ConnectionResponse extends PondResponse {
591
+ declare class ConnectionResponse {
600
592
  /**
601
593
  * @desc Whether the server has responded to the request
602
594
  */
@@ -624,7 +616,7 @@ declare class ConnectionResponse extends PondResponse {
624
616
  send (event: string, payload: PondMessage, assigns?: PondAssigns): void;
625
617
  }
626
618
 
627
- export declare class PondChannel {
619
+ export declare class PondChannel <EventType extends PondEvenType = PondEvenType> {
628
620
  /**
629
621
  * @desc Handles an event request made by a user
630
622
  * @param event - The event to listen for
@@ -636,7 +628,7 @@ export declare class PondChannel {
636
628
  * });
637
629
  * });
638
630
  */
639
- onEvent<Event extends string> (event: PondPath<Event>, handler: (request: EventRequest<Event>, response: EventResponse) => void | Promise<void>): void;
631
+ onEvent<Event extends string> (event: PondPath<Event>, handler: (request: EventRequest<Event>, response: EventResponse<EventType>) => void | Promise<void>): void;
640
632
 
641
633
  /**
642
634
  * @desc Broadcasts a message to all users in a channel
@@ -650,7 +642,7 @@ export declare class PondChannel {
650
642
  * channel: 'my_channel',
651
643
  *});
652
644
  */
653
- broadcast (event: string, payload: PondMessage, channelName?: string): void;
645
+ broadcast <Key extends keyof EventType> (event: Key, payload: EventType[Key], channelName?: string): void;
654
646
 
655
647
  /**
656
648
  * @desc Handles the leave event for a user, can occur when a user disconnects or leaves a channel, use this to clean up any resources
@@ -662,7 +654,7 @@ export declare class PondChannel {
662
654
  * @desc Gets a channel by name
663
655
  * @param channelName - The name of the channel to get
664
656
  */
665
- public getChannel (channelName: string): Channel | null;
657
+ public getChannel <EventType extends PondEvenType = PondEvenType> (channelName: string): Channel<EventType> | null;
666
658
  }
667
659
 
668
660
  declare class PondSocket {