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