import {z} from 'zod' export type ForexProduct = 'card_payouts' | 'daily_acquiring' | 'scheme_acquiring'; export type ForexSource = 'mastercard' | 'visa'; export type ForexProcessingChannelId = string; export type GetApiForexRatesArgs = {product: ForexProduct, source: ForexSource, currency_pairs: string, processing_channel_id: ForexProcessingChannelId}; export type GetApiForexRatesOptions = {baseUrl: string, OAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const forexProduct = z.enum(["card_payouts", "daily_acquiring", "scheme_acquiring"]); export const forexSource = z.enum(["mastercard", "visa"]); export const timestamp = z.string(); export const forexRate = z.object({exchange_rate: z.number().optional(), currency_pair: z.string().optional(), valid_from: timestamp.optional()}); export const forexRatesResponse = z.object({product: forexProduct.optional(), source: forexSource.optional(), rates: z.array(forexRate).optional(), invalid_currency_pairs: z.array(z.string()).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 FX rates * * GET /forex/rates */ export const getApiForexRates = async (args: GetApiForexRatesArgs, options: GetApiForexRatesOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.product != null) query.append("product", String(args.product)) if (args.source != null) query.append("source", String(args.source)) if (args.currency_pairs != null) query.append("currency_pairs", String(args.currency_pairs)) if (args.processing_channel_id != null) query.append("processing_channel_id", String(args.processing_channel_id)) if (OAuth !== undefined) headers.set('authorization', `Bearer ${OAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/forex/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(forexRatesResponse, await res.json()) };