import {z} from 'zod' export type GetApiApiWebCrawlIdArgs = {id: string, page?: number | undefined, batchSize?: number | undefined}; export type GetApiApiWebCrawlIdOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const jobStatus = z.enum(["pending", "running", "completed", "failed", "stopped"]); export const pageStatus = z.enum(["completed", "failed", "pending", "running"]); export const brandingProfile = z.object({colorScheme: z.string().optional(), colors: z.object({}).optional(), fonts: z.array(z.object({family: z.string().optional(), role: z.string().optional()})).optional(), typography: z.object({}).optional(), spacing: z.object({}).optional(), components: z.object({}).optional(), images: z.object({}).optional(), personality: z.object({}).optional(), designSystem: z.object({}).optional(), confidence: z.object({}).optional()}); export const webCrawlPageData = z.object({url: z.string(), status: pageStatus, error: z.string().nullable().optional(), metadata: z.record(z.string(), z.union([z.string(), z.array(z.string())])).optional(), markdown: z.string().optional(), html: z.string().optional(), links: z.array(z.string()).optional(), screenshot: z.string().optional(), json: z.object({}).optional(), branding: brandingProfile.optional()}); export const webCrawlJobResponse = z.object({jobId: z.string(), status: jobStatus, error: z.string().nullable().optional(), totalPages: z.number().int().gte(0).optional(), totalPageBatches: z.number().int().gte(0).optional(), currentPageBatch: z.number().int().gte(0).optional(), batchSize: z.number().int().gte(1).optional(), data: z.array(webCrawlPageData).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 web crawl job results * * GET /api/web/crawl/{id} */ export const getApiApiWebCrawlId = async (args: GetApiApiWebCrawlIdArgs, options?: GetApiApiWebCrawlIdOptions) => { const {baseUrl = "https://api.hyperbrowser.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page != null) query.append("page", String(args.page)) if (args.batchSize != null) query.append("batchSize", String(args.batchSize)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/web/crawl/${encodeURIComponent(String(args.id))}${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(webCrawlJobResponse, await res.json()) };