import {z} from 'zod' export type GetApiV2LeaguesArgs = {limit?: number | undefined, offset?: number | undefined}; export type GetApiV2LeaguesOptions = {baseUrl?: string | undefined, XPMAccessKey?: string | undefined, XPMTimestamp?: string | undefined, XPMSignature?: string | undefined, fetch?: typeof fetch | undefined}; export const gatewaySportsV2League = z.object({name: z.string().optional(), slug: z.string().optional(), image: z.string().nullable().optional(), isOperational: z.boolean().nullable().optional(), id: z.number().int().optional(), sportId: z.number().int().optional(), tagId: z.number().int().nullable().optional(), resolution: z.string().nullable().optional(), ordering: z.string().nullable().optional(), activeSeriesId: z.number().int().nullable().optional(), createdAt: z.string().nullable().optional(), abbreviation: z.string().nullable().optional()}); export const gatewaySportsV2GetLeaguesResponse = z.object({leagues: z.array(gatewaySportsV2League).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 All Leagues * * GET /v2/leagues */ export const getApiV2Leagues = async (args: GetApiV2LeaguesArgs, options?: GetApiV2LeaguesOptions) => { const {baseUrl = "https://api.polymarket.us", fetch: fetchFn = fetch, XPMAccessKey, XPMTimestamp, XPMSignature} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (XPMAccessKey !== undefined) headers.set("X-PM-Access-Key", XPMAccessKey) if (XPMTimestamp !== undefined) headers.set("X-PM-Timestamp", XPMTimestamp) if (XPMSignature !== undefined) headers.set("X-PM-Signature", XPMSignature) const url = `${baseUrl.replace(/\/+$/, '')}/v2/leagues${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(gatewaySportsV2GetLeaguesResponse, await res.json()) };