Removing all "any" from back.
To do this, I used generic-type-guard package which generates both an interface AND a valid type guard from code. With this, we are 100% sure that the messages we receive are validated at runtime! The client cannot pass us an object that is invalid! \o/
This commit is contained in:
parent
ac0b7a7361
commit
a373626e24
11 changed files with 113 additions and 59 deletions
|
@ -1,9 +1,10 @@
|
|||
import {Socket} from "socket.io";
|
||||
import {PointInterface} from "./PointInterface";
|
||||
import {Identificable} from "./Identificable";
|
||||
import {TokenInterface} from "../../Controller/AuthenticateController";
|
||||
|
||||
export interface ExSocketInterface extends Socket, Identificable {
|
||||
token: any;
|
||||
token: TokenInterface;
|
||||
roomId: string;
|
||||
webRtcRoomId: string;
|
||||
userId: string;
|
||||
|
|
9
back/src/Model/Websocket/JoinRoomMessage.ts
Normal file
9
back/src/Model/Websocket/JoinRoomMessage.ts
Normal file
|
@ -0,0 +1,9 @@
|
|||
import * as tg from "generic-type-guard";
|
||||
import {isPointInterface} from "./PointInterface";
|
||||
|
||||
export const isJoinRoomMessageInterface =
|
||||
new tg.IsInterface().withProperties({
|
||||
roomId: tg.isString,
|
||||
position: isPointInterface,
|
||||
}).get();
|
||||
export type JoinRoomMessageInterface = tg.GuardedType<typeof isJoinRoomMessageInterface>;
|
|
@ -1,4 +1,3 @@
|
|||
import {Point} from "./MessageUserPosition";
|
||||
import {PointInterface} from "_Model/Websocket/PointInterface";
|
||||
|
||||
export class MessageUserJoined {
|
||||
|
|
|
@ -1,5 +1,17 @@
|
|||
export interface PointInterface {
|
||||
import * as tg from "generic-type-guard";
|
||||
|
||||
/*export interface PointInterface {
|
||||
readonly x: number;
|
||||
readonly y: number;
|
||||
readonly direction: string;
|
||||
}
|
||||
readonly moving: boolean;
|
||||
}*/
|
||||
|
||||
export const isPointInterface =
|
||||
new tg.IsInterface().withProperties({
|
||||
x: tg.isNumber,
|
||||
y: tg.isNumber,
|
||||
direction: tg.isString,
|
||||
moving: tg.isBoolean
|
||||
}).get();
|
||||
export type PointInterface = tg.GuardedType<typeof isPointInterface>;
|
||||
|
|
|
@ -1,4 +1,8 @@
|
|||
export interface SetPlayerDetailsMessage {
|
||||
name: string,
|
||||
character: string
|
||||
}
|
||||
import * as tg from "generic-type-guard";
|
||||
|
||||
export const isSetPlayerDetailsMessage =
|
||||
new tg.IsInterface().withProperties({
|
||||
name: tg.isString,
|
||||
character: tg.isString
|
||||
}).get();
|
||||
export type SetPlayerDetailsMessage = tg.GuardedType<typeof isSetPlayerDetailsMessage>;
|
||||
|
|
5
back/src/Model/Websocket/UserInGroupInterface.ts
Normal file
5
back/src/Model/Websocket/UserInGroupInterface.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
export interface UserInGroupInterface {
|
||||
userId: string,
|
||||
name: string,
|
||||
initiator: boolean
|
||||
}
|
10
back/src/Model/Websocket/WebRtcSignalMessage.ts
Normal file
10
back/src/Model/Websocket/WebRtcSignalMessage.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import * as tg from "generic-type-guard";
|
||||
|
||||
export const isWebRtcSignalMessageInterface =
|
||||
new tg.IsInterface().withProperties({
|
||||
userId: tg.isString,
|
||||
receiverId: tg.isString,
|
||||
roomId: tg.isString,
|
||||
signal: tg.isUnknown
|
||||
}).get();
|
||||
export type WebRtcSignalMessageInterface = tg.GuardedType<typeof isWebRtcSignalMessageInterface>;
|
Loading…
Add table
Add a link
Reference in a new issue