import {z} from 'zod' export type GetApiV3IntegrationsGscWebsiteTrafficStatsArgs = {teamId?: string | undefined, startDate?: string | undefined, endDate?: string | undefined, days?: number | undefined, device?: string | undefined, type?: string | undefined, country?: string | undefined, includePrevious?: string | undefined}; export type GetApiV3IntegrationsGscWebsiteTrafficStatsOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV3IntegrationsGscWebsiteTrafficStatsResponse = z.object({totalClicks: z.number().optional(), totalImpressions: z.number().optional(), averageCTR: z.number().optional(), averagePosition: z.number().optional(), clicksTrend: z.array(z.record(z.string(), z.unknown())).optional(), impressionsTrend: z.array(z.record(z.string(), z.unknown())).optional(), ctrTrend: z.array(z.record(z.string(), z.unknown())).optional(), positionTrend: z.array(z.record(z.string(), z.unknown())).optional(), prevClicksTrend: z.array(z.record(z.string(), z.unknown())).optional(), prevImpressionsTrend: z.array(z.record(z.string(), z.unknown())).optional(), prevCtrTrend: z.array(z.record(z.string(), z.unknown())).optional(), prevPositionTrend: z.array(z.record(z.string(), z.unknown())).optional(), clicksChange: z.record(z.string(), z.unknown()).optional(), impressionsChange: z.record(z.string(), z.unknown()).optional(), ctrChange: z.record(z.string(), z.unknown()).optional(), positionChange: z.record(z.string(), z.unknown()).optional(), coverage: z.record(z.string(), z.unknown()).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 }; /** * Get GSC traffic stats * * GET /v3/integrations/gsc/website-traffic/stats * * Also accepts auth: apiKey */ export const getApiV3IntegrationsGscWebsiteTrafficStats = async (args: GetApiV3IntegrationsGscWebsiteTrafficStatsArgs, options?: GetApiV3IntegrationsGscWebsiteTrafficStatsOptions) => { 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.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.device != null) query.append("device", String(args.device)) if (args.type != null) query.append("type", String(args.type)) if (args.country != null) query.append("country", String(args.country)) if (args.includePrevious != null) query.append("includePrevious", String(args.includePrevious)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/integrations/gsc/website-traffic/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(getApiV3IntegrationsGscWebsiteTrafficStatsResponse, await res.json()) };