import {z} from 'zod' export type GetApiBrandIdPageAuditsArgs = {brand_id: number, status?: ('pending' | 'running' | 'completed' | 'failed' | null) | undefined, url?: (string | null) | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiBrandIdPageAuditsOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const pageTestListing = z.object({request_id: z.string(), brand_id: z.number().int().nullable().optional(), url: z.string(), status: z.enum(["pending", "running", "completed", "failed"]), created_at: z.string(), updated_at: z.string()}); export const aggregationGranularity = z.enum(["daily", "weekly", "monthly"]); export const topDomainSummary = z.object({domain: z.string(), domain_owner: z.string(), observation_count: z.number().int()}); export const ownerTimeSeriesPoint = z.object({time_bucket: z.string(), brand: z.number().int(), competitor: z.number().int(), other: z.number().int()}); export const segmentTotal = z.object({name: z.string(), observation_count: z.number().int()}); export const topDomainsMetadata = z.object({domains: z.array(topDomainSummary), grand_total: z.number().int(), owner_totals: z.record(z.string(), z.number().int()), owner_time_series: z.array(ownerTimeSeriesPoint).nullable().optional(), segment_totals: z.array(segmentTotal).optional()}); export const timeSeriesMetadata = z.object({aggregation_granularity: aggregationGranularity, period_count: z.number().int(), start_date: z.string(), end_date: z.string(), top_domains: topDomainsMetadata.nullable().optional()}); export const collectionResponsePageTestListing = z.object({total: z.number().int(), offset: z.number().int().optional(), limit: z.number().int().nullable().optional(), items: z.array(pageTestListing), metadata: timeSeriesMetadata.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 }; /** * List Page Audits * * GET /{brand_id}/page-audits */ export const getApiBrandIdPageAudits = async (args: GetApiBrandIdPageAuditsArgs, options?: GetApiBrandIdPageAuditsOptions) => { const {baseUrl = "https://api.scrunchai.com/v1", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.status != null) query.append("status", String(args.status)) if (args.url != null) query.append("url", String(args.url)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.brand_id))}/page-audits${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(collectionResponsePageTestListing, await res.json()) };