import {z} from 'zod' export type CreateApiV024AgentsTransactionsArgs = {agentId: string, subAccount?: string | undefined, body: {page?: number | undefined, limit?: number | undefined, filters?: ({startDate?: string | undefined, endDate?: string | undefined, status?: string | undefined, currency?: string | undefined}) | undefined}}; export type CreateApiV024AgentsTransactionsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV024AgentsTransactionsResponse = z.object({status: z.string().optional(), message: z.string().optional(), data: z.array(z.object({})).optional(), totalCount: z.number().optional(), meta: z.object({agentId: z.string().optional(), page: z.number().optional(), limit: z.number().optional()}).optional(), balances: z.object({}).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 all transactions for an agent * * POST /v0.2.4/agents/transactions */ export const createApiV024AgentsTransactions = async (args: CreateApiV024AgentsTransactionsArgs, options?: CreateApiV024AgentsTransactionsOptions) => { const {baseUrl = "https://api.chimoney.io", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.agentId != null) query.append("agentId", String(args.agentId)) if (args.subAccount != null) query.append("subAccount", String(args.subAccount)) if (apiKeyAuth !== undefined) headers.set("X-API-KEY", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v0.2.4/agents/transactions${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(createApiV024AgentsTransactionsResponse, await res.json()) };