import {z} from 'zod' export type PhoneLiveViewOptions = {auth?: ('token' | 'org') | undefined, disabled?: boolean | undefined, view_only?: boolean | undefined}; export type PhoneSessionTTLOptions = {idle_timeout_seconds?: number | undefined}; export type PhoneAllocateRequest = {$schema?: string | undefined, accessibility?: boolean | undefined, capture?: boolean | undefined, live_view?: PhoneLiveViewOptions | undefined, name?: string | undefined, phone_id?: string | undefined, phone_type: 'android', pool?: ('shared' | 'dedicated') | undefined, recording?: boolean | undefined, tags?: {[key: string]: string} | undefined, telemetry?: boolean | undefined, ttl?: PhoneSessionTTLOptions | undefined}; export type CreateApiPhonesAllocateArgs = {body: PhoneAllocateRequest}; export type CreateApiPhonesAllocateOptions = {baseUrl: string, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneAllocateResponse = z.object({$schema: z.string().optional(), accessibility: z.boolean(), allocated_at: z.string(), control_url: z.string().optional(), live_view_url: z.string().optional(), phone_id: z.string(), region: z.string().optional(), session_id: z.string(), 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 }; /** * Allocate a phone * * POST /phones:allocate */ export const createApiPhonesAllocate = async (args: CreateApiPhonesAllocateArgs, options: CreateApiPhonesAllocateOptions) => { const {baseUrl, fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/phones:allocate${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(phoneAllocateResponse, await res.json()) };