import {z} from 'zod' export type GetApiCashFlowArgs = {symbol?: string | undefined, figi?: string | undefined, isin?: string | undefined, cusip?: string | undefined, exchange?: string | undefined, mic_code?: string | undefined, country?: string | undefined, period?: ('annual' | 'quarterly') | undefined, start_date?: string | undefined, end_date?: string | undefined, outputsize?: number | undefined}; export type GetApiCashFlowOptions = {baseUrl?: string | undefined, authorizationHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const cashFlowStruct = z.object({fiscal_date: z.string(), quarter: z.string().optional(), year: z.number().int().optional(), operating_activities: z.object({net_income: z.number().optional(), depreciation: z.number().optional(), deferred_taxes: z.number().optional(), stock_based_compensation: z.number().optional(), other_non_cash_items: z.number().optional(), accounts_receivable: z.number().optional(), accounts_payable: z.number().optional(), other_assets_liabilities: z.number().optional(), operating_cash_flow: z.number().optional()}).optional(), investing_activities: z.object({capital_expenditures: z.number().optional(), net_intangibles: z.number().optional(), net_acquisitions: z.number().optional(), purchase_of_investments: z.number().optional(), sale_of_investments: z.number().optional(), other_investing_activity: z.number().optional(), investing_cash_flow: z.number().optional()}).optional(), financing_activities: z.object({long_term_debt_issuance: z.number().optional(), long_term_debt_payments: z.number().optional(), short_term_debt_issuance: z.number().optional(), common_stock_issuance: z.number().optional(), common_stock_repurchase: z.number().optional(), common_dividends: z.number().optional(), other_financing_charges: z.number().optional(), financing_cash_flow: z.number().optional()}).optional(), end_cash_position: z.number().optional(), income_tax_paid: z.number().optional(), interest_paid: z.number().optional(), free_cash_flow: z.number().optional()}); export const getApiCashFlowResponse = z.object({meta: z.object({symbol: z.string(), name: z.string(), currency: z.string(), exchange: z.string(), mic_code: z.string(), exchange_timezone: z.string(), period: z.string()}), cash_flow: z.array(cashFlowStruct)}); 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 * * GET /cash_flow * * Also accepts auth: queryParameter */ export const getApiCashFlow = async (args: GetApiCashFlowArgs, options?: GetApiCashFlowOptions) => { const {baseUrl = "https://api.twelvedata.com", fetch: fetchFn = fetch, authorizationHeader} = 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.figi != null) query.append("figi", String(args.figi)) if (args.isin != null) query.append("isin", String(args.isin)) if (args.cusip != null) query.append("cusip", String(args.cusip)) if (args.exchange != null) query.append("exchange", String(args.exchange)) if (args.mic_code != null) query.append("mic_code", String(args.mic_code)) if (args.country != null) query.append("country", String(args.country)) if (args.period != null) query.append("period", String(args.period)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.outputsize != null) query.append("outputsize", String(args.outputsize)) if (authorizationHeader !== undefined) headers.set("Authorization", authorizationHeader) const url = `${baseUrl.replace(/\/+$/, '')}/cash_flow${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(getApiCashFlowResponse, await res.json()) };