import {z} from 'zod' export type RippleNetNodeRequestForPaymentRequest = {sender_address: string, receiver_address: string, amount: number, currency: string, quote_type: 'SENDER_AMOUNT' | 'RECEIVER_AMOUNT' | 'SENDER_INSTITUTION_AMOUNT' | 'RECEIVER_INSTITUTION_AMOUNT', end_to_end_id?: string | undefined, user_info?: Record | undefined}; export type CreateApiRequestForPaymentArgs = {body: RippleNetNodeRequestForPaymentRequest}; export type CreateApiRequestForPaymentOptions = {baseUrl: string, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const userInfoEntry = z.object({json: z.object({}), created_at: z.string(), subState: z.string().optional()}); export const rippleNetNodeRequestForPaymentUserInfo = z.object({node_address: z.string(), requested: z.array(userInfoEntry).optional(), accepted: z.array(userInfoEntry).optional(), failed: z.array(userInfoEntry).optional()}); export const rippleNetNodeRequestForPayment = z.object({request_for_payment_id: z.string(), request_for_payment_state: z.enum(["REQUESTED", "ACCEPTED", "FAILED"]), sender_address: z.string(), receiver_address: z.string(), amount: z.number(), currency: z.string(), quote_type: z.enum(["SENDER_AMOUNT", "RECEIVER_AMOUNT", "SENDER_INSTITUTION_AMOUNT", "RECEIVER_INSTITUTION_AMOUNT"]), end_to_end_id: z.string().optional(), user_info: z.array(rippleNetNodeRequestForPaymentUserInfo), created_at: z.string(), payment_id: 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 }; /** * Send request for payment * * POST /request_for_payment */ export const createApiRequestForPayment = async (args: CreateApiRequestForPaymentArgs, options: CreateApiRequestForPaymentOptions) => { const {baseUrl, fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Bearer !== undefined) headers.set("Authorization", Bearer) const url = `${baseUrl.replace(/\/+$/, '')}/request_for_payment${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(rippleNetNodeRequestForPayment, await res.json()) };