import {z} from 'zod' export type GetApiV3EventsEventIdsCompetitorsArgs = {event_ids: Array}; export type GetApiV3EventsEventIdsCompetitorsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV3EventsEventIdsCompetitorsResponse = z.object({competitors: z.array(z.object({country_code: z.string().nullable(), event_id: z.string(), id: z.string(), info: z.object({color_primary: z.string().min(6).max(6).optional(), color_secondary: z.string().min(6).max(6).optional(), keywords: z.string().optional()}).optional(), name: z.string(), short_code: z.string().nullable(), short_name: z.string().nullable(), slug: z.string(), type: z.enum(["a", "b", "home", "away", "Runner", "NonRunner"]).nullable()}))}); 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 }; /** * Retrieve competitors of a set of events. * * GET /v3/events/{event_ids}/competitors/ */ export const getApiV3EventsEventIdsCompetitors = async (args: GetApiV3EventsEventIdsCompetitorsArgs, options?: GetApiV3EventsEventIdsCompetitorsOptions) => { const {baseUrl = "https://api.smarkets.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v3/events/${encodeURIComponent(String(args.event_ids))}/competitors/${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(getApiV3EventsEventIdsCompetitorsResponse, await res.json()) };