From 3c87fd4d8cafe79961ad7cb46512c0a19189c62e Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Sat, 16 Mar 2024 17:42:28 +0100 Subject: [PATCH] New api interface --- frontend/src/API/API.ts | 135 +++++++++++++++++++++++++++++----------- 1 file changed, 99 insertions(+), 36 deletions(-) diff --git a/frontend/src/API/API.ts b/frontend/src/API/API.ts index f33c87c..248ad37 100644 --- a/frontend/src/API/API.ts +++ b/frontend/src/API/API.ts @@ -1,57 +1,120 @@ import { NewProject, Project } from "../Types/Project"; import { NewUser, User } from "../Types/Users"; +// This type of pattern should be hard to misuse +interface APIResponse { + success: boolean; + message?: string; + data?: T; +} + +// Note that all protected routes also require a token // Defines all the methods that an instance of the API must implement interface API { /** Register a new user */ - registerUser(user: NewUser): Promise; + registerUser(user: NewUser): Promise>; /** Remove a user */ - removeUser(username: string): Promise; + removeUser(username: string, token: string): Promise>; /** Create a project */ - createProject(project: NewProject): Promise; + createProject( + project: NewProject, + token: string, + ): Promise>; /** Renew the token */ - renewToken(token: string): Promise; + renewToken(token: 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 registerUser(user: NewUser): Promise> { + try { + const response = await fetch("/api/register", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(user), + }); + + if (!response.ok) { + return { success: false, message: "Failed to register user" }; + } else { + const data = (await response.json()) as User; + return { success: true, data }; + } + } catch (e) { + return { success: false, message: "Failed to register user" }; + } }, - 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); + async removeUser( + username: string, + token: string, + ): Promise> { + try { + const response = await fetch("/api/userdelete", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer " + token, + }, + body: JSON.stringify(username), + }); + + if (!response.ok) { + return { success: false, message: "Failed to remove user" }; + } else { + const data = (await response.json()) as User; + return { success: true, data }; + } + } catch (e) { + return { success: false, message: "Failed to remove user" }; + } }, - async createProject(project: NewProject): Promise { - return fetch("/api/project", { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify(project), - }).then((res) => res.json() as Promise); + async createProject( + project: NewProject, + token: string, + ): Promise> { + try { + const response = await fetch("/api/project", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer " + token, + }, + body: JSON.stringify(project), + }); + + if (!response.ok) { + return { success: false, message: "Failed to create project" }; + } else { + const data = (await response.json()) as Project; + return { success: true, data }; + } + } catch (e) { + return { success: false, message: "Failed to create project" }; + } }, - async renewToken(token: string): Promise { - return fetch("/api/loginrenew", { - method: "POST", - headers: { - "Content-Type": "application/json", - Authorization: "Bearer " + token, - }, - }).then((res) => res.json() as Promise); + async renewToken(token: string): Promise> { + try { + const response = await fetch("/api/loginrenew", { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: "Bearer " + token, + }, + }); + + if (!response.ok) { + return { success: false, message: "Failed to renew token" }; + } else { + const data = (await response.json()) as string; + return { success: true, data }; + } + } catch (e) { + return { success: false, message: "Failed to renew token" }; + } }, };