import {z} from 'zod' export type GetApiV1SiresSireIdAnalysisDistancesArgs = {sire_id: string, start_date?: (string | null) | undefined, end_date?: (string | null) | undefined, region?: (Array | null) | undefined, course?: (Array | null) | undefined, type?: (Array | null) | undefined, going?: (Array | null) | undefined, race_class?: (Array | null) | undefined, min_distance_y?: (number | null) | undefined, max_distance_y?: (number | null) | undefined, age_band?: (Array | null) | undefined, sex_restriction?: (Array | null) | undefined}; export type GetApiV1SiresSireIdAnalysisDistancesOptions = {baseUrl: string, HTTPBasic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const appModelsSiresDistance = z.object({dist: z.string(), dist_y: z.string(), dist_m: z.string(), dist_f: z.string(), runners: z.number().int(), "1st": z.number().int(), "2nd": z.number().int(), "3rd": z.number().int(), "4th": z.number().int(), "a/e": z.number(), "win_%": z.number(), "1_pl": z.number()}); export const sireDistanceAnalysis = z.object({id: z.string(), sire: z.string(), total_runners: z.number().int(), distances: z.array(appModelsSiresDistance), query: z.array(z.array(z.unknown()))}); 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 }; /** * Sire Progeny Distance Analysis * * GET /v1/sires/{sire_id}/analysis/distances */ export const getApiV1SiresSireIdAnalysisDistances = async (args: GetApiV1SiresSireIdAnalysisDistancesArgs, options: GetApiV1SiresSireIdAnalysisDistancesOptions) => { const {baseUrl, fetch: fetchFn = fetch, HTTPBasic} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) for (const item of args.region ?? []) query.append("region", String(item)) for (const item of args.course ?? []) query.append("course", String(item)) for (const item of args.type ?? []) query.append("type", String(item)) for (const item of args.going ?? []) query.append("going", String(item)) for (const item of args.race_class ?? []) query.append("race_class", String(item)) if (args.min_distance_y != null) query.append("min_distance_y", String(args.min_distance_y)) if (args.max_distance_y != null) query.append("max_distance_y", String(args.max_distance_y)) for (const item of args.age_band ?? []) query.append("age_band", String(item)) for (const item of args.sex_restriction ?? []) query.append("sex_restriction", String(item)) if (HTTPBasic !== undefined) headers.set('authorization', `Basic ${btoa(`${HTTPBasic.username}:${HTTPBasic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sires/${encodeURIComponent(String(args.sire_id))}/analysis/distances${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(sireDistanceAnalysis, await res.json()) };