import {z} from 'zod' export type GetApiV2SpreadsArgs = {category?: (string | null) | undefined, sport?: (string | null) | undefined, competition?: (string | null) | undefined, location?: (string | null) | undefined, asset?: (string | null) | undefined, indicator?: (string | null) | undefined, basis?: (string | null) | undefined, institution?: (string | null) | undefined, min_gap?: (number | null) | undefined, limit?: number | undefined, authorization?: string | undefined}; export type GetApiV2SpreadsOptions = {baseUrl?: string | undefined, bearerApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV2SpreadsResponse = z.unknown(); 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 }; /** * Cross-venue price-gap view over matched pairs (v2) * * GET /v2/spreads */ export const getApiV2Spreads = async (args: GetApiV2SpreadsArgs, options?: GetApiV2SpreadsOptions) => { const {baseUrl = "https://api.dino.markets", fetch: fetchFn = fetch, bearerApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.category != null) query.append("category", String(args.category)) if (args.sport != null) query.append("sport", String(args.sport)) if (args.competition != null) query.append("competition", String(args.competition)) if (args.location != null) query.append("location", String(args.location)) if (args.asset != null) query.append("asset", String(args.asset)) if (args.indicator != null) query.append("indicator", String(args.indicator)) if (args.basis != null) query.append("basis", String(args.basis)) if (args.institution != null) query.append("institution", String(args.institution)) if (args.min_gap != null) query.append("min_gap", String(args.min_gap)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.authorization != null) headers.set("authorization", String(args.authorization)) if (bearerApiKey !== undefined) headers.set('authorization', `Bearer ${bearerApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/spreads${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(getApiV2SpreadsResponse, await res.json()) };