import {z} from 'zod' export type GetApiV1ProjectsProjectIdGeoTrafficJourneyStatsArgs = {projectId: string, days?: number | undefined, from?: string | undefined, to?: string | undefined}; export type GetApiV1ProjectsProjectIdGeoTrafficJourneyStatsOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const geoJourneyStatsResponse = z.object({configured: z.boolean(), sources: z.array(z.object({source: z.string(), visitorType: z.enum(["crawler", "ai_referral", "human", "unknown"]), journeys: z.number().int(), previousJourneys: z.number().int(), pages: z.number().int(), singleFetch: z.number().int(), deepCrawls: z.number().int(), lastSeenAt: z.string().nullable(), daily: z.array(z.object({day: z.string(), journeys: z.number().int()}))})), pages: z.array(z.object({path: z.string(), journeys: z.number().int(), previousJourneys: z.number().int(), entries: z.number().int(), lastSeenAt: z.string().nullable(), daily: z.array(z.object({day: z.string(), journeys: z.number().int()}))})), totalPages: z.number().int(), previousTotalPages: 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 journey statistics * * GET /v1/projects/{projectId}/geo/traffic/journey-stats */ export const getApiV1ProjectsProjectIdGeoTrafficJourneyStats = async (args: GetApiV1ProjectsProjectIdGeoTrafficJourneyStatsArgs, options?: GetApiV1ProjectsProjectIdGeoTrafficJourneyStatsOptions) => { 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 (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/projects/${encodeURIComponent(String(args.projectId))}/geo/traffic/journey-stats${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(geoJourneyStatsResponse, await res.json()) };