| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  | import type { Recipe } from "../types/recipe"; | 
					
						
							|  |  |  | import type { ApiRequestInstance, PaginationData } from "~/lib/api/types/non-generated"; | 
					
						
							|  |  |  | import { type QueryValue, route } from "~/lib/api/base/route"; | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-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
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  | export abstract class BaseCRUDAPIReadOnly<ReadType> | 
					
						
							| 
									
										
										
										
											2022-10-22 11:51:07 -08:00
										 |  |  |   extends BaseAPI | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  |   implements CrudAPIInterface { | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |   public baseRoute: string; | 
					
						
							|  |  |  |   public itemRouteFn: (itemId: string | number) => string; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor( | 
					
						
							|  |  |  |     requests: ApiRequestInstance, | 
					
						
							|  |  |  |     baseRoute: string, | 
					
						
							|  |  |  |     itemRoute: (itemId: string | number) => string, | 
					
						
							|  |  |  |   ) { | 
					
						
							|  |  |  |     super(requests); | 
					
						
							|  |  |  |     this.baseRoute = baseRoute; | 
					
						
							|  |  |  |     this.itemRouteFn = itemRoute; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   get baseRouteValue() { | 
					
						
							|  |  |  |     return this.baseRoute; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   itemRoute(itemId: string | number): string { | 
					
						
							|  |  |  |     return this.itemRouteFn(itemId); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-26 20:20:26 +01:00
										 |  |  |   async getAll(page = 1, perPage = -1, params = {} as Record<string, QueryValue>) { | 
					
						
							|  |  |  |     params = Object.fromEntries(Object.entries(params).filter(([_, v]) => v !== null && v !== undefined)); | 
					
						
							|  |  |  |     return await this.requests.get<PaginationData<ReadType>>(route(this.baseRoute, { page, perPage, ...params })); | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-22 15:23:45 -08:00
										 |  |  |   async getOne(itemId: string | number) { | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  |     return await this.requests.get<ReadType>(this.itemRoute(itemId)); | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2023-09-14 09:01:24 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export abstract class BaseCRUDAPI<CreateType, ReadType, UpdateType = CreateType> | 
					
						
							|  |  |  |   extends BaseCRUDAPIReadOnly<ReadType> | 
					
						
							|  |  |  |   implements CrudAPIInterface { | 
					
						
							|  |  |  |   async createOne(payload: CreateType) { | 
					
						
							|  |  |  |     return await this.requests.post<ReadType>(this.baseRoute, payload); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  |   async updateOne(itemId: string | number, payload: UpdateType) { | 
					
						
							|  |  |  |     return await this.requests.put<ReadType, UpdateType>(this.itemRoute(itemId), payload); | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  |   async patchOne(itemId: string, payload: Partial<UpdateType>) { | 
					
						
							|  |  |  |     return await this.requests.patch<ReadType, Partial<UpdateType>>(this.itemRoute(itemId), payload); | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |   async deleteOne(itemId: string | number) { | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  |     return await this.requests.delete<ReadType>(this.itemRoute(itemId)); | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2022-12-01 06:57:26 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   async duplicateOne(itemId: string | number, newName: string | undefined) { | 
					
						
							|  |  |  |     return await this.requests.post<Recipe>(`${this.itemRoute(itemId)}/duplicate`, { | 
					
						
							|  |  |  |       name: newName, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | } |