import {z} from 'zod' export type AccuracyOverviewQuery = {start_date: string, end_date: string, comparison_start_date?: (string | null) | undefined, comparison_end_date?: (string | null) | undefined, category_id: string, topic_ids?: (Array | null) | undefined, exclude_topic_ids?: boolean | undefined, tag_ids?: (Array | null) | undefined, tag_filter_type?: ('all' | 'any') | undefined, include_no_tag?: boolean | undefined, region_ids?: (Array | null) | undefined, platform_ids?: (Array | null) | undefined, persona_ids?: (Array | null) | undefined, include_no_persona?: boolean | undefined, prompt_ids?: (Array | null) | undefined, citation_categories?: (Array | null) | undefined, date_bucket?: (string | null) | undefined, group_by?: ('period' | 'theme') | undefined}; export type CreateApiV1ReportsAccuracyOverviewArgs = {body: AccuracyOverviewQuery}; export type CreateApiV1ReportsAccuracyOverviewOptions = {baseUrl?: string | undefined, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const accuracyTrendPoint: z.ZodType = z.object({date: z.string(), total: z.number().int(), accurate: z.number().int(), ratio: z.number(), prevPeriodData: z.lazy(() => accuracyTrendPoint.nullable().optional())}); export const accuracyScoreBreakdown = z.object({status: z.string(), count: z.number().int(), share: z.number(), countChange: z.number().int().nullable().optional(), shareChange: z.number().nullable().optional()}); export const accuracyThemeTrendPoint = z.object({date: z.string(), total: z.number().int(), accurate: z.number().int(), ratio: z.number()}); export const accuracyThemeTrendSeries = z.object({id: z.string(), label: z.string(), data: z.array(accuracyThemeTrendPoint)}); export const accuracyTrendSeriesMeta = z.object({id: z.string(), label: z.string(), total: z.number().int()}); export const accuracyOverviewResponse = z.object({trendByPeriod: z.array(accuracyTrendPoint), overallAccuracy: z.number(), accuracyChange: z.number().nullable().optional(), scoreBreakdown: z.array(accuracyScoreBreakdown), themeTrend: z.array(accuracyThemeTrendSeries).nullable().optional(), availableSeries: z.array(accuracyTrendSeriesMeta).nullable().optional()}); 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 }; /** * Accuracy Overview * * POST /v1/reports/accuracy/overview * * Also accepts auth: BearerAuth */ export const createApiV1ReportsAccuracyOverview = async (args: CreateApiV1ReportsAccuracyOverviewArgs, options?: CreateApiV1ReportsAccuracyOverviewOptions) => { const {baseUrl = "https://api.tryprofound.com", fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/v1/reports/accuracy/overview${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(accuracyOverviewResponse, await res.json()) };