import {z} from 'zod' export type Currency = 'USD' | 'CAD' | 'EUR' | 'JPY' | 'GBP' | 'KRW' | 'CHF' | 'SEK' | 'SGD' | 'MYR' | 'AUD' | 'MXN' | 'INR' | 'BRL' | 'DKK' | 'NZD' | 'NOK' | 'ZAR' | 'PHP' | 'CNY' | 'SAR' | 'GTQ' | 'IDR' | 'ARS' | 'COP' | 'PEN'; export type CurrencyExchangeRateRequest = {baseCurrency: Currency, counterCurrency: Currency, rate: number}; export type CreateApiChannelV1ExchangeRatesArgs = {body: CurrencyExchangeRateRequest}; export type CreateApiChannelV1ExchangeRatesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const currency = z.enum(["USD", "CAD", "EUR", "JPY", "GBP", "KRW", "CHF", "SEK", "SGD", "MYR", "AUD", "MXN", "INR", "BRL", "DKK", "NZD", "NOK", "ZAR", "PHP", "CNY", "SAR", "GTQ", "IDR", "ARS", "COP", "PEN"]); export const currencyExchangeRateResponse = z.object({baseCurrency: currency.optional(), counterCurrency: currency.optional(), rate: z.number().optional(), validFrom: z.number().optional(), active: z.boolean().optional(), authenticationType: z.string().optional(), oauthKey: z.string().optional(), userId: z.number().optional(), currencyExchangeRateUuid: 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 }; /** * Create new Currency Exchange Rate * * POST /channel/v1/exchangeRates */ export const createApiChannelV1ExchangeRates = async (args: CreateApiChannelV1ExchangeRatesArgs, options?: CreateApiChannelV1ExchangeRatesOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/channel/v1/exchangeRates${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(currencyExchangeRateResponse, await res.json()) };