import {z} from 'zod' export type GetApiCompatV2RecipesGetRecipeArgs = {id: string}; export type GetApiCompatV2RecipesGetRecipeOptions = {baseUrl?: string | undefined, Authorization?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiCompatV2RecipesGetRecipeResponse = z.object({id: z.string(), userId: z.string(), fromUserId: z.string().nullable(), title: z.string(), description: z.string(), yield: z.string(), activeTime: z.string(), totalTime: z.string(), source: z.string(), url: z.string(), folder: z.string(), ingredients: z.string(), instructions: z.string(), notes: z.string(), createdAt: z.string(), updatedAt: z.string(), lastMadeAt: z.string().nullable(), rating: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable(), nutritionServingSize: z.string().nullable(), nutritionCalories: z.number().nullable(), nutritionTotalFat: z.number().nullable(), nutritionSaturatedFat: z.number().nullable(), nutritionTransFat: z.number().nullable(), nutritionPolyunsaturatedFat: z.number().nullable(), nutritionMonounsaturatedFat: z.number().nullable(), nutritionCholesterol: z.number().nullable(), nutritionSodium: z.number().nullable(), nutritionTotalCarbs: z.number().nullable(), nutritionDietaryFiber: z.number().nullable(), nutritionTotalSugars: z.number().nullable(), nutritionAddedSugars: z.number().nullable(), nutritionProtein: z.number().nullable(), nutritionVitaminD: z.number().nullable(), nutritionCalcium: z.number().nullable(), nutritionIron: z.number().nullable(), nutritionPotassium: z.number().nullable(), nutritionOtherDetails: z.string().nullable(), recipeLabels: z.array(z.object({id: z.string(), labelId: z.string(), recipeId: z.string(), createdAt: z.string(), updatedAt: z.string(), label: z.object({id: z.string(), userId: z.string(), title: z.string(), createdAt: z.string(), updatedAt: z.string(), labelGroupId: z.string().nullable(), labelGroup: z.object({id: z.string(), userId: z.string(), title: z.string(), warnWhenNotPresent: z.boolean(), createdAt: z.string(), updatedAt: z.string()}).nullable(), _count: z.object({recipeLabels: z.number().int().gte(-9007199254740991).lte(9007199254740991)})})})), recipeImages: z.array(z.object({id: z.string(), order: z.number().int().gte(-9007199254740991).lte(9007199254740991), imageId: z.string(), image: z.object({id: z.string(), location: z.string()})})), recipeLinks: z.array(z.object({id: z.string(), linkedRecipe: z.object({id: z.string(), userId: z.string(), fromUserId: z.string().nullable(), title: z.string(), description: z.string(), yield: z.string(), activeTime: z.string(), totalTime: z.string(), source: z.string(), url: z.string(), folder: z.string(), createdAt: z.string(), updatedAt: z.string(), lastMadeAt: z.string().nullable(), rating: z.number().int().gte(-9007199254740991).lte(9007199254740991).nullable(), recipeLabels: z.array(z.object({label: z.object({title: z.string()})})), recipeImages: z.array(z.object({order: z.number().int().gte(-9007199254740991).lte(9007199254740991), image: z.object({location: z.string()})})), fromUser: z.object({id: z.string(), name: z.string(), handle: z.string().nullable(), enableProfile: z.boolean(), profileImages: z.array(z.object({order: z.number().int().gte(-9007199254740991).lte(9007199254740991), imageId: z.string(), image: z.object({id: z.string(), location: z.string()})}))}).nullable(), user: z.object({id: z.string(), name: z.string(), handle: z.string().nullable(), enableProfile: z.boolean(), profileImages: z.array(z.object({order: z.number().int().gte(-9007199254740991).lte(9007199254740991), imageId: z.string(), image: z.object({id: z.string(), location: z.string()})}))})})})), fromUser: z.object({id: z.string(), name: z.string(), handle: z.string().nullable(), enableProfile: z.boolean(), profileImages: z.array(z.object({order: z.number().int().gte(-9007199254740991).lte(9007199254740991), imageId: z.string(), image: z.object({id: z.string(), location: z.string()})}))}).nullable(), user: z.object({id: z.string(), name: z.string(), handle: z.string().nullable(), enableProfile: z.boolean(), profileImages: z.array(z.object({order: z.number().int().gte(-9007199254740991).lte(9007199254740991), imageId: z.string(), image: z.object({id: z.string(), location: z.string()})}))})}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Fetch a single recipe by id * * GET /compat/v2/recipes/getRecipe */ export const getApiCompatV2RecipesGetRecipe = async (args: GetApiCompatV2RecipesGetRecipeArgs, options?: GetApiCompatV2RecipesGetRecipeOptions) => { const {baseUrl = "https://api.recipesage.com", fetch: fetchFn = fetch, Authorization} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.id != null) query.append("id", String(args.id)) if (Authorization !== undefined) headers.set('authorization', `Bearer ${Authorization}`) const url = `${baseUrl.replace(/\/+$/, '')}/compat/v2/recipes/getRecipe${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiCompatV2RecipesGetRecipeResponse, await res.json()) };