fixed module standart comparability, now ws sends initial data in message format

This commit is contained in:
2021-09-04 21:28:16 +03:00
parent 8be147c3b8
commit f0df9d9233
7 changed files with 83 additions and 5 deletions

View File

@@ -3,6 +3,7 @@
"main": "dist/src/index.js",
"version": "0.0.0",
"types": "dist/src/index.d.ts",
"type": "module",
"scripts": {
"build": "tsc --build"
},

View File

@@ -11,6 +11,21 @@ export type UpdateMessage = Message & {
};
};
export type Room = {
id: number;
title: string;
free: boolean;
x: number;
y: number;
width: number;
height: number;
};
export type ListMessage = Message & {
type: "list";
args: Room[];
};
const isObjLike = (obj: unknown): obj is object =>
Boolean(obj) && typeof obj === "object";
@@ -31,3 +46,40 @@ export const isUpdateMessage = (message: Message): message is UpdateMessage =>
typeof message.args.id === "number" &&
hasProperty(message.args, "value") &&
typeof message.args.value === "boolean";
export const isRoom = (obj: unknown): obj is Room => {
if (!(typeof obj === "object") || !obj) return false;
const ns = ["id", "x", "y", "width", "height"] as const;
for (const key of ns) {
if (!hasProperty(obj, key) || typeof obj[key] !== "number") return false;
}
if (
!hasProperty(obj, "title") ||
typeof obj.title !== "string" ||
!hasProperty(obj, "free") ||
typeof obj.free !== "boolean"
)
return false;
return true;
};
export const isArrayOf = <T extends unknown>(
obj: T,
itemCheck: (arg: T) => boolean
) => {
if (Array.isArray(obj)) {
for (const el of obj) if (!itemCheck(el)) return false;
return true;
} else return false;
};
export const composeMessage = <T extends Message>(
type: T["type"],
args: T["args"]
): Message => ({
type,
args,
});

View File

@@ -1,10 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"module": "es6",
"target": "es6",
"outDir": "dist",
"composite": true
},
"include": ["src"]
"include": ["src"],
}