import {z} from 'zod' export type UpdateApiV2PaymentsArgs = {'x-idempotency-key': string, body: {from: string, to: Array<{to: string, amount_cents: number, currency_symbol?: 'USD' | undefined, invoice_description?: string | undefined}>, pay_now: boolean, network?: 'arbitrum' | undefined, signer: string, typed_data: {from: string, to: string, salt: string, expires: string, data: {groupID: string, merkleRoot: string, paymentsCount: string, maxAmountPerPayment: string, deadline: string}} | {from: string, to: string, salt: string, expires: string, data: {groupID: string, token: string, payments: Array<{id: string, recipient: string, amount: string}>}}, signature: string, payment_data: {checksum: string}}}; export type UpdateApiV2PaymentsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const updateApiV2PaymentsResponse = z.object({success: z.boolean(), data: z.object({transaction: z.string().min(15).max(15)})}); 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 }; /** * Execute Payments * * PUT /v2/payments */ export const updateApiV2Payments = async (args: UpdateApiV2PaymentsArgs, options?: UpdateApiV2PaymentsOptions) => { const {baseUrl = "https://b2b-api.dev-riseworks.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-idempotency-key'] != null) headers.set("x-idempotency-key", String(args['x-idempotency-key'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/payments${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV2PaymentsResponse, await res.json()) };