import {z} from 'zod' export type GetApiV1CompaniesIdFinancialsArgs = {id: string, account_id: string}; export type GetApiV1CompaniesIdFinancialsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const companyFinancialsV1 = z.object({attributes: z.object({available_indicators: z.array(z.object({estimated: z.boolean(), name: z.enum(["accounts_receivable", "balance_sheet_total", "cash_and_cash_equivalents", "creditors_after_one_year", "creditors_within_one_year", "current_assets", "debt", "earnings", "earnings_cagr", "employees", "equity", "equity_ratio", "fixed_assets", "issued_share_capital", "liabilities", "materials_expenditure", "net_current_assets", "operation_expenses", "operation_result", "pension_provisions", "real_estate_expenditures", "return_on_equity", "return_on_sales", "revenue", "revenue_cagr", "revenue_per_employee", "salaries_paid", "salaries_per_employee", "tax_paid", "tax_ratio", "total_assets_less_current_liabilities"]), type: z.union([z.enum(["currency", "percentage", "number"]), z.number()]), unit: z.string().nullable(), value: z.number()})), consolidated: z.boolean(), year: z.number()}), id: z.string(), type: z.literal("company_financials")}); export const getApiV1CompaniesIdFinancialsResponse = z.object({data: z.array(companyFinancialsV1), meta: z.object({credits: z.object({charged: z.number()}), request_id: z.string()})}); 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 }; /** * Company Financial Reports * * GET /v1/companies/{id}/financials * * Also accepts auth: OAuth2 */ export const getApiV1CompaniesIdFinancials = async (args: GetApiV1CompaniesIdFinancialsArgs, options?: GetApiV1CompaniesIdFinancialsOptions) => { const {baseUrl = "https://api.leadfeeder.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.account_id != null) query.append("account_id", String(args.account_id)) if (ApiKeyAuth !== undefined) headers.set("X-Api-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/v1/companies/${encodeURIComponent(String(args.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(getApiV1CompaniesIdFinancialsResponse, await res.json()) };