import {z} from 'zod' export type BrowserUserAgentType = 'windows' | 'macos' | 'linux'; export type BrowserProfileType = 'light' | 'stealth' | 'tf-browser' | 'tf-browser-154'; export type ShutdownMode = 'on_disconnect' | 'on_inactivity_timeout'; export type TetraProxy = {type?: 'tetra' | undefined, country_code?: string | undefined}; export type CustomProxy = {type?: 'custom' | undefined, url: string, username?: (string | null) | undefined, password?: (string | null) | undefined}; export type BrowserRequest = {browser_ua_preset?: BrowserUserAgentType | undefined, browser_profile?: BrowserProfileType | undefined, profile_id?: (string | null) | undefined, shutdown_mode?: ShutdownMode | undefined, inactivity_timeout_seconds?: number | undefined, session_max_duration_seconds?: (number | null) | undefined, proxy?: (TetraProxy | CustomProxy | null) | undefined, sub_user_id?: (string | null) | undefined, request_origin?: (string | null) | undefined, branding?: boolean | undefined, browser_startup_url?: (string | 'about:blank' | null) | undefined}; export type CreateApiTetraSessionsArgs = {body: BrowserRequest | null}; export type CreateApiTetraSessionsOptions = {baseUrl: string, APIKeyHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const browserSession = z.object({session_id: z.string(), cdp_url: z.string(), base_url: z.string()}); 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 }; /** * Create Session * * POST /tetra/sessions */ export const createApiTetraSessions = async (args: CreateApiTetraSessionsArgs, options: CreateApiTetraSessionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIKeyHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APIKeyHeader !== undefined) headers.set("X-API-Key", APIKeyHeader) const url = `${baseUrl.replace(/\/+$/, '')}/tetra/sessions${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(browserSession, await res.json()) };