import {z} from 'zod' export type GetApiQuotesArgs = {page?: number | undefined, size?: number | undefined}; export type GetApiQuotesOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const fXRate = z.object({rate: z.number(), base_currency_code: z.string(), counter_currency_code: z.string(), type: z.string()}); export const quoteElement = z.object({quote_element_id: z.string(), quote_element_type: z.enum(["TRANSFER", "EXCHANGE", "EXCHANGE_TRADE", "CRYPTO_TRANSFER"]), quote_element_order: z.string(), sender_address: z.string(), receiver_address: z.string(), sending_amount: z.number(), receiving_amount: z.number(), sending_fee: z.number(), receiving_fee: z.number(), sending_currency_code: z.string().optional(), receiving_currency_code: z.string().optional(), fx_rate: fXRate.optional(), transfer_currency_code: z.string().optional()}); export const quote = z.object({quote_id: z.string(), created_at: z.string(), expires_at: z.string(), type: z.enum(["SENDER_AMOUNT", "RECEIVER_AMOUNT", "SENDER_INSTITUTION_AMOUNT", "RECEIVER_INSTITUTION_AMOUNT", "REVERSAL_AMOUNT"]), price_guarantee: z.string(), sender_address: z.string(), receiver_address: z.string(), amount: z.string(), currency_code: z.string(), currency_code_filter: z.string(), service_type: z.string(), quote_elements: z.array(quoteElement), liquidity_warning: z.string(), payment_method: z.string().optional(), payment_method_fields: z.string().optional(), payout_method_info: z.object({payout_method_name: z.string().optional(), payout_method_category: z.enum(["REAL_TIME_GROSS_SETTLEMENT_SYSTEM", "REAL_TIME_NET_SETTLEMENT_SYSTEM", "MASS_NET_PAYMENT_SYSTEM", "BOOK_TRANSFER", "CASH_PAYOUT", "WALLET_PAYMENT", "OTHER"]).optional(), description: z.string().optional(), estimated_time_to_credit: z.string().optional()}).optional()}); export const getApiQuotesResponse = z.array(quote); 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 quotes * * GET /quotes */ export const getApiQuotes = async (args: GetApiQuotesArgs, options: GetApiQuotesOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/quotes${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(getApiQuotesResponse, await res.json()) };