import {z} from 'zod' export type GetApiApiV1NewsWikipediaEditorsAggregateArgs = {project?: string | undefined, granularity?: string | undefined, start_date: string, end_date: string, editor_type?: (string | null) | undefined, page_type?: (string | null) | undefined}; export type GetApiApiV1NewsWikipediaEditorsAggregateOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const wikipediaEditorsRow = z.object({timestamp: z.string().nullable().optional(), editors: z.number().int().nullable().optional()}); export const wikipediaEditorsAggregatePayload = z.object({project: z.string(), editor_type: z.string(), page_type: z.string(), granularity: z.string(), start_date: z.string(), end_date: z.string(), rows: z.array(wikipediaEditorsRow)}); export const sugraMeta = z.object({endpoint: z.string(), data_time: z.string(), response_time: z.string(), provider: z.string(), data_age_days: z.number().nullable().optional(), source: z.string().nullable().optional(), attribution: z.string().nullable().optional(), source_url: z.string().nullable().optional(), citation: z.string().nullable().optional(), fallback_used: z.boolean().nullable().optional(), fallback_chain: z.array(z.string()).nullable().optional(), cached: z.boolean().nullable().optional(), stale: z.boolean().nullable().optional(), stale_since: z.string().nullable().optional(), period: z.string().nullable().optional(), notes: z.string().nullable().optional()}); export const envelopeWikipediaEditorsAggregatePayload = z.object({data: wikipediaEditorsAggregatePayload, meta: sugraMeta}); 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 }; /** * Distinct-editor activity * * GET /api/v1/news/wikipedia/editors/aggregate */ export const getApiApiV1NewsWikipediaEditorsAggregate = async (args: GetApiApiV1NewsWikipediaEditorsAggregateArgs, options?: GetApiApiV1NewsWikipediaEditorsAggregateOptions) => { const {baseUrl = "https://sugra.ai", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.project != null) query.append("project", String(args.project)) if (args.granularity != null) query.append("granularity", String(args.granularity)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.end_date != null) query.append("end_date", String(args.end_date)) if (args.editor_type != null) query.append("editor_type", String(args.editor_type)) if (args.page_type != null) query.append("page_type", String(args.page_type)) if (APIKeyHeader !== undefined) headers.set("x-api-key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/news/wikipedia/editors/aggregate${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(envelopeWikipediaEditorsAggregatePayload, await res.json()) };