import {z} from 'zod' export type GetApiExchangesExchangeIdArgs = {quotes?: string | undefined, exchange_id: string}; export type GetApiExchangesExchangeIdOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const exchangeLinks = z.object({website: z.array(z.string()).optional(), twitter: z.array(z.string()).optional()}); export const exchangeFiat = z.object({name: z.string().optional(), symbol: z.string().optional()}); export const exchangesQuote = z.object({reported_volume_24h: z.number().optional(), adjusted_volume_24h: z.number().optional(), reported_volume_7d: z.number().optional(), adjusted_volume_7d: z.number().optional(), reported_volume_30d: z.number().optional(), adjusted_volume_30d: z.number().optional()}); export const exchangesQuotes = z.object({$KEY: exchangesQuote.optional()}); export const exchange = z.object({id: z.string().optional(), name: z.string().optional(), type: z.array(z.enum(["cex", "dex", "perpetuals", "spot", "otc", "other"])).optional(), active: z.boolean().optional(), website_status: z.boolean().optional(), api_status: z.boolean().optional(), description: z.string().nullable().optional(), message: z.string().optional(), links: exchangeLinks.optional(), markets_data_fetched: z.boolean().optional(), adjusted_rank: z.number().int().optional(), reported_rank: z.number().int().optional(), currencies: z.number().int().optional(), markets: z.number().int().optional(), fiats: z.array(exchangeFiat).optional(), quotes: exchangesQuotes.optional(), last_updated: z.string().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 exchange by ID * * GET /exchanges/{exchange_id} */ export const getApiExchangesExchangeId = async (args: GetApiExchangesExchangeIdArgs, options?: GetApiExchangesExchangeIdOptions) => { const {baseUrl = "https://api.coinpaprika.com/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.quotes != null) query.append("quotes", String(args.quotes)) const url = `${baseUrl.replace(/\/+$/, '')}/exchanges/${encodeURIComponent(String(args.exchange_id))}${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(exchange, await res.json()) };