import {z} from 'zod' export type GetApiFinancialsBalanceSheetSymbolArgs = {symbol: string, period?: ('annual' | 'quarter') | undefined, datatype?: ('json' | 'csv') | undefined, limit?: number | undefined}; export type GetApiFinancialsBalanceSheetSymbolOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiFinancialsBalanceSheetSymbolResponse = z.array(z.object({date: z.string().optional(), symbol: z.string().optional(), "reported-currency": z.string().optional(), cik: z.string().optional(), "filling-date": z.string().optional(), "accepted-date": z.string().optional(), "calendar-year": z.string().optional(), period: z.string().optional(), "cash-and-cash-equivalents": z.number().optional(), "short-term-investments": z.number().optional(), "cash-and-short-term-investments": z.number().optional(), "net-receivables": z.number().optional(), inventory: z.number().optional(), "other-current-assets": z.number().optional(), "total-current-assets": z.number().optional(), "property-plant-equipment-net": z.number().optional(), goodwill: z.number().optional(), "intangible-assets": z.number().optional(), "goodwill-and-intangible-assets": z.number().optional(), "long-term-investments": z.number().optional(), "tax-assets": z.number().optional(), "other-non-current-assets": z.number().optional(), "total-non-current-assets": z.number().optional(), "other-assets": z.number().optional(), "total-assets": z.number().optional(), "account-payables": z.number().optional(), "short-term-debt": z.number().optional(), "tax-payables": z.number().optional(), "deferred-revenue": z.number().optional(), "other-current-liabilities": z.number().optional(), "total-current-liabilities": z.number().optional(), "long-term-debt": z.number().optional(), "deferred-revenue-non-current": z.number().optional(), "deferred-tax-liabilities-non-current": z.number().optional(), "other-non-current-liabilities": z.number().optional(), "total-non-current-liabilities": z.number().optional(), "other-liabilities": z.number().optional(), "capital-lease-obligations": z.number().optional(), "total-liabilities": z.number().optional(), "preferred-stock": z.number().optional(), "common-stock": z.number().optional(), "retained-earnings": z.number().optional(), "accumulated-other-comprehensive-income-loss": z.number().optional(), "othertotal-stockholders-equity": z.number().optional(), "total-stockholders-equity": z.number().optional(), "total-equity": z.number().optional(), "total-liabilities-and-stockholders-equity": z.number().optional(), "minority-interest": z.number().optional(), "total-liabilities-and-total-equity": z.number().optional(), "total-investments": z.number().optional(), "total-debt": z.number().optional(), "net-debt": z.number().optional(), link: z.string().optional(), "final-link": 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 }; /** * Get Annual Balance Sheet Statement * * GET /financials/balance-sheet/{symbol} */ export const getApiFinancialsBalanceSheetSymbol = async (args: GetApiFinancialsBalanceSheetSymbolArgs, options: GetApiFinancialsBalanceSheetSymbolOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.period != null) query.append("period", String(args.period)) if (args.datatype != null) query.append("datatype", String(args.datatype)) if (args.limit != null) query.append("limit", String(args.limit)) if (ApiKeyAuth !== undefined) headers.set("X-API-KEY", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/financials/balance-sheet/${encodeURIComponent(String(args.symbol))}${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(getApiFinancialsBalanceSheetSymbolResponse, await res.json()) };