import {z} from 'zod' export type GetApiApiScreenerAnalystsArgs = {action?: ('initiated' | 'reiterated' | 'downgraded' | 'upgraded' | 'maintained') | undefined, limit?: number | undefined, recommendation?: ('buy' | 'hold' | 'sell') | undefined, ticker?: string | undefined}; export type GetApiApiScreenerAnalystsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const analystRatingEntry = z.object({action: z.enum(["initiated", "reiterated", "downgraded", "upgraded", "maintained", "target raised", "target lowered"]).optional(), analyst_name: z.string().optional(), firm: z.string().optional(), recommendation: z.enum(["buy", "hold", "sell"]).optional(), sector: z.enum(["Basic Materials", "Communication Services", "Consumer Cyclical", "Consumer Defensive", "Energy", "Financial Services", "Healthcare", "Industrials", "Real Estate", "Technology", "Utilities"]).optional(), target: z.string().optional(), ticker: z.string().optional(), timestamp: z.string().optional()}); export const analystRatingResponse = z.object({data: z.array(analystRatingEntry).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 }; /** * Returns the latest analyst ratings for the given ticker * * GET /api/screener/analysts */ export const getApiApiScreenerAnalysts = async (args: GetApiApiScreenerAnalystsArgs, options?: GetApiApiScreenerAnalystsOptions) => { const {baseUrl = "https://api.unusualwhales.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.action != null) query.append("action", String(args.action)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.recommendation != null) query.append("recommendation", String(args.recommendation)) if (args.ticker != null) query.append("ticker", String(args.ticker)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/screener/analysts${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(analystRatingResponse, await res.json()) };