import {z} from 'zod' export type GetApiV1StocksTickerAnalystEstimatesArgs = {ticker: string, metric?: string | undefined, period?: string | undefined, dimensions?: string | undefined, snapshotId?: string | undefined, asOf?: string | undefined, snapshotOffset?: number | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiV1StocksTickerAnalystEstimatesOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const analystEstimateSnapshotViewModel = z.object({id: z.string().optional(), capturedAt: z.string().optional(), publicationLabel: z.string().nullable().optional()}); export const analystEstimateResponseRow = z.object({publicationLabel: z.string().nullable().optional(), metric: z.string().nullable().optional(), period: z.string().nullable().optional(), dimensions: z.string().nullable().optional(), unit: z.string().nullable().optional(), basis: z.string().nullable().optional(), statistic: z.string().nullable().optional(), analyst: z.string().nullable().optional(), value: z.string().nullable().optional(), footnotes: z.string().nullable().optional()}); export const pageMeta = z.object({limit: z.number().int().optional(), offset: z.number().int().optional(), count: z.number().int().optional(), hasMore: z.boolean().optional(), total: z.number().int().nullable().optional()}); export const analystEstimatesResponse = z.object({ticker: z.string().nullable().optional(), companyName: z.string().nullable().optional(), status: z.string().nullable().optional(), provider: z.string().nullable().optional(), coverageNote: z.string().nullable().optional(), stale: z.boolean().optional(), newerSnapshotPending: z.boolean().optional(), snapshotId: z.string().nullable().optional(), capturedAt: z.string().nullable().optional(), verifiedAt: z.string().nullable().optional(), lastCheckedAt: z.string().nullable().optional(), publicationLabel: z.string().nullable().optional(), snapshots: z.array(analystEstimateSnapshotViewModel).nullable().optional(), data: z.array(analystEstimateResponseRow).nullable().optional(), snapshotsMeta: pageMeta.optional(), meta: pageMeta.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 }; /** * Read verified analyst consensus, including company, product, segment and operating forecasts with original units and publication dates. Values and period labels retain the source's units and accounting basis. Reading never runs a scraper. * * GET /v1/stocks/{ticker}/analyst-estimates */ export const getApiV1StocksTickerAnalystEstimates = async (args: GetApiV1StocksTickerAnalystEstimatesArgs, options?: GetApiV1StocksTickerAnalystEstimatesOptions) => { 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 (args.metric != null) query.append("metric", String(args.metric)) if (args.period != null) query.append("period", String(args.period)) if (args.dimensions != null) query.append("dimensions", String(args.dimensions)) if (args.snapshotId != null) query.append("snapshotId", String(args.snapshotId)) if (args.asOf != null) query.append("asOf", String(args.asOf)) if (args.snapshotOffset != null) query.append("snapshotOffset", String(args.snapshotOffset)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (ApiKey !== undefined) headers.set('authorization', `Bearer ${ApiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/stocks/${encodeURIComponent(String(args.ticker))}/analyst-estimates${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(analystEstimatesResponse, await res.json()) };