Terarea  2
The automation project
Loading...
Searching...
No Matches
querier.ts
Go to the documentation of this file.
1const url: string = "https://ifttt-back.pingpal.news";
2const port: number = -1;
3
4async function query(
5 method: "GET" | "POST" | "PATCH" | "PUT" | "DELETE" = "GET",
6 path: string = "/",
7 body: object = {},
8 token: string = ""
9): Promise<any> {
10 try {
11 const payload: RequestInit = {
12 method: method,
13 mode: "cors",
14 headers: {
15 'Content-Type': 'application/json'
16 }
17 };
18
19 if (token !== "") {
20 (payload.headers as HeadersInit)["Authorization"] = `Bearer ${token}`;
21 }
22
23 if (method !== "GET" && Object.keys(body).length > 0) {
24 payload.body = JSON.stringify(body);
25 }
26
27 let final_url: string;
28
29 if (port === -1) {
30 final_url = `${url}${path}`;
31 } else {
32 final_url = `${url}:${port}${path}`;
33 }
34 const response: Response = await fetch(final_url, payload);
35 if (!response.ok) {
36 throw new Error(`Error: ${response.status}`);
37 }
38
39 const data = await response.json();
40 return data;
41 } catch (error) {
42 throw error;
43 }
44}
45
46async function get(path: string = "/", body: object = {}, token: string = ""): Promise<any> {
47 return await query("GET", path, body, token);
48}
49
50async function put(path: string = "/", body: object = {}, token: string = ""): Promise<any> {
51 return await query("PUT", path, body, token);
52}
53
54async function post(path: string = "/", body: object = {}, token: string = ""): Promise<any> {
55 return await query("POST", path, body, token);
56}
57
58async function patch(path: string = "/", body: object = {}, token: string = ""): Promise<any> {
59 return await query("PATCH", path, body, token);
60}
61
62async function delete_query(path: string = "/", body: object = {}, token: string = ""): Promise<any> {
63 return await query("DELETE", path, body, token);
64}
65
66const queries = {
67 query,
68 get,
69 put,
70 post,
71 patch,
72 delete_query
73};
74
75export { queries };