| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  | import { ApiRequestInstance } from "~/types/api"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | export interface CrudAPIInterface { | 
					
						
							|  |  |  |   requests: ApiRequestInstance; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Route Properties / Methods
 | 
					
						
							|  |  |  |   baseRoute: string; | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |   itemRoute(itemId: string | number): string; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Methods
 | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  | export const crudMixins = <T>( | 
					
						
							|  |  |  |   requests: ApiRequestInstance, | 
					
						
							|  |  |  |   baseRoute: string, | 
					
						
							|  |  |  |   itemRoute: (itemId: string) => string | 
					
						
							|  |  |  | ) => { | 
					
						
							|  |  |  |   async function getAll(start = 0, limit = 9999) { | 
					
						
							|  |  |  |     return await requests.get<T[]>(baseRoute, { | 
					
						
							|  |  |  |       params: { start, limit }, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |   async function createOne(payload: T) { | 
					
						
							|  |  |  |     return await requests.post<T>(baseRoute, payload); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  |   async function getOne(itemId: string) { | 
					
						
							|  |  |  |     return await requests.get<T>(itemRoute(itemId)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async function updateOne(itemId: string, payload: T) { | 
					
						
							|  |  |  |     return await requests.put<T>(itemRoute(itemId), payload); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async function patchOne(itemId: string, payload: T) { | 
					
						
							|  |  |  |     return await requests.patch(itemRoute(itemId), payload); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async function deleteOne(itemId: string) { | 
					
						
							|  |  |  |     return await requests.delete<T>(itemRoute(itemId)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |   return { getAll, getOne, updateOne, patchOne, deleteOne, createOne }; | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | export abstract class BaseAPI { | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   requests: ApiRequestInstance; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor(requests: ApiRequestInstance) { | 
					
						
							|  |  |  |     this.requests = requests; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export abstract class BaseCRUDAPI<T, U> extends BaseAPI implements CrudAPIInterface { | 
					
						
							|  |  |  |   abstract baseRoute: string; | 
					
						
							|  |  |  |   abstract itemRoute(itemId: string | number): string; | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   async getAll(start = 0, limit = 9999) { | 
					
						
							|  |  |  |     return await this.requests.get<T[]>(this.baseRoute, { | 
					
						
							|  |  |  |       params: { start, limit }, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |   async createOne(payload: U) { | 
					
						
							|  |  |  |     return await this.requests.post<T>(this.baseRoute, payload); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   async getOne(itemId: string) { | 
					
						
							|  |  |  |     return await this.requests.get<T>(this.itemRoute(itemId)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  |   async updateOne(itemId: string, payload: T) { | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |     return await this.requests.put<T>(this.itemRoute(itemId), payload); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async patchOne(itemId: string, payload: T) { | 
					
						
							|  |  |  |     return await this.requests.patch(this.itemRoute(itemId), payload); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |   async deleteOne(itemId: string | number) { | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |     return await this.requests.delete<T>(this.itemRoute(itemId)); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |