import {z} from 'zod' export type GetApiApiV1TransfersArgs = {status: 'CREATED' | 'UNKNOWN' | 'COMPLIANCE_HOLD' | 'AWAITING_FUNDS_COLLECTION' | 'FUNDS_COLLECTED' | 'IN_PROGRESS' | 'PAYMENT_SUBMITTED' | 'PAYMENT_PROCESSED' | 'COMPLETED' | 'FAILED' | 'CANCELLED' | 'EXPIRED', type: 'TRANSFER_TYPE_OFFRAMP' | 'TRANSFER_TYPE_ONRAMP', customer_id?: string | undefined, page_size?: number | undefined, page_token?: string | undefined}; export type GetApiApiV1TransfersOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const metadata = z.record(z.string(), z.string()); export const addressApiOutput = z.object({street: z.string(), city: z.string(), state: z.string(), postal_code: z.string(), country: z.string()}); export const transferResponse = z.object({id: z.string(), tenant_id: z.string(), customer_id: z.string(), quote_id: z.string(), type: z.enum(["offramp", "onramp"]), status: z.enum(["created", "unknown", "compliance_hold", "awaiting_funds_collection", "funds_collected", "in_progress", "payment_submitted", "payment_processed", "completed", "failed", "cancelled", "expired"]), created_at: z.string(), updated_at: z.string(), metadata: metadata.optional(), source_deposit_instructions: z.union([z.object({wallet_address: z.string(), currency: z.string(), network: z.string(), amount: z.string()}), z.object({bank_name: z.string().optional(), account_number: z.string().optional(), iban: z.string().optional(), bic: z.string().optional(), payment_rails: z.array(z.string()).optional(), bsb: z.string().optional(), account_name: z.string().optional(), reference: z.string().optional(), currency: z.string(), amount: z.string()})]).nullable().optional(), destination: z.union([z.object({amount: z.string(), currency: z.string(), network: z.string(), wallet_address: z.string()}), z.object({amount: z.string(), currency: z.string(), network: z.string(), account_holder_name: z.string().optional(), account_number: z.string().optional(), iban: z.string().optional(), bank_name: z.string().optional(), bank_country: z.string().min(2).max(2).optional(), account_type: z.enum(["savings", "checking", "payment"]).optional(), branch_name: z.string().optional(), address: addressApiOutput.optional(), swift_code: z.string().optional(), bic_code: z.string().optional(), ifsc_code: z.string().optional(), aba_code: z.string().optional(), sort_code: z.string().optional(), branch_code: z.string().optional(), bsb_code: z.string().optional(), bank_code: z.string().optional(), cnaps: z.string().optional(), name_in_local_language: z.string().optional(), national_identification_number: z.string().optional(), logistics_tracking_details: z.array(z.object({tracking_number: z.string().optional(), logistics_provider: z.object({name: z.string().optional(), code: z.string().optional()}).optional()})).optional(), documents: z.array(z.object({type: z.enum(["invoice", "contract", "other"]).optional()})).optional()})]).nullable().optional(), fees: z.object({fx_fee: z.object({amount: z.string(), currency: z.string()}).optional(), integrator_fee: z.object({amount: z.string(), currency: z.string()}).optional(), platform_fee: z.object({amount: z.string(), currency: z.string()}).optional(), payment_method_fee: z.object({amount: z.string(), currency: z.string()}).optional(), network_fee: z.object({amount: z.string(), currency: z.string()}).optional(), total_fee: z.object({amount: z.string(), currency: z.string()}).optional()}).nullable().optional(), exchange_rate: z.number().nullable().optional(), origin: z.enum(["api", "otc"])}); export const getApiApiV1TransfersResponse = z.object({transfers: z.array(transferResponse), page: z.object({next_page_token: z.string(), total: z.number().int().gte(-9007199254740991).lte(9007199254740991)})}); 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 }; /** * List all transfers for a tenant with optional filtering by status, type, and customer_id * * GET /api/v1/transfers * * Also accepts auth: apiKeyAuth */ export const getApiApiV1Transfers = async (args: GetApiApiV1TransfersArgs, options?: GetApiApiV1TransfersOptions) => { const {baseUrl = "https://api.stables.money", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.type != null) query.append("type", String(args.type)) if (args.customer_id != null) query.append("customer_id", String(args.customer_id)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.page_token != null) query.append("page_token", String(args.page_token)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/transfers${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiApiV1TransfersResponse, await res.json()) };