import {z} from 'zod' export type CurrencyCode = unknown; export type Percentage = unknown; export type QuoteItem = {base_currency?: CurrencyCode | undefined, base_amount?: string | undefined, quote_currency?: CurrencyCode | undefined, markup_percent?: Percentage | undefined, fx_id?: string | undefined}; export type QuoteItemList = Array; export type QuoteExchangeRatesRequest = {organization?: string | undefined, quote_items: QuoteItemList}; export type CreateApiV2PricingQuoteExchangeRatesArgs = {body: QuoteExchangeRatesRequest}; export type CreateApiV2PricingQuoteExchangeRatesOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, fetch?: typeof fetch | undefined}; export const currencyCode = z.string().min(3).max(3); export const money = z.object({currency_code: currencyCode, value: z.string().max(32)}); export const dateTime = z.string().min(20).max(64); export const rateDetail = z.object({base_amount: money, quote_amount: money, exchange_rate: z.string().min(1).max(32), fx_id: z.string().min(1).max(4000).optional(), expiry_time: dateTime.optional(), rate_refresh_time: dateTime.optional(), update_time: dateTime.optional()}); export const rateDetailList = z.array(rateDetail); export const quoteExchangeRatesResponse = z.object({exchange_rate_quotes: rateDetailList}); 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 Exchange Rate Quote * * POST /v2/pricing/quote-exchange-rates */ export const createApiV2PricingQuoteExchangeRates = async (args: CreateApiV2PricingQuoteExchangeRatesArgs, options?: CreateApiV2PricingQuoteExchangeRatesOptions) => { const {baseUrl = "https://api-m.paypal.com", fetch: fetchFn = fetch, Oauth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/pricing/quote-exchange-rates${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(quoteExchangeRatesResponse, await res.json()) };