import {z} from 'zod' export type GetApiV1MetricsAssetListingArgs = {metric_id?: string | undefined, exchange_id?: string | undefined, chain_id?: string | undefined, network_id?: string | undefined, asset_id?: string | undefined, asset_id_external?: string | undefined}; export type GetApiV1MetricsAssetListingOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const v1ListingItem = z.object({metric_id: z.string().nullable().optional(), symbol_id: z.string().nullable().optional(), symbol_id_external: z.string().nullable().optional(), exchange_id: z.string().nullable().optional(), asset_id: z.string().nullable().optional(), asset_id_external: z.string().nullable().optional(), chain_id: z.string().nullable().optional(), network_id: z.string().nullable().optional()}); export const getApiV1MetricsAssetListingResponse = z.array(v1ListingItem); 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 }; /** * Listing of all supported metrics for asset * * GET /v1/metrics/asset/listing * * Also accepts auth: JWT */ export const getApiV1MetricsAssetListing = async (args: GetApiV1MetricsAssetListingArgs, options?: GetApiV1MetricsAssetListingOptions) => { const {baseUrl = "https://rest.coinapi.io", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.metric_id != null) query.append("metric_id", String(args.metric_id)) if (args.exchange_id != null) query.append("exchange_id", String(args.exchange_id)) if (args.chain_id != null) query.append("chain_id", String(args.chain_id)) if (args.network_id != null) query.append("network_id", String(args.network_id)) if (args.asset_id != null) query.append("asset_id", String(args.asset_id)) if (args.asset_id_external != null) query.append("asset_id_external", String(args.asset_id_external)) if (APIKey !== undefined) headers.set("Authorization", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/metrics/asset/listing${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(getApiV1MetricsAssetListingResponse, await res.json()) };