import {z} from 'zod' export type GetApiV3UrlInspectionIndexCoverageArgs = {teamId: string}; export type GetApiV3UrlInspectionIndexCoverageOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV3UrlInspectionIndexCoverageResponse = z.object({total: z.number().optional(), indexed: z.number().optional(), notIndexed: z.number().optional(), crawledNotIndexed: z.number().optional(), discoveredNotCrawled: z.number().optional(), pageWithRedirect: z.number().optional(), excludedNoindex: z.number().optional(), alternateCanonical: z.number().optional(), duplicateContent: z.number().optional(), notFound: z.number().optional(), serverError: z.number().optional(), blocked: z.number().optional(), crawlAnomaly: z.number().optional(), error: z.number().optional(), pending: z.number().optional(), lastInspectedAt: z.string().nullable().optional(), inspectionStatus: z.enum(["running", "idle"]).optional(), inspectionProgress: z.number().nullable().optional(), inspectedCount: z.number().nullable().optional(), inspectionTotal: z.number().nullable().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 }; /** * Get Google index coverage tally * * GET /v3/url-inspection/index-coverage * * Also accepts auth: apiKey */ export const getApiV3UrlInspectionIndexCoverage = async (args: GetApiV3UrlInspectionIndexCoverageArgs, options?: GetApiV3UrlInspectionIndexCoverageOptions) => { 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 (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/url-inspection/index-coverage${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(getApiV3UrlInspectionIndexCoverageResponse, await res.json()) };