| 
									
										
										
										
											2021-10-23 16:42:20 -08:00
										 |  |  | import { BaseCRUDAPI } from "../_base"; | 
					
						
							| 
									
										
										
										
											2021-10-28 19:28:33 -08:00
										 |  |  | import { Category } from "./categories"; | 
					
						
							|  |  |  | import { Tag } from "./tags"; | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  | import { Recipe, CreateRecipe } from "~/types/api-types/recipe"; | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const prefix = "/api"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const routes = { | 
					
						
							|  |  |  |   recipesCreate: `${prefix}/recipes/create`, | 
					
						
							|  |  |  |   recipesBase: `${prefix}/recipes`, | 
					
						
							|  |  |  |   recipesTestScrapeUrl: `${prefix}/recipes/test-scrape-url`, | 
					
						
							|  |  |  |   recipesCreateUrl: `${prefix}/recipes/create-url`, | 
					
						
							| 
									
										
										
										
											2021-10-28 19:28:33 -08:00
										 |  |  |   recipesCreateUrlBulk: `${prefix}/recipes/create-url/bulk`, | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |   recipesCreateFromZip: `${prefix}/recipes/create-from-zip`, | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   recipesCategory: `${prefix}/recipes/category`, | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |   recipesParseIngredient: `${prefix}/parser/ingredient`, | 
					
						
							|  |  |  |   recipesParseIngredients: `${prefix}/parser/ingredients`, | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |   recipesRecipeSlug: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}`, | 
					
						
							|  |  |  |   recipesRecipeSlugZip: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}/zip`, | 
					
						
							|  |  |  |   recipesRecipeSlugImage: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}/image`, | 
					
						
							|  |  |  |   recipesRecipeSlugAssets: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}/assets`, | 
					
						
							| 
									
										
										
										
											2021-08-08 14:10:32 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   recipesSlugComments: (slug: string) => `${prefix}/recipes/${slug}/comments`, | 
					
						
							|  |  |  |   recipesSlugCommentsId: (slug: string, id: number) => `${prefix}/recipes/${slug}/comments/${id}`, | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  | export type Parser = "nlp" | "brute"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface Confidence { | 
					
						
							|  |  |  |   average?: number; | 
					
						
							|  |  |  |   comment?: number; | 
					
						
							|  |  |  |   name?: number; | 
					
						
							|  |  |  |   unit?: number; | 
					
						
							|  |  |  |   quantity?: number; | 
					
						
							|  |  |  |   food?: number; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface Unit { | 
					
						
							|  |  |  |   name: string; | 
					
						
							|  |  |  |   description: string; | 
					
						
							|  |  |  |   fraction: boolean; | 
					
						
							|  |  |  |   abbreviation: string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface Food { | 
					
						
							|  |  |  |   name: string; | 
					
						
							|  |  |  |   description: string; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface Ingredient { | 
					
						
							|  |  |  |   title: string; | 
					
						
							|  |  |  |   note: string; | 
					
						
							|  |  |  |   unit: Unit; | 
					
						
							|  |  |  |   food: Food; | 
					
						
							|  |  |  |   disableAmount: boolean; | 
					
						
							|  |  |  |   quantity: number; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface ParsedIngredient { | 
					
						
							|  |  |  |   confidence: Confidence; | 
					
						
							|  |  |  |   ingredient: Ingredient; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-28 19:28:33 -08:00
										 |  |  | export interface BulkCreateRecipe { | 
					
						
							|  |  |  |   url: string; | 
					
						
							|  |  |  |   categories: Category[]; | 
					
						
							|  |  |  |   tags: Tag[]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export interface BulkCreatePayload { | 
					
						
							|  |  |  |   imports: BulkCreateRecipe[]; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  | export class RecipeAPI extends BaseCRUDAPI<Recipe, CreateRecipe> { | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |   baseRoute: string = routes.recipesBase; | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  |   itemRoute = routes.recipesRecipeSlug; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   async getAllByCategory(categories: string[]) { | 
					
						
							|  |  |  |     return await this.requests.get<Recipe[]>(routes.recipesCategory, { | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  |       categories, | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  |   updateImage(slug: string, fileObject: File) { | 
					
						
							|  |  |  |     const formData = new FormData(); | 
					
						
							|  |  |  |     formData.append("image", fileObject); | 
					
						
							| 
									
										
										
										
											2021-08-07 15:12:25 -08:00
										 |  |  |     // @ts-ignore
 | 
					
						
							| 
									
										
										
										
											2021-08-03 21:38:45 -08:00
										 |  |  |     formData.append("extension", fileObject.name.split(".").pop()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return this.requests.put<any>(routes.recipesRecipeSlugImage(slug), formData); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   updateImagebyURL(slug: string, url: string) { | 
					
						
							|  |  |  |     return this.requests.post(routes.recipesRecipeSlugImage(slug), { url }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-19 18:45:03 -08:00
										 |  |  |   async testCreateOneUrl(url: string) { | 
					
						
							|  |  |  |     return await this.requests.post<Recipe | null>(routes.recipesTestScrapeUrl, { url }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-02 22:15:11 -08:00
										 |  |  |   async createOneByUrl(url: string) { | 
					
						
							|  |  |  |     return await this.requests.post(routes.recipesCreateUrl, { url }); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-28 19:28:33 -08:00
										 |  |  |   async createManyByUrl(payload: BulkCreatePayload) { | 
					
						
							|  |  |  |     return await this.requests.post(routes.recipesCreateUrlBulk, payload); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-08 14:10:32 -08:00
										 |  |  |   // Recipe Comments
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Methods to Generate reference urls for assets/images *
 | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  |   recipeImage(recipeSlug: string, version = null, key = null) { | 
					
						
							|  |  |  |     return `/api/media/recipes/${recipeSlug}/images/original.webp?&rnd=${key}&version=${version}`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   recipeSmallImage(recipeSlug: string, version = null, key = null) { | 
					
						
							|  |  |  |     return `/api/media/recipes/${recipeSlug}/images/min-original.webp?&rnd=${key}&version=${version}`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   recipeTinyImage(recipeSlug: string, version = null, key = null) { | 
					
						
							|  |  |  |     return `/api/media/recipes/${recipeSlug}/images/tiny-original.webp?&rnd=${key}&version=${version}`; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   recipeAssetPath(recipeSlug: string, assetName: string) { | 
					
						
							|  |  |  |     return `/api/media/recipes/${recipeSlug}/assets/${assetName}`; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-08 14:10:32 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   async createComment(slug: string, payload: Object) { | 
					
						
							|  |  |  |     return await this.requests.post(routes.recipesSlugComments(slug), payload); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Update comment in the Database | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async updateComment(slug: string, id: number, payload: Object) { | 
					
						
							|  |  |  |     return await this.requests.put(routes.recipesSlugCommentsId(slug, id), payload); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** Delete comment from the Database | 
					
						
							|  |  |  |    */ | 
					
						
							|  |  |  |   async deleteComment(slug: string, id: number) { | 
					
						
							|  |  |  |     return await this.requests.delete(routes.recipesSlugCommentsId(slug, id)); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-28 14:14:21 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |   async parseIngredients(parser: Parser, ingredients: Array<string>) { | 
					
						
							|  |  |  |     parser = parser || "nlp"; | 
					
						
							|  |  |  |     return await this.requests.post<ParsedIngredient[]>(routes.recipesParseIngredients, { parser, ingredients }); | 
					
						
							| 
									
										
										
										
											2021-08-28 14:14:21 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-16 16:06:13 -08:00
										 |  |  |   async parseIngredient(parser: Parser, ingredient: string) { | 
					
						
							|  |  |  |     parser = parser || "nlp"; | 
					
						
							|  |  |  |     return await this.requests.post<ParsedIngredient>(routes.recipesParseIngredient, { parser, ingredient }); | 
					
						
							| 
									
										
										
										
											2021-10-09 13:08:23 -08:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-01 19:24:47 -08:00
										 |  |  | } |