import {z} from 'zod' export type GetApiQuotesFiatCryptoArgs = {Authorization?: string | undefined, amount?: number | undefined, paymentMethod?: string | undefined, uuid?: string | undefined, clientName?: string | undefined, type?: string | undefined, walletAddress?: string | undefined, isRecurringPayment?: boolean | undefined, country?: string | undefined, txInitiation?: boolean | undefined, platform?: ('web' | 'mobile-ios' | 'mobile-android' | 'webview-ios' | 'webview-android' | 'browser-extension' | 'desktop') | undefined, fiat: string, crypto: string}; export type GetApiQuotesFiatCryptoOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiQuotesFiatCryptoResponse = z.object({}); 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 Buy Quotes * * GET /quotes/{fiat}/{crypto} */ export const getApiQuotesFiatCrypto = async (args: GetApiQuotesFiatCryptoArgs, options?: GetApiQuotesFiatCryptoOptions) => { const {baseUrl = "https://api.onramper.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.amount != null) query.append("amount", String(args.amount)) if (args.paymentMethod != null) query.append("paymentMethod", String(args.paymentMethod)) if (args.uuid != null) query.append("uuid", String(args.uuid)) if (args.clientName != null) query.append("clientName", String(args.clientName)) if (args.type != null) query.append("type", String(args.type)) if (args.walletAddress != null) query.append("walletAddress", String(args.walletAddress)) if (args.isRecurringPayment != null) query.append("isRecurringPayment", String(args.isRecurringPayment)) if (args.country != null) query.append("country", String(args.country)) if (args.txInitiation != null) query.append("txInitiation", String(args.txInitiation)) if (args.platform != null) query.append("platform", String(args.platform)) const url = `${baseUrl.replace(/\/+$/, '')}/quotes/${encodeURIComponent(String(args.fiat))}/${encodeURIComponent(String(args.crypto))}${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(getApiQuotesFiatCryptoResponse, await res.json()) };