import {z} from 'zod' export type CreateApiCompatV2DiscoverSearchDiscoverRecipesArgs = {body: {searchTerm?: string | undefined, languages?: Array | undefined, categories?: Array | undefined, matchAllCategories?: boolean | undefined, minRating?: number | undefined, minRatingCount?: number | undefined, photo?: ('optional' | 'required' | 'none') | undefined, sortBy?: ('trending' | 'newest' | 'topRated' | 'mostSaved') | undefined, offset?: number | undefined, limit?: number | undefined}}; export type CreateApiCompatV2DiscoverSearchDiscoverRecipesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCompatV2DiscoverSearchDiscoverRecipesResponse = z.object({recipes: z.array(z.object({id: z.string(), title: z.string(), description: z.string(), language: z.string(), categories: z.array(z.string()), approvalState: z.enum(["PENDING", "ACTIVE", "SHADOWBANNED"]), ratingAverage: z.number(), ratingCount: z.number().int().gte(-9007199254740991).lte(9007199254740991), saveCount: z.number().int().gte(-9007199254740991).lte(9007199254740991), createdAt: z.string(), modifiedAt: z.string().nullable(), author: z.object({id: z.string(), handle: z.string().nullable(), name: z.string()}), discoverRecipeImages: z.array(z.object({order: z.number().int().gte(-9007199254740991).lte(9007199254740991), 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 }; /** * Browse and search the public discover recipe catalog * * POST /compat/v2/discover/searchDiscoverRecipes */ export const createApiCompatV2DiscoverSearchDiscoverRecipes = async (args: CreateApiCompatV2DiscoverSearchDiscoverRecipesArgs, options?: CreateApiCompatV2DiscoverSearchDiscoverRecipesOptions) => { const {baseUrl = "https://api.recipesage.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/compat/v2/discover/searchDiscoverRecipes${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiCompatV2DiscoverSearchDiscoverRecipesResponse, await res.json()) };