import {z} from 'zod' export type PromptScheduleSelection = 'default' | 'manual' | string; export type MetricsApiFilters = {schedule_id?: PromptScheduleSelection | undefined, start_date: string, end_date?: string | undefined, models?: (Array<'chatgpt' | 'perplexity' | 'gemini' | 'google_ai_overview' | 'copilot' | 'claude' | 'ai_mode' | 'grok' | 'deepseek' | 'meta_ai' | 'mistral'>) | undefined, prompt_ids?: Array | undefined, competitor_ids?: Array | undefined, location_ids?: Array | undefined, prompt_status?: ('active' | 'paused') | undefined, prompt_type?: ('branded' | 'non_branded') | undefined, saved_view_id?: string | undefined}; export type PositionRequest = {website_id: string, filters: MetricsApiFilters}; export type CreateApiApiV1MetricsPositionCumulativeArgs = {body: PositionRequest}; export type CreateApiApiV1MetricsPositionCumulativeOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const cumulativePositionEntry = z.object({avg_position: z.number().optional(), is_self: z.boolean().optional()}); export const createApiApiV1MetricsPositionCumulativeResponse = z.object({data: z.record(z.string(), cumulativePositionEntry)}); 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 }; /** * Cumulative Position * * POST /api/v1/metrics/position/cumulative */ export const createApiApiV1MetricsPositionCumulative = async (args: CreateApiApiV1MetricsPositionCumulativeArgs, options?: CreateApiApiV1MetricsPositionCumulativeOptions) => { const {baseUrl = "https://api.athenahq.ai", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("x-api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/metrics/position/cumulative${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(createApiApiV1MetricsPositionCumulativeResponse, await res.json()) };