@@ -14,26 +14,32 @@ export type RouterResponse = {
14
14
status : ( code : HttpStatusCode | null ) => RouterResponse ;
15
15
data ?: any | null ;
16
16
groupedClients : ServerClient ;
17
+ socket : WebSocket ;
17
18
send : ( data : any ) => RouterResponse ;
18
- group : Omit < RouterResponse , 'group' | 'othersInGroup' | 'groupedClients' > ;
19
- othersInGroup : Omit < RouterResponse , 'group' | 'othersInGroup' | 'groupedClients' > ;
19
+ group : Omit < RouterResponse , 'group' | 'othersInGroup' | 'groupedClients' | 'socket' | 'clients' > ;
20
+ othersInGroup : Omit < RouterResponse , 'group' | 'othersInGroup' | 'groupedClients' | 'socket' | 'clients' > ;
21
+ clients : ServerClients ;
20
22
} ;
21
- export type RouterRequest = {
23
+ export type RouterRequest < P extends object = object > = {
22
24
id ?: number ;
23
25
body ?: any ;
24
26
get ?: string ;
25
27
post ?: string ;
26
28
put ?: string ;
27
29
patch ?: string ;
28
30
delete ?: string ;
29
- } & MatchResult < Record < string , string | number > > ;
30
- export type RouterCallback = ( request : RouterRequest , response : RouterResponse ) => Promise < void > ;
31
+ } & MatchResult < P > ;
32
+ export type RouterCallback < P extends object = object > = (
33
+ request : RouterRequest < P > ,
34
+ response : RouterResponse
35
+ ) => Promise < void > ;
31
36
export type Route = {
32
37
literalRoute : string ;
33
38
match : MatchFunction < any > ;
34
39
callbacks : RouterCallback [ ] ;
35
40
} ;
36
41
42
+ type Params = Record < string , string | number > ;
37
43
export class Router {
38
44
store : RouterStore = {
39
45
get : [ ] ,
@@ -50,19 +56,19 @@ export class Router {
50
56
callbacks,
51
57
} ) ;
52
58
}
53
- get ( url : string , ...callbacks : RouterCallback [ ] ) {
59
+ get < P extends object = Params > ( url : string , ...callbacks : RouterCallback < P > [ ] ) {
54
60
this . registerRoute ( 'get' , url , ...callbacks ) ;
55
61
}
56
- put ( url : string , ...callbacks : RouterCallback [ ] ) {
62
+ put < P extends object = Params > ( url : string , ...callbacks : RouterCallback < P > [ ] ) {
57
63
this . registerRoute ( 'put' , url , ...callbacks ) ;
58
64
}
59
- post ( url : string , ...callbacks : RouterCallback [ ] ) {
65
+ post < P extends object = Params > ( url : string , ...callbacks : RouterCallback < P > [ ] ) {
60
66
this . registerRoute ( 'post' , url , ...callbacks ) ;
61
67
}
62
- patch ( url : string , ...callbacks : RouterCallback [ ] ) {
68
+ patch < P extends object = Params > ( url : string , ...callbacks : RouterCallback < P > [ ] ) {
63
69
this . registerRoute ( 'patch' , url , ...callbacks ) ;
64
70
}
65
- delete ( url : string , ...callbacks : RouterCallback [ ] ) {
71
+ delete < P extends object = Params > ( url : string , ...callbacks : RouterCallback < P > [ ] ) {
66
72
this . registerRoute ( 'delete' , url , ...callbacks ) ;
67
73
}
68
74
async listener ( message : RouterRequest , socket : WebSocket ) {
@@ -86,9 +92,14 @@ export class Router {
86
92
method = 'delete' ;
87
93
store = this . store . delete ;
88
94
}
89
- const groupedClients = this . clients . find ( socket [ 'groupId' ] ) ;
95
+ const clients = this . clients ;
90
96
const response : RouterResponse = {
91
- groupedClients,
97
+ socket,
98
+ clients : this . clients ,
99
+ get groupedClients ( ) {
100
+ console . log ( socket [ 'groupId' ] ) ;
101
+ return clients . find ( socket [ 'groupId' ] ) ;
102
+ } ,
92
103
status : function ( status : HttpStatusCode | null ) {
93
104
if ( this . code !== undefined )
94
105
throw new Error ( `Cannot overwrite status status(${ status } ) from previously set status(${ this . code } ) ` ) ;
@@ -138,12 +149,12 @@ export class Router {
138
149
socket . send ( JSON . stringify ( { _id : message . id , data : response . data , status : response . code } ) ) ;
139
150
}
140
151
if ( response . group . data != null || response . group . code != null ) {
141
- groupedClients . method ( method , message [ method ] , {
152
+ response . groupedClients . method ( method , message [ method ] , {
142
153
data : response . group . data ,
143
154
} ) ;
144
155
}
145
156
if ( response . othersInGroup . data != null || response . othersInGroup . code != null ) {
146
- groupedClients . method ( method , message [ method ] , {
157
+ response . groupedClients . method ( method , message [ method ] , {
147
158
data : response . data ,
148
159
} ) ;
149
160
}
0 commit comments