import {z} from 'zod' export type GetApiPaymentsBrEnrollmentsArgs = {customer__identifier?: string | undefined, customer?: string | undefined, external_id?: string | undefined, external_id__in?: Array | undefined, institution?: string | undefined, page?: number | undefined, page_size?: number | undefined, status?: string | undefined, status__in?: Array | undefined}; export type GetApiPaymentsBrEnrollmentsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const externalIdPayments = z.string(); export const enrollmentBiometricPix = z.object({id: z.string().optional(), created_by: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), external_id: externalIdPayments.optional(), type: z.literal("open_finance_biometric_pix").optional(), status: z.enum(["PENDING", "SUCCEEDED", "CANCELED", "FAILED"]).optional(), status_reason_code: z.string().nullable().optional(), status_reason_message: z.string().nullable().optional(), details: z.object({status: z.enum(["AWAITING_ACCOUNT_HOLDER_VALIDATION", "AWAITING_ENROLLMENT", "AUTHORIZED", "REJECTED", "REVOKED"]).optional(), customer: z.string().optional(), institution: z.string().optional(), platform: z.enum(["ANDROID", "BROWSER", "CROSS_PLATFORM", "IOS"]).optional(), name: z.string().nullable().optional(), callback_url: z.string().optional(), redirect_url: z.string().optional(), risk_signals: z.string().optional(), expires_at: z.string().optional()}).optional(), metadata: z.object({}).optional()}); export const getApiPaymentsBrEnrollmentsResponse = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(enrollmentBiometricPix).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 enrollments * * GET /payments/br/enrollments/ */ export const getApiPaymentsBrEnrollments = async (args: GetApiPaymentsBrEnrollmentsArgs, options?: GetApiPaymentsBrEnrollmentsOptions) => { 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.customer__identifier != null) query.append("customer__identifier", String(args.customer__identifier)) if (args.customer != null) query.append("customer", String(args.customer)) if (args.external_id != null) query.append("external_id", String(args.external_id)) if (args.external_id__in != null) query.append("external_id__in", args.external_id__in.join(',')) if (args.institution != null) query.append("institution", String(args.institution)) 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.status != null) query.append("status", String(args.status)) if (args.status__in != null) query.append("status__in", args.status__in.join(',')) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments/br/enrollments/${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(getApiPaymentsBrEnrollmentsResponse, await res.json()) };