import {z} from 'zod' export type GetApiV1ProtocolPricesArgs = {granularity?: ('5m' | '1h' | '1d') | undefined, days?: number | undefined, from?: string | undefined, to?: string | undefined, token?: string | undefined, ohlc?: boolean | undefined}; export type GetApiV1ProtocolPricesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const date = z.string(); export const decimal = z.string(); export const protocolPrices = z.object({since: date, until: date, granularity: z.enum(["5m", "1h", "1d"]), tokens: z.array(z.object({token: z.string(), series: z.array(z.object({ts: z.string(), usd: decimal, sol: decimal.optional(), open: decimal.optional(), high: decimal.optional(), low: decimal.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 }; /** * Token prices * * GET /v1/protocol/prices */ export const getApiV1ProtocolPrices = async (args: GetApiV1ProtocolPricesArgs, options?: GetApiV1ProtocolPricesOptions) => { const {baseUrl = "https://api.hylo.so", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.days != null) query.append("days", String(args.days)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.token != null) query.append("token", String(args.token)) if (args.ohlc != null) query.append("ohlc", String(args.ohlc)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/protocol/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(protocolPrices, await res.json()) };