import {z} from 'zod' export type GetApiV3EventsEventIdsMarketsArgs = {sort?: string | undefined, limit_by_event?: number | undefined, popular?: boolean | undefined, market_types?: string | undefined, include_hidden?: boolean | undefined, jurisdiction?: ('CDG' | 'CFTC' | 'DGA' | 'IGC' | 'IRGC' | 'MGA' | 'SGA' | 'UKGC') | undefined, platform?: ('exchange' | 'sbk') | undefined, event_ids: Array}; export type GetApiV3EventsEventIdsMarketsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV3EventsEventIdsMarketsResponse = z.object({markets: z.array(z.object({bet_delay: z.number().int(), cashout_enabled: z.boolean(), categories: z.array(z.enum(["goals", "half_time", "handicap", "totals", "corners", "cards", "players", "winner", "other", "teams", "place", "relegation", "nationality", "make_the_cut", "first_round_leader", "one_eighty", "set", "game", "half", "quarter", "frame", "hole_in_one", "hole_group_betting", "hole_match_bet", "three_ball", "bet_builder", "popular", "overs", "next_action", "flash"])).optional(), category: z.enum(["goals", "half_time", "handicap", "totals", "corners", "cards", "players", "winner", "other", "teams", "place", "relegation", "nationality", "make_the_cut", "first_round_leader", "one_eighty", "set", "game", "half", "quarter", "frame", "hole_in_one", "hole_group_betting", "hole_match_bet", "three_ball", "bet_builder", "popular", "overs", "next_action", "flash"]).optional(), cftc: z.object({}).optional(), complete: z.boolean(), contract_selections: z.array(z.number().int()).nullable().optional(), created: z.string(), description: z.string().nullable().optional(), display_order: z.number().int().nullable().optional(), display_type: z.enum(["bars", "bars2", "cross", "default", "donut", "population", "poster", "graph", "top", "prices", "line", "gauge", "leaderboard"]), event_id: z.string(), hidden: z.boolean(), id: z.string(), info: z.object({}).nullable().optional(), inplay_enabled: z.boolean().optional(), market_type: z.object({name: z.string(), param: z.string().optional(), params: z.object({}).optional()}).nullable(), modified: z.string(), name: z.string(), slug: z.string(), state: z.enum(["new", "open", "live", "halted", "settled", "voided", "unavailable"]), winner_count: z.number().int()}))}); 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 markets by event ids. This should be done after getting events. * * GET /v3/events/{event_ids}/markets/ */ export const getApiV3EventsEventIdsMarkets = async (args: GetApiV3EventsEventIdsMarketsArgs, options?: GetApiV3EventsEventIdsMarketsOptions) => { const {baseUrl = "https://api.smarkets.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sort != null) query.append("sort", String(args.sort)) if (args.limit_by_event != null) query.append("limit_by_event", String(args.limit_by_event)) if (args.popular != null) query.append("popular", String(args.popular)) if (args.market_types != null) query.append("market_types", String(args.market_types)) if (args.include_hidden != null) query.append("include_hidden", String(args.include_hidden)) if (args.jurisdiction != null) query.append("jurisdiction", String(args.jurisdiction)) if (args.platform != null) query.append("platform", String(args.platform)) const url = `${baseUrl.replace(/\/+$/, '')}/v3/events/${encodeURIComponent(String(args.event_ids))}/markets/${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(getApiV3EventsEventIdsMarketsResponse, await res.json()) };