import {z} from 'zod' export type GetApiConsensusRatingsArgs = {page?: number | undefined, pagesize?: number | undefined, 'parameters[date_from]'?: string | undefined, 'parameters[date_to]'?: string | undefined, 'parameters[tickers]'?: string | undefined, simplify?: string | undefined, aggregate_type?: ('number' | 'percentage') | undefined}; export type GetApiConsensusRatingsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiAggregateRating = z.object({buy: z.number().int().optional(), hold: z.number().int().optional(), sell: z.number().int().optional(), strong_buy: z.number().int().optional(), strong_sell: z.number().int().optional()}); export const apiConsensusRating = z.object({aggregate_ratings: apiAggregateRating.optional(), aggregate_type: z.string().optional(), consensus_price_target: z.number().optional(), consensus_rating: z.string().optional(), consensus_rating_val: z.number().optional(), high_price_target: z.number().optional(), low_price_target: z.number().optional(), total_analyst_count: z.number().int().optional(), unique_analyst_count: z.number().int().optional(), updated_at: 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 Consensus Ratings * * GET /consensus_ratings */ export const getApiConsensusRatings = async (args: GetApiConsensusRatingsArgs, options?: GetApiConsensusRatingsOptions) => { const {baseUrl = "https://api.benzinga.com", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.pagesize != null) query.append("pagesize", String(args.pagesize)) if (args['parameters[date_from]'] != null) query.append("parameters[date_from]", String(args['parameters[date_from]'])) if (args['parameters[date_to]'] != null) query.append("parameters[date_to]", String(args['parameters[date_to]'])) if (args['parameters[tickers]'] != null) query.append("parameters[tickers]", String(args['parameters[tickers]'])) if (args.simplify != null) query.append("simplify", String(args.simplify)) if (args.aggregate_type != null) query.append("aggregate_type", String(args.aggregate_type)) if (ApiKeyAuth !== undefined) query.set("token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/consensus_ratings${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(apiConsensusRating, await res.json()) };