import {z} from 'zod' export type IntervalEnum = '1min' | '5min' | '15min' | '1hour' | '4hour' | '12hour' | '1day'; export type RangeEnum = '1day' | '1month' | '3month' | '6month' | '1year' | 'all'; export type GetApiV1AssetsSymbolPricesOhlcArgs = {symbol: string, interval: IntervalEnum, range: RangeEnum}; export type GetApiV1AssetsSymbolPricesOhlcOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const intervalEnum = z.enum(["1min", "5min", "15min", "1hour", "4hour", "12hour", "1day"]); export const rangeEnum = z.enum(["1day", "1month", "3month", "6month", "1year", "all"]); export const oHLCDataPoint = z.object({timestamp: z.number(), open: z.string(), high: z.string(), low: z.string(), close: z.string()}); export const oHLCResponse = z.object({interval: intervalEnum, range: rangeEnum, primaryMarket: z.object({symbol: z.string().optional(), data: z.array(oHLCDataPoint).optional()}), underlyingMarket: z.object({ticker: z.string().optional(), data: z.array(oHLCDataPoint).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 OHLC (Open, High, Low, Close) Data for an Asset * * GET /v1/assets/{symbol}/prices/ohlc */ export const getApiV1AssetsSymbolPricesOhlc = async (args: GetApiV1AssetsSymbolPricesOhlcArgs, options?: GetApiV1AssetsSymbolPricesOhlcOptions) => { const {baseUrl = "https://api.gm.ondo.finance", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.interval != null) query.append("interval", String(args.interval)) if (args.range != null) query.append("range", String(args.range)) if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/assets/${encodeURIComponent(String(args.symbol))}/prices/ohlc${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(oHLCResponse, await res.json()) };