diff options
Diffstat (limited to 'helpers/helpers.ts')
| -rw-r--r-- | helpers/helpers.ts | 54 |
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; +} |
