import {z} from 'zod' export type GetApiPaymentsBrInstitutionsArgs = {page?: number | undefined, page_size?: number | undefined, name?: string | undefined, institution_type?: ('INDIVIDUAL' | 'BUSINESS') | undefined, id__in?: Array | undefined}; export type GetApiPaymentsBrInstitutionsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const paymentInstitutionBR = z.object({id: z.string(), code: z.string().optional(), active: z.boolean(), institution_type: z.enum(["BUSINESS", "INDIVIDUAL"]).optional(), name: z.string(), display_name: z.string(), legal_entity_name: z.string().nullable(), website: z.string(), logo: z.string(), icon_logo: z.string(), text_logo: z.string(), primary_color: z.string(), country: z.string().max(3), form_fields: z.array(z.unknown()).nullable(), sort_priority: z.number().int(), description: z.string().nullable()}); export const getApiPaymentsBrInstitutionsResponse = z.object({count: z.number().int().optional(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(paymentInstitutionBR).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 institutions * * GET /payments/br/institutions/ */ export const getApiPaymentsBrInstitutions = async (args: GetApiPaymentsBrInstitutionsArgs, options?: GetApiPaymentsBrInstitutionsOptions) => { 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.name != null) query.append("name", String(args.name)) if (args.institution_type != null) query.append("institution_type", String(args.institution_type)) if (args.id__in != null) query.append("id__in", args.id__in.join(',')) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/payments/br/institutions/${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(getApiPaymentsBrInstitutionsResponse, await res.json()) };