TTime/frontend/src/API/API.ts

121 lines
3.3 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";
2024-03-16 17:42:28 +01:00
// This type of pattern should be hard to misuse
interface APIResponse<T> {
success: boolean;
message?: string;
data?: T;
}
// Note that all protected routes also require a token
2024-03-13 17:06:26 +01:00
// Defines all the methods that an instance of the API must implement
interface API {
/** Register a new user */
2024-03-16 17:42:28 +01:00
registerUser(user: NewUser): Promise<APIResponse<User>>;
2024-03-13 17:06:26 +01:00
/** Remove a user */
2024-03-16 17:42:28 +01:00
removeUser(username: string, token: string): Promise<APIResponse<User>>;
2024-03-13 17:52:56 +01:00
/** Create a project */
2024-03-16 17:42:28 +01:00
createProject(
project: NewProject,
token: string,
): Promise<APIResponse<Project>>;
2024-03-13 20:56:47 +01:00
/** Renew the token */
2024-03-16 17:42:28 +01:00
renewToken(token: string): Promise<APIResponse<string>>;
2024-03-13 17:06:26 +01:00
}
// Export an instance of the API
export const api: API = {
2024-03-16 17:42:28 +01:00
async registerUser(user: NewUser): Promise<APIResponse<User>> {
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" };
}
2024-03-13 17:06:26 +01:00
},
2024-03-16 17:42:28 +01:00
async removeUser(
username: string,
token: string,
): Promise<APIResponse<User>> {
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" };
}
2024-03-13 17:06:26 +01:00
},
2024-03-13 17:52:56 +01:00
2024-03-16 17:42:28 +01:00
async createProject(
project: NewProject,
token: string,
): Promise<APIResponse<Project>> {
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" };
}
2024-03-13 17:52:56 +01:00
},
2024-03-13 20:56:47 +01:00
2024-03-16 17:42:28 +01:00
async renewToken(token: string): Promise<APIResponse<string>> {
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" };
}
2024-03-13 20:56:47 +01:00
},
2024-03-13 17:06:26 +01:00
};