import {z} from 'zod' export type GetApiV1MarketDataMetricsArgs = {pair?: string | undefined, quoteCurrency?: ('USD' | 'AED') | undefined, limit?: unknown | undefined, offset?: unknown | undefined}; export type GetApiV1MarketDataMetricsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1MarketDataMetricsResponse = z.array(z.object({pair: z.string().optional(), lastUpdated: z.string().optional(), marketCap: z.string().optional(), globalVolume24h: z.string().optional(), priceChange24h: z.string().optional(), priceChange24hPercent: z.string().optional(), allTimeHighPrice: z.string().optional(), allTimeHighTimestamp: z.string().optional(), allTimeLowPrice: z.string().optional(), allTimeLowTimestamp: 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 market metrics * * GET /v1/market-data/metrics */ export const getApiV1MarketDataMetrics = async (args: GetApiV1MarketDataMetricsArgs, options?: GetApiV1MarketDataMetricsOptions) => { const {baseUrl = "https://cams.aquanow.com/api", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.pair != null) query.append("pair", String(args.pair)) if (args.quoteCurrency != null) query.append("quoteCurrency", String(args.quoteCurrency)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/market-data/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(getApiV1MarketDataMetricsResponse, await res.json()) };