import {z} from 'zod' export type GetApiTimeseriesInstitutionMetricsArgs = {institutions: Array, metrics: 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, sort?: ('institution' | 'time') | undefined, limit_per_institution?: number | undefined, pretty?: boolean | undefined, format?: ('json' | 'csv') | undefined, next_page_token?: string | undefined}; export type GetApiTimeseriesInstitutionMetricsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const institution = z.string(); export const time = z.string(); export const nextPageToken = z.string(); export const nextPageUrl = z.string(); export const institutionMetricsResponse = z.object({data: z.array(z.object({institution: institution, time: time}).and(z.record(z.string(), z.string().nullable()))), 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 }; /** * Institution metrics * * GET /timeseries/institution-metrics */ export const getApiTimeseriesInstitutionMetrics = async (args: GetApiTimeseriesInstitutionMetricsArgs, options?: GetApiTimeseriesInstitutionMetricsOptions) => { 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.institutions != null) query.append("institutions", args.institutions.join(',')) if (args.metrics != null) query.append("metrics", args.metrics.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.sort != null) query.append("sort", String(args.sort)) if (args.limit_per_institution != null) query.append("limit_per_institution", String(args.limit_per_institution)) 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/institution-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(institutionMetricsResponse, await res.json()) };