getAllUsersProject API, fetches all users in a project
This commit is contained in:
		
							parent
							
								
									056ca3660d
								
							
						
					
					
						commit
						cd452459c2
					
				
					 1 changed files with 37 additions and 1 deletions
				
			
		|  | @ -4,6 +4,7 @@ import { | |||
|   User, | ||||
|   Project, | ||||
|   NewProject, | ||||
|   PublicUser, | ||||
| } from "../Types/goTypes"; | ||||
| 
 | ||||
| // This type of pattern should be hard to misuse
 | ||||
|  | @ -45,8 +46,13 @@ interface API { | |||
|   getUserProjects(token: string): Promise<APIResponse<Project[]>>; | ||||
|   /** Gets a project from id*/ | ||||
|   getProject(id: number): Promise<APIResponse<Project>>; | ||||
|   /** Gets a project from id*/ | ||||
|   /** Gets all users*/ | ||||
|   getAllUsers(token: string): Promise<APIResponse<string[]>>; | ||||
|   /** Gets all users in a project from name*/ | ||||
|   getAllUsersProject( | ||||
|     projectName: string, | ||||
|     token: string, | ||||
|   ): Promise<APIResponse<PublicUser[]>>; | ||||
| } | ||||
| 
 | ||||
| // Export an instance of the API
 | ||||
|  | @ -308,4 +314,34 @@ export const api: API = { | |||
|       }); | ||||
|     } | ||||
|   }, | ||||
|   //Gets all users in a project
 | ||||
|   async getAllUsersProject( | ||||
|     projectName: string, | ||||
|     token: string, | ||||
|   ): Promise<APIResponse<PublicUser[]>> { | ||||
|     try { | ||||
|       const response = await fetch(`/api/getUsersProject/${projectName}`, { | ||||
|         method: "GET", | ||||
|         headers: { | ||||
|           "Content-Type": "application/json", | ||||
|           Authorization: "Bearer " + token, | ||||
|         }, | ||||
|       }); | ||||
| 
 | ||||
|       if (!response.ok) { | ||||
|         return Promise.resolve({ | ||||
|           success: false, | ||||
|           message: "Failed to get users", | ||||
|         }); | ||||
|       } else { | ||||
|         const data = (await response.json()) as User[]; | ||||
|         return Promise.resolve({ success: true, data }); | ||||
|       } | ||||
|     } catch (e) { | ||||
|       return Promise.resolve({ | ||||
|         success: false, | ||||
|         message: "API is not ok", | ||||
|       }); | ||||
|     } | ||||
|   }, | ||||
| }; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Peter KW
						Peter KW