import {z} from 'zod' export type DueDate = string; export type FineDate = string; export type PrincipalValue = number; export type RebateValue = number; export type CollectionOrderProductType = '0 - Ammount' | '1 - Percentage'; export type ReceiverContactInfo = {Mail?: string | undefined, Phone?: string | undefined}; export type ReceiverAccountInfo = {PixKey?: string | undefined, TaxNumber: string, Bank?: string | undefined, BankBranch?: string | undefined, BankAccount?: string | undefined, BankAccountDigit?: string | undefined}; export type Receiver = Array<{SplitValue: number, Name: string, Nickname?: string | undefined, BirthDate?: string | undefined, LegalName?: string | undefined, TradingName?: string | undefined, ConstitutionDate?: string | undefined, ReceiverContactInfo: ReceiverContactInfo, ReceiverAccountInfo: ReceiverAccountInfo}>; export type Receivers = {CollectionOrderProductType: CollectionOrderProductType, Receiver: Receiver}; export type Changes = {DueDate?: DueDate | undefined, FineDate?: FineDate | undefined, PrincipalValue?: PrincipalValue | undefined, RebateValue?: RebateValue | undefined, Receivers?: Receivers | undefined}; export type CreateApiChangeBoletoArgs = {body: {Method: string, PartnerId: number, BusinessUnitId: number, DocumentNumber: string, TaxNumber: string, Changes: Array}}; export type CreateApiChangeBoletoOptions = {baseUrl?: string | undefined, BASE64UsernamePassword?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiChangeBoletoResponse = 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 }; /** * ChangeBoleto * * POST /ChangeBoleto */ export const createApiChangeBoleto = async (args: CreateApiChangeBoletoArgs, options?: CreateApiChangeBoletoOptions) => { 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(/\/+$/, '')}/ChangeBoleto${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(createApiChangeBoletoResponse, await res.json()) };