import {z} from 'zod' export type GetApiApiV1TrendingTickersArgs = {interval: string, tickers: string, source: string, timeframe?: string | undefined, page?: number | undefined, pagesize?: number | undefined}; export type GetApiApiV1TrendingTickersOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const httpapiTickerTrendMetrics = z.object({count: z.number().int().optional(), count_mavg: z.number().optional(), market_count_average: z.number().optional(), scaled_count: z.number().optional(), scaled_count_mavg: z.number().optional(), time_bucket: z.string().optional()}); export const httpapiTickerTrendData = z.object({exchange: z.string().optional(), metrics: z.array(httpapiTickerTrendMetrics).optional(), ticker: z.string().optional()}); export const httpapiTickerTrendAPIResponse = z.object({data: z.array(httpapiTickerTrendData).optional(), ok: z.boolean().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 ticker trend data * * GET /api/v1/trending-tickers */ export const getApiApiV1TrendingTickers = async (args: GetApiApiV1TrendingTickersArgs, options?: GetApiApiV1TrendingTickersOptions) => { 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.interval != null) query.append("interval", String(args.interval)) if (args.tickers != null) query.append("tickers", String(args.tickers)) if (args.source != null) query.append("source", String(args.source)) if (args.timeframe != null) query.append("timeframe", String(args.timeframe)) if (args.page != null) query.append("page", String(args.page)) if (args.pagesize != null) query.append("pagesize", String(args.pagesize)) if (ApiKeyAuth !== undefined) query.set("token", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/trending-tickers${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(httpapiTickerTrendAPIResponse, await res.json()) };