import {z} from 'zod' export type CreateApiTransfersIdCancelArgs = {id: string}; export type CreateApiTransfersIdCancelOptions = {baseUrl?: string | undefined, secretKey?: string | undefined, fetch?: typeof fetch | undefined}; export const transferStatus = z.enum(["pending", "in_review", "processing", "sent", "unclaimed", "completed", "failed", "cancelled"]); export const transferBankReferenceStatus = z.enum(["available", "pending", "unsupported"]); export const transferDestinationType = z.enum(["bank_account", "account", "phone_number", "crypto_address"]); export const transferDestination = z.object({type: transferDestinationType.optional(), id: z.string().nullable().optional(), bank_name: z.string().optional(), holder_name: z.string().optional(), currency: z.string().optional(), name: z.string().optional(), number: z.string().optional(), address: z.string().optional(), chain: z.string().optional(), amount: z.number().int().optional()}); export const transferSender = z.object({id: z.string().optional(), name: z.string().optional(), type: z.string().optional()}); export const transferRecipient = z.object({id: z.string().nullable().optional(), name: z.string().optional(), type: z.string().optional()}); export const transfer = z.object({id: z.string().optional(), status: transferStatus.optional(), status_detail: z.string().optional(), amount_in_cents: z.number().int().optional(), currency: z.string().optional(), fee_in_cents: z.number().int().optional(), net_amount_in_cents: z.number().int().optional(), note: z.string().nullable().optional(), recipient_email: z.string().nullable().optional(), tracking_url: z.string().nullable().optional(), account_id: z.string().optional(), sent_at: z.string().nullable().optional(), settled_at: z.string().nullable().optional(), bank_reference: z.string().nullable().optional(), bank_reference_status: transferBankReferenceStatus.nullable().optional(), statement_descriptor: z.string().nullable().optional(), balance_transaction_id: z.string().nullable().optional(), destination: transferDestination.optional(), created_at: z.string().optional(), sender: transferSender.optional(), recipient: transferRecipient.optional(), reversal_of_id: z.string().optional(), refund_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 }; /** * Cancelar un movimiento * * POST /transfers/{id}/cancel */ export const createApiTransfersIdCancel = async (args: CreateApiTransfersIdCancelArgs, options?: CreateApiTransfersIdCancelOptions) => { const {baseUrl = "https://app.recurrente.com/api", fetch: fetchFn = fetch, secretKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (secretKey !== undefined) headers.set("X-SECRET-KEY", secretKey) const url = `${baseUrl.replace(/\/+$/, '')}/transfers/${encodeURIComponent(String(args.id))}/cancel${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(transfer, await res.json()) };