import {z} from 'zod' export type GetApiIntelV1AssetsArgs = {symbol?: Array | undefined, name?: Array | undefined, limit?: number | undefined, page?: number | undefined}; export type GetApiIntelV1AssetsOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const intelAsset = z.object({id: z.string(), name: z.string(), slug: z.string(), symbol: z.string()}); export const pagination = z.object({limit: z.number().int(), page: z.number().int(), totalPages: z.number().int(), totalRows: z.number().int()}); export const getApiIntelV1AssetsResponse = z.object({data: z.array(intelAsset), error: z.string().optional(), metadata: pagination.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 }; /** * List all assets * * GET /intel/v1/assets */ export const getApiIntelV1Assets = async (args: GetApiIntelV1AssetsArgs, options?: GetApiIntelV1AssetsOptions) => { const {baseUrl = "https://api.messari.io", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.symbol ?? []) query.append("symbol", String(item)) for (const item of args.name ?? []) query.append("name", String(item)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (apiKey !== undefined) headers.set("X-Messari-API-Key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/intel/v1/assets${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(getApiIntelV1AssetsResponse, await res.json()) };