import {z} from 'zod' export type GetApiPublicCatsArgs = {query?: string | undefined, page?: string | undefined, limit?: string | undefined}; export type GetApiPublicCatsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiPublicCatsResponse = z.object({data: z.object({currentPageItems: z.number().optional(), data: z.array(z.object({adaptability: z.number().optional(), affection_level: z.number().optional(), alt_names: z.string().optional(), cfa_url: z.string().optional(), child_friendly: z.number().optional(), country_code: z.string().optional(), country_codes: z.string().optional(), description: z.string().optional(), dog_friendly: z.number().optional(), energy_level: z.number().optional(), experimental: z.number().optional(), grooming: z.number().optional(), hairless: z.number().optional(), health_issues: z.number().optional(), hypoallergenic: z.number().optional(), id: z.number().optional(), image: z.string().optional(), indoor: z.number().optional(), intelligence: z.number().optional(), lap: z.number().optional(), life_span: z.string().optional(), name: z.string().optional(), natural: z.number().optional(), origin: z.string().optional(), rare: z.number().optional(), rex: z.number().optional(), shedding_level: z.number().optional(), short_legs: z.number().optional(), social_needs: z.number().optional(), stranger_friendly: z.number().optional(), suppressed_tail: z.number().optional(), temperament: z.string().optional(), vcahospitals_url: z.string().optional(), vetstreet_url: z.string().optional(), vocalisation: z.number().optional(), weight: z.object({imperial: z.string().optional(), metric: z.string().optional()}).optional(), wikipedia_url: 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 cats * * GET /public/cats */ export const getApiPublicCats = async (args: GetApiPublicCatsArgs, options?: GetApiPublicCatsOptions) => { 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.query != null) query.append("query", String(args.query)) if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/public/cats${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(getApiPublicCatsResponse, await res.json()) };