import {z} from 'zod' export type GetApiPhonesSessionsSessionIdArgs = {session_id: string}; export type GetApiPhonesSessionsSessionIdOptions = {baseUrl: string, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneSessionDetailResponse = z.object({$schema: z.string().optional(), accessibility: z.boolean(), agent_id: z.string().optional(), agent_name: z.string().optional(), allocated_at: z.string(), allocated_by: z.enum(["dashboard_run", "api_run", "interactive"]).optional(), capture_enabled: z.boolean(), deallocated_at: z.string().optional(), end_reason: z.string().optional(), is_dedicated_phone: z.boolean(), location: z.string().optional(), model_name: z.string().optional(), name: z.string().optional(), phone_id: z.string(), phone_name: z.string().optional(), phone_nickname: z.string().optional(), phone_status: z.enum(["active", "inactive", "maintenance", "suspended", "recovering", "out_of_service"]), phone_type: z.enum(["android", "iphone", "unknown"]).optional(), recording_status: z.enum(["ready", "pending", "expired", "off", "unavailable"]), recording_url: z.string().optional(), session_id: z.string(), source: z.enum(["agent", "interactive"]), status: z.enum(["active", "completed", "cancelled", "expired"]), tags: z.record(z.string(), z.string()).optional(), telemetry_disabled: z.boolean(), telemetry_url: z.string().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 a session * * GET /phones/sessions/{session_id} */ export const getApiPhonesSessionsSessionId = async (args: GetApiPhonesSessionsSessionIdArgs, options: GetApiPhonesSessionsSessionIdOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/phones/sessions/${encodeURIComponent(String(args.session_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(phoneSessionDetailResponse, await res.json()) };