import {z} from 'zod' export type TransactionType = 'DEPOSIT' | 'WITHDRAWAL' | 'TRANSFER'; export type TransactionStatus = 'FAILED' | 'PROCESSING' | 'CONFIRMING' | 'COMPLETED'; export type SortDirection = 'ASC' | 'DESC'; export type GetApiV1TransactionsArgs = {transactionType?: TransactionType | undefined, transactionStatus?: TransactionStatus | undefined, createdAtStart?: string | undefined, createdAtEnd?: string | undefined, accountId?: string | undefined, after?: string | undefined, limit?: unknown | undefined, sortField?: string | undefined, sortDirection?: SortDirection | undefined}; export type GetApiV1TransactionsOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionType = z.enum(["DEPOSIT", "WITHDRAWAL", "TRANSFER"]); export const transactionStatus = z.enum(["FAILED", "PROCESSING", "CONFIRMING", "COMPLETED"]); export const deposit = z.object({depositId: z.string().optional(), transactionType: transactionType.optional(), transactionStatus: transactionStatus.optional(), customerId: z.string().nullable().optional(), accountId: z.string().nullable().optional(), quantity: z.string().optional(), currency: z.string().optional(), confirmations: z.string().optional(), txHash: z.string().optional(), network: z.string().optional(), sourceAddresses: z.array(z.object({address: z.string().optional()})).optional(), receivedAt: z.string().optional(), completedAt: z.string().optional(), externalDepositId: z.string().nullable().optional()}); export const iD = z.string(); export const financial = z.string(); export const withdrawalFeeType = z.enum(["FIXED", "VARIABLE", "LIVE"]); export const withdrawal = z.object({transactionType: transactionType, transactionStatus: transactionStatus.optional(), withdrawalId: iD, customerId: iD, accountId: iD, withdrawalCurrency: z.string(), netWithdrawalQuantity: financial, withdrawalFeeType: withdrawalFeeType, withdrawalFeeRate: financial, withdrawalFeeQuantity: financial, withdrawalFeeCurrency: z.string(), memo: z.string().nullable().optional(), txHash: z.string().nullable().optional(), clientMemo: z.string().nullable().optional(), grossWithdrawalQuantity: financial, requestedAt: z.string(), completedAt: z.string().nullable().optional()}); export const transferDirection = z.enum(["IN", "OUT"]); export const transactionTransfer = z.object({transactionType: transactionType, transactionStatus: transactionStatus, transferId: z.string(), transferDirection: transferDirection, customerId: z.string(), accountId: z.string(), transferQuantity: financial, transferCurrency: z.string(), transferReason: z.string().max(255).nullable().optional(), externalReferenceId: z.string().max(255).nullable().optional(), requestedAt: z.string(), completedAt: z.string().nullable()}); export const getApiV1TransactionsResponse = z.object({after: z.string().optional(), totalCount: z.string().optional(), items: z.array(z.union([deposit, withdrawal, transactionTransfer])).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 }; /** * List Transactions * * GET /v1/transactions */ export const getApiV1Transactions = async (args: GetApiV1TransactionsArgs, options?: GetApiV1TransactionsOptions) => { const {baseUrl = "https://cams.aquanow.com/api", fetch: fetchFn = fetch, Bearer} = 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.transactionStatus != null) query.append("transactionStatus", String(args.transactionStatus)) if (args.createdAtStart != null) query.append("createdAtStart", String(args.createdAtStart)) if (args.createdAtEnd != null) query.append("createdAtEnd", String(args.createdAtEnd)) if (args.accountId != null) query.append("accountId", String(args.accountId)) if (args.after != null) query.append("after", String(args.after)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sortField != null) query.append("sortField", String(args.sortField)) if (args.sortDirection != null) query.append("sortDirection", String(args.sortDirection)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(getApiV1TransactionsResponse, await res.json()) };