import {z} from 'zod' export type GetApiApiV1MarketDataPricesArgs = {market: string, token_id?: string | undefined, outcome_index?: number | undefined, token_ids?: string | undefined, outcome_indices?: string | undefined, projection?: string | undefined}; export type GetApiApiV1MarketDataPricesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const internalHandlerPriceResponse = z.object({market: z.string().optional(), mintedQuantity: z.number().optional(), outcome: z.string().optional(), outcomeCap: z.number().optional(), price: z.number().optional(), tokenId: z.string().optional(), totalMarketCap: z.number().optional(), updatedAt: z.string().optional()}); export const getApiApiV1MarketDataPricesResponse = z.array(internalHandlerPriceResponse); 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 current outcome token prices * * GET /api/v1/market-data/prices */ export const getApiApiV1MarketDataPrices = async (args: GetApiApiV1MarketDataPricesArgs, options?: GetApiApiV1MarketDataPricesOptions) => { const {baseUrl = "https://rest.ft.42.space", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.market != null) query.append("market", String(args.market)) if (args.token_id != null) query.append("token_id", String(args.token_id)) if (args.outcome_index != null) query.append("outcome_index", String(args.outcome_index)) if (args.token_ids != null) query.append("token_ids", String(args.token_ids)) if (args.outcome_indices != null) query.append("outcome_indices", String(args.outcome_indices)) if (args.projection != null) query.append("projection", String(args.projection)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/market-data/prices${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(getApiApiV1MarketDataPricesResponse, await res.json()) };