import {z} from 'zod' export type CreateApiCheckoutV2IntentArgs = {Authorization?: string | undefined, 'x-onramper-signature': string, 'x-onramper-timestamp': string, 'x-onramper-nonce': string, body: {onramp: string, source: string, destination: string, amount: number, type: 'buy' | 'sell', paymentMethod: string, network?: string | undefined, endUserIpHash: string, clientIpHash?: string | undefined, platform?: ('web' | 'mobile-ios' | 'mobile-android' | 'webview-ios' | 'webview-android' | 'browser-extension' | 'desktop') | undefined, wallet?: ({address: string, memo?: string | undefined}) | undefined, country?: string | undefined, subdivision?: string | undefined, email?: string | undefined, partnerContext?: string | undefined, externalCustomerId?: string | undefined, uuid?: string | undefined, originatingHost?: string | undefined, allowOnrampPmSelection?: boolean | undefined, targetContractAddress?: string | undefined, supportedParams?: {[key: string]: unknown} | undefined, metaData?: {[key: string]: unknown} | undefined} | {[key: string]: unknown}}; export type CreateApiCheckoutV2IntentOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiCheckoutV2IntentResponse = z.object({redirectUrl: z.string(), sessionId: z.string(), resumeToken: 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 }; /** * Initiate a Transaction (V2) * * POST /checkout/v2/intent */ export const createApiCheckoutV2Intent = async (args: CreateApiCheckoutV2IntentArgs, options?: CreateApiCheckoutV2IntentOptions) => { const {baseUrl = "https://api.onramper.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args['x-onramper-signature'] != null) headers.set("x-onramper-signature", String(args['x-onramper-signature'])) if (args['x-onramper-timestamp'] != null) headers.set("x-onramper-timestamp", String(args['x-onramper-timestamp'])) if (args['x-onramper-nonce'] != null) headers.set("x-onramper-nonce", String(args['x-onramper-nonce'])) const url = `${baseUrl.replace(/\/+$/, '')}/checkout/v2/intent${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(createApiCheckoutV2IntentResponse, await res.json()) };