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 { 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
 | ||||
| interface API { | ||||
|   /** Register a new user */ | ||||
|   registerUser(user: NewUser): Promise<User>; | ||||
|   registerUser(user: NewUser): Promise<APIResponse<User>>; | ||||
|   /** Remove a user */ | ||||
|   removeUser(username: string): Promise<User>; | ||||
|   removeUser(username: string, token: string): Promise<APIResponse<User>>; | ||||
|   /** Create a project */ | ||||
|   createProject(project: NewProject): Promise<Project>; | ||||
|   createProject( | ||||
|     project: NewProject, | ||||
|     token: string, | ||||
|   ): Promise<APIResponse<Project>>; | ||||
|   /** Renew the token */ | ||||
|   renewToken(token: string): Promise<string>; | ||||
|   renewToken(token: string): Promise<APIResponse<string>>; | ||||
| } | ||||
| 
 | ||||
| // 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 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" }; | ||||
|     } | ||||
|   }, | ||||
| 
 | ||||
|   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>); | ||||
|   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" }; | ||||
|     } | ||||
|   }, | ||||
| 
 | ||||
|   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>); | ||||
|   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" }; | ||||
|     } | ||||
|   }, | ||||
| 
 | ||||
|   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>); | ||||
|   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" }; | ||||
|     } | ||||
|   }, | ||||
| }; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Imbus
						Imbus