import {z} from 'zod' export type GetApiApiV1TransactionQuoteArgs = {customerId?: string | undefined, destinationId?: string | undefined, destinationChannel?: ('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' | 'PAYMENT_LINQ' | 'ADMIN' | 'WALLET') | undefined, destinationCountry?: string | undefined, sourceCurrency: string, destinationCurrency: string, sourceAmount?: string | undefined, destinationAmount?: string | undefined}; export type GetApiApiV1TransactionQuoteOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV1TransactionQuoteResponse = z.object({data: z.object({anchor: z.enum(["source", "destination"]), sourceAmount: z.string().optional(), sourceCurrency: z.string(), destinationAmount: z.string().optional(), destinationCurrency: z.string(), indicative: z.boolean().optional(), rate: z.string().optional(), fee: 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 }; /** * Quote a withdrawal * * GET /api/v1/transaction/quote */ export const getApiApiV1TransactionQuote = async (args: GetApiApiV1TransactionQuoteArgs, options?: GetApiApiV1TransactionQuoteOptions) => { 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.customerId != null) query.append("customerId", String(args.customerId)) if (args.destinationId != null) query.append("destinationId", String(args.destinationId)) if (args.destinationChannel != null) query.append("destinationChannel", String(args.destinationChannel)) if (args.destinationCountry != null) query.append("destinationCountry", String(args.destinationCountry)) if (args.sourceCurrency != null) query.append("sourceCurrency", String(args.sourceCurrency)) if (args.destinationCurrency != null) query.append("destinationCurrency", String(args.destinationCurrency)) if (args.sourceAmount != null) query.append("sourceAmount", String(args.sourceAmount)) if (args.destinationAmount != null) query.append("destinationAmount", String(args.destinationAmount)) if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/transaction/quote${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(getApiApiV1TransactionQuoteResponse, await res.json()) };