New api interface
This commit is contained in:
parent
17c8a17ebf
commit
3c87fd4d8c
1 changed files with 99 additions and 36 deletions
|
@ -1,57 +1,120 @@
|
||||||
import { NewProject, Project } from "../Types/Project";
|
import { NewProject, Project } from "../Types/Project";
|
||||||
import { NewUser, User } from "../Types/Users";
|
import { NewUser, User } from "../Types/Users";
|
||||||
|
|
||||||
|
// 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
|
||||||
// Defines all the methods that an instance of the API must implement
|
// Defines all the methods that an instance of the API must implement
|
||||||
interface API {
|
interface API {
|
||||||
/** Register a new user */
|
/** Register a new user */
|
||||||
registerUser(user: NewUser): Promise<User>;
|
registerUser(user: NewUser): Promise<APIResponse<User>>;
|
||||||
/** Remove a user */
|
/** Remove a user */
|
||||||
removeUser(username: string): Promise<User>;
|
removeUser(username: string, token: string): Promise<APIResponse<User>>;
|
||||||
/** Create a project */
|
/** Create a project */
|
||||||
createProject(project: NewProject): Promise<Project>;
|
createProject(
|
||||||
|
project: NewProject,
|
||||||
|
token: string,
|
||||||
|
): Promise<APIResponse<Project>>;
|
||||||
/** Renew the token */
|
/** Renew the token */
|
||||||
renewToken(token: string): Promise<string>;
|
renewToken(token: string): Promise<APIResponse<string>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Export an instance of the API
|
// Export an instance of the API
|
||||||
export const api: API = {
|
export const api: API = {
|
||||||
async registerUser(user: NewUser): Promise<User> {
|
async registerUser(user: NewUser): Promise<APIResponse<User>> {
|
||||||
return fetch("/api/register", {
|
try {
|
||||||
method: "POST",
|
const response = await fetch("/api/register", {
|
||||||
headers: {
|
method: "POST",
|
||||||
"Content-Type": "application/json",
|
headers: {
|
||||||
},
|
"Content-Type": "application/json",
|
||||||
body: JSON.stringify(user),
|
},
|
||||||
}).then((res) => res.json() as Promise<User>);
|
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<User> {
|
async removeUser(
|
||||||
return fetch("/api/userdelete", {
|
username: string,
|
||||||
method: "POST",
|
token: string,
|
||||||
headers: {
|
): Promise<APIResponse<User>> {
|
||||||
"Content-Type": "application/json",
|
try {
|
||||||
},
|
const response = await fetch("/api/userdelete", {
|
||||||
body: JSON.stringify(username),
|
method: "POST",
|
||||||
}).then((res) => res.json() as Promise<User>);
|
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<Project> {
|
async createProject(
|
||||||
return fetch("/api/project", {
|
project: NewProject,
|
||||||
method: "POST",
|
token: string,
|
||||||
headers: {
|
): Promise<APIResponse<Project>> {
|
||||||
"Content-Type": "application/json",
|
try {
|
||||||
},
|
const response = await fetch("/api/project", {
|
||||||
body: JSON.stringify(project),
|
method: "POST",
|
||||||
}).then((res) => res.json() as Promise<Project>);
|
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<string> {
|
async renewToken(token: string): Promise<APIResponse<string>> {
|
||||||
return fetch("/api/loginrenew", {
|
try {
|
||||||
method: "POST",
|
const response = await fetch("/api/loginrenew", {
|
||||||
headers: {
|
method: "POST",
|
||||||
"Content-Type": "application/json",
|
headers: {
|
||||||
Authorization: "Bearer " + token,
|
"Content-Type": "application/json",
|
||||||
},
|
Authorization: "Bearer " + token,
|
||||||
}).then((res) => res.json() as Promise<string>);
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
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" };
|
||||||
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue