mirror of
				https://github.com/mealie-recipes/mealie.git
				synced 2025-10-31 10:13:32 -04:00 
			
		
		
		
	* feature/frontend: Add duplicate button to recipe * feature/backend: Add recipe duplication endpoint * feature/frontend: add duplication API call * Regenerate API docs * Fix linter errors * Fix backend linter error * Move recipe duplication logic to recipe service * Add test for recipe duplication * Improve recipe ingredients copy test * generate types * import type Co-authored-by: Hayden <64056131+hay-kot@users.noreply.github.com>
		
			
				
	
	
		
			60 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			60 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| import { Recipe } from "../types/recipe";
 | |
| import { ApiRequestInstance, PaginationData } from "~/lib/api/types/non-generated";
 | |
| 
 | |
| export interface CrudAPIInterface {
 | |
|   requests: ApiRequestInstance;
 | |
| 
 | |
|   // Route Properties / Methods
 | |
|   baseRoute: string;
 | |
|   itemRoute(itemId: string | number): string;
 | |
| 
 | |
|   // Methods
 | |
| }
 | |
| 
 | |
| export abstract class BaseAPI {
 | |
|   requests: ApiRequestInstance;
 | |
| 
 | |
|   constructor(requests: ApiRequestInstance) {
 | |
|     this.requests = requests;
 | |
|   }
 | |
| }
 | |
| 
 | |
| export abstract class BaseCRUDAPI<CreateType, ReadType, UpdateType = CreateType>
 | |
|   extends BaseAPI
 | |
|   implements CrudAPIInterface {
 | |
|   abstract baseRoute: string;
 | |
|   abstract itemRoute(itemId: string | number): string;
 | |
| 
 | |
|   async getAll(page = 1, perPage = -1, params = {} as any) {
 | |
|     return await this.requests.get<PaginationData<ReadType>>(this.baseRoute, {
 | |
|       params: { page, perPage, ...params },
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   async createOne(payload: CreateType) {
 | |
|     return await this.requests.post<ReadType>(this.baseRoute, payload);
 | |
|   }
 | |
| 
 | |
|   async getOne(itemId: string | number) {
 | |
|     return await this.requests.get<ReadType>(this.itemRoute(itemId));
 | |
|   }
 | |
| 
 | |
|   async updateOne(itemId: string | number, payload: UpdateType) {
 | |
|     return await this.requests.put<ReadType, UpdateType>(this.itemRoute(itemId), payload);
 | |
|   }
 | |
| 
 | |
|   async patchOne(itemId: string, payload: Partial<UpdateType>) {
 | |
|     return await this.requests.patch<ReadType, Partial<UpdateType>>(this.itemRoute(itemId), payload);
 | |
|   }
 | |
| 
 | |
|   async deleteOne(itemId: string | number) {
 | |
|     return await this.requests.delete<ReadType>(this.itemRoute(itemId));
 | |
|   }
 | |
| 
 | |
|   async duplicateOne(itemId: string | number, newName: string | undefined) {
 | |
|     return await this.requests.post<Recipe>(`${this.itemRoute(itemId)}/duplicate`, {
 | |
|       name: newName,
 | |
|     });
 | |
|   }
 | |
| }
 |