import {z} from 'zod' export type GetApiGatewayV1MarketPriceArgs = {symbol: string, time_range?: ('1d' | '7d' | '14d' | '30d' | '90d' | '180d' | '365d' | 'max') | undefined, from?: string | undefined, to?: string | undefined, currency?: string | undefined}; export type GetApiGatewayV1MarketPriceOptions = {baseUrl?: string | undefined, AccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const marketMetricPoint = z.object({exchange: z.string().optional(), exchange_name: z.string().optional(), metric: z.string().optional(), project_id: z.string().optional(), symbol: z.string().optional(), timestamp: z.number().int(), value: z.number()}); export const objectResponseMeta = z.object({cached: z.boolean(), credits_used: z.number().int(), empty_reason: z.string().optional()}); export const marketPriceSummary = z.object({change_abs: z.number(), change_pct: z.number(), first: z.number(), high: z.number(), last: z.number(), latest_dt: z.number().int(), low: z.number(), n_points: z.number().int()}); export const marketPriceResponse = z.object({$schema: z.string().optional(), data: z.array(marketMetricPoint).nullable(), meta: objectResponseMeta, project_id: z.string().optional(), summary: marketPriceSummary, symbol: 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 }; /** * Token Price History * * GET /gateway/v1/market/price */ export const getApiGatewayV1MarketPrice = async (args: GetApiGatewayV1MarketPriceArgs, options?: GetApiGatewayV1MarketPriceOptions) => { const {baseUrl = "https://api.asksurf.ai", fetch: fetchFn = fetch, AccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.time_range != null) query.append("time_range", String(args.time_range)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.currency != null) query.append("currency", String(args.currency)) if (AccessToken !== undefined) headers.set('authorization', `Bearer ${AccessToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/gateway/v1/market/price${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(marketPriceResponse, await res.json()) };