import {z} from 'zod' export type GetApiV1SiresSireIdResultsArgs = {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, limit?: (number | null) | undefined, skip?: (number | null) | undefined}; export type GetApiV1SiresSireIdResultsOptions = {baseUrl: string, HTTPBasic?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const runnerStandard = z.object({horse_id: z.string(), horse: z.string(), sp: z.string(), sp_dec: z.string(), bsp: z.string().nullable().optional(), number: z.string(), position: z.string(), draw: z.string(), btn: z.string(), ovr_btn: z.string(), age: z.string(), sex: z.string(), weight: z.string(), weight_lbs: z.string(), headgear: z.string(), time: z.string(), or: z.string(), rpr: z.string(), tsr: z.string(), prize: z.string(), prize_currency: z.string().optional(), jockey: z.string(), jockey_claim_lbs: z.string().nullable().optional(), jockey_id: z.string(), trainer: z.string(), trainer_id: z.string(), owner: z.string(), owner_id: z.string(), sire: z.string(), sire_id: z.string(), dam: z.string(), dam_id: z.string(), damsire: z.string(), damsire_id: z.string(), comment: z.string(), silk_url: z.string().nullable().optional(), performance_rating: z.string().nullable().optional(), speed_rating: z.string().nullable().optional()}); export const resultStandard = z.object({race_id: z.string(), date: z.string(), region: z.string(), course: z.string(), course_id: z.string(), off: z.string(), off_dt: z.string().nullable().optional(), race_name: z.string(), type: z.string(), class: z.string(), pattern: z.string(), rating_band: z.string(), age_band: z.string(), sex_rest: z.string(), dist: z.string(), dist_y: z.string(), dist_m: z.string(), dist_f: z.string(), going: z.string(), surface: z.string().nullable().optional(), jumps: z.string().nullable().optional(), runners: z.array(runnerStandard), winning_time_detail: z.string().nullable().optional(), comments: z.string().nullable().optional(), non_runners: z.string().nullable().optional(), tote_win: z.string().nullable().optional(), tote_pl: z.string().nullable().optional(), tote_ex: z.string().nullable().optional(), tote_csf: z.string().nullable().optional(), tote_tricast: z.string().nullable().optional(), tote_trifecta: z.string().nullable().optional()}); export const resultsStandardPage = z.object({results: z.array(resultStandard).nullable(), total: z.number().int(), limit: z.number().int(), skip: z.number().int(), 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 Results * * GET /v1/sires/{sire_id}/results */ export const getApiV1SiresSireIdResults = async (args: GetApiV1SiresSireIdResultsArgs, options: GetApiV1SiresSireIdResultsOptions) => { 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 (args.limit != null) query.append("limit", String(args.limit)) if (args.skip != null) query.append("skip", String(args.skip)) if (HTTPBasic !== undefined) headers.set('authorization', `Basic ${btoa(`${HTTPBasic.username}:${HTTPBasic.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sires/${encodeURIComponent(String(args.sire_id))}/results${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(resultsStandardPage, await res.json()) };