created API Client

This commit is contained in:
mmacierzanka committed 2021-10-03 16:03:36 +02:00
1 parent 5723208721
commit 4ee3d0b39a
2 files changed
+29

No files matched your search

+5
View File
@@ -1 +1,6 @@
import { createContext } from "react";
import { APIClient } from "./utils/APIClient";
export const BACKEND_URL = "http://localhost:3000";
export const EVENT_CONTEXT = createContext([]);
export const API_CLIENT = new APIClient(BACKEND_URL);
+24
View File
@@ -0,0 +1,24 @@
import axios from "axios";
export class APIClient {
#dbUrl = "";
constructor(dbUrl) {
if (typeof dbUrl !== "string") throw new Error("Database URL is not a string");
this.#dbUrl = dbUrl;
}
async updateEvents() {
const rq = await axios.get(`${this.#dbUrl}/api/getEvents`);
if (rq.status !== 200) throw new Error(`Request failed with status code ${rq.status}: ${rq.statusText}`);
return rq.data;
}
async getEvent(id) {
if (typeof id !== "string") throw new TypeError("Invalid ID type");
const events = await this.updateEvents();
return events.find((e) => e.id == id) ?? null;
}
async createEvent(data) {
return;
// Not finished
}
}