import {z} from 'zod' export type GetApiMultivariateEventCollectionsArgs = {status?: ('unopened' | 'open' | 'closed') | undefined, associated_event_ticker?: string | undefined, series_ticker?: string | undefined, limit?: number | undefined, cursor?: string | undefined}; export type GetApiMultivariateEventCollectionsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const exchangeIndex = z.number().int(); export const associatedEvent = z.object({ticker: z.string(), is_yes_only: z.boolean(), size_max: z.number().int().nullable().optional(), size_min: z.number().int().nullable().optional(), active_quoters: z.array(z.string())}); export const priceRange = z.object({start: z.string(), end: z.string(), step: z.string()}); export const multivariateEventCollection = z.object({collection_ticker: z.string(), series_ticker: z.string(), exchange_index: exchangeIndex.optional(), title: z.string(), description: z.string(), open_date: z.string(), close_date: z.string(), associated_events: z.array(associatedEvent), associated_event_tickers: z.array(z.string()), is_ordered: z.boolean(), is_single_market_per_event: z.boolean(), is_all_yes: z.boolean(), size_min: z.number().int(), size_max: z.number().int(), functional_description: z.string(), price_level_structure: z.string(), price_ranges: z.array(priceRange)}); export const getMultivariateEventCollectionsResponse = z.object({multivariate_contracts: z.array(multivariateEventCollection), cursor: z.string().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 }; /** * Get Multivariate Event Collections * * GET /multivariate_event_collections */ export const getApiMultivariateEventCollections = async (args: GetApiMultivariateEventCollectionsArgs, options?: GetApiMultivariateEventCollectionsOptions) => { const {baseUrl = "https://external-api.kalshi.com/trade-api/v2", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.associated_event_ticker != null) query.append("associated_event_ticker", String(args.associated_event_ticker)) if (args.series_ticker != null) query.append("series_ticker", String(args.series_ticker)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) const url = `${baseUrl.replace(/\/+$/, '')}/multivariate_event_collections${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(getMultivariateEventCollectionsResponse, await res.json()) };