import {z} from 'zod' export type GetApiV1SessionsSessionIdWindowsArgs = {sessionId: string}; export type GetApiV1SessionsSessionIdWindowsOptions = {baseUrl?: string | undefined, defaultValue?: string | undefined, fetch?: typeof fetch | undefined}; export const windowId = z.object({targetId: z.string(), title: z.string().optional(), url: z.string().optional(), windowId: z.string()}); export const windowsWithMeta = z.object({windows: z.array(windowId).optional()}); export const issue = z.object({code: z.string().optional(), details: z.record(z.string(), z.unknown()).optional(), message: z.string(), reason: z.string().optional()}); export const envelopeDefaultMeta = z.object({requestId: z.string().optional()}); export const windowsResponse = z.object({data: windowsWithMeta, errors: z.array(issue).optional(), meta: envelopeDefaultMeta, warnings: z.array(issue).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 }; /** * List windows * * GET /v1/sessions/{sessionId}/windows */ export const getApiV1SessionsSessionIdWindows = async (args: GetApiV1SessionsSessionIdWindowsArgs, options?: GetApiV1SessionsSessionIdWindowsOptions) => { const {baseUrl = "https://api.airtop.ai/api", fetch: fetchFn = fetch, defaultValue} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (defaultValue !== undefined) headers.set('authorization', `Bearer ${defaultValue}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/sessions/${encodeURIComponent(String(args.sessionId))}/windows${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(windowsResponse, await res.json()) };