import {z} from 'zod' export type GetApiMessagesAggregatedArgs = {address: string, order_by: 'amount' | 'count', from?: string | undefined, to?: string | undefined, limit?: number | undefined}; export type GetApiMessagesAggregatedOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const bunbigInt = z.object({}); export const aggregateMessagesRes = z.object({received_count: z.number().int().optional(), received_from_address: z.array(z.object({amount: bunbigInt.optional(), count: z.number().int().optional(), sender: z.array(z.number().int()).optional()})).optional(), received_ton_amount: bunbigInt.optional(), sent_count: z.number().int().optional(), sent_to_address: z.array(z.object({amount: bunbigInt.optional(), count: z.number().int().optional(), receiver: z.array(z.number().int()).optional()})).optional(), sent_ton_amount: bunbigInt.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 }; /** * aggregated messages * * GET /messages/aggregated */ export const getApiMessagesAggregated = async (args: GetApiMessagesAggregatedArgs, options?: GetApiMessagesAggregatedOptions) => { const {baseUrl = "https://anton.tools/api/v0", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.address != null) query.append("address", String(args.address)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/messages/aggregated${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(aggregateMessagesRes, await res.json()) };