import {z} from 'zod' export type GetApiCatalogV2AssetMetricsArgs = {assets?: Array | undefined, metrics?: Array | undefined, reviewable?: boolean | undefined, page_size?: number | undefined, paging_from?: ('start' | 'end') | undefined, next_page_token?: string | undefined, format?: string | undefined, pretty?: boolean | undefined}; export type GetApiCatalogV2AssetMetricsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const assetId = z.string(); export const metricId = z.string(); export const minTime = z.string(); export const maxTime = z.string(); export const minHeight = z.string(); export const maxHeight = z.string(); export const minHash = z.string(); export const maxHash = z.string(); export const community = z.boolean(); export const experimentalAssetMetric = z.boolean(); export const assetMetricFrequency = z.object({frequency: z.string(), min_time: minTime, max_time: maxTime, min_height: minHeight.optional(), max_height: maxHeight.optional(), min_hash: minHash.optional(), max_hash: maxHash.optional(), community: community.optional(), experimental: experimentalAssetMetric.optional()}); export const assetMetricFrequencies = z.array(assetMetricFrequency); export const assetMetricInfo = z.object({metric: metricId, frequencies: assetMetricFrequencies}); export const metricsIds = z.array(assetMetricInfo); export const catalogV2AssetMetricsInfo = z.object({asset: assetId, metrics: metricsIds}); export const nextPageToken = z.string(); export const nextPageUrl = z.string(); export const catalogV2AssetMetricsResponse = z.object({data: z.array(catalogV2AssetMetricsInfo), 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 }; /** * Available asset metrics * * GET /catalog-v2/asset-metrics */ export const getApiCatalogV2AssetMetrics = async (args: GetApiCatalogV2AssetMetricsArgs, options?: GetApiCatalogV2AssetMetricsOptions) => { 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.assets != null) query.append("assets", args.assets.join(',')) if (args.metrics != null) query.append("metrics", args.metrics.join(',')) if (args.reviewable != null) query.append("reviewable", String(args.reviewable)) 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.next_page_token != null) query.append("next_page_token", String(args.next_page_token)) if (args.format != null) query.append("format", String(args.format)) if (args.pretty != null) query.append("pretty", String(args.pretty)) if (apiKey !== undefined) query.set("api_key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/catalog-v2/asset-metrics${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(catalogV2AssetMetricsResponse, await res.json()) };