import {z} from 'zod' export type GetApiCollectionsArgs = {test?: string | undefined}; export type GetApiCollectionsOptions = {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 bigOvenModelAPI2CollectionInfo = z.object({ID: z.number().int().optional(), Description: z.string().optional(), Title: z.string().optional(), MobileUrl: z.string().optional(), PhotoUrl: z.string().optional(), PRO: z.boolean().optional(), IsFiltered: z.boolean().optional(), Token: z.string().optional(), Results: z.array(bigOvenModelAPI2RecipeInfox).optional(), WebUrl: z.string().optional(), IsSponsored: z.boolean().optional()}); export const getApiCollectionsResponse = z.array(bigOvenModelAPI2CollectionInfo); 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 the list of current, seasonal recipe collections. From here, you can use the /collection/{id} endpoint to retrieve the recipes in those collections. * * GET /collections */ export const getApiCollections = async (args: GetApiCollectionsArgs, options?: GetApiCollectionsOptions) => { 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.test != null) query.append("test", String(args.test)) const url = `${baseUrl.replace(/\/+$/, '')}/collections${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(getApiCollectionsResponse, await res.json()) };