| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | import { BaseCRUDAPI } from "./_base"; | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  | import { UserIn, UserOut } from "~/types/api-types/user"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Interfaces
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface ChangePassword { | 
					
						
							|  |  |  |   currentPassword: string; | 
					
						
							|  |  |  |   newPassword: string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface CreateAPIToken { | 
					
						
							|  |  |  |   name: string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Code
 | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const prefix = "/api"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const routes = { | 
					
						
							|  |  |  |   usersSelf: `${prefix}/users/self`, | 
					
						
							|  |  |  |   users: `${prefix}/users`, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   usersIdImage: (id: string) => `${prefix}/users/${id}/image`, | 
					
						
							|  |  |  |   usersIdResetPassword: (id: string) => `${prefix}/users/${id}/reset-password`, | 
					
						
							|  |  |  |   usersId: (id: string) => `${prefix}/users/${id}`, | 
					
						
							|  |  |  |   usersIdPassword: (id: string) => `${prefix}/users/${id}/password`, | 
					
						
							|  |  |  |   usersIdFavorites: (id: string) => `${prefix}/users/${id}/favorites`, | 
					
						
							|  |  |  |   usersIdFavoritesSlug: (id: string, slug: string) => `${prefix}/users/${id}/favorites/${slug}`, | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   usersApiTokens: `${prefix}/users/api-tokens`, | 
					
						
							|  |  |  |   usersApiTokensTokenId: (token_id: string) => `${prefix}/users/api-tokens/${token_id}`, | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | export class UserApi extends BaseCRUDAPI<UserOut, UserIn> { | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |   baseRoute: string = routes.users; | 
					
						
							|  |  |  |   itemRoute = (itemid: string) => routes.usersId(itemid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async addFavorite(id: string, slug: string) { | 
					
						
							|  |  |  |     return await this.requests.post(routes.usersIdFavoritesSlug(id, slug), {}); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async removeFavorite(id: string, slug: string) { | 
					
						
							|  |  |  |     return await this.requests.delete(routes.usersIdFavoritesSlug(id, slug)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async getFavorites(id: string) { | 
					
						
							|  |  |  |     await this.requests.get(routes.usersIdFavorites(id)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async changePassword(id: string, changePassword: ChangePassword) { | 
					
						
							|  |  |  |     return await this.requests.put(routes.usersIdPassword(id), changePassword); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async resetPassword(id: string) { | 
					
						
							|  |  |  |     return await this.requests.post(routes.usersIdResetPassword(id), {}); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async createAPIToken(tokenName: CreateAPIToken) { | 
					
						
							|  |  |  |     return await this.requests.post(routes.usersApiTokens, tokenName); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |   async deleteApiToken(tokenId: string) { | 
					
						
							|  |  |  |     return await this.requests.delete(routes.usersApiTokensTokenId(tokenId)); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |   userProfileImage(id: string) { | 
					
						
							|  |  |  |     if (!id || id === undefined) return; | 
					
						
							|  |  |  |     return `/api/users/${id}/image`; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  | } |