import {z} from 'zod' export type GetApiAnalystInsightsArgs = {page?: number | undefined, pageSize?: number | undefined, symbols?: string | undefined, analyst?: string | undefined, rating_id?: string | undefined, search_keys_type?: ('firm_id' | 'firm' | 'analyst_id' | 'analyst_name') | undefined, search_keys?: string | undefined}; export type GetApiAnalystInsightsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const modelsAnalystInsightsJSON = z.object({analystInsights: z.array(z.record(z.string(), z.unknown())).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 Analyst Insights V1 * * GET /analyst/insights */ export const getApiAnalystInsights = async (args: GetApiAnalystInsightsArgs, options?: GetApiAnalystInsightsOptions) => { 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.symbols != null) query.append("symbols", String(args.symbols)) if (args.analyst != null) query.append("analyst", String(args.analyst)) if (args.rating_id != null) query.append("rating_id", String(args.rating_id)) if (args.search_keys_type != null) query.append("search_keys_type", String(args.search_keys_type)) if (args.search_keys != null) query.append("search_keys", String(args.search_keys)) if (ApiKeyAuth !== undefined) query.set("token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/analyst/insights${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(modelsAnalystInsightsJSON, await res.json()) };