import {z} from 'zod' export type GetApiApiV2NewsArgs = {tickers?: string | undefined, cusips?: string | undefined, isins?: string | undefined, channels?: string | undefined, topics?: string | undefined, authors?: string | undefined, contentTypes?: string | undefined, sort?: string | undefined, date?: string | undefined, publishedSince?: string | undefined, updatedSince?: string | undefined, dateFrom?: string | undefined, dateTo?: string | undefined, pageSize?: number | undefined, page?: number | undefined, lastID?: string | undefined, displayOutput?: string | undefined, noise?: string | undefined, format?: 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({size: z.string().optional(), url: z.string().optional()}); export const apiStock = z.object({cusip: z.string().optional(), isin: z.string().optional(), name: 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(), 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 News * * 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.tickers != null) query.append("tickers", String(args.tickers)) if (args.cusips != null) query.append("cusips", String(args.cusips)) if (args.isins != null) query.append("isins", String(args.isins)) if (args.channels != null) query.append("channels", String(args.channels)) if (args.topics != null) query.append("topics", String(args.topics)) if (args.authors != null) query.append("authors", String(args.authors)) if (args.contentTypes != null) query.append("contentTypes", String(args.contentTypes)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.date != null) query.append("date", String(args.date)) if (args.publishedSince != null) query.append("publishedSince", String(args.publishedSince)) if (args.updatedSince != null) query.append("updatedSince", String(args.updatedSince)) if (args.dateFrom != null) query.append("dateFrom", String(args.dateFrom)) if (args.dateTo != null) query.append("dateTo", String(args.dateTo)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.page != null) query.append("page", String(args.page)) if (args.lastID != null) query.append("lastID", String(args.lastID)) if (args.displayOutput != null) query.append("displayOutput", String(args.displayOutput)) if (args.noise != null) query.append("noise", String(args.noise)) if (args.format != null) query.append("format", String(args.format)) if (ApiKeyAuth !== undefined) query.set("token", 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()) };