import {z} from 'zod' export type GetApiV1OptionsExchangeIdCurrentArgs = {exchange_id: string}; export type GetApiV1OptionsExchangeIdCurrentOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const v1LastTrade = z.object({time_exchange: z.string().optional(), time_coinapi: z.string().optional(), uuid: z.string().optional(), price: z.number().optional(), size: z.number().optional(), taker_side: z.string().nullable().optional()}); export const v1QuoteTrade = z.object({symbol_id: z.string().nullable().optional(), time_exchange: z.string().optional(), time_coinapi: z.string().optional(), ask_price: z.number().nullable().optional(), ask_size: z.number().nullable().optional(), bid_price: z.number().nullable().optional(), bid_size: z.number().nullable().optional(), last_trade: v1LastTrade.optional()}); export const v1Strike = z.object({strike_price: z.number().optional(), call: v1QuoteTrade.optional(), put: v1QuoteTrade.optional()}); export const v1OptionExchangeGroup = z.object({asset_id_base: z.string().nullable().optional(), asset_id_quote: z.string().nullable().optional(), underlying_price: z.number().nullable().optional(), time_expiration: z.string().optional(), strikes: z.array(v1Strike).nullable().optional()}); export const getApiV1OptionsExchangeIdCurrentResponse = z.array(v1OptionExchangeGroup); 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 }; /** * Current data by Exchange * * GET /v1/options/{exchange_id}/current * * Also accepts auth: JWT */ export const getApiV1OptionsExchangeIdCurrent = async (args: GetApiV1OptionsExchangeIdCurrentArgs, options?: GetApiV1OptionsExchangeIdCurrentOptions) => { const {baseUrl = "https://rest.coinapi.io", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (APIKey !== undefined) headers.set("Authorization", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/options/${encodeURIComponent(String(args.exchange_id))}/current${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(getApiV1OptionsExchangeIdCurrentResponse, await res.json()) };