import {z} from 'zod' export type GetApiTimeseriesStreamIndexLevelsArgs = {indexes: Array, include_verification?: boolean | undefined, backfill?: ('latest' | 'none') | undefined, pretty?: boolean | undefined}; export type GetApiTimeseriesStreamIndexLevelsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const index = z.string(); export const time = z.string(); export const indexLevelValue = z.string(); export const verificationTime = z.string(); export const verificationIndexLevelValue = z.string(); export const signature = z.string(); export const verification = z.object({timestamp: verificationTime.optional(), level: verificationIndexLevelValue.optional(), signature: signature}); export const cmSequenceId = z.string(); export const streamingIndexLevel = z.object({index: index, time: time, level: indexLevelValue, verification: verification.optional(), cm_sequence_id: cmSequenceId}); 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 }; /** * Index levels * * GET /timeseries-stream/index-levels */ export const getApiTimeseriesStreamIndexLevels = async (args: GetApiTimeseriesStreamIndexLevelsArgs, options?: GetApiTimeseriesStreamIndexLevelsOptions) => { 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.indexes != null) query.append("indexes", args.indexes.join(',')) if (args.include_verification != null) query.append("include_verification", String(args.include_verification)) if (args.backfill != null) query.append("backfill", String(args.backfill)) if (args.pretty != null) query.append("pretty", String(args.pretty)) if (apiKey !== undefined) query.set("api_key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/timeseries-stream/index-levels${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(streamingIndexLevel, await res.json()) };