TTime/frontend/src/API/API.ts

58 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-03-13 17:52:56 +01:00
import { NewProject, Project } from "../Types/Project";
2024-03-13 17:06:26 +01:00
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<User>;
/** Remove a user */
removeUser(username: string): Promise<User>;
2024-03-13 17:52:56 +01:00
/** Create a project */
createProject(project: NewProject): Promise<Project>;
2024-03-13 20:56:47 +01:00
/** Renew the token */
renewToken(token: string): Promise<string>;
2024-03-13 17:06:26 +01:00
}
// Export an instance of the API
export const api: API = {
async registerUser(user: NewUser): Promise<User> {
return fetch("/api/register", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(user),
}).then((res) => res.json() as Promise<User>);
},
async removeUser(username: string): Promise<User> {
return fetch("/api/userdelete", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(username),
}).then((res) => res.json() as Promise<User>);
},
2024-03-13 17:52:56 +01:00
async createProject(project: NewProject): Promise<Project> {
return fetch("/api/project", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(project),
}).then((res) => res.json() as Promise<Project>);
},
2024-03-13 20:56:47 +01:00
async renewToken(token: string): Promise<string> {
return fetch("/api/loginrenew", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + token,
},
}).then((res) => res.json() as Promise<string>);
},
2024-03-13 17:06:26 +01:00
};