From dd66b8ad239d5ceae219746a702963645259b784 Mon Sep 17 00:00:00 2001 From: Maks Jopek Date: Sat, 10 Apr 2021 01:06:09 +0200 Subject: Added joining rooms and logic to start game by votes --- helpers/helpers.ts | 54 ++++++++++++++++++++++++++++++++++++++++++++------- helpers/tsconfig.json | 12 ++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) create mode 100644 helpers/tsconfig.json (limited to 'helpers') diff --git a/helpers/helpers.ts b/helpers/helpers.ts index 3a16d18..de6744f 100644 --- a/helpers/helpers.ts +++ b/helpers/helpers.ts @@ -1,11 +1,8 @@ import fetch from "node-fetch"; +import { Response } from "express"; -export async function makeQuery( - query: string, - params: Array -): Promise { - console.log("makeQuery"); - return await ( +export async function makeQuery(query: string, params: Array): Promise { + let t = await ( await fetch("https://jopek.eu/maks/szkola/apkKli/fiaFiles/fia.php", { method: "POST", headers: { @@ -14,11 +11,49 @@ export async function makeQuery( }, body: JSON.stringify({ apiKey: process.env.API_KEY, - stmt: query, + query: query, params: params }) }) ).text(); + // console.log(t); + return JSON.parse(t); +} + +export function dbResToFiaTable(dbRes: dbRes): fiaTable { + if (typeof dbRes.data === "string") + dbRes.data = JSON.parse(dbRes.data); + else + throw new Error("dbResToFiaTable : wrong $1 type; "); + + // @ts-ignore + return dbRes; +} + +export function checkApiRes(res: any, nRes = {} as Response): void { + if (res.api === false) { + if (Object.keys(nRes).length === 0) { + alert("API Error!"); + throw new Error("API Error!"); + } else { + nRes.status(500); + res.send(""); + } + } + if (Object.keys(nRes).length !== 0) + nRes.status(200); + return res; +} + +export async function mFetch(url: string, body: object) /* : Promise */ { + return fetch(url, { + method: "POST", + headers: { + Accept: "application/json", + "Content-Type": "application/json" + }, + body: JSON.stringify(body) + }).then(checkApiRes); } export interface Colors { @@ -50,3 +85,8 @@ export interface fiaTable { data: Data; started: boolean; } +export interface dbRes { + id: number; + data: string | Data; + started: boolean; +} diff --git a/helpers/tsconfig.json b/helpers/tsconfig.json new file mode 100644 index 0000000..21a1541 --- /dev/null +++ b/helpers/tsconfig.json @@ -0,0 +1,12 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "module": "esnext", + "lib": [ + "DOM" + ], + }, + "include": [ + "*" + ] +} \ No newline at end of file -- cgit v1.3.1