import {z} from 'zod' export type PartnerId = number; export type BusinessUnitId = number; export type FromTaxNumber = string; export type FromBank = string; export type FromBankBranch = string; export type FromBankAccount = string; export type FromBankAccountDigit = string; export type ToTaxNumber = string; export type ToBank = string; export type ToBankBranch = string; export type ToBankAccount = string; export type ToBankAccountDigit = string; export type Value = number; export type RateValue = number; export type RateValueType = number; export type CashbackValue = number; export type TransferDate = string; export type Identifier = string; export type Tags = Array; export type Description = string; export type FeePayerTaxNumber = string; export type FeePayerFullName = string; export type FeePayerMail = string; export type FeePayerIdentyDocument = string; export type FeePayerTradingName = string; export type FeePayerLegalName = string; export type FeePayerPhone = string; export type FeePayerBank = string; export type FeePayerBankBranch = string; export type FeePayerBankAccount = string; export type FeePayerBankAccountDigit = string; export type Category = string; export type InternalTransfer = {Method: string, PartnerId: PartnerId, BusinessUnitId: BusinessUnitId, FromTaxNumber: FromTaxNumber, FromBank?: FromBank | undefined, FromBankBranch?: FromBankBranch | undefined, FromBankAccount?: FromBankAccount | undefined, FromBankAccountDigit?: FromBankAccountDigit | undefined, ToTaxNumber: ToTaxNumber, ToBank?: ToBank | undefined, ToBankBranch?: ToBankBranch | undefined, ToBankAccount?: ToBankAccount | undefined, ToBankAccountDigit?: ToBankAccountDigit | undefined, Value: Value, RateValue?: RateValue | undefined, RateValueType?: RateValueType | undefined, CashbackValue?: CashbackValue | undefined, TransferDate: TransferDate, Identifier: Identifier, Tags?: Tags | undefined, Description?: Description | undefined, FeePayerTaxNumber?: FeePayerTaxNumber | undefined, FeePayerFullName?: FeePayerFullName | undefined, FeePayerMail?: FeePayerMail | undefined, FeePayerIdentyDocument?: FeePayerIdentyDocument | undefined, FeePayerTradingName?: FeePayerTradingName | undefined, FeePayerLegalName?: FeePayerLegalName | undefined, FeePayerPhone?: FeePayerPhone | undefined, FeePayerBank?: FeePayerBank | undefined, FeePayerBankBranch?: FeePayerBankBranch | undefined, FeePayerBankAccount?: FeePayerBankAccount | undefined, FeePayerBankAccountDigit?: FeePayerBankAccountDigit | undefined, Category?: Category | undefined}; export type CreateApiInternalTransferArgs = {body: InternalTransfer}; export type CreateApiInternalTransferOptions = {baseUrl?: string | undefined, BASE64UsernamePassword?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const success = z.boolean(); export const documentNumber = z.number().int(); export const message = z.string(); export const alreadyExists = z.string(); export const url = z.string(); export const internalTransferSuccess = z.object({Success: success.optional(), DocumentNumber: documentNumber.optional(), Message: message.optional(), AlreadyExists: alreadyExists.optional(), Url: url.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 }; /** * Internal Transfer * * POST /InternalTransfer */ export const createApiInternalTransfer = async (args: CreateApiInternalTransferArgs, options?: CreateApiInternalTransferOptions) => { 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(/\/+$/, '')}/InternalTransfer${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(internalTransferSuccess, await res.json()) };