import {z} from 'zod' export type GetApiFinancialsIncomeStatementsSegmentsArgs = {ticker?: string | undefined, period: 'annual' | 'quarterly', limit?: number | undefined, cik?: string | undefined, report_period?: string | undefined, report_period_gte?: string | undefined, report_period_lte?: string | undefined, report_period_gt?: string | undefined, report_period_lt?: string | undefined, cursor?: string | undefined}; export type GetApiFinancialsIncomeStatementsSegmentsOptions = {baseUrl?: string | undefined, XAPIKEY?: string | undefined, fetch?: typeof fetch | undefined}; export const segmentBreakdown = z.object({label: z.string().optional(), value: z.number().optional()}); export const segmentCategory = z.object({product: z.array(segmentBreakdown).optional(), segment: z.array(segmentBreakdown).optional()}); export const incomeStatementSegments = z.object({ticker: z.string().optional(), report_period: z.string().optional(), fiscal_period: z.string().optional(), period: z.enum(["quarterly", "annual"]).optional(), currency: z.string().optional(), accession_number: z.string().optional(), filing_url: z.string().optional(), revenue: segmentCategory.nullable().optional(), operating_income: segmentCategory.nullable().optional(), depreciation: segmentCategory.nullable().optional()}); export const incomeStatementSegmentsResponse = z.object({segmented_financials: z.array(incomeStatementSegments).optional(), next_page_url: 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 income statement segments * * GET /financials/income-statements/segments */ export const getApiFinancialsIncomeStatementsSegments = async (args: GetApiFinancialsIncomeStatementsSegmentsArgs, options?: GetApiFinancialsIncomeStatementsSegmentsOptions) => { const {baseUrl = "https://api.financialdatasets.ai", fetch: fetchFn = fetch, XAPIKEY} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ticker != null) query.append("ticker", String(args.ticker)) if (args.period != null) query.append("period", String(args.period)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cik != null) query.append("cik", String(args.cik)) if (args.report_period != null) query.append("report_period", String(args.report_period)) if (args.report_period_gte != null) query.append("report_period_gte", String(args.report_period_gte)) if (args.report_period_lte != null) query.append("report_period_lte", String(args.report_period_lte)) if (args.report_period_gt != null) query.append("report_period_gt", String(args.report_period_gt)) if (args.report_period_lt != null) query.append("report_period_lt", String(args.report_period_lt)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (XAPIKEY !== undefined) headers.set("X-API-KEY", XAPIKEY) const url = `${baseUrl.replace(/\/+$/, '')}/financials/income-statements/segments${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(incomeStatementSegmentsResponse, await res.json()) };