diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts new file mode 100644 index 0000000..2dbd51e --- /dev/null +++ b/frontend/src/API/API.ts @@ -0,0 +1,32 @@ +import { NewUser, User } from "../Types/Users"; + +// Defines all the methods that an instance of the API must implement +interface API { + /** Register a new user */ + registerUser(user: NewUser): Promise; + /** Remove a user */ + removeUser(username: string): Promise; +} + +// Export an instance of the API +export const api: API = { + async registerUser(user: NewUser): Promise { + return fetch("/api/register", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(user), + }).then((res) => res.json() as Promise); + }, + + async removeUser(username: string): Promise { + return fetch("/api/userdelete", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(username), + }).then((res) => res.json() as Promise); + }, +}; diff --git a/frontend/src/Types/Users.ts b/frontend/src/Types/Users.ts new file mode 100644 index 0000000..2a195b2 --- /dev/null +++ b/frontend/src/Types/Users.ts @@ -0,0 +1,11 @@ +// This is how the API responds +export interface User { + id: number; + name: string; +} + +// Used to create a new user +export interface NewUser { + name: string; + password: string; +}