import {z} from 'zod' export type GetApiPublicMealsArgs = {page?: string | undefined, limit?: string | undefined, query?: string | undefined}; export type GetApiPublicMealsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPublicMealsResponse = z.object({data: z.object({currentPageItems: z.number().optional(), data: z.array(z.object({dateModified: z.unknown(), id: z.number().optional(), idMeal: z.string().optional(), strArea: z.string().optional(), strCategory: z.string().optional(), strCreativeCommonsConfirmed: z.unknown(), strDrinkAlternate: z.unknown(), strImageSource: z.unknown(), strIngredient1: z.string().optional(), strIngredient10: z.string().optional(), strIngredient11: z.string().optional(), strIngredient12: z.string().optional(), strIngredient13: z.string().optional(), strIngredient14: z.string().optional(), strIngredient15: z.string().optional(), strIngredient16: z.string().optional(), strIngredient17: z.string().optional(), strIngredient18: z.string().optional(), strIngredient19: z.string().optional(), strIngredient2: z.string().optional(), strIngredient20: z.string().optional(), strIngredient3: z.string().optional(), strIngredient4: z.string().optional(), strIngredient5: z.string().optional(), strIngredient6: z.string().optional(), strIngredient7: z.string().optional(), strIngredient8: z.string().optional(), strIngredient9: z.string().optional(), strInstructions: z.string().optional(), strMeal: z.string().optional(), strMealThumb: z.string().optional(), strMeasure1: z.string().optional(), strMeasure10: z.string().optional(), strMeasure11: z.string().optional(), strMeasure12: z.string().optional(), strMeasure13: z.string().optional(), strMeasure14: z.string().optional(), strMeasure15: z.string().optional(), strMeasure16: z.string().optional(), strMeasure17: z.string().optional(), strMeasure18: z.string().optional(), strMeasure19: z.string().optional(), strMeasure2: z.string().optional(), strMeasure20: z.string().optional(), strMeasure3: z.string().optional(), strMeasure4: z.string().optional(), strMeasure5: z.string().optional(), strMeasure6: z.string().optional(), strMeasure7: z.string().optional(), strMeasure8: z.string().optional(), strMeasure9: z.string().optional(), strSource: z.string().optional(), strTags: z.string().nullable().optional(), strYoutube: z.string().optional()})).optional(), limit: z.number().optional(), nextPage: z.boolean().optional(), page: z.number().optional(), previousPage: z.boolean().optional(), totalItems: z.number().optional(), totalPages: z.number().optional()}).optional(), message: z.string().optional(), statusCode: z.number().optional(), success: z.boolean().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 }; /** * Get meals * * GET /public/meals */ export const getApiPublicMeals = async (args: GetApiPublicMealsArgs, options?: GetApiPublicMealsOptions) => { const {baseUrl = "https://api.freeapi.app/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.query != null) query.append("query", String(args.query)) const url = `${baseUrl.replace(/\/+$/, '')}/public/meals${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(getApiPublicMealsResponse, await res.json()) };