@eleven-am/pondsocket 0.1.121 → 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 +40 -56
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.121",
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,7 @@ 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;
490
474
 
491
475
  /**
492
476
  * @desc Broadcasts a message to every client in the channel except the sender,
@@ -494,7 +478,7 @@ export declare class Channel {
494
478
  * @param event - The event to send.
495
479
  * @param payload - The message to send.
496
480
  */
497
- broadcastMessageFromUser (userId: string, event: string, payload: PondMessage): void;
481
+ broadcastMessageFromUser <Key extends keyof EventType> (userId: string, event: Key, payload: EventType[Key]): void;
498
482
 
499
483
  /**
500
484
  * @desc Sends a message to a specific client in the channel.
@@ -502,7 +486,7 @@ export declare class Channel {
502
486
  * @param event - The event to send.
503
487
  * @param payload - The message to send.
504
488
  */
505
- sendToUser (userId: string, event: string, payload: PondMessage): void;
489
+ sendToUser <Key extends keyof EventType> (userId: string, event: Key, payload: EventType[Key]): void;
506
490
 
507
491
  /**
508
492
  * @desc Sends a message to specific clients in the channel.
@@ -510,7 +494,7 @@ export declare class Channel {
510
494
  * @param event - The event to send.
511
495
  * @param payload - The message to send.
512
496
  */
513
- sendToUsers (userIds: string[], event: string, payload: PondMessage): void;
497
+ sendToUsers <Key extends keyof EventType> (userIds: string[], event: Key, payload: EventType[Key]): void;
514
498
 
515
499
  /**
516
500
  * @desc Bans a user from the channel.
@@ -548,7 +532,7 @@ declare class JoinRequest<Path extends string> extends AbstractRequest<Path> {
548
532
  channel: Channel;
549
533
  }
550
534
 
551
- declare class JoinResponse extends PondResponse {
535
+ declare class JoinResponse<EventType extends PondEvenType = PondEvenType> {
552
536
  /**
553
537
  * @desc Whether the server has responded to the request
554
538
  */
@@ -573,21 +557,21 @@ declare class JoinResponse extends PondResponse {
573
557
  * @param payload - the payload to send
574
558
  * @param assigns - the data to assign to the client
575
559
  */
576
- send (event: string, payload: PondMessage, assigns?: PondAssigns): this;
560
+ send <Key extends keyof EventType> (event: Key, payload: EventType[Key], assigns?: PondAssigns): JoinResponse;
577
561
 
578
562
  /**
579
563
  * @desc Emits a message to all clients in the channel
580
564
  * @param event - the event name
581
565
  * @param payload - the payload to send
582
566
  */
583
- broadcast (event: string, payload: PondMessage): JoinResponse;
567
+ broadcast <Key extends keyof EventType> (event: Key, payload: EventType[Key]): JoinResponse;
584
568
 
585
569
  /**
586
570
  * @desc Emits a message to all clients in the channel except the sender
587
571
  * @param event - the event name
588
572
  * @param payload - the payload to send
589
573
  */
590
- broadcastFromUser (event: string, payload: PondMessage): JoinResponse;
574
+ broadcastFromUser <Key extends keyof EventType> (event: Key, payload: EventType[Key]): JoinResponse;
591
575
 
592
576
  /**
593
577
  * @desc Emits a message to a specific set of clients
@@ -595,7 +579,7 @@ declare class JoinResponse extends PondResponse {
595
579
  * @param payload - the payload to send
596
580
  * @param userIds - the ids of the clients to send the message to
597
581
  */
598
- sendToUsers (event: string, payload: PondMessage, userIds: string[]): JoinResponse;
582
+ sendToUsers <Key extends keyof EventType> (event: Key, payload: EventType[Key], userIds: string[]): JoinResponse;
599
583
 
600
584
  /**
601
585
  * @desc tracks the presence of a client
@@ -604,7 +588,7 @@ declare class JoinResponse extends PondResponse {
604
588
  trackPresence (presence: PondPresence): JoinResponse;
605
589
  }
606
590
 
607
- declare class ConnectionResponse extends PondResponse {
591
+ declare class ConnectionResponse {
608
592
  /**
609
593
  * @desc Whether the server has responded to the request
610
594
  */
@@ -632,7 +616,7 @@ declare class ConnectionResponse extends PondResponse {
632
616
  send (event: string, payload: PondMessage, assigns?: PondAssigns): void;
633
617
  }
634
618
 
635
- export declare class PondChannel {
619
+ export declare class PondChannel <EventType extends PondEvenType = PondEvenType> {
636
620
  /**
637
621
  * @desc Handles an event request made by a user
638
622
  * @param event - The event to listen for
@@ -644,7 +628,7 @@ export declare class PondChannel {
644
628
  * });
645
629
  * });
646
630
  */
647
- 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;
648
632
 
649
633
  /**
650
634
  * @desc Broadcasts a message to all users in a channel
@@ -658,7 +642,7 @@ export declare class PondChannel {
658
642
  * channel: 'my_channel',
659
643
  *});
660
644
  */
661
- broadcast (event: string, payload: PondMessage, channelName?: string): void;
645
+ broadcast <Key extends keyof EventType> (event: Key, payload: EventType[Key], channelName?: string): void;
662
646
 
663
647
  /**
664
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
@@ -670,7 +654,7 @@ export declare class PondChannel {
670
654
  * @desc Gets a channel by name
671
655
  * @param channelName - The name of the channel to get
672
656
  */
673
- public getChannel (channelName: string): Channel | null;
657
+ public getChannel <EventType extends PondEvenType = PondEvenType> (channelName: string): Channel<EventType> | null;
674
658
  }
675
659
 
676
660
  declare class PondSocket {