import {z} from 'zod' export type TransactionStatus = 'PENDING' | 'PROCESSING' | 'SUCCESS' | 'FAILED' | 'CANCELLED' | 'REFUNDED' | 'RETRY' | 'UNKNOWN' | 'SCHEDULED' | 'CUSTOMER_ACTION_REQUIRED' | 'REJECTED' | 'IN_REVIEW' | 'RFI_REQUESTED' | 'DISPUTED' | 'DISPUTE_RESOLVED' | 'DISPUTE_WON' | 'DISPUTE_LOST' | 'DISPUTE_EVIDENCE_SUBMITTED'; export type GetApiApiV1TransactionArgs = {page?: number | undefined, limit?: number | undefined, transactionId?: string | undefined, reference?: string | undefined, status?: Array | undefined, type?: (Array<'DEPOSIT' | 'WITHDRAW' | 'SWAP' | 'REVERSAL' | 'SCHEDULED' | 'SEND'>) | undefined, channel?: (Array<'BANK_ACCOUNT' | 'MOBILE_MONEY' | 'CARD' | 'CRYPTO' | 'VIRTUAL_BANK_ACCOUNT' | 'POOL_ACCOUNT' | 'ACH_BANK_ACCOUNT' | 'INTERAC' | 'PAYBILL_TILL' | 'RFP' | 'UPI' | 'VIRTUAL_CARD' | 'SWIFT' | 'WE_CHAT' | 'ALIPAY' | 'WALLET' | 'PAYMENT_LINQ' | 'ADMIN'>) | undefined, currency?: Array | undefined, fromDate?: string | undefined, toDate?: string | undefined}; export type GetApiApiV1TransactionOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const transactionStatus = z.enum(["PENDING", "PROCESSING", "SUCCESS", "FAILED", "CANCELLED", "REFUNDED", "RETRY", "UNKNOWN", "SCHEDULED", "CUSTOMER_ACTION_REQUIRED", "REJECTED", "IN_REVIEW", "RFI_REQUESTED", "DISPUTED", "DISPUTE_RESOLVED", "DISPUTE_WON", "DISPUTE_LOST", "DISPUTE_EVIDENCE_SUBMITTED"]); export const transaction = z.object({transactionId: z.string().optional(), customerId: z.string().optional(), sourceId: z.string().optional(), destinationId: z.string().optional(), sourceAmount: z.string().optional(), sourceCurrency: z.string().optional(), destinationAmount: z.string().optional(), destinationCurrency: z.string().optional(), type: z.enum(["DEPOSIT", "WITHDRAW", "SWAP", "REVERSAL", "SCHEDULED", "SEND"]).optional(), channel: z.enum(["BANK_ACCOUNT", "SWIFT", "MOBILE_MONEY", "UPI", "INTERAC", "WE_CHAT", "CARD", "CRYPTO", "VIRTUAL_BANK_ACCOUNT", "POOL_ACCOUNT", "ACH_BANK_ACCOUNT", "PAYBILL_TILL", "RFP", "VIRTUAL_CARD", "ALIPAY", "WALLET", "PAYMENT_LINQ", "ADMIN"]).optional(), status: transactionStatus.optional(), merchantReference: z.string().optional(), rate: z.string().optional(), fee: z.string().optional(), meta: z.object({narration: z.string().optional(), otpRequired: z.boolean().optional(), failureReason: z.object({code: z.enum(["AFX_REQUEST_FAILED", "AFX_SYSTEM_ERROR", "AFX_SERVICE_UNAVAILABLE", "AFX_INVALID_CURRENCY", "AFX_INVALID_AMOUNT", "AFX_INVALID_RECIPIENT", "AFX_RECIPIENT_NOT_FOUND", "AFX_ACCOUNT_CLOSED", "AFX_NAME_MISMATCH", "AFX_BENEFICIARY_RESTRICTED", "AFX_INVALID_SENDER", "AFX_INVALID_REQUEST", "AFX_VELOCITY_LIMIT_EXCEEDED", "AFX_AMOUNT_LIMIT_EXCEEDED", "AFX_RECIPIENT_WALLET_LIMIT_REACHED", "AFX_PAYMENT_FAILED", "AFX_COMPLIANCE_REJECTED", "AFX_PROPOSAL_EXPIRED"]), message: z.string(), retryable: z.boolean()}).optional()}).and(z.record(z.string(), z.unknown())).optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const getApiApiV1TransactionResponse = z.object({data: z.array(transaction).optional(), page: z.number().int().optional(), total: z.number().int().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 transaction list * * GET /api/v1/transaction */ export const getApiApiV1Transaction = async (args: GetApiApiV1TransactionArgs, options?: GetApiApiV1TransactionOptions) => { const {baseUrl = "https://sandbox.api.afriex.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.transactionId != null) query.append("transactionId", String(args.transactionId)) if (args.reference != null) query.append("reference", String(args.reference)) if (args.status != null) query.append("status", args.status.join(',')) if (args.type != null) query.append("type", args.type.join(',')) if (args.channel != null) query.append("channel", args.channel.join(',')) if (args.currency != null) query.append("currency", args.currency.join(',')) if (args.fromDate != null) query.append("fromDate", String(args.fromDate)) if (args.toDate != null) query.append("toDate", String(args.toDate)) if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/transaction${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(getApiApiV1TransactionResponse, await res.json()) };