import {z} from 'zod' export type GetApiV1GlobalPayoutExchangeRatesArgs = {Authorization: string, accountId: string, from: string, to: string, region?: string | undefined}; export type GetApiV1GlobalPayoutExchangeRatesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1GlobalPayoutExchangeRatesResponse = z.object({code: z.string().optional(), description: z.string().optional(), data: z.object({rates: z.array(z.object({exchangeRateId: z.string().optional(), currencyPairName: z.string().optional(), bidRate: z.string().optional(), askRate: z.string().optional(), midRate: z.string().optional(), createdAt: z.string().optional()})).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 }; /** * Retrieve the latest exchange rates for a currency pair before initiating a transfer. * * GET /v1/global-payout/exchange-rates */ export const getApiV1GlobalPayoutExchangeRates = async (args: GetApiV1GlobalPayoutExchangeRatesArgs, options?: GetApiV1GlobalPayoutExchangeRatesOptions) => { const {baseUrl = "https://api.nomba.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() 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)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.region != null) query.append("region", String(args.region)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/global-payout/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(getApiV1GlobalPayoutExchangeRatesResponse, await res.json()) };