import {z} from 'zod' export type GetApiApiV2NewsArgs = {accept: 'application/json' | 'application/xml', page?: number | undefined, pageSize?: number | undefined, displayOutput?: ('full' | 'abstract' | 'headline') | undefined, date?: string | undefined, dateFrom?: string | undefined, dateTo?: string | undefined, updatedSince?: number | undefined, publishedSince?: number | undefined, sort?: ('id:asc' | 'id:desc' | 'created:asc' | 'created:desc' | 'updated:asc' | 'updated:desc') | undefined, isin?: string | undefined, cusips?: string | undefined, tickers?: string | undefined, primaryTickers?: string | undefined, channels?: string | undefined, topics?: string | undefined, topic_group_by?: ('and' | 'or') | undefined, authors?: string | undefined, content_types?: string | undefined, format?: 'text' | undefined, importance?: string | undefined, importanceRank?: number | undefined, region?: string | undefined}; export type GetApiApiV2NewsOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiChannelTag = z.object({name: z.string().optional()}); export const apiImage = z.object({alt: z.string().optional(), size: z.string().optional(), url: z.string().optional()}); export const apiStock = z.object({cusip: z.string().optional(), exchange: z.string().optional(), gics: z.string().optional(), gics_code: z.string().optional(), isin: z.string().optional(), name: z.string().optional(), sector: z.string().optional()}); export const apiNewsItem = z.object({author: z.string().optional(), body: z.string().optional(), channels: z.array(apiChannelTag).optional(), created: z.string().optional(), id: z.number().int().optional(), image: z.array(apiImage).optional(), importance_rank: z.number().int().optional(), original_id: z.number().int().optional(), stocks: z.array(apiStock).optional(), tags: z.array(apiChannelTag).optional(), teaser: z.string().optional(), title: z.string().optional(), updated: z.string().optional(), url: z.string().optional()}); export const getApiApiV2NewsResponse = z.array(apiNewsItem); 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 the news items * * GET /api/v2/news */ export const getApiApiV2News = async (args: GetApiApiV2NewsArgs, options?: GetApiApiV2NewsOptions) => { 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.accept != null) headers.set("accept", String(args.accept)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.displayOutput != null) query.append("displayOutput", String(args.displayOutput)) if (args.date != null) query.append("date", String(args.date)) if (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) if (args.updatedSince != null) query.append("updatedSince", String(args.updatedSince)) if (args.publishedSince != null) query.append("publishedSince", String(args.publishedSince)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.isin != null) query.append("isin", String(args.isin)) if (args.cusips != null) query.append("cusips", String(args.cusips)) if (args.tickers != null) query.append("tickers", String(args.tickers)) if (args.primaryTickers != null) query.append("primaryTickers", String(args.primaryTickers)) if (args.channels != null) query.append("channels", String(args.channels)) if (args.topics != null) query.append("topics", String(args.topics)) if (args.topic_group_by != null) query.append("topic_group_by", String(args.topic_group_by)) if (args.authors != null) query.append("authors", String(args.authors)) if (args.content_types != null) query.append("content_types", String(args.content_types)) if (args.format != null) query.append("format", String(args.format)) if (args.importance != null) query.append("importance", String(args.importance)) if (args.importanceRank != null) query.append("importanceRank", String(args.importanceRank)) if (args.region != null) query.append("region", String(args.region)) if (ApiKeyAuth !== undefined) headers.set("Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/news${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(getApiApiV2NewsResponse, await res.json()) };