Initial commit
This commit is contained in:
commit
3fcae4eba1
1 changed files with 59 additions and 0 deletions
59
types/ServerMessage.ts
Normal file
59
types/ServerMessage.ts
Normal file
|
@ -0,0 +1,59 @@
|
|||
export enum MessageType {
|
||||
ACK = "0",
|
||||
TEXT = "1",
|
||||
}
|
||||
|
||||
export type ServerMessage = {
|
||||
type: string;
|
||||
date: number;
|
||||
};
|
||||
export function isServerMessage(obj: unknown): obj is ServerMessage {
|
||||
if (typeof obj !== "object") return false;
|
||||
if (obj === null) return false;
|
||||
if (
|
||||
!Object.hasOwnProperty.call(obj, "type") ||
|
||||
typeof (obj as { type: unknown }).type !== "string"
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
!Object.hasOwnProperty.call(obj, "date") ||
|
||||
typeof (obj as { date: unknown }).date !== "number"
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
export type TextMessage = ServerMessage & {
|
||||
type: MessageType.TEXT;
|
||||
author: string;
|
||||
content: string;
|
||||
};
|
||||
export function isTextMessage(obj: unknown): obj is TextMessage {
|
||||
if (!isServerMessage(obj)) return false;
|
||||
if (obj.type !== MessageType.TEXT) return false;
|
||||
if (
|
||||
!Object.hasOwnProperty.call(obj, "author") ||
|
||||
typeof (obj as ServerMessage & { author: unknown }).author !== "string"
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
if (
|
||||
!Object.hasOwnProperty.call(obj, "author") ||
|
||||
typeof (obj as ServerMessage & { content: unknown }).content !== "string"
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export type AckMessage = ServerMessage & {
|
||||
type: MessageType.ACK;
|
||||
};
|
||||
export function isAckMessage(obj: unknown): obj is AckMessage {
|
||||
if (!isServerMessage(obj)) return false;
|
||||
if (obj.type !== MessageType.ACK) return false;
|
||||
return true;
|
||||
}
|
Reference in a new issue