import {z} from 'zod' export type GetApiV1DistributionArticlesDomainHitsSurfaceArgs = {domain_names: Array, year: number, month: number}; export type GetApiV1DistributionArticlesDomainHitsSurfaceOptions = {baseUrl?: string | undefined, APIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const domainHitsSurfaceResponse = z.object({hits: z.number().int(), surfaced: z.number().int()}); 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 total hits and surfaced articles for domains * * GET /v1/distribution/articles/domain_hits_surface */ export const getApiV1DistributionArticlesDomainHitsSurface = async (args: GetApiV1DistributionArticlesDomainHitsSurfaceArgs, options?: GetApiV1DistributionArticlesDomainHitsSurfaceOptions) => { const {baseUrl = "https://api.asknews.app", fetch: fetchFn = fetch, APIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") for (const item of args.domain_names ?? []) query.append("domain_names", String(item)) if (args.year != null) query.append("year", String(args.year)) if (args.month != null) query.append("month", String(args.month)) if (APIKey !== undefined) headers.set("Authorization", APIKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/distribution/articles/domain_hits_surface${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(domainHitsSurfaceResponse, await res.json()) };