import {z} from 'zod' export type Period = 'annual' | 'quarter' | 'ttm'; export type GetApiApiFinancialsBalanceSheetArgs = {symbol?: string | undefined, cik?: number | undefined, cusip?: string | undefined, composite_figi?: string | undefined, share_class_figi?: string | undefined, currency?: string | undefined, includeSourceDetails?: boolean | undefined, period?: Period | undefined, limit?: number | undefined, splitAdjust?: boolean | undefined}; export type GetApiApiFinancialsBalanceSheetOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const fiscalPeriod = z.enum(["FY", "Q1", "Q2", "Q3", "Q4", "TTM"]); export const fxConversion = z.object({targetCurrency: z.string(), originalCurrencies: z.array(z.string()), rates: z.array(z.object({from: z.string(), method: z.enum(["spot", "average"]), rate: z.number(), effectiveDate: z.string().optional(), start: z.string().optional(), end: z.string().optional()})), unconverted: z.record(z.string(), z.array(z.string())).optional()}); export const acceptedAt = z.string(); export const factSources = z.record(z.string(), z.object({type: z.string(), dateFiled: z.string().optional(), acceptedAt: acceptedAt.optional(), amendment: z.boolean(), facts: z.record(z.string(), z.object({before: z.number().optional(), concept: z.string().optional(), unit: z.string().optional()}))})); export const derivedFacts = z.array(z.string()); export const getApiApiFinancialsBalanceSheetResponse = z.array(z.object({period: z.string().optional(), fiscalYear: z.number().int().optional(), fiscalPeriod: fiscalPeriod.optional(), fx: fxConversion.optional(), facts: z.object({assets: z.number().optional(), currentAssets: z.number().optional(), cash: z.number().optional(), netReceivables: z.number().optional(), accountsReceivable: z.number().optional(), inventory: z.number().optional(), finishedGoods: z.number().optional(), otherCurrentAssets: z.number().optional(), accumulatedDepreciation: z.number().optional(), goodwill: z.number().optional(), otherIntangibleAssets: z.number().optional(), otherNonCurrentAssets: z.number().optional(), nonCurrentAssets: z.number().optional(), liabilities: z.number().optional(), currentLiabilities: z.number().optional(), accountsPayable: z.number().optional(), payablesToBrokerDealers: z.number().optional(), payablesToCustomers: z.number().optional(), taxesPayable: z.number().optional(), totalPayable: z.number().optional(), otherCurrentAccruedLiabilities: z.number().optional(), currentCapitalLeaseObligations: z.number().optional(), operatingLeaseLiabilityCurrent: z.number().optional(), financeLeaseLiabilityCurrent: z.number().optional(), currentDeferredLiabilities: z.number().optional(), otherCurrentLiabilities: z.number().optional(), totalNonCurrentLiabilities: z.number().optional(), longTermDebt: z.number().optional(), longTermCapitalLeaseObligations: z.number().optional(), operatingLeaseLiabilityNoncurrent: z.number().optional(), financeLeaseLiabilityNoncurrent: z.number().optional(), otherNonCurrentLiabilities: z.number().optional(), stockholdersEquity: z.number().optional(), minorityInterest: z.number().optional(), totalEquity: z.number().optional(), commonStock: z.number().optional(), preferredStock: z.number().optional(), retainedEarnings: z.number().optional(), otherEquityAdjustments: z.number().optional(), additionalPaidInCapital: z.number().optional(), capitalLeaseObligations: z.number().optional(), operatingLeaseLiability: z.number().optional(), financeLeaseLiability: z.number().optional(), workingCapital: z.number().optional(), totalDebt: z.number().optional(), totalBorrowings: z.number().optional(), sharesIssued: z.number().optional(), storeCount: z.number().optional(), sharesOutstanding: z.number().optional(), treasuryShares: z.number().optional(), currentSharesOutstanding: z.number().optional(), preferredSharesOutstanding: z.number().optional(), publicFloat: z.number().optional(), policyReserves: z.number().optional(), deferredAcquisitionCosts: z.number().optional(), propertyPlantAndEquipmentGross: z.number().optional(), propertyPlantAndEquipmentNet: z.number().optional(), constructionInProgress: z.number().optional(), costsInExcessOfBillings: z.number().optional(), billingsInExcessOfCosts: z.number().optional(), contractReceivableRetainage: z.number().optional(), debtCurrent: z.number().optional(), longTermDebtCurrent: z.number().optional(), deposits: z.number().optional(), loans: z.number().optional(), allowanceForDoubtfulAccounts: z.number().optional(), allowanceForCreditLossesLoans: z.number().optional(), assetRetirementObligation: z.number().optional(), assetRetirementObligationCurrent: z.number().optional(), assetRetirementObligationNoncurrent: z.number().optional(), warrantyLiability: z.number().optional(), contractLiabilityNoncurrent: z.number().optional(), remainingPerformanceObligations: z.number().optional(), investments: z.number().optional(), shortTermInvestments: z.number().optional(), cashAndShortTermInvestments: z.number().optional(), longTermInvestments: z.number().optional(), lettersOfCreditOutstanding: z.number().optional()}).optional(), splitFactor: z.number().optional(), sources: factSources.optional(), derived: derivedFacts.optional(), dateFiled: z.string().optional(), acceptedAt: acceptedAt.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 }; /** * Balance sheet * * GET /api/financials/balance-sheet */ export const getApiApiFinancialsBalanceSheet = async (args: GetApiApiFinancialsBalanceSheetArgs, options?: GetApiApiFinancialsBalanceSheetOptions) => { const {baseUrl = "https://api.stockfit.io/v1", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.symbol != null) query.append("symbol", String(args.symbol)) if (args.cik != null) query.append("cik", String(args.cik)) if (args.cusip != null) query.append("cusip", String(args.cusip)) if (args.composite_figi != null) query.append("composite_figi", String(args.composite_figi)) if (args.share_class_figi != null) query.append("share_class_figi", String(args.share_class_figi)) if (args.currency != null) query.append("currency", String(args.currency)) if (args.includeSourceDetails != null) query.append("includeSourceDetails", String(args.includeSourceDetails)) if (args.period != null) query.append("period", String(args.period)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.splitAdjust != null) query.append("splitAdjust", String(args.splitAdjust)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/financials/balance-sheet${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(getApiApiFinancialsBalanceSheetResponse, await res.json()) };