import {z} from 'zod' export type TransactionsSvcTransactionType = 'ONRAMP' | 'OFFRAMP'; export type TransactionOrderStatus = 'INITIATED' | 'DEPOSITED' | 'CONFIRMED' | 'COMPLETED' | 'PENDING' | 'CANCELLED' | 'IN_DISPUTE' | 'AWAITING_ACCEPTANCE'; export type GetApiTransactionsCustomersIdHistoryAggregateArgs = {id: string, transactionType?: TransactionsSvcTransactionType | undefined, from?: string | undefined, to?: string | undefined, statuses?: Array | undefined, token?: string | undefined, fiat?: string | undefined, network?: string | undefined, paymentChannel?: string | undefined, reference?: string | undefined, otherPartyId?: string | undefined, isTradeRequestBroadcast?: boolean | undefined}; export type GetApiTransactionsCustomersIdHistoryAggregateOptions = {baseUrl?: string | undefined, HMACAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionHistoryAggregate = z.object({userId: z.string().optional(), tradeCount: z.number().int(), tradeUsdTotal: z.number(), fiatTotal: z.number().optional(), tokenTotal: z.number().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 }; /** * Retrieve list of customer transactions * * GET /transactions/customers/{id}/history-aggregate */ export const getApiTransactionsCustomersIdHistoryAggregate = async (args: GetApiTransactionsCustomersIdHistoryAggregateArgs, options?: GetApiTransactionsCustomersIdHistoryAggregateOptions) => { const {baseUrl = "https://external.dev.onboardpay.co", fetch: fetchFn = fetch, HMACAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.transactionType != null) query.append("transactionType", String(args.transactionType)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.statuses != null) query.append("statuses", args.statuses.join(',')) if (args.token != null) query.append("token", String(args.token)) if (args.fiat != null) query.append("fiat", String(args.fiat)) if (args.network != null) query.append("network", String(args.network)) if (args.paymentChannel != null) query.append("paymentChannel", String(args.paymentChannel)) if (args.reference != null) query.append("reference", String(args.reference)) if (args.otherPartyId != null) query.append("otherPartyId", String(args.otherPartyId)) if (args.isTradeRequestBroadcast != null) query.append("isTradeRequestBroadcast", String(args.isTradeRequestBroadcast)) if (HMACAuth !== undefined) headers.set("x-api-key", HMACAuth) const url = `${baseUrl.replace(/\/+$/, '')}/transactions/customers/${encodeURIComponent(String(args.id))}/history-aggregate${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(transactionHistoryAggregate, await res.json()) };