import {z} from 'zod' export type GetApiRecipesInformationBulkArgs = {ids: string, includeNutrition?: boolean | undefined}; export type GetApiRecipesInformationBulkOptions = {baseUrl?: string | undefined, apiKeyScheme?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiRecipesInformationBulkResponse = z.array(z.object({id: z.number().int(), title: z.string().min(1), image: z.string().min(1), imageType: z.string().min(1), servings: z.number(), readyInMinutes: z.number().int(), license: z.string().min(1), sourceName: z.string().min(1), sourceUrl: z.string().min(1), spoonacularSourceUrl: z.string().min(1), aggregateLikes: z.number().int(), healthScore: z.number(), spoonacularScore: z.number(), pricePerServing: z.number(), analyzedInstructions: z.array(z.string()), cheap: z.boolean(), creditsText: z.string().min(1), cuisines: z.array(z.string()), dairyFree: z.boolean(), diets: z.array(z.string()), gaps: z.string().min(1), glutenFree: z.boolean(), instructions: z.string(), ketogenic: z.boolean(), lowFodmap: z.boolean(), occasions: z.array(z.string()), sustainable: z.boolean(), vegan: z.boolean(), vegetarian: z.boolean(), veryHealthy: z.boolean(), veryPopular: z.boolean(), whole30: z.boolean(), weightWatcherSmartPoints: z.number(), dishTypes: z.array(z.string()), extendedIngredients: z.array(z.object({aisle: z.string().min(1), amount: z.number(), consitency: z.string().min(1), id: z.number().int(), image: z.string().min(1), measures: z.object({metric: z.object({amount: z.number(), unitLong: z.string(), unitShort: z.string()}), us: z.object({amount: z.number(), unitLong: z.string(), unitShort: z.string()})}).optional(), meta: z.array(z.string()).optional(), name: z.string().min(1), original: z.string().min(1), originalName: z.string().min(1), unit: z.string()})), summary: z.string().min(1), winePairing: z.object({pairedWines: z.array(z.string()), pairingText: z.string().min(1), productMatches: z.array(z.object({id: z.number().int(), title: z.string().min(1), description: z.string().min(1), price: z.string().min(1), imageUrl: z.string().min(1), averageRating: z.number(), ratingCount: z.number().int(), score: z.number(), link: z.string().min(1)}))})})); 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 }; /** * Get Recipe Information Bulk * * GET /recipes/informationBulk */ export const getApiRecipesInformationBulk = async (args: GetApiRecipesInformationBulkArgs, options?: GetApiRecipesInformationBulkOptions) => { const {baseUrl = "https://api.spoonacular.com", fetch: fetchFn = fetch, apiKeyScheme} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ids != null) query.append("ids", String(args.ids)) if (args.includeNutrition != null) query.append("includeNutrition", String(args.includeNutrition)) if (apiKeyScheme !== undefined) headers.set("x-api-key", apiKeyScheme) const url = `${baseUrl.replace(/\/+$/, '')}/recipes/informationBulk${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(getApiRecipesInformationBulkResponse, await res.json()) };