import {z} from 'zod' export type CreateApiQuotesGenerateArgs = {body: {sourceCurrency: string, destinationCurrency: 'NGN' | 'USD' | 'EUR' | 'GBP' | 'GHS' | 'KES' | 'UGX' | 'TZX' | 'ZMW' | 'XAF' | 'XOF' | 'USDT' | 'USDC' | 'CNGN', amount: string, action: 'send' | 'receive', transactionType: 'conversion' | 'disbursement', business: string, feeBearer?: ('customer' | 'business') | undefined, paymentDestination: 'bank_account' | 'fliqpay_wallet' | 'mobile_money_wallet' | 'crypto_wallet', paymentScheme?: ('swift' | 'ach' | 'fps' | 'sepa' | 'sepa_instant' | 'chaps' | 'fed_wire' | 'usdt_erc20' | 'usdt_trc20' | 'usdt_solana' | 'usdt_bep20' | 'usdc_erc20' | 'usdc_solana') | undefined, beneficiaryType?: ('individual' | 'corporate') | undefined, delay?: boolean | undefined}}; export type CreateApiQuotesGenerateOptions = {baseUrl?: string | undefined, sec0?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiQuotesGenerateResponse = z.union([z.object({success: z.boolean().optional(), message: z.string().optional(), data: z.object({sourceCurrency: z.string().optional(), destinationCurrency: z.string().optional(), sourceAmount: z.number().int().optional(), destinationAmount: z.number().optional(), action: z.string().optional(), transactionType: z.string().optional(), fee: z.number().int().optional(), initialAmount: z.number().int().optional(), quotedAmount: z.number().optional(), rate: z.number().optional(), amountToCharge: z.number().int().optional(), amountToReceive: z.number().int().optional(), reference: z.string().optional(), expireAt: z.string().optional(), settlementDate: z.string().optional()}).optional()}), z.object({success: z.boolean().optional(), message: z.string().optional(), data: z.object({sourceCurrency: z.string().optional(), destinationCurrency: z.string().optional(), sourceAmount: z.number().int().optional(), destinationAmount: z.number().optional(), action: z.string().optional(), transactionType: z.string().optional(), fee: z.number().int().optional(), initialAmount: z.number().int().optional(), quotedAmount: z.number().optional(), rate: z.number().optional(), amountToCharge: z.number().int().optional(), amountToReceive: z.number().int().optional(), reference: z.string().optional(), expireAt: z.string().optional(), feeCurrency: z.string().optional(), feeRate: z.number().int().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 }; /** * Generate a Quote * * POST /quotes/generate */ export const createApiQuotesGenerate = async (args: CreateApiQuotesGenerateArgs, options?: CreateApiQuotesGenerateOptions) => { const {baseUrl = "https://sandboxapi.fincra.com", fetch: fetchFn = fetch, sec0} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (sec0 !== undefined) headers.set("api-key", sec0) const url = `${baseUrl.replace(/\/+$/, '')}/quotes/generate${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiQuotesGenerateResponse, await res.json()) };