import {z} from 'zod' export type GetApiPhonesSessionsArgs = {limit?: number | undefined, offset?: number | undefined, search?: string | undefined, agent_id?: string | undefined, status?: (Array<'active' | 'completed' | 'cancelled' | 'expired'> | null) | undefined, source?: (Array<'agent' | 'interactive'> | null) | undefined, dedicated?: (Array<'shared' | 'dedicated'> | null) | undefined, allocated_after?: string | undefined, allocated_before?: string | undefined, deallocated_after?: string | undefined, deallocated_before?: string | undefined, order_by?: string | undefined}; export type GetApiPhonesSessionsOptions = {baseUrl: string, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneSessionListItem = z.object({agent_id: z.string().optional(), agent_name: z.string().optional(), allocated_at: z.string(), allocated_by: z.enum(["dashboard_run", "api_run", "interactive"]).optional(), deallocated_at: z.string().optional(), duration_seconds: z.number().int().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_type: z.enum(["android", "iphone", "unknown"]).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()}); export const phoneSessionListResponse = z.object({$schema: z.string().optional(), history_unavailable: z.boolean().optional(), limit: z.number().int(), offset: z.number().int(), sessions: z.array(phoneSessionListItem).nullable(), total: z.number().int()}); 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 sessions * * GET /phones/sessions */ export const getApiPhonesSessions = async (args: GetApiPhonesSessionsArgs, options: GetApiPhonesSessionsOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.search != null) query.append("search", String(args.search)) if (args.agent_id != null) query.append("agent_id", String(args.agent_id)) if (args.status != null) query.append("status", args.status.join(',')) if (args.source != null) query.append("source", args.source.join(',')) if (args.dedicated != null) query.append("dedicated", args.dedicated.join(',')) if (args.allocated_after != null) query.append("allocated_after", String(args.allocated_after)) if (args.allocated_before != null) query.append("allocated_before", String(args.allocated_before)) if (args.deallocated_after != null) query.append("deallocated_after", String(args.deallocated_after)) if (args.deallocated_before != null) query.append("deallocated_before", String(args.deallocated_before)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/phones/sessions${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(phoneSessionListResponse, await res.json()) };