import {z} from 'zod' export type GetApiV3SeoAuditPageIssuesArgs = {teamId: string, link?: string | undefined, scope?: ('domain' | 'page') | undefined, page?: number | undefined, pageSize?: string | undefined, sortBy?: ('url' | 'statusCode') | undefined, sortDir?: ('asc' | 'desc') | undefined, search?: string | undefined, types?: string | undefined, statuses?: string | undefined}; export type GetApiV3SeoAuditPageIssuesOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV3SeoAuditPageIssuesResponse = z.object({pagination: z.object({page: z.number().int().optional(), pageSize: z.number().int().optional(), total: z.number().int().optional(), totalPages: z.number().int().optional()}).and(z.record(z.string(), z.unknown())).optional(), pageIssues: z.array(z.object({url: z.string().optional(), types: z.array(z.string()).optional(), statusCode: z.number().nullable().optional(), sourcePages: z.array(z.string()).optional()}).and(z.record(z.string(), z.unknown()))).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 }; /** * Per-URL SEO issues across the site * * GET /v3/seo-audit/page-issues * * Also accepts auth: apiKey */ export const getApiV3SeoAuditPageIssues = async (args: GetApiV3SeoAuditPageIssuesArgs, options?: GetApiV3SeoAuditPageIssuesOptions) => { 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.link != null) query.append("link", String(args.link)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.sortDir != null) query.append("sortDir", String(args.sortDir)) if (args.search != null) query.append("search", String(args.search)) if (args.types != null) query.append("types", String(args.types)) if (args.statuses != null) query.append("statuses", String(args.statuses)) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/seo-audit/page-issues${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(getApiV3SeoAuditPageIssuesResponse, await res.json()) };