import {z} from 'zod' export type GetApiApiFinancialStatementsIdArgs = {id: string, omit?: string | undefined, fields?: string | undefined}; export type GetApiApiFinancialStatementsIdOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const id = z.string(); export const link = z.string().nullable(); export const collectedAt = z.string(); export const createdAt = z.string(); export const financialStatementBalanceSheetCurrentAssets = z.object({cash_and_equivalents: z.number().nullable(), short_term_investments: z.number().nullable(), accounts_receivable: z.number().nullable(), notes_receivable: z.number().nullable(), other_debtors: z.number().nullable(), bad_debt_provision: z.number().nullable(), tax_recoverable: z.number().nullable(), inventory: z.number().nullable(), prepaid_expenses: z.number().nullable(), assets_available_for_sale: z.number().nullable(), total: z.number().nullable()}); export const financialStatementBalanceSheetNonCurrentAssets = z.object({property_plant_and_equipment: z.number().nullable(), accumulated_depreciation_and_amortization: z.number().nullable(), long_term_accounts_receivable: z.number().nullable(), prepayment_to_suppliers: z.number().nullable(), goodwill: z.number().nullable(), intangible_assets: z.number().nullable(), investments_in_associates: z.number().nullable(), long_term_financial_instruments: z.number().nullable(), total: z.number().nullable()}); export const financialStatementBalanceSheetCurrentLiabilities = z.object({bank_loans: z.number().nullable(), accounts_payable: z.number().nullable(), notes_payable: z.number().nullable(), financial_instruments: z.number().nullable(), other_creditors: z.number().nullable(), income_tax_payable: z.number().nullable(), customer_advances: z.number().nullable(), provisions: z.number().nullable(), taxes_payable: z.number().nullable(), total: z.number().nullable()}); export const financialStatementBalanceSheetNonCurrentLiabilities = z.object({long_term_accounts_payable: z.number().nullable(), long_term_financial_instruments: z.number().nullable(), deferred_revenue: z.number().nullable(), contributions_for_future_capital_increases: z.number().nullable(), deferred_income_tax: z.number().nullable(), employee_benefits: z.number().nullable(), long_term_provisions: z.number().nullable(), total: z.number().nullable()}); export const financialStatementBalanceSheetEquity = z.object({stockholders_equity: z.number().nullable(), future_capital_contributions: z.number().nullable(), legal_reserve: z.number().nullable(), capital_update_excess: z.number().nullable(), capital_update_insufficiency: z.number().nullable(), capital_reserve: z.number().nullable(), share_premium_on_stock_sales: z.number().nullable(), retained_earnings: z.number().nullable(), other_comprehensive_income: z.number().nullable(), controlling_interest: z.number().nullable(), non_controlling_interest: z.number().nullable(), total: z.number().nullable()}); export const financialStatementBalanceSheet = z.object({current_assets: financialStatementBalanceSheetCurrentAssets.optional(), non_current_assets: financialStatementBalanceSheetNonCurrentAssets.optional(), current_liabilities: financialStatementBalanceSheetCurrentLiabilities.optional(), non_current_liabilities: financialStatementBalanceSheetNonCurrentLiabilities.optional(), equity: financialStatementBalanceSheetEquity.optional()}); export const financialStatementIncomeStatement = z.object({net_revenue: z.number().nullable(), domestic_sales: z.number().nullable(), foreign_sales: z.number().nullable(), materials_used: z.number().nullable(), cost_of_goods_sold: z.number().nullable(), cost_of_services_sold: z.number().nullable(), gross_profit: z.number().nullable(), gross_loss: z.number().nullable(), operating_expenses: z.number().nullable(), operating_income: z.number().nullable(), operating_loss: z.number().nullable(), financial_result: z.number().nullable(), income_statement_financial_gains: z.number().gte(0).nullable(), income_statement_financial_costs: z.number().lte(0).nullable(), equity_in_earnings_of_affiliates: z.number().nullable(), income_before_taxes: z.number().nullable(), loss_before_taxes: z.number().nullable(), income_taxes: z.number().nullable(), income_from_continuing_operations: z.number().nullable(), loss_from_continuing_operations: z.number().nullable(), discontinued_operations: z.number().nullable(), net_income: z.number().nullable(), net_loss: z.number().nullable()}); export const financialStatement = z.object({id: id, link: link, collected_at: collectedAt, created_at: createdAt, error: z.string().nullable(), year: z.string(), currency: z.string(), balance_sheet: financialStatementBalanceSheet, income_statement: financialStatementIncomeStatement}); 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 }; /** * Get a Financial Statement's details * * GET /api/financial-statements/{id}/ */ export const getApiApiFinancialStatementsId = async (args: GetApiApiFinancialStatementsIdArgs, options?: GetApiApiFinancialStatementsIdOptions) => { const {baseUrl = "https://sandbox.belvo.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.omit != null) query.append("omit", String(args.omit)) if (args.fields != null) query.append("fields", String(args.fields)) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/financial-statements/${encodeURIComponent(String(args.id))}/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(financialStatement, await res.json()) };