import {z} from 'zod' export type CaptchaEvaluationType = 'turnstile' | 'cloudflare-challenge' | 'aliexpress' | 'recaptcha' | 'amazon'; export type ImageCaptchaParam = {imageSelector?: string | undefined, inputSelector?: string | undefined}; export type CaptchaEvaluationParams = {captcha?: CaptchaEvaluationType | undefined, captchaType?: CaptchaEvaluationType | undefined, text?: CaptchaEvaluationType | undefined, iterations?: number | undefined, maxIterations?: number | undefined, solverType?: 'visual' | undefined, imageCaptchaParams?: Array | undefined, useUltraStealth?: boolean | undefined}; export type CreateApiApiSessionIdCaptchaEvaluateArgs = {id: string, body: CaptchaEvaluationParams}; export type CreateApiApiSessionIdCaptchaEvaluateOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const captchaEvaluationType = z.enum(["turnstile", "cloudflare-challenge", "aliexpress", "recaptcha", "amazon"]); export const captchaEvaluationPageResult = z.object({url: z.string().optional(), targetId: z.string().nullable().optional(), iterationsRun: z.number().int().optional(), solved: z.boolean().optional(), solvedCaptchas: z.array(captchaEvaluationType).optional(), checkedCaptchas: z.array(captchaEvaluationType).optional(), captchaSolvedCounts: z.record(z.string(), z.number()).optional(), lastSolveTime: z.record(z.string(), z.number()).optional()}); export const captchaEvaluationResponse = z.object({success: z.boolean().optional(), captcha: captchaEvaluationType.nullable().optional(), iterationsRequested: z.number().int().optional(), iterationsRun: z.number().int().optional(), solved: z.boolean().optional(), solvedCaptchas: z.array(captchaEvaluationType).optional(), pages: z.array(captchaEvaluationPageResult).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 }; /** * Run manual CAPTCHA evaluation * * POST /api/session/{id}/captcha/evaluate */ export const createApiApiSessionIdCaptchaEvaluate = async (args: CreateApiApiSessionIdCaptchaEvaluateArgs, options?: CreateApiApiSessionIdCaptchaEvaluateOptions) => { const {baseUrl = "https://api.hyperbrowser.ai", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/session/${encodeURIComponent(String(args.id))}/captcha/evaluate${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(captchaEvaluationResponse, await res.json()) };