import {z} from 'zod' export type GetApiV3EventsArgs = {id?: Array | undefined, inplay_enabled?: boolean | undefined, state?: (Array<'new' | 'upcoming' | 'live' | 'ended' | 'settled' | 'cancelled' | 'suspended'>) | undefined, type?: (Array<'american_football_match' | 'american_football_outright' | 'baseball_match' | 'baseball_outright' | 'basketball_esports_match' | 'basketball_match' | 'boxing_match' | 'call_of_duty_match' | 'cricket_match' | 'cricket_outright' | 'csgo_match' | 'current_affairs' | 'cycling' | 'darts_match' | 'darts_outright' | 'dota_2_match' | 'football_esports_match' | 'football_match' | 'football_outright' | 'golf_match' | 'golf_outright' | 'greyhound_racing_race' | 'handball_match' | 'horse_racing_race' | 'ice_hockey_match' | 'league_of_legends_match' | 'mma_match' | 'motorsports_race' | 'motorsports_outright' | 'politics' | 'politics_outright' | 'rowing' | 'rugby_league_match' | 'rugby_league_outright' | 'rugby_union_match' | 'rugby_union_outright' | 'sailing_race' | 'sailing_outright' | 'snooker_match' | 'snooker_outright' | 'table_tennis_match' | 'table_tennis_outright' | 'tennis_match' | 'tennis_outright' | 'volleyball_match' | 'generic' | 'top_level_event' | 'tv_entertainment'>) | undefined, type_domain?: (Array<'accumulator' | 'american_football' | 'australian_rules' | 'baseball' | 'basketball' | 'basketball_esports' | 'boxing' | 'call_of_duty' | 'chess' | 'cricket' | 'csgo' | 'current_affairs' | 'cycling' | 'darts' | 'dota_2' | 'esports' | 'football' | 'football_esports' | 'golf' | 'greyhound_racing' | 'handball' | 'horse_racing' | 'ice_hockey' | 'king_of_glory' | 'league_of_legends' | 'lol_wild_rift' | 'mma' | 'motorsports' | 'olympics' | 'overwatch' | 'poker' | 'politics' | 'pubg' | 'rainbow_six_siege' | 'rocket_league' | 'rowing' | 'rugby_league' | 'rugby_union' | 'sailing' | 'snooker' | 'starcraft' | 'starcraft_2' | 'tv_and_entertainment' | 'table_tennis' | 'tennis' | 'valorant' | 'volleyball' | 'winter_sports' | 'gaelic_football' | 'gaelic_hurling' | 'manual_mapping'>) | undefined, type_scope?: (Array<'root' | 'round' | 'category_root' | 'category' | 'single_event' | 'acca_root' | 'acca_category' | 'acca' | 'outright_root' | 'outright_category' | 'outright' | 'tour' | 'antepost_root' | 'antepost_category' | 'antepost'>) | undefined, with_new_type?: boolean | undefined, parent_id?: Array | undefined, start_datetime_min?: string | undefined, start_datetime_max?: string | undefined, last_modified_min?: string | undefined, last_modified_max?: string | undefined, pagination_last_display_order?: number | undefined, pagination_last_start_datetime?: string | undefined, pagination_last_id?: string | undefined, pagination_last_name?: string | undefined, sort?: ('display_order,start_datetime,name' | 'display_order,start_datetime,id' | 'start_datetime,name' | 'start_datetime,id' | 'id') | undefined, limit?: number | undefined, include_hidden?: boolean | undefined, jurisdiction?: ('CDG' | 'CFTC' | 'DGA' | 'IGC' | 'IRGC' | 'MGA' | 'SGA' | 'UKGC') | undefined, platform?: ('exchange' | 'sbk') | undefined}; export type GetApiV3EventsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV3EventsResponse = z.object({events: z.array(z.object({bet_allowed: z.boolean().optional(), bettable: z.boolean().optional(), cftc: z.object({}).optional(), chart_time_period: z.string().nullable().optional(), created: z.string(), description: z.string().nullable(), display_order: z.number().int().nullable().optional(), end_date: z.string().nullable(), full_slug: z.string(), hidden: z.boolean(), id: z.string(), inplay_enabled: z.boolean().optional(), modified: z.string(), name: z.string(), parent_id: z.string().nullable(), seo_description: z.string().nullable().optional(), short_name: z.string().nullable(), slug: z.string(), special_rules: z.string().nullable().optional(), start_date: z.string().nullable(), start_datetime: z.string().nullable(), state: z.enum(["new", "upcoming", "live", "ended", "settled", "cancelled", "suspended"]), super_sub_enabled: z.boolean().optional(), type: z.union([z.enum(["american_football_match", "american_football_outright", "baseball_match", "baseball_outright", "basketball_esports_match", "basketball_match", "boxing_match", "call_of_duty_match", "cricket_match", "cricket_outright", "csgo_match", "current_affairs", "cycling", "darts_match", "darts_outright", "dota_2_match", "football_esports_match", "football_match", "football_outright", "golf_match", "golf_outright", "greyhound_racing_race", "handball_match", "horse_racing_race", "ice_hockey_match", "league_of_legends_match", "mma_match", "motorsports_race", "motorsports_outright", "politics", "politics_outright", "rowing", "rugby_league_match", "rugby_league_outright", "rugby_union_match", "rugby_union_outright", "sailing_race", "sailing_outright", "snooker_match", "snooker_outright", "table_tennis_match", "table_tennis_outright", "tennis_match", "tennis_outright", "volleyball_match", "generic", "top_level_event", "tv_entertainment"]), z.object({domain: z.enum(["accumulator", "american_football", "australian_rules", "baseball", "basketball", "basketball_esports", "boxing", "call_of_duty", "chess", "cricket", "csgo", "current_affairs", "cycling", "darts", "dota_2", "esports", "football", "football_esports", "golf", "greyhound_racing", "handball", "horse_racing", "ice_hockey", "king_of_glory", "league_of_legends", "lol_wild_rift", "mma", "motorsports", "olympics", "overwatch", "poker", "politics", "pubg", "rainbow_six_siege", "rocket_league", "rowing", "rugby_league", "rugby_union", "sailing", "snooker", "starcraft", "starcraft_2", "tv_and_entertainment", "table_tennis", "tennis", "valorant", "volleyball", "winter_sports", "gaelic_football", "gaelic_hurling", "manual_mapping"]), scope: z.enum(["root", "round", "category_root", "category", "single_event", "acca_root", "acca_category", "acca", "outright_root", "outright_category", "outright", "tour", "antepost_root", "antepost_category", "antepost"])})]), venue: z.object({}).nullable().optional()})), pagination: z.object({next_page: z.string().nullable()}).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 }; /** * Retrieve events that are available on Smarkets to bet on. * * GET /v3/events/ */ export const getApiV3Events = async (args: GetApiV3EventsArgs, options?: GetApiV3EventsOptions) => { const {baseUrl = "https://api.smarkets.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.id ?? []) query.append("id", String(item)) if (args.inplay_enabled != null) query.append("inplay_enabled", String(args.inplay_enabled)) for (const item of args.state ?? []) query.append("state", String(item)) for (const item of args.type ?? []) query.append("type", String(item)) for (const item of args.type_domain ?? []) query.append("type_domain", String(item)) for (const item of args.type_scope ?? []) query.append("type_scope", String(item)) if (args.with_new_type != null) query.append("with_new_type", String(args.with_new_type)) for (const item of args.parent_id ?? []) query.append("parent_id", String(item)) if (args.start_datetime_min != null) query.append("start_datetime_min", String(args.start_datetime_min)) if (args.start_datetime_max != null) query.append("start_datetime_max", String(args.start_datetime_max)) if (args.last_modified_min != null) query.append("last_modified_min", String(args.last_modified_min)) if (args.last_modified_max != null) query.append("last_modified_max", String(args.last_modified_max)) if (args.pagination_last_display_order != null) query.append("pagination_last_display_order", String(args.pagination_last_display_order)) if (args.pagination_last_start_datetime != null) query.append("pagination_last_start_datetime", String(args.pagination_last_start_datetime)) if (args.pagination_last_id != null) query.append("pagination_last_id", String(args.pagination_last_id)) if (args.pagination_last_name != null) query.append("pagination_last_name", String(args.pagination_last_name)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.limit != null) query.append("limit", String(args.limit)) 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/${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(getApiV3EventsResponse, await res.json()) };