import {z} from 'zod' export type GetApiV1ProjectsProjectIdGeoTrafficOverviewArgs = {projectId: string, days?: number | undefined, from?: string | undefined, to?: string | undefined, host?: string | undefined, path?: string | undefined, sites?: 'exclude' | undefined}; export type GetApiV1ProjectsProjectIdGeoTrafficOverviewOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const geoTrafficOverviewResponse = z.object({configured: z.boolean(), totals: z.object({crawler: z.number().int(), cited: z.number().int(), aiReferral: z.number().int(), conversions: z.number().int().nullable()}), previousConversions: z.number().int().nullable(), sources: z.array(z.object({source: z.string(), visitorType: z.enum(["crawler", "ai_referral", "human", "unknown"]), agent: z.string(), category: z.string(), confidence: z.string(), visits: z.number().int(), previousVisits: z.number().int().optional(), markdownVisits: z.number().int(), previousMarkdownVisits: z.number().int().optional(), paths: z.number().int(), previousPaths: z.number().int().optional(), lastSeenAt: z.string()})), points: z.array(z.object({day: z.string(), visitorType: z.enum(["crawler", "ai_referral", "human", "unknown"]), source: z.string(), visits: z.number().int()})), organization: z.object({id: z.string(), slug: z.string(), name: z.string(), logo: z.string().nullable()})}); 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 }; /** * Get AI traffic totals and sources * * GET /v1/projects/{projectId}/geo/traffic/overview */ export const getApiV1ProjectsProjectIdGeoTrafficOverview = async (args: GetApiV1ProjectsProjectIdGeoTrafficOverviewArgs, options?: GetApiV1ProjectsProjectIdGeoTrafficOverviewOptions) => { const {baseUrl = "https://api.usenotra.com", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.days != null) query.append("days", String(args.days)) if (args.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.host != null) query.append("host", String(args.host)) if (args.path != null) query.append("path", String(args.path)) if (args.sites != null) query.append("sites", String(args.sites)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectId))}/geo/traffic/overview${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(geoTrafficOverviewResponse, await res.json()) };