import {z} from 'zod' export type Period = 'annual' | 'quarter' | 'ttm'; export type GetApiApiFinancialsCashFlowStatementArgs = {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}; export type GetApiApiFinancialsCashFlowStatementOptions = {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 getApiApiFinancialsCashFlowStatementResponse = z.array(z.object({period: z.string().optional(), fiscalYear: z.number().int().optional(), fiscalPeriod: fiscalPeriod.optional(), fx: fxConversion.optional(), facts: z.object({operatingCashFlow: z.number().optional(), netIncome: z.number().optional(), operatingGainsLosses: z.number().optional(), depreciationAndAmortization: z.number().optional(), depreciation: z.number().optional(), amortization: z.number().optional(), stockBasedCompensation: z.number().optional(), deferredIncomeTax: z.number().optional(), impairments: z.number().optional(), otherNonCashItems: z.number().optional(), changeInWorkingCapital: z.number().optional(), changeInInventory: z.number().optional(), changeInAccountsReceivable: z.number().optional(), changeInPrepaidAssets: z.number().optional(), changeInPayablesAndAccruedExpenses: z.number().optional(), changeInAccountsPayable: z.number().optional(), changeInAccruedExpenses: z.number().optional(), changeInOtherNonCurrentAssets: z.number().optional(), changeInOtherCurrentLiabilities: z.number().optional(), changeInOtherWorkingCapital: z.number().optional(), investingCashFlow: z.number().optional(), purchaseOfIntangibleAssets: z.number().optional(), purchasesOfInvestments: z.number().optional(), proceedsFromSaleOfInvestments: z.number().optional(), netOtherInvestingChanges: z.number().optional(), financingCashFlow: z.number().optional(), commonStockRepurchased: z.number().optional(), proceedsFromCommonStockIssuance: z.number().optional(), netCommonStockIssuance: z.number().optional(), proceedsFromStockOptionsExercised: z.number().optional(), endCash: z.number().optional(), changeInCash: z.number().optional(), effectOfExchangeRateChanges: z.number().optional(), netChangeInCashIncludingFx: z.number().optional(), beginningCash: z.number().optional(), incomeTaxPaid: z.number().optional(), interestPaid: z.number().optional(), capitalExpenditure: z.number().optional(), purchaseOfPropertyPlantAndEquipment: z.number().optional(), issuanceOfDebt: z.number().optional(), repaymentOfDebt: z.number().optional(), issuanceOfShortTermDebt: z.number().optional(), repaymentOfShortTermDebt: z.number().optional(), issuanceOfLongTermDebt: z.number().optional(), repaymentOfLongTermDebt: z.number().optional(), netIssuancePaymentsOfDebt: z.number().optional(), netIssuanceOfLongTermDebt: z.number().optional(), cashDividendsPaid: z.number().optional(), freeCashFlow: z.number().optional(), acquisitionsNet: z.number().optional(), netOtherFinancingChanges: z.number().optional(), taxWithholdingShareBasedComp: z.number().optional()}).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 }; /** * Cash flow statement * * GET /api/financials/cash-flow-statement */ export const getApiApiFinancialsCashFlowStatement = async (args: GetApiApiFinancialsCashFlowStatementArgs, options?: GetApiApiFinancialsCashFlowStatementOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/financials/cash-flow-statement${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(getApiApiFinancialsCashFlowStatementResponse, await res.json()) };