import {z} from 'zod' export type GetApiCollectionIdArgs = {id: number, rpp?: number | undefined, pg?: number | undefined, test?: boolean | undefined, sessionForLogging?: string | undefined}; export type GetApiCollectionIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const bigOvenModelAPI2UserInfoTinyx = z.object({UserName: z.string().optional(), UserID: z.number().int().optional(), FirstName: z.string().optional(), LastName: z.string().optional(), PhotoUrl: z.string().optional()}); export const bigOvenModelAPI2RecipeInfox = z.object({RecipeID: z.number().int().optional(), Title: z.string().optional(), Cuisine: z.string().optional(), Category: z.string().optional(), Subcategory: z.string().optional(), Microcategory: z.string().optional(), StarRating: z.number().optional(), WebURL: z.string().optional(), ReviewCount: z.number().int().optional(), Poster: bigOvenModelAPI2UserInfoTinyx.optional(), IsPrivate: z.boolean().optional(), Servings: z.number().optional(), CreationDate: z.string().optional(), IsRecipeScan: z.boolean().optional(), IsBookmark: z.boolean().optional(), TotalTries: z.number().int().optional(), PhotoUrl: z.string().optional(), HasVideos: z.boolean().optional()}); export const bigOvenModelAPI2RecipeSearchResult = z.object({ResultCount: z.number().int().optional(), Results: z.array(bigOvenModelAPI2RecipeInfox).optional(), SpellSuggest: z.string().optional()}); 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 }; /** * Gets a recipe collection. A recipe collection is a curated set of recipes. * * GET /collection/{id} */ export const getApiCollectionId = async (args: GetApiCollectionIdArgs, options?: GetApiCollectionIdOptions) => { const {baseUrl = "https://api2.bigoven.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.rpp != null) query.append("rpp", String(args.rpp)) if (args.pg != null) query.append("pg", String(args.pg)) if (args.test != null) query.append("test", String(args.test)) if (args.sessionForLogging != null) query.append("sessionForLogging", String(args.sessionForLogging)) const url = `${baseUrl.replace(/\/+$/, '')}/collection/${encodeURIComponent(String(args.id))}${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(bigOvenModelAPI2RecipeSearchResult, await res.json()) };