import {z} from 'zod' export type GetApiV1IndexCountsArgs = {start_datetime?: (string | null) | undefined, end_datetime?: (string | null) | undefined, domains?: (Array | null) | undefined, sampling?: ('5m' | '1h' | '12h' | '1d' | '1w' | '1m') | undefined, time_filter?: ('crawl_date' | 'pub_date') | undefined, categories?: (Array<'All' | 'Business' | 'Crime' | 'Politics' | 'Science' | 'Sports' | 'Technology' | 'Military' | 'Health' | 'Entertainment' | 'Finance' | 'Culture' | 'Climate' | 'Environment' | 'World'> | null) | undefined, provocative?: ('unknown' | 'low' | 'medium' | 'high' | 'all' | null) | undefined, reporting_voice?: (Array<'Objective' | 'Subjective' | 'Investigative' | 'Narrative' | 'Analytical' | 'Advocacy' | 'Conversational' | 'Satirical' | 'Emotive' | 'Explanatory' | 'Persuasive' | 'Sensational' | 'Unknown' | 'all'> | null) | undefined, bad_domain_url?: (Array | string | null) | undefined, page_rank?: (number | null) | undefined, string_guarantee?: (Array | null) | undefined, string_guarantee_op?: ('AND' | 'OR') | undefined, reverse_string_guarantee?: (Array | null) | undefined, entity_guarantee?: (Array | null) | undefined, entity_guarantee_op?: ('AND' | 'OR') | undefined, languages?: (Array<'sq' | 'ar' | 'az' | 'ca' | 'zh' | 'hr' | 'cs' | 'da' | 'nl' | 'en' | 'eo' | 'fi' | 'fr' | 'gl' | 'de' | 'el' | 'he' | 'hi' | 'hu' | 'is' | 'id' | 'ia' | 'it' | 'ja' | 'kk' | 'km' | 'ko' | 'ky' | 'ms' | 'mt' | 'mhr' | 'mn' | 'no' | 'or' | 'pl' | 'pt' | 'pa' | 'ro' | 'ru' | 'sr' | 'sk' | 'sl' | 'es' | 'sv' | 'ta' | 'te' | 'th' | 'tr' | 'uk' | 'vi' | 'unknown'> | null) | undefined, countries?: (Array | null) | undefined, countries_blacklist?: (Array | null) | undefined, continents?: (Array<'Africa' | 'Asia' | 'Oceania' | 'Europe' | 'Middle East' | 'North America' | 'South America'> | null) | undefined, sentiment?: ('negative' | 'neutral' | 'positive' | null) | undefined}; export type GetApiV1IndexCountsOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const indexCountItem = z.object({start: z.string(), end: z.string(), count: z.number().int()}); export const getApiV1IndexCountsResponse = z.array(indexCountItem); 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 index counts underlying AskNews * * GET /v1/index_counts */ export const getApiV1IndexCounts = async (args: GetApiV1IndexCountsArgs, options?: GetApiV1IndexCountsOptions) => { const {baseUrl = "https://api.asknews.app", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_datetime != null) query.append("start_datetime", String(args.start_datetime)) if (args.end_datetime != null) query.append("end_datetime", String(args.end_datetime)) for (const item of args.domains ?? []) query.append("domains", String(item)) if (args.sampling != null) query.append("sampling", String(args.sampling)) if (args.time_filter != null) query.append("time_filter", String(args.time_filter)) for (const item of args.categories ?? []) query.append("categories", String(item)) if (args.provocative != null) query.append("provocative", String(args.provocative)) for (const item of args.reporting_voice ?? []) query.append("reporting_voice", String(item)) if (args.bad_domain_url != null) query.append("bad_domain_url", String(args.bad_domain_url)) if (args.page_rank != null) query.append("page_rank", String(args.page_rank)) for (const item of args.string_guarantee ?? []) query.append("string_guarantee", String(item)) if (args.string_guarantee_op != null) query.append("string_guarantee_op", String(args.string_guarantee_op)) for (const item of args.reverse_string_guarantee ?? []) query.append("reverse_string_guarantee", String(item)) for (const item of args.entity_guarantee ?? []) query.append("entity_guarantee", String(item)) if (args.entity_guarantee_op != null) query.append("entity_guarantee_op", String(args.entity_guarantee_op)) for (const item of args.languages ?? []) query.append("languages", String(item)) for (const item of args.countries ?? []) query.append("countries", String(item)) for (const item of args.countries_blacklist ?? []) query.append("countries_blacklist", String(item)) for (const item of args.continents ?? []) query.append("continents", String(item)) if (args.sentiment != null) query.append("sentiment", String(args.sentiment)) if (APIKey !== undefined) headers.set("Authorization", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/index_counts${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(getApiV1IndexCountsResponse, await res.json()) };