import {z} from 'zod' export type GetApiAgentFriendlyAssetsSearchArgs = {search?: string | undefined, limit?: number | undefined, offset?: number | undefined, sortBy?: ('price' | 'volume' | 'marketCap' | 'changePercent24Hr') | undefined, sortOrder?: ('asc' | 'desc') | undefined}; export type GetApiAgentFriendlyAssetsSearchOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiAgentFriendlyAssetsSearchResponse = z.object({timestamp: z.number().int().optional(), data: z.array(z.object({slug: z.string().optional(), price: z.string().optional(), volume: z.string().optional(), symbol: z.string().optional(), name: z.string().optional(), marketCap: z.string().optional(), changePercent24Hr: z.string().nullable().optional()})).optional(), pagination: z.object({total: z.number().int().optional(), limit: z.number().int().optional(), offset: z.number().int().optional()}).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 }; /** * Search and discover crypto assets * * GET /agentFriendly/assets_search * * Also accepts auth: apiKey, x402 */ export const getApiAgentFriendlyAssetsSearch = async (args: GetApiAgentFriendlyAssetsSearchArgs, options?: GetApiAgentFriendlyAssetsSearchOptions) => { const {baseUrl = "https://rest.coincap.io/v3", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortOrder != null) query.append("sortOrder", String(args.sortOrder)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/agentFriendly/assets_search${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(getApiAgentFriendlyAssetsSearchResponse, await res.json()) };