import {z} from 'zod' export type GetApiTimeseriesPairCandlesArgs = {pairs: Array, frequency?: string | undefined, start_time?: string | undefined, end_time?: string | undefined, start_inclusive?: boolean | undefined, end_inclusive?: boolean | undefined, timezone?: string | undefined, page_size?: number | undefined, paging_from?: ('start' | 'end') | undefined, limit_per_pair?: number | undefined, pretty?: boolean | undefined, format?: ('json' | 'csv') | undefined, next_page_token?: string | undefined}; export type GetApiTimeseriesPairCandlesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const pairId = z.string(); export const time = z.string(); export const candlePriceOpen = z.string(); export const candlePriceClose = z.string(); export const candlePriceHigh = z.string(); export const candlePriceLow = z.string(); export const pairCandle = z.object({pair: pairId, time: time, price_open: candlePriceOpen, price_close: candlePriceClose, price_high: candlePriceHigh, price_low: candlePriceLow}); export const nextPageToken = z.string(); export const nextPageUrl = z.string(); export const pairCandlesResponse = z.object({data: z.array(pairCandle), next_page_token: nextPageToken.optional(), next_page_url: nextPageUrl.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 }; /** * Pair candles * * GET /timeseries/pair-candles */ export const getApiTimeseriesPairCandles = async (args: GetApiTimeseriesPairCandlesArgs, options?: GetApiTimeseriesPairCandlesOptions) => { const {baseUrl = "https://api.coinmetrics.io/v4", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pairs != null) query.append("pairs", args.pairs.join(',')) if (args.frequency != null) query.append("frequency", String(args.frequency)) if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.start_inclusive != null) query.append("start_inclusive", String(args.start_inclusive)) if (args.end_inclusive != null) query.append("end_inclusive", String(args.end_inclusive)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.paging_from != null) query.append("paging_from", String(args.paging_from)) if (args.limit_per_pair != null) query.append("limit_per_pair", String(args.limit_per_pair)) if (args.pretty != null) query.append("pretty", String(args.pretty)) if (args.format != null) query.append("format", String(args.format)) if (args.next_page_token != null) query.append("next_page_token", String(args.next_page_token)) if (apiKey !== undefined) query.set("api_key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/timeseries/pair-candles${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(pairCandlesResponse, await res.json()) };