import {z} from 'zod' export type GetApiV1CompanyCompanyIdFinancialsArgs = {company_id: string}; export type GetApiV1CompanyCompanyIdFinancialsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const reportRow: z.ZodType = z.object({name: z.string(), formatted_name: z.string(), current_value: z.number().int().nullable(), previous_value: z.number().int().nullable(), children: z.array(z.lazy(() => reportRow))}); export const reportTable = z.object({rows: z.array(reportRow)}); export const reportSource = z.object({html_url: z.string()}); export const report = z.object({report_id: z.string(), consolidated: z.boolean(), aktiva: reportTable, passiva: reportTable, guv: reportTable.optional(), report_start_date: z.string().nullable(), report_end_date: z.string(), sources: z.array(reportSource)}); export const companyIndicator = z.object({date: z.string(), report_id: z.string(), balance_sheet_total: z.number().int().nullable(), net_income: z.number().int().nullable(), parent_net_income: z.number().int().nullable(), income_before_tax: z.number().int().nullable(), income_after_tax: z.number().int().nullable(), ebit: z.number().int().nullable(), ebitda: z.number().int().nullable(), revenue: z.number().int().nullable(), gross_profit: z.number().int().nullable(), cash: z.number().int().nullable(), employees: z.number().int().nullable(), equity: z.number().int().nullable(), real_estate: z.number().int().nullable(), materials: z.number().int().nullable(), pension_provisions: z.number().int().nullable(), salaries: z.number().int().nullable(), taxes: z.number().int().nullable(), other_taxes: z.number().int().nullable(), commission_income: z.number().int().nullable(), commission_expense: z.number().int().nullable(), liabilities: z.number().int().nullable(), capital_reserves: z.number().int().nullable(), active_accruals: z.number().int().nullable(), passive_accruals: z.number().int().nullable(), fixed_assets: z.number().int().nullable(), current_assets: z.number().int().nullable(), receivables: z.number().int().nullable(), trade_receivables: z.number().int().nullable(), inventory: z.number().int().nullable(), provisions: z.number().int().nullable(), bank_debt: z.number().int().nullable(), trade_payables: z.number().int().nullable(), tangible_assets: z.number().int().nullable(), financial_assets: z.number().int().nullable(), retained_earnings: z.number().int().nullable(), profit_carryforward: z.number().int().nullable(), other_provisions: z.number().int().nullable(), shareholder_liabilities: z.number().int().nullable(), operating_depreciation: z.number().int().nullable(), financial_depreciation: z.number().int().nullable(), other_operating_income: z.number().int().nullable(), interest_income: z.number().int().nullable(), interest_expense: z.number().int().nullable(), other_liabilities: z.number().int().nullable(), financial_debt: z.number().int().nullable(), intangible_assets: z.number().int().nullable(), other_operating_expenses: z.number().int().nullable(), affiliated_liabilities: z.number().int().nullable()}); export const companyIndicators = z.array(companyIndicator); export const mergedReportRow: z.ZodType = z.object({name: z.string(), formatted_name: z.string(), values: z.record(z.string(), z.number().int()), children: z.array(z.lazy(() => mergedReportRow))}); export const mergedReportTable = z.object({rows: z.array(mergedReportRow)}); export const mergedFinancials = z.object({aktiva: mergedReportTable, passiva: mergedReportTable, guv: mergedReportTable.optional()}); export const financialsResponse = z.object({reports: z.array(report), indicators: companyIndicators, merged: mergedFinancials.nullable()}); 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 financial reports * * GET /v1/company/{company_id}/financials */ export const getApiV1CompanyCompanyIdFinancials = async (args: GetApiV1CompanyCompanyIdFinancialsArgs, options?: GetApiV1CompanyCompanyIdFinancialsOptions) => { const {baseUrl = "https://api.openregister.de", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/company/${encodeURIComponent(String(args.company_id))}/financials${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(financialsResponse, await res.json()) };