import {z} from 'zod' export type DocsDestinationAccountRequest = {account_type?: string | undefined, bank?: string | undefined, bank_account?: string | undefined, bank_account_digit?: string | undefined, bank_branch?: string | undefined, document?: string | undefined, name?: string | undefined, receiver_pix_key?: string | undefined, receiver_pix_key_type?: string | undefined}; export type DocsExampleSimulationRequest = {customer_type: 'PF' | 'PJ', first_due_date: string, include_iof?: boolean | undefined, interest_rate: number, iof_handling_type: 'FINANCED' | 'DISCOUNTED', loan_amount: number, number_of_installments: number, periodicity_interval?: number | undefined, periodicity_unit: 'MONTH' | 'DAY' | 'WEEK', registration_fee?: number | undefined, registration_fee_handling_type: 'FINANCED' | 'DISCOUNTED', release_date: string, simulation_type: 'SAC' | 'PRICE'}; export type DocsCreateProposalRequest = {customer_id: string, destination_account: DocsDestinationAccountRequest, email: string, phone: string, product_id: string, simulation_input: DocsExampleSimulationRequest}; export type CreateApiProposalsArgs = {body: DocsCreateProposalRequest}; export type CreateApiProposalsOptions = {baseUrl?: string | undefined, OAuth2Application?: string | undefined, fetch?: typeof fetch | undefined}; export const docsCustomerDataListItem = z.object({dados_adcionais: z.string().optional(), mail: z.string().optional(), phone: z.string().optional()}); export const docsDestinationAccountListItem = z.object({account_type: z.string().optional(), bank: z.string().optional(), bank_account: z.string().optional(), bank_account_digit: z.string().optional(), bank_branch: z.string().optional(), document: z.string().optional(), name: z.string().optional(), receiver_pix_key: z.string().optional(), receiver_pix_key_type: z.string().optional()}); export const docsInstallmentExample = z.object({balance: z.number().optional(), due_date: z.string().optional(), installment_number: z.number().int().optional(), interest_amount: z.number().optional(), payment_amount: z.number().optional(), principal_amount: z.number().optional()}); export const docsSimulationResponseProposal = z.object({cet_annual: z.number().optional(), cet_monthly: z.number().optional(), customer_type: z.string().optional(), first_due_date: z.string().optional(), include_iof: z.boolean().optional(), installments: z.array(docsInstallmentExample).optional(), interest_rate: z.number().optional(), iof_amount: z.number().optional(), iof_handling_type: z.string().optional(), loan_amount: z.number().optional(), net_amount_released: z.number().optional(), number_of_installments: z.number().int().optional(), periodicity_interval: z.number().int().optional(), periodicity_unit: z.string().optional(), registration_fee: z.number().optional(), registration_fee_handling_type: z.string().optional(), release_date: z.string().optional(), simulation_type: z.string().optional(), total_amount_paid: z.number().optional(), total_financed_amount: z.number().optional()}); export const docsProposalResponseCreated = z.object({code: z.string().optional(), created_at: z.string().optional(), customer_data: docsCustomerDataListItem.optional(), destination_account: docsDestinationAccountListItem.optional(), document: z.string().optional(), id: z.string().optional(), simulation: docsSimulationResponseProposal.optional(), status: z.string().optional(), step: z.string().optional(), updated_at: z.string().optional()}); export const docsExampleCreateProposal = z.object({code: z.string().optional(), data: docsProposalResponseCreated.optional(), message: z.string().optional(), request_id: 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 }; /** * Create a new proposal | Criar uma nova proposta * * POST /proposals */ export const createApiProposals = async (args: CreateApiProposalsArgs, options?: CreateApiProposalsOptions) => { const {baseUrl = "https://sandboxapi.fitbank.com.br/main/execute", fetch: fetchFn = fetch, OAuth2Application} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2Application !== undefined) headers.set('authorization', `Bearer ${OAuth2Application}`) const url = `${baseUrl.replace(/\/+$/, '')}/proposals${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(docsExampleCreateProposal, await res.json()) };