import {z} from 'zod' export type GetApiMetricsV1ExchangesArgs = {limit?: number | undefined, pageSize?: number | undefined, page?: number | undefined, type?: ('centralized' | 'decentralized') | undefined, typeRankCutoff?: number | undefined}; export type GetApiMetricsV1ExchangesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const exchangeMetrics = z.record(z.string(), z.unknown()); export const exchange = z.object({country: z.string(), globalRank30D: z.number().int(), id: z.string(), metrics: exchangeMetrics, name: z.string(), region: z.string(), relatedExchangeIDs: z.array(z.string()), slug: z.string(), type: z.string(), typeRank30D: z.number().int(), yearEstablished: z.number().int()}); export const snapshotListingMetadata = z.object({page: z.number().int(), pageSize: z.number().int(), totalPages: z.number().int(), totalRows: z.number().int()}); export const getApiMetricsV1ExchangesResponse = z.object({data: z.array(exchange), error: z.string().optional(), metadata: snapshotListingMetadata.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 exchanges * * GET /metrics/v1/exchanges */ export const getApiMetricsV1Exchanges = async (args: GetApiMetricsV1ExchangesArgs, options?: GetApiMetricsV1ExchangesOptions) => { const {baseUrl = "https://api.messari.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.page != null) query.append("page", String(args.page)) if (args.type != null) query.append("type", String(args.type)) if (args.typeRankCutoff != null) query.append("typeRankCutoff", String(args.typeRankCutoff)) if (apiKey !== undefined) headers.set("X-Messari-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/metrics/v1/exchanges${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(getApiMetricsV1ExchangesResponse, await res.json()) };