import {z} from 'zod' export type GetApiPaymentsBrCustomersArgs = {'X-Belvo-API-Resource-Version'?: 'Payments-BR.V2' | undefined, 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, customer__type?: ('INDIVIDUAL' | 'BUSINESS') | undefined, external_id?: string | undefined, external_id__in?: Array | undefined, search?: string | undefined}; export type GetApiPaymentsBrCustomersOptions = {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 updatedAt = z.string(); export const externalIdPayments = z.string(); export const customerV2 = z.object({id: id, created_at: createdAt, updated_at: updatedAt, name: z.string().max(120), identifier: z.string().min(11).max(14), external_id: externalIdPayments.optional()}); export const createdBy = z.string(); export const enumCustomerType = z.enum(["INDIVIDUAL", "BUSINESS"]).min(8).max(10); export const enumPaymentsCountry = z.enum(["BRA", "null"]).max(3).nullable(); export const enumCustomerIdentifierTypeOfpi = z.enum(["CPF", "CNPJ"]); export const customerOfpi = z.object({id: id, created_at: createdAt, created_by: createdBy, customer_type: enumCustomerType, name: z.string().min(5).max(200).nullable(), external_id: externalIdPayments.optional(), country: enumPaymentsCountry, email: z.string().nullable(), identifier: z.string().min(11).max(14), identifier_type: enumCustomerIdentifierTypeOfpi, address: z.string().min(5).max(150).nullable().optional(), phone: z.string().nullable().optional()}); export const getApiPaymentsBrCustomersResponse = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(z.union([customerV2, customerOfpi])).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 customers * * GET /payments/br/customers/ */ export const getApiPaymentsBrCustomers = async (args: GetApiPaymentsBrCustomersArgs, options?: GetApiPaymentsBrCustomersOptions) => { 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['X-Belvo-API-Resource-Version'] != null) headers.set("X-Belvo-API-Resource-Version", String(args['X-Belvo-API-Resource-Version'])) 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.customer__type != null) query.append("customer__type", String(args.customer__type)) 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.search != null) query.append("search", String(args.search)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments/br/customers/${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(getApiPaymentsBrCustomersResponse, await res.json()) };