aboutsummaryrefslogtreecommitdiffstats
path: root/helpers/helpers.ts
diff options
context:
space:
mode:
authorMaks Jopek <maksymilian.jopek@prym-soft.pl>2021-04-10 01:06:09 +0200
committerMaks Jopek <maksymilian.jopek@prym-soft.pl>2021-04-10 01:06:09 +0200
commitdd66b8ad239d5ceae219746a702963645259b784 (patch)
tree47462463911a018a31cd1ede3d8206fd96032b99 /helpers/helpers.ts
parentd928eab3ecefbf778f1cadece9beac010dc42332 (diff)
downloadfia-dd66b8ad239d5ceae219746a702963645259b784.tar.gz
fia-dd66b8ad239d5ceae219746a702963645259b784.tar.zst
fia-dd66b8ad239d5ceae219746a702963645259b784.zip
Added joining rooms and logic to start game by votes
Diffstat (limited to 'helpers/helpers.ts')
-rw-r--r--helpers/helpers.ts54
1 files changed, 47 insertions, 7 deletions
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<string>
-): Promise<string> {
- console.log("makeQuery");
- return await (
+export async function makeQuery(query: string, params: Array<any>): Promise<object> {
+ 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<Response> */ {
+ 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;
+}