import {z} from 'zod' export type CreateApiV1GlobalPayoutMoneyConvertArgs = {Authorization: string, accountId: string, body: {amount: number, currency: string, destinationCurrency: string, transactionType: string}}; export type CreateApiV1GlobalPayoutMoneyConvertOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1GlobalPayoutMoneyConvertResponse = z.object({code: z.string().optional(), description: z.string().optional(), data: z.object({fromAmount: z.number().optional(), fromCurrency: z.string().optional(), fromFormatted: z.string().optional(), toAmount: z.number().optional(), toCurrency: z.string().optional(), toFormatted: z.string().optional(), spreadAmount: z.number().optional(), spreadCurrency: z.string().optional(), exchangeRateId: z.string().optional(), currencyPairName: z.string().optional(), feeAmount: z.number().optional(), feeCurrency: 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 }; /** * Calculate a currency conversion and lock an exchange rate before initiating a transfer. * * POST /v1/global-payout/money/convert */ export const createApiV1GlobalPayoutMoneyConvert = async (args: CreateApiV1GlobalPayoutMoneyConvertArgs, options?: CreateApiV1GlobalPayoutMoneyConvertOptions) => { const {baseUrl = "https://api.nomba.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.accountId != null) headers.set("accountId", String(args.accountId)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/global-payout/money/convert${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(createApiV1GlobalPayoutMoneyConvertResponse, await res.json()) };