import {z} from 'zod' export type GetApiV3ExchangeRatesArgs = {amount: string, sourceCurrency: string, destinationCurrency: string, direction?: ('forward' | 'reverse') | undefined, orderType: 'onramp' | 'offramp' | 'payin' | 'payout' | 'fiat_prefund' | 'crypto_prefund' | 'swap' | 'gaming', paymentCode?: string | undefined, paymentType?: ('bank_transfer' | 'local_wallet' | 'card' | 'DEFAULT') | undefined, toPaymentCode?: string | undefined, toPaymentType?: ('bank_transfer' | 'local_wallet' | 'card' | 'DEFAULT') | undefined, MID: string}; export type GetApiV3ExchangeRatesOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const getApiV3ExchangeRatesResponse = z.object({status: z.string().optional(), data: z.object({sourceCurrency: z.string().optional(), sourceAmount: z.number().int().optional(), sourceAmountInUsd: z.number().optional(), sourceCurrencyType: z.string().optional(), destinationCurrency: z.string().optional(), destinationAmount: z.number().optional(), destinationAmountInUsd: z.number().optional(), destinationCurrencyType: z.string().optional(), conversionRate: z.number().optional(), minLimit: z.number().int().optional(), maxLimit: z.number().optional(), orderType: z.string().optional(), direction: z.string().optional(), processingFee: z.number().optional(), processingFeeRate: z.number().optional(), customerFee: z.string().optional(), customerFeeRate: z.number().int().optional(), rrFeeRate: z.string().optional(), discount: z.string().optional(), discountRate: z.string().optional(), additionalFee: z.number().int().optional(), fixedFee: z.object({totalFixedFees: z.number().int().optional(), totalFixedTfFees: z.number().int().optional(), totalFixedCxFees: z.number().int().optional(), fixedFeesCurrency: z.string().optional(), fixedFeeDetails: z.object({baseFeeFixedRate: z.number().int().optional(), tfFeeFixedRate: z.number().int().optional(), cxFeeFixedRate: z.number().int().optional(), currency: z.string().optional()}).optional()}).optional(), totalFee: z.number().optional(), feeMode: z.string().optional()}).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 Exchange Rates * * GET /v3/exchange-rates */ export const getApiV3ExchangeRates = async (args: GetApiV3ExchangeRatesArgs, options?: GetApiV3ExchangeRatesOptions) => { const {baseUrl = "https://sandbox-api.transfi.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.amount != null) query.append("amount", String(args.amount)) if (args.sourceCurrency != null) query.append("sourceCurrency", String(args.sourceCurrency)) if (args.destinationCurrency != null) query.append("destinationCurrency", String(args.destinationCurrency)) if (args.direction != null) query.append("direction", String(args.direction)) if (args.orderType != null) query.append("orderType", String(args.orderType)) if (args.paymentCode != null) query.append("paymentCode", String(args.paymentCode)) if (args.paymentType != null) query.append("paymentType", String(args.paymentType)) if (args.toPaymentCode != null) query.append("toPaymentCode", String(args.toPaymentCode)) if (args.toPaymentType != null) query.append("toPaymentType", String(args.toPaymentType)) if (args.MID != null) headers.set("MID", String(args.MID)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/exchange-rates${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(getApiV3ExchangeRatesResponse, await res.json()) };