import {z} from 'zod' export type JsonPayeeBoletoInCarnet = {name: string, bank: string, bankBranch: string, bankAccount: string, bankAccountDigit: string, taxNumber: string}; export type JsonReceiverBoletoInCarnet = {splitPercent: number, name: string, bank: string, bankBranch: string, bankAccount: string, bankAccountDigit: string, taxNumber: string}; export type JsonContactInfo = {mail?: (string | null) | undefined, phone?: (string | null) | undefined}; export type JsonAddressBoletoInCarnet = {city: string, state: string, zipCode: string, address: string}; export type JsonPayerBoletoInCarnet = {name: string, contact?: JsonContactInfo | undefined, address?: JsonAddressBoletoInCarnet | undefined, taxNumber: string}; export type BoletoType = 0 | 1; export type BoletoInRecurrenceType = 0 | 1; export type JsonRecurrence = {type?: BoletoInRecurrenceType | undefined, installmentsNumber: number}; export type BoletoInCarnetEspecie = 2 | 4 | 12 | 23 | 99; export type JsonDate = {dueDate: string, daysAfterDueDate?: number | undefined}; export type DivergentPaymentType = '1 - AnyValue' | '2 - MinAndMaxValue' | '3 - NotAllowed' | '4 - OnlyMinValue'; export type JsonPaymentBoletoInCarnet = {divergentPaymentType?: DivergentPaymentType | undefined, minValue?: (number | null) | undefined, maxValue?: (number | null) | undefined}; export type JsonBoletoInformation = {type?: BoletoType | undefined, recurrence: JsonRecurrence, externalNumber?: number | undefined, especie?: BoletoInCarnetEspecie | undefined, instruction?: (string | null) | undefined, date: JsonDate, payment?: JsonPaymentBoletoInCarnet | undefined}; export type BoletoInCarnetFineType = 1 | 2 | 3; export type JsonFine = {type?: BoletoInCarnetFineType | undefined, value?: (number | null) | undefined, date?: (string | null) | undefined}; export type BoletoInCarnetRebateType = 1 | 3; export type JsonRebate = {type?: BoletoInCarnetRebateType | undefined, value: number}; export type BoletoInCarnetInterestType = 1 | 2 | 3 | 5 | 6; export type JsonInterest = {type?: BoletoInCarnetInterestType | undefined, value?: (number | null) | undefined, date?: (string | null) | undefined}; export type BoletoInCarnetDiscountType = 0 | 1 | 2; export type JsonDiscountRange = {date?: (string | null) | undefined, value?: (number | null) | undefined}; export type JsonDiscount = {type?: BoletoInCarnetDiscountType | undefined, discountRange?: (Array | null) | undefined}; export type JsonValues = {originalValue: number, fine?: JsonFine | undefined, rebate?: JsonRebate | undefined, interest?: JsonInterest | undefined, discount?: JsonDiscount | undefined}; export type JsonNotifications = {mailToSend?: (string | null) | undefined, phoneToSend?: (string | null) | undefined}; export type BoletoInCarnetSent = 0 | 1 | 2; export type JsonRate = {sent?: BoletoInCarnetSent | undefined, value?: (number | null) | undefined}; export type JsonAdditionalInformation = {name: string, value: number}; export type JsonOperationBoletoInCarnet = {method?: (string | null) | undefined, partnerId: number, businessUnitId: number, payee: JsonPayeeBoletoInCarnet, receivers?: (Array | null) | undefined, payer: JsonPayerBoletoInCarnet, boletoInformation: JsonBoletoInformation, values: JsonValues, notifications?: JsonNotifications | undefined, rate?: JsonRate | undefined, tags?: (Array | null) | undefined, additionalInformation?: (Array | null) | undefined}; export type CreateApiGenerateRecurrenceBoletoInArgs = {body: JsonOperationBoletoInCarnet}; export type CreateApiGenerateRecurrenceBoletoInOptions = {baseUrl?: string | undefined, BASE64UsernamePassword?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiGenerateRecurrenceBoletoInResponse = z.object({Success: z.string().optional(), Message: 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 }; /** * GenerateRecurrenceBoletoIn * * POST /GenerateRecurrenceBoletoIn */ export const createApiGenerateRecurrenceBoletoIn = async (args: CreateApiGenerateRecurrenceBoletoInArgs, options?: CreateApiGenerateRecurrenceBoletoInOptions) => { 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(/\/+$/, '')}/GenerateRecurrenceBoletoIn${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(createApiGenerateRecurrenceBoletoInResponse, await res.json()) };