import {z} from 'zod' export type CreateApiV2AccountsBulkArgs = {currency?: string | undefined, body: {account_ids: Array}}; export type CreateApiV2AccountsBulkOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const ecPreview = z.object({decimals: z.number().int(), id: z.number().int(), image: z.string(), symbol: z.string()}); export const extraCurrency = z.object({amount: z.string(), preview: ecPreview}); export const accountStatus = z.enum(["nonexist", "uninit", "active", "frozen"]); export const account = z.object({address: z.string(), balance: z.number().int(), currencies_balance: z.record(z.string(), z.unknown()).optional(), extra_balance: z.array(extraCurrency).optional(), get_methods: z.array(z.string()), icon: z.string().optional(), interfaces: z.array(z.string()).optional(), is_scam: z.boolean().optional(), is_suspended: z.boolean().optional(), is_wallet: z.boolean(), last_activity: z.number().int(), memo_required: z.boolean().optional(), name: z.string().optional(), status: accountStatus}); export const accounts = z.object({accounts: z.array(account)}); 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 human-friendly information about several accounts without low-level details. * * POST /v2/accounts/_bulk */ export const createApiV2AccountsBulk = async (args: CreateApiV2AccountsBulkArgs, options?: CreateApiV2AccountsBulkOptions) => { const {baseUrl = "https://tonapi.io", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.currency != null) query.append("currency", String(args.currency)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/accounts/_bulk${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(accounts, await res.json()) };