import {z} from 'zod' export type GetApiV1StocksTickerRevenueBreakdownArgs = {ticker: string}; export type GetApiV1StocksTickerRevenueBreakdownOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const revenueBreakdownMemberDto = z.object({member: z.string().nullable().optional(), label: z.string().nullable().optional(), values: z.array(z.number().nullable()).nullable().optional()}); export const revenueBreakdownAxisDto = z.object({unit: z.string().nullable().optional(), periodEnds: z.array(z.string()).nullable().optional(), members: z.array(revenueBreakdownMemberDto).nullable().optional()}); export const revenueConsolidatedTotalDto = z.object({periodEnd: z.string().optional(), unit: z.string().nullable().optional(), value: z.number().optional()}); export const revenueBreakdownResponse = z.object({ticker: z.string().nullable().optional(), companyName: z.string().nullable().optional(), segments: revenueBreakdownAxisDto.optional(), geographies: revenueBreakdownAxisDto.optional(), products: revenueBreakdownAxisDto.optional(), segmentOperatingIncome: revenueBreakdownAxisDto.optional(), segmentOperatingMargin: revenueBreakdownAxisDto.optional(), consolidatedRevenue: z.array(revenueConsolidatedTotalDto).nullable().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 }; /** * A company's revenue disaggregated by business segment, geography, and product/service from its dimensional XBRL facts, annual fiscal years only, latest restated values. Also carries per-segment operating income and margin when tagged. Empty axes mean the issuer reports revenue only as a consolidated total. Axis members may overlap hierarchically (a parent line tagged alongside its children), so never sum members — consolidatedRevenue carries the real per-year totals. * * GET /v1/stocks/{ticker}/revenue-breakdown */ export const getApiV1StocksTickerRevenueBreakdown = async (args: GetApiV1StocksTickerRevenueBreakdownArgs, options?: GetApiV1StocksTickerRevenueBreakdownOptions) => { const {baseUrl = "https://api.equibles.com", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/stocks/${encodeURIComponent(String(args.ticker))}/revenue-breakdown${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(revenueBreakdownResponse, await res.json()) };