import {z} from 'zod' export type GetApiPaymentsBrTransactionsArgs = {page?: number | undefined, page_size?: number | undefined, id__in?: Array | undefined, created_at?: string | undefined, created_at__gt?: string | undefined, created_at__gte?: string | undefined, created_at__lt?: string | undefined, created_at__lte?: string | undefined, created_at__range?: Array | undefined, charge?: string | undefined, charge__in?: Array | undefined, beneficiary?: string | undefined, beneficiary__in?: Array | undefined, payer?: string | undefined, payer__in?: Array | undefined, customer?: string | undefined, customer__in?: Array | undefined, payment_intent?: string | undefined, payment_intent__in?: Array | undefined, transaction__type?: ('INFLOW' | 'OUTFLOW') | undefined, currency?: string | undefined, description?: string | undefined, amount?: number | undefined, amount__gt?: number | undefined, amount__gte?: number | undefined, amount__lt?: number | undefined, amount__lte?: number | undefined, amount__range?: Array | undefined}; export type GetApiPaymentsBrTransactionsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const id = z.string(); export const createdAt = z.string(); export const createdBy = z.string(); export const enumPaymentsCurrency = z.literal("BRL"); export const enumPaymentTransactionType = z.enum(["INFLOW", "OUTFLOW"]); export const paymentTransaction = z.object({id: id, created_at: createdAt, created_by: createdBy, amount: z.string(), currency: enumPaymentsCurrency, description: z.string().max(128), transaction_type: enumPaymentTransactionType, beneficiary: z.string(), payer: z.unknown(), payment_intent: z.string().optional(), customer: z.string().optional()}); export const getApiPaymentsBrTransactionsResponse = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(paymentTransaction).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 }; /** * List all payment transactions * * GET /payments/br/transactions/ */ export const getApiPaymentsBrTransactions = async (args: GetApiPaymentsBrTransactionsArgs, options?: GetApiPaymentsBrTransactionsOptions) => { const {baseUrl = "https://sandbox.belvo.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.id__in != null) query.append("id__in", args.id__in.join(',')) if (args.created_at != null) query.append("created_at", String(args.created_at)) if (args.created_at__gt != null) query.append("created_at__gt", String(args.created_at__gt)) if (args.created_at__gte != null) query.append("created_at__gte", String(args.created_at__gte)) if (args.created_at__lt != null) query.append("created_at__lt", String(args.created_at__lt)) if (args.created_at__lte != null) query.append("created_at__lte", String(args.created_at__lte)) if (args.created_at__range != null) query.append("created_at__range", args.created_at__range.join(',')) if (args.charge != null) query.append("charge", String(args.charge)) if (args.charge__in != null) query.append("charge__in", args.charge__in.join(',')) if (args.beneficiary != null) query.append("beneficiary", String(args.beneficiary)) if (args.beneficiary__in != null) query.append("beneficiary__in", args.beneficiary__in.join(',')) if (args.payer != null) query.append("payer", String(args.payer)) if (args.payer__in != null) query.append("payer__in", args.payer__in.join(',')) if (args.customer != null) query.append("customer", String(args.customer)) if (args.customer__in != null) query.append("customer__in", args.customer__in.join(',')) if (args.payment_intent != null) query.append("payment_intent", String(args.payment_intent)) if (args.payment_intent__in != null) query.append("payment_intent__in", args.payment_intent__in.join(',')) if (args.transaction__type != null) query.append("transaction__type", String(args.transaction__type)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.description != null) query.append("description", String(args.description)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.amount__gt != null) query.append("amount__gt", String(args.amount__gt)) if (args.amount__gte != null) query.append("amount__gte", String(args.amount__gte)) if (args.amount__lt != null) query.append("amount__lt", String(args.amount__lt)) if (args.amount__lte != null) query.append("amount__lte", String(args.amount__lte)) if (args.amount__range != null) query.append("amount__range", args.amount__range.join(',')) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments/br/transactions/${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(getApiPaymentsBrTransactionsResponse, await res.json()) };