import {z} from 'zod' export type ProductType = 'LOAN' | 'CARD' | 'REFINANCING_AUTO' | 'REFINANCING_HOME' | 'FINANCING_AUTO' | 'CAAS' | 'WORKING_CAPITAL' | 'DEBT'; export type ProductLoan = {type?: ProductType | undefined, value?: number | undefined, installments?: number | undefined}; export type Network = 'VISA' | 'MASTERCARD' | 'ELO'; export type ProductCard = {type?: ProductType | undefined, network?: Network | undefined, payDay?: string | undefined}; export type ProductAuto = {type?: ProductType | undefined, value?: number | undefined, vehicleBrand?: string | undefined, vehicleModel?: string | undefined, installments?: number | undefined, vehicleModelYear?: string | undefined, codeFipe?: string | undefined, vehicleFipeValue?: number | undefined}; export type RealEstateType = 'house' | 'apartment' | 'commercial' | 'land' | 'others'; export type ProductHome = {type?: ProductType | undefined, value?: number | undefined, installments?: number | undefined, realEstateType?: RealEstateType | undefined, realEstateValue?: number | undefined, outstandingBalance?: number | undefined}; export type ProductFinancingAuto = {type?: ProductType | undefined, value?: number | undefined, vehicleBrand?: string | undefined, vehicleModel?: string | undefined, installments?: number | undefined, vehicleModelYear?: string | undefined, codeFipe?: string | undefined, vehicleFipeValue?: number | undefined, downPayment?: number | undefined, used?: boolean | undefined}; export type ProductCaas = {type?: ProductType | undefined, release_date?: string | undefined, due_date?: string | undefined, installments?: number | undefined, monthlyTax?: number | undefined, cadValue?: number | undefined, value?: number | undefined}; export type BusinessProfession = 'SOCIO_DONO' | 'ADMINISTRADOR_DIRETOR' | 'PROCURADOR' | 'OUTROS'; export type EmployeesCount = 'NENHUM' | 'DE_1_A_5' | 'DE_6_A_10' | 'DE_11_A_20' | 'DE_21_A_50' | 'DE_51_A_100' | 'MAIS_DE_100'; export type WorkingCapitalLoanObjectives = 'CAPITAL_DE_GIRO' | 'RENEGOCIACAO_DE_DIVIDAS' | 'MARKETING_E_VENDAS' | 'EXPANSAO_DE_NEGOCIOS' | 'EXPANSAO' | 'ESTOQUE' | 'REFORMA' | 'USO_PESSOAL'; export type Banks = string; export type AccountType = 'CONTA_CORRENTE_INDIVIDUAL' | 'CONTA_CORRENTE_CONJUNTA' | 'CONTA_POUPANCA_CONJUNTA' | 'CONTA_POUPANCA_INDIVIDUAL'; export type ProductWorkingCapital = {type?: ProductType | undefined, installments?: number | undefined, value?: number | undefined, cnpj?: string | undefined, businessProfession?: BusinessProfession | undefined, employeesCount?: EmployeesCount | undefined, businessIncomeCnpj?: number | undefined, loanObjectives?: WorkingCapitalLoanObjectives | undefined, bank?: Banks | undefined, accountType?: AccountType | undefined, agency?: string | undefined, account?: string | undefined}; export type ProductDebt = {type?: ProductType | undefined}; export type Products = ProductLoan | ProductCard | ProductAuto | ProductHome | ProductFinancingAuto | ProductCaas | ProductWorkingCapital | ProductDebt; export type Tags = Array; export type OperationVars = Array<{Key?: string | undefined, Value?: string | undefined}>; export type CreateApiGenerateBoletoArgs = {body: {Method: string, MktPlaceId: number, PartnerId: number, BusinessUnitId: number, GroupTemplate: string, CustomerName: string, CustomerTaxNumber: string, CustomerMail: string, CustomerPhone: string, Neighborhood: string, City: string, State: string, ZipCode: string, AddressLine1: string, AddressLine2?: string | undefined, MailToSend: string, PhoneToSend?: string | undefined, SupplierTaxNumber: string, SupplierFullName: string, SupplierTradingName: string, SupplierLegalName: string, SupplierBank?: string | undefined, SupplierBankBranch?: string | undefined, SupplierBankAccount?: string | undefined, SupplierBankAccountDigit?: string | undefined, SupplierMail?: string | undefined, SupplierPhone?: string | undefined, RateValue: number, RateSent?: boolean | undefined, ExternalNumber: string, OurNumber: string, Identifier: string, Comments: string, Products: Array, DueDate: string, TotalValue: number, FineDate?: string | undefined, FinePercent?: number | undefined, FineValue?: number | undefined, InterestPercent?: number | undefined, InterestValue?: number | undefined, DiscountDate?: string | undefined, DiscountValue?: number | undefined, DiscountDate2?: string | undefined, DiscountValue2?: number | undefined, DiscountDate3?: string | undefined, DiscountValue3?: number | undefined, RebateValue?: number | undefined, Tags?: Tags | undefined, OperationVars?: Array | undefined, DivergentPaymentType?: number | undefined, MinPaymentValue?: number | undefined, MaxPaymentValue?: number | undefined, ExpirationDays?: number | undefined, InterestType?: number | undefined, InterestDate?: string | undefined}}; export type CreateApiGenerateBoletoOptions = {baseUrl?: string | undefined, BASE64UsernamePassword?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiGenerateBoletoResponse = z.object({Success: z.string().optional(), DocumentNumber: z.number().optional(), EntryId: z.number().optional(), Message: z.string().optional(), Boleto: z.string().optional(), Url: z.string().optional(), AlreadyExists: z.string().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 }; /** * GenerateBoleto * * POST /GenerateBoleto */ export const createApiGenerateBoleto = async (args: CreateApiGenerateBoletoArgs, options?: CreateApiGenerateBoletoOptions) => { const {baseUrl = "https://sandboxapi.fitbank.com.br/main/execute", fetch: fetchFn = fetch, BASE64UsernamePassword} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BASE64UsernamePassword !== undefined) headers.set('authorization', `Basic ${btoa(`${BASE64UsernamePassword.username}:${BASE64UsernamePassword.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/GenerateBoleto${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiGenerateBoletoResponse, await res.json()) };