import {z} from 'zod' export type CreateApiStablecoinsPaymentsArgs = {accountId: string, body: {budgetId: string, remitterId: string, amount: number, displayName?: (string | null) | undefined, sourceCurrencyCode: string, destinationCurrencyCode: string, returnUrl?: (string | null) | undefined, requesterIpAddress?: string | undefined, expiryMinutes?: number | undefined}}; export type CreateApiStablecoinsPaymentsOptions = {baseUrl?: string | undefined, CommonAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiStablecoinsPaymentsResponse = z.object({id: z.string(), accountId: z.string(), budgetId: z.string().max(36), status: z.enum(["PENDING", "PROCESSING", "CANCELLED", "COMPLETE", "UNDERPAID", "OVERPAID", "EXPIRED"]), remitterId: z.string(), amount: z.number().gt(0), displayName: z.string().min(1).max(255).nullable().optional(), sourceCurrencyCode: z.string().min(3).max(4), destinationCurrencyCode: z.string().min(3).max(4), returnUrl: z.string().nullable().optional(), link: z.object({url: z.string(), chains: z.array(z.object({protocol: z.string().min(1).max(255), address: z.string().min(1).max(255)}))}).optional(), expiresAt: z.string().optional(), createdBy: z.string().min(1).max(255), createdAt: z.string(), updatedAt: z.string()}); 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 }; /** * Create a new stablecoin inbound payment * * POST /stablecoins/payments */ export const createApiStablecoinsPayments = async (args: CreateApiStablecoinsPaymentsArgs, options?: CreateApiStablecoinsPaymentsOptions) => { const {baseUrl = "https://api.equalsmoney.com/v2", fetch: fetchFn = fetch, CommonAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.accountId != null) query.append("accountId", String(args.accountId)) if (CommonAuth !== undefined) headers.set("Authorization", CommonAuth) const url = `${baseUrl.replace(/\/+$/, '')}/stablecoins/payments${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(createApiStablecoinsPaymentsResponse, await res.json()) };