2022-10-22 11:51:07 -08:00
|
|
|
import { BaseCRUDAPI } from "../../base/base-clients";
|
2023-02-11 21:26:10 -09:00
|
|
|
import { route } from "../../base";
|
2021-11-22 20:10:48 -09:00
|
|
|
import { CommentsApi } from "./recipe-comments";
|
2021-12-05 11:55:46 -09:00
|
|
|
import { RecipeShareApi } from "./recipe-share";
|
2025-06-20 00:09:12 +07:00
|
|
|
import type {
|
2022-08-28 20:08:33 -08:00
|
|
|
Recipe,
|
|
|
|
CreateRecipe,
|
|
|
|
RecipeAsset,
|
|
|
|
CreateRecipeByUrlBulk,
|
|
|
|
ParsedIngredient,
|
|
|
|
UpdateImageResponse,
|
|
|
|
RecipeZipTokenResponse,
|
2023-02-21 21:59:22 -06:00
|
|
|
RecipeLastMade,
|
2024-12-03 07:27:41 -06:00
|
|
|
RecipeSuggestionQuery,
|
|
|
|
RecipeSuggestionResponse,
|
2022-11-13 17:12:53 -06:00
|
|
|
RecipeTimelineEventIn,
|
2022-12-11 15:16:55 -06:00
|
|
|
RecipeTimelineEventOut,
|
|
|
|
RecipeTimelineEventUpdate,
|
2022-10-22 11:51:07 -08:00
|
|
|
} from "~/lib/api/types/recipe";
|
2025-06-20 00:09:12 +07:00
|
|
|
import type { ApiRequestInstance, PaginationData } from "~/lib/api/types/non-generated";
|
2021-08-01 19:24:47 -08:00
|
|
|
|
2024-05-22 04:45:07 -05:00
|
|
|
export type Parser = "nlp" | "brute" | "openai";
|
2022-05-21 21:22:02 +02:00
|
|
|
|
|
|
|
export interface CreateAsset {
|
|
|
|
name: string;
|
|
|
|
icon: string;
|
|
|
|
extension: string;
|
|
|
|
file: File;
|
|
|
|
}
|
|
|
|
|
2021-08-01 19:24:47 -08:00
|
|
|
const prefix = "/api";
|
|
|
|
|
|
|
|
const routes = {
|
|
|
|
recipesCreate: `${prefix}/recipes/create`,
|
|
|
|
recipesBase: `${prefix}/recipes`,
|
2024-12-03 07:27:41 -06:00
|
|
|
recipesSuggestions: `${prefix}/recipes/suggestions`,
|
2021-08-01 19:24:47 -08:00
|
|
|
recipesTestScrapeUrl: `${prefix}/recipes/test-scrape-url`,
|
2024-09-30 10:52:13 -05:00
|
|
|
recipesCreateUrl: `${prefix}/recipes/create/url`,
|
|
|
|
recipesCreateUrlBulk: `${prefix}/recipes/create/url/bulk`,
|
|
|
|
recipesCreateFromZip: `${prefix}/recipes/create/zip`,
|
|
|
|
recipesCreateFromImage: `${prefix}/recipes/create/image`,
|
|
|
|
recipesCreateFromHtmlOrJson: `${prefix}/recipes/create/html-or-json`,
|
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`,
|
2023-04-25 12:46:00 -05:00
|
|
|
recipesTimelineEvent: `${prefix}/recipes/timeline/events`,
|
2021-08-02 22:15:11 -08:00
|
|
|
|
2021-08-01 19:24:47 -08:00
|
|
|
recipesRecipeSlug: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}`,
|
2021-11-04 18:15:23 -08:00
|
|
|
recipesRecipeSlugExport: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}/exports`,
|
|
|
|
recipesRecipeSlugExportZip: (recipe_slug: string) => `${prefix}/recipes/${recipe_slug}/exports/zip`,
|
2021-08-01 19:24:47 -08:00
|
|
|
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}`,
|
2022-11-13 17:12:53 -06:00
|
|
|
|
2023-02-21 21:59:22 -06:00
|
|
|
recipesSlugLastMade: (slug: string) => `${prefix}/recipes/${slug}/last-made`,
|
2023-04-25 12:46:00 -05:00
|
|
|
recipesTimelineEventId: (id: string) => `${prefix}/recipes/timeline/events/${id}`,
|
2023-08-06 12:49:30 -05:00
|
|
|
recipesTimelineEventIdImage: (id: string) => `${prefix}/recipes/timeline/events/${id}/image`,
|
2021-08-01 19:24:47 -08:00
|
|
|
};
|
|
|
|
|
2023-02-26 20:20:26 +01:00
|
|
|
export type RecipeSearchQuery = {
|
2024-09-22 09:59:20 -05:00
|
|
|
search?: string;
|
2023-02-26 20:20:26 +01:00
|
|
|
orderDirection?: "asc" | "desc";
|
2023-02-11 21:26:10 -09:00
|
|
|
groupId?: string;
|
|
|
|
|
|
|
|
queryFilter?: string;
|
|
|
|
|
|
|
|
cookbook?: string;
|
2024-09-22 09:59:20 -05:00
|
|
|
households?: string[];
|
2023-02-11 21:26:10 -09:00
|
|
|
|
|
|
|
categories?: string[];
|
|
|
|
requireAllCategories?: boolean;
|
|
|
|
|
|
|
|
tags?: string[];
|
|
|
|
requireAllTags?: boolean;
|
|
|
|
|
|
|
|
tools?: string[];
|
|
|
|
requireAllTools?: boolean;
|
|
|
|
|
|
|
|
foods?: string[];
|
|
|
|
requireAllFoods?: boolean;
|
|
|
|
|
2023-02-26 20:20:26 +01:00
|
|
|
page?: number;
|
|
|
|
perPage?: number;
|
2023-02-11 21:26:10 -09:00
|
|
|
orderBy?: string;
|
2025-01-30 09:33:58 +00:00
|
|
|
orderByNullPosition?: "first" | "last";
|
2023-05-30 02:56:20 +02:00
|
|
|
|
|
|
|
_searchSeed?: string;
|
2023-02-26 20:20:26 +01:00
|
|
|
};
|
2023-02-11 21:26:10 -09:00
|
|
|
|
2022-05-21 21:22:02 +02:00
|
|
|
export class RecipeAPI extends BaseCRUDAPI<CreateRecipe, Recipe, Recipe> {
|
2021-08-06 16:28:12 -08:00
|
|
|
baseRoute: string = routes.recipesBase;
|
2021-08-03 21:38:45 -08:00
|
|
|
itemRoute = routes.recipesRecipeSlug;
|
|
|
|
|
2021-12-05 11:55:46 -09:00
|
|
|
comments: CommentsApi;
|
|
|
|
share: RecipeShareApi;
|
2021-11-22 20:10:48 -09:00
|
|
|
|
|
|
|
constructor(requests: ApiRequestInstance) {
|
|
|
|
super(requests);
|
|
|
|
|
|
|
|
this.comments = new CommentsApi(requests);
|
2021-12-05 11:55:46 -09:00
|
|
|
this.share = new RecipeShareApi(requests);
|
2021-11-22 20:10:48 -09:00
|
|
|
}
|
|
|
|
|
2023-02-26 20:20:26 +01:00
|
|
|
async search(rsq: RecipeSearchQuery) {
|
2023-02-11 21:26:10 -09:00
|
|
|
return await this.requests.get<PaginationData<Recipe>>(route(routes.recipesBase, rsq));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2025-06-20 00:09:12 +07:00
|
|
|
async getSuggestions(q: RecipeSuggestionQuery, foods: string[] | null = null, tools: string[] | null = null) {
|
2024-12-03 07:27:41 -06:00
|
|
|
return await this.requests.get<RecipeSuggestionResponse>(
|
2025-06-20 00:09:12 +07:00
|
|
|
route(routes.recipesSuggestions, { ...q, foods, tools }),
|
2024-12-03 07:27:41 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-22 20:10:48 -09:00
|
|
|
async createAsset(recipeSlug: string, payload: CreateAsset) {
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("file", payload.file);
|
|
|
|
formData.append("name", payload.name);
|
|
|
|
formData.append("extension", payload.extension);
|
|
|
|
formData.append("icon", payload.icon);
|
|
|
|
|
2022-05-21 21:22:02 +02:00
|
|
|
return await this.requests.post<RecipeAsset>(routes.recipesRecipeSlugAssets(recipeSlug), formData);
|
2021-11-22 20:10:48 -09:00
|
|
|
}
|
|
|
|
|
2021-08-03 21:38:45 -08:00
|
|
|
updateImage(slug: string, fileObject: File) {
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("image", fileObject);
|
2022-01-16 03:38:11 +01:00
|
|
|
formData.append("extension", fileObject.name.split(".").pop() ?? "");
|
2021-08-03 21:38:45 -08:00
|
|
|
|
2022-05-21 21:22:02 +02:00
|
|
|
return this.requests.put<UpdateImageResponse, FormData>(routes.recipesRecipeSlugImage(slug), formData);
|
2021-08-03 21:38:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
updateImagebyURL(slug: string, url: string) {
|
2022-05-21 21:22:02 +02:00
|
|
|
return this.requests.post<UpdateImageResponse>(routes.recipesRecipeSlugImage(slug), { url });
|
2021-08-03 21:38:45 -08:00
|
|
|
}
|
|
|
|
|
2024-06-07 06:45:50 -05:00
|
|
|
async testCreateOneUrl(url: string, useOpenAI = false) {
|
|
|
|
return await this.requests.post<Recipe | null>(routes.recipesTestScrapeUrl, { url, useOpenAI });
|
2021-10-19 18:45:03 -08:00
|
|
|
}
|
|
|
|
|
2024-09-30 10:52:13 -05:00
|
|
|
async createOneByHtmlOrJson(data: string, includeTags: boolean) {
|
|
|
|
return await this.requests.post<string>(routes.recipesCreateFromHtmlOrJson, { data, includeTags });
|
|
|
|
}
|
|
|
|
|
2022-04-23 12:23:12 -08:00
|
|
|
async createOneByUrl(url: string, includeTags: boolean) {
|
|
|
|
return await this.requests.post<string>(routes.recipesCreateUrl, { url, includeTags });
|
2021-08-02 22:15:11 -08:00
|
|
|
}
|
2021-08-01 19:24:47 -08:00
|
|
|
|
2022-05-21 21:22:02 +02:00
|
|
|
async createManyByUrl(payload: CreateRecipeByUrlBulk) {
|
|
|
|
return await this.requests.post<string>(routes.recipesCreateUrlBulk, payload);
|
2021-10-28 19:28:33 -08:00
|
|
|
}
|
|
|
|
|
2025-06-28 22:11:12 +02:00
|
|
|
async createOneFromImages(fileObjects: (Blob | File)[], translateLanguage: string | null = null) {
|
2024-08-17 17:07:01 -05:00
|
|
|
const formData = new FormData();
|
2025-06-28 22:11:12 +02:00
|
|
|
|
|
|
|
fileObjects.forEach((file) => {
|
|
|
|
formData.append("images", file);
|
|
|
|
});
|
2024-08-17 17:07:01 -05:00
|
|
|
|
2025-06-20 00:09:12 +07:00
|
|
|
let apiRoute = routes.recipesCreateFromImage;
|
2024-08-17 17:07:01 -05:00
|
|
|
if (translateLanguage) {
|
2025-06-20 00:09:12 +07:00
|
|
|
apiRoute = `${apiRoute}?translateLanguage=${translateLanguage}`;
|
2024-08-17 17:07:01 -05:00
|
|
|
}
|
|
|
|
|
2025-07-15 14:47:35 +00:00
|
|
|
return await this.requests.post<string>(apiRoute, formData, { timeout: 1200000 });
|
2024-08-17 17:07:01 -05: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-11-04 18:15:23 -08:00
|
|
|
|
|
|
|
async getZipToken(recipeSlug: string) {
|
2022-05-21 21:22:02 +02:00
|
|
|
return await this.requests.post<RecipeZipTokenResponse>(routes.recipesRecipeSlugExport(recipeSlug), {});
|
2021-11-04 18:15:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
getZipRedirectUrl(recipeSlug: string, token: string) {
|
|
|
|
return `${routes.recipesRecipeSlugExportZip(recipeSlug)}?token=${token}`;
|
|
|
|
}
|
2022-09-25 15:00:45 -08:00
|
|
|
|
2024-10-19 04:33:32 -05:00
|
|
|
async updateMany(payload: Recipe[]) {
|
|
|
|
return await this.requests.put<Recipe[]>(routes.recipesBase, payload);
|
|
|
|
}
|
|
|
|
|
2024-10-19 15:36:34 -05:00
|
|
|
async patchMany(payload: Recipe[]) {
|
|
|
|
return await this.requests.patch<Recipe[]>(routes.recipesBase, payload);
|
|
|
|
}
|
|
|
|
|
2023-02-21 21:59:22 -06:00
|
|
|
async updateLastMade(recipeSlug: string, timestamp: string) {
|
2025-06-20 00:09:12 +07:00
|
|
|
return await this.requests.patch<Recipe, RecipeLastMade>(routes.recipesSlugLastMade(recipeSlug), { timestamp });
|
2023-02-21 21:59:22 -06:00
|
|
|
}
|
|
|
|
|
2023-04-25 12:46:00 -05:00
|
|
|
async createTimelineEvent(payload: RecipeTimelineEventIn) {
|
|
|
|
return await this.requests.post<RecipeTimelineEventOut>(routes.recipesTimelineEvent, payload);
|
2022-12-11 15:16:55 -06:00
|
|
|
}
|
|
|
|
|
2023-04-25 12:46:00 -05:00
|
|
|
async updateTimelineEvent(eventId: string, payload: RecipeTimelineEventUpdate) {
|
2023-02-26 20:20:26 +01:00
|
|
|
return await this.requests.put<RecipeTimelineEventOut, RecipeTimelineEventUpdate>(
|
2023-04-25 12:46:00 -05:00
|
|
|
routes.recipesTimelineEventId(eventId),
|
2025-06-20 00:09:12 +07:00
|
|
|
payload,
|
2023-02-26 20:20:26 +01:00
|
|
|
);
|
2022-12-11 15:16:55 -06:00
|
|
|
}
|
|
|
|
|
2023-04-25 12:46:00 -05:00
|
|
|
async deleteTimelineEvent(eventId: string) {
|
|
|
|
return await this.requests.delete<RecipeTimelineEventOut>(routes.recipesTimelineEventId(eventId));
|
2022-12-11 15:16:55 -06:00
|
|
|
}
|
|
|
|
|
2023-04-25 12:46:00 -05:00
|
|
|
async getAllTimelineEvents(page = 1, perPage = -1, params = {} as any) {
|
2023-02-26 20:20:26 +01:00
|
|
|
return await this.requests.get<PaginationData<RecipeTimelineEventOut>>(
|
2025-07-10 11:57:20 -05:00
|
|
|
routes.recipesTimelineEvent, { page, perPage, ...params },
|
2023-02-26 20:20:26 +01:00
|
|
|
);
|
2022-11-13 17:12:53 -06:00
|
|
|
}
|
2023-08-06 12:49:30 -05:00
|
|
|
|
2023-08-21 10:00:37 -05:00
|
|
|
async updateTimelineEventImage(eventId: string, fileObject: Blob | File, fileName: string) {
|
2023-08-06 12:49:30 -05:00
|
|
|
const formData = new FormData();
|
|
|
|
formData.append("image", fileObject);
|
2023-08-21 10:00:37 -05:00
|
|
|
formData.append("extension", fileName.split(".").pop() ?? "");
|
2023-08-06 12:49:30 -05:00
|
|
|
|
|
|
|
return await this.requests.put<UpdateImageResponse, FormData>(routes.recipesTimelineEventIdImage(eventId), formData);
|
|
|
|
}
|
2021-08-01 19:24:47 -08:00
|
|
|
}
|