Refactor API.ts for improved readability and maintainability
This commit is contained in:
parent
f3931f905a
commit
46eebee84f
1 changed files with 33 additions and 14 deletions
|
@ -22,12 +22,17 @@ interface API {
|
|||
token: string,
|
||||
): Promise<APIResponse<Project>>;
|
||||
/** Submit a weekly report */
|
||||
submitWeeklyReport(project: NewWeeklyReport, token: string): Promise<APIResponse<Project>>;
|
||||
submitWeeklyReport(
|
||||
project: NewWeeklyReport,
|
||||
token: string,
|
||||
): Promise<APIResponse<Project>>;
|
||||
/** Renew the token */
|
||||
renewToken(token: string): Promise<APIResponse<string>>;
|
||||
/** Gets all the projects of a user*/
|
||||
getUserProjects(username: string, token: string): Promise<APIResponse<Project[]>>;
|
||||
|
||||
getUserProjects(
|
||||
username: string,
|
||||
token: string,
|
||||
): Promise<APIResponse<Project[]>>;
|
||||
}
|
||||
|
||||
// Export an instance of the API
|
||||
|
@ -55,7 +60,7 @@ export const api: API = {
|
|||
|
||||
async removeUser(
|
||||
username: string,
|
||||
token: string
|
||||
token: string,
|
||||
): Promise<APIResponse<User>> {
|
||||
try {
|
||||
const response = await fetch("/api/userdelete", {
|
||||
|
@ -80,7 +85,7 @@ export const api: API = {
|
|||
|
||||
async createProject(
|
||||
project: NewProject,
|
||||
token: string
|
||||
token: string,
|
||||
): Promise<APIResponse<Project>> {
|
||||
try {
|
||||
const response = await fetch("/api/project", {
|
||||
|
@ -135,18 +140,26 @@ export const api: API = {
|
|||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return Promise.resolve({ success: false, message: "Failed to get user projects" });
|
||||
return Promise.resolve({
|
||||
success: false,
|
||||
message: "Failed to get user projects",
|
||||
});
|
||||
} else {
|
||||
const data = (await response.json()) as Project[];
|
||||
return Promise.resolve({ success: true, data });
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
return Promise.resolve({ success: false, message: "Failed to get user projects" });
|
||||
} catch (e) {
|
||||
return Promise.resolve({
|
||||
success: false,
|
||||
message: "Failed to get user projects",
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
submitWeeklyReport: function (project: NewWeeklyReport, token: string): Promise<APIResponse<Project>> {
|
||||
|
||||
submitWeeklyReport: function (
|
||||
project: NewWeeklyReport,
|
||||
token: string,
|
||||
): Promise<APIResponse<Project>> {
|
||||
try {
|
||||
return fetch("/api/submitWeeklyReport", {
|
||||
method: "POST",
|
||||
|
@ -158,7 +171,10 @@ export const api: API = {
|
|||
})
|
||||
.then((response) => {
|
||||
if (!response.ok) {
|
||||
return { success: false, message: "Failed to submit weekly report" };
|
||||
return {
|
||||
success: false,
|
||||
message: "Failed to submit weekly report",
|
||||
};
|
||||
} else {
|
||||
return response.json();
|
||||
}
|
||||
|
@ -167,7 +183,10 @@ export const api: API = {
|
|||
return { success: true, data };
|
||||
});
|
||||
} catch (e) {
|
||||
return Promise.resolve({ success: false, message: "Failed to submit weekly report" });
|
||||
return Promise.resolve({
|
||||
success: false,
|
||||
message: "Failed to submit weekly report",
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue