import {z} from 'zod' export type CreateApiConnectWebviewsGetArgs = {body: {connect_webview_id: string}}; export type CreateApiConnectWebviewsGetOptions = {baseUrl?: string | undefined, clientSession?: string | undefined, fetch?: typeof fetch | undefined}; export const connectWebview = z.object({accepted_capabilities: z.array(z.enum(["lock", "thermostat", "noise_sensor", "access_control", "camera"])), accepted_providers: z.array(z.string()), any_provider_allowed: z.boolean(), authorized_at: z.string().nullable(), automatically_manage_new_devices: z.boolean(), connect_webview_id: z.string(), connected_account_id: z.string().nullable(), created_at: z.string(), custom_metadata: z.record(z.string(), z.union([z.string(), z.boolean()])), custom_redirect_failure_url: z.string().nullable(), custom_redirect_url: z.string().nullable(), customer_key: z.string().optional(), device_selection_mode: z.enum(["none", "single", "multiple"]), login_successful: z.boolean(), selected_provider: z.string().nullable(), status: z.enum(["pending", "failed", "authorized"]), url: z.string(), wait_for_device_creation: z.boolean(), workspace_id: z.string()}); export const createApiConnectWebviewsGetResponse = z.object({connect_webview: connectWebview}); 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 a Connect Webview * * POST /connect_webviews/get * * Also accepts auth: client_session_with_customer, pat_with_workspace, console_session_with_workspace, api_key */ export const createApiConnectWebviewsGet = async (args: CreateApiConnectWebviewsGetArgs, options?: CreateApiConnectWebviewsGetOptions) => { const {baseUrl = "https://connect.getseam.com", fetch: fetchFn = fetch, clientSession} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (clientSession !== undefined) headers.set('authorization', `Bearer ${clientSession}`) const url = `${baseUrl.replace(/\/+$/, '')}/connect_webviews/get${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(createApiConnectWebviewsGetResponse, await res.json()) };