mirror of
				https://github.com/mealie-recipes/mealie.git
				synced 2025-10-31 10:13:32 -04:00 
			
		
		
		
	refactor(frontend): ♻️ update API folder structure
This commit is contained in:
		
							
								
								
									
										60
									
								
								frontend/api/_base.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								frontend/api/_base.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,60 @@ | ||||
| import { ApiRequestInstance } from "~/types/api"; | ||||
|  | ||||
| export interface CrudAPIInterface { | ||||
|   requests: ApiRequestInstance; | ||||
|  | ||||
|   // Route Properties / Methods | ||||
|   baseRoute: string; | ||||
|   itemRoute(itemId: string | number): string; | ||||
|  | ||||
|   // Methods | ||||
| } | ||||
|  | ||||
| export interface CrudAPIMethodsInterface { | ||||
|   // CRUD Methods | ||||
|   getAll(): any; | ||||
|   createOne(): any; | ||||
|   getOne(): any; | ||||
|   updateOne(): any; | ||||
|   patchOne(): any; | ||||
|   deleteOne(): any; | ||||
| } | ||||
|  | ||||
| export abstract class BaseAPI { | ||||
|   requests: ApiRequestInstance; | ||||
|  | ||||
|   constructor(requests: ApiRequestInstance) { | ||||
|     this.requests = requests; | ||||
|   } | ||||
| } | ||||
|  | ||||
| export abstract class BaseCRUDAPI<T, U> extends BaseAPI implements CrudAPIInterface { | ||||
|   abstract baseRoute: string; | ||||
|   abstract itemRoute(itemId: string | number): string; | ||||
|  | ||||
|   async getAll(start = 0, limit = 9999, params = {}) { | ||||
|     return await this.requests.get<T[]>(this.baseRoute, { | ||||
|       params: { start, limit, ...params }, | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   async createOne(payload: U) { | ||||
|     return await this.requests.post<T>(this.baseRoute, payload); | ||||
|   } | ||||
|  | ||||
|   async getOne(itemId: string | number) { | ||||
|     return await this.requests.get<T>(this.itemRoute(itemId)); | ||||
|   } | ||||
|  | ||||
|   async updateOne(itemId: string | number, payload: T) { | ||||
|     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); | ||||
|   } | ||||
|  | ||||
|   async deleteOne(itemId: string | number) { | ||||
|     return await this.requests.delete<T>(this.itemRoute(itemId)); | ||||
|   } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user