import {z} from 'zod' export type GetApiV3AiTrackerVisibilityBrandsArgs = {teamId?: string | undefined, platform?: string | undefined, competitor?: string | undefined, country?: string | undefined, startDate?: string | undefined, endDate?: string | undefined, days?: number | undefined, promptBrand?: string | undefined, tracking?: string | undefined, intent?: string | undefined, limit?: string | undefined, onlyYou?: ('true' | 'false') | undefined, search?: string | undefined}; export type GetApiV3AiTrackerVisibilityBrandsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV3AiTrackerVisibilityBrandsResponse = z.object({meta: z.object({teamInfo: z.record(z.string(), z.unknown()).optional(), appliedFilters: z.record(z.string(), z.unknown()).optional(), totalPrompts: z.number().optional(), activePrompts: z.number().optional(), activeRegionRuns: z.number().optional(), totalResponses: z.number().optional(), promptStatus: z.record(z.string(), z.unknown()).optional()}).and(z.record(z.string(), z.unknown())).optional(), brands: z.record(z.string(), z.unknown()).optional(), total: z.number().optional(), truncated: z.boolean().optional()}).and(z.record(z.string(), z.unknown())); 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 }; /** * Brand leaderboard across the filtered dataset * * GET /v3/ai-tracker/visibility/brands * * Also accepts auth: apiKey */ export const getApiV3AiTrackerVisibilityBrands = async (args: GetApiV3AiTrackerVisibilityBrandsArgs, options?: GetApiV3AiTrackerVisibilityBrandsOptions) => { const {baseUrl = "https://api.snowseo.com", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.teamId != null) query.append("teamId", String(args.teamId)) if (args.platform != null) query.append("platform", String(args.platform)) if (args.competitor != null) query.append("competitor", String(args.competitor)) if (args.country != null) query.append("country", String(args.country)) if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.days != null) query.append("days", String(args.days)) if (args.promptBrand != null) query.append("promptBrand", String(args.promptBrand)) if (args.tracking != null) query.append("tracking", String(args.tracking)) if (args.intent != null) query.append("intent", String(args.intent)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.onlyYou != null) query.append("onlyYou", String(args.onlyYou)) if (args.search != null) query.append("search", String(args.search)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/ai-tracker/visibility/brands${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(getApiV3AiTrackerVisibilityBrandsResponse, await res.json()) };