import {z} from 'zod' export type GetApiApiV1MoneyAccountsCashAccountIdTransactionsArgs = {'x-request-id': string, 'x-api-key': string, 'x-user-key': string, accountId: string, pageSize?: number | undefined, pageToken?: string | undefined}; export type GetApiApiV1MoneyAccountsCashAccountIdTransactionsOptions = {baseUrl?: string | undefined, oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const counterparty = z.object({name: z.string().optional(), type: z.enum(["merchant", "bank_account", "internal_account", "unknown"]).optional()}); export const cardTransactionDetails = z.object({cardId: z.string().optional(), merchantName: z.string().optional(), country: z.string().optional(), authorizationStatus: z.enum(["unknown", "normal", "preAuthorize", "finalAuthorize", "incremental", "instalment", "preferredCustomer", "recurring", "delayedCharges", "noShow", "authorizeAdvice", "refund", "reversal", "sysReversal", "accountFunding"]).optional()}); export const bankIdentifierEntry = z.object({name: z.string(), value: z.string()}); export const bankTransferDetails = z.object({bankIdentifier: z.array(bankIdentifierEntry).optional(), description: z.string().optional(), paymentReference: z.string().optional()}); export const internalTransferDetails = z.object({transferId: z.string()}); export const cashAccountTransaction = z.object({id: z.string(), accountId: z.string(), transactionType: z.enum(["card", "internalTransfer", "bankTransfer", "balanceAdjustment"]), transactionSubtype: z.enum(["unknown", "cardPayment", "contactless", "onlinePayment", "cashWithdrawal", "transferReceived", "transfer", "paymentReceived", "payment", "refund", "fee", "creditBalanceAdjustment", "debitBalanceAdjustment", "directDebit", "cryptoToFiat"]), direction: z.enum(["debit", "credit"]), status: z.enum(["failed", "authorized", "settled", "rejected", "returned", "expired", "unknown"]), amount: z.string(), currency: z.string(), originalAmount: z.string().nullable().optional(), originalCurrency: z.string().nullable().optional(), conversionRate: z.string().nullable().optional(), postedAt: z.string(), counterparty: counterparty.optional(), cardTransactionDetails: cardTransactionDetails.optional(), bankTransferTransactionDetails: bankTransferDetails.optional(), internalTransferTransactionDetails: internalTransferDetails.optional()}); export const paginationCursor = z.object({pageSize: z.number().int(), nextPageToken: z.string().nullable().optional(), hasNext: z.boolean()}); export const cashAccountTransactionsResponse = z.object({results: z.array(cashAccountTransaction), pagination: paginationCursor}); 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 }; /** * List cash account transactions (paginated) * * GET /api/v1/money/accounts/cash/{accountId}/transactions */ export const getApiApiV1MoneyAccountsCashAccountIdTransactions = async (args: GetApiApiV1MoneyAccountsCashAccountIdTransactionsArgs, options?: GetApiApiV1MoneyAccountsCashAccountIdTransactionsOptions) => { const {baseUrl = "https://public-api.etoro.com", fetch: fetchFn = fetch, oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-request-id'] != null) headers.set("x-request-id", String(args['x-request-id'])) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) if (args['x-user-key'] != null) headers.set("x-user-key", String(args['x-user-key'])) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.pageToken != null) query.append("pageToken", String(args.pageToken)) if (oauth2 !== undefined) headers.set('authorization', `Bearer ${oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/money/accounts/cash/${encodeURIComponent(String(args.accountId))}/transactions${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(cashAccountTransactionsResponse, await res.json()) };