import {z} from 'zod' export type GetApiBrandIdSitemapPagesArgs = {brand_id: number, domain?: (string | null) | undefined, limit?: number | undefined, offset?: number | undefined, max_depth?: (number | null) | undefined, path_prefix?: (string | null) | undefined, is_priority?: (boolean | null) | undefined, has_optimized_content?: (boolean | null) | undefined, search?: (string | null) | undefined}; export type GetApiBrandIdSitemapPagesOptions = {baseUrl?: string | undefined, HTTPBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const sitemapPageListing = z.object({id: z.number().int(), url: z.string(), title: z.string().nullable(), description: z.string().nullable(), depth: z.number().int(), audit_score: z.number().int().nullable(), canonical_url: z.string().nullable(), is_priority: z.boolean(), has_optimized_content: z.boolean()}); export const sitemapPagesResponse = z.object({total: z.number().int(), offset: z.number().int().optional(), limit: z.number().int().nullable().optional(), items: z.array(sitemapPageListing), domain: z.string(), last_crawl_completed: 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 }; /** * List Sitemap Pages * * GET /{brand_id}/sitemap/pages */ export const getApiBrandIdSitemapPages = async (args: GetApiBrandIdSitemapPagesArgs, options?: GetApiBrandIdSitemapPagesOptions) => { const {baseUrl = "https://api.scrunchai.com/v1", fetch: fetchFn = fetch, HTTPBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.domain != null) query.append("domain", String(args.domain)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.max_depth != null) query.append("max_depth", String(args.max_depth)) if (args.path_prefix != null) query.append("path_prefix", String(args.path_prefix)) if (args.is_priority != null) query.append("is_priority", String(args.is_priority)) if (args.has_optimized_content != null) query.append("has_optimized_content", String(args.has_optimized_content)) if (args.search != null) query.append("search", String(args.search)) if (HTTPBearer !== undefined) headers.set('authorization', `Bearer ${HTTPBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.brand_id))}/sitemap/pages${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(sitemapPagesResponse, await res.json()) };