import {z} from 'zod' export type GetApiPhonesArgs = {ownership: 'dedicated', include_expired?: boolean | undefined, limit?: number | undefined, offset?: number | undefined, search?: string | undefined, status?: (Array<'active' | 'inactive' | 'maintenance' | 'suspended' | 'recovering' | 'out_of_service'> | null) | undefined, phone_type?: (Array<'android' | 'iphone' | 'unknown'> | null) | undefined, rental_expires_after?: string | undefined, rental_expires_before?: string | undefined, last_heartbeat_after?: string | undefined, last_heartbeat_before?: string | undefined, order_by?: string | undefined}; export type GetApiPhonesOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneMessaging = z.object({checked_at: z.string().nullable(), receiving: z.enum(["on", "off", "unknown"]), sending: z.enum(["on", "off", "unknown"]), service: z.enum(["in_service", "no_service", "radio_off", "no_sim", "unknown"])}); export const phoneSummary = z.object({created_at: z.string(), current_session_id: z.string().nullable(), last_heartbeat_at: z.string().nullable(), last_interaction_at: z.string().nullable(), location: z.string().nullable(), messaging: phoneMessaging.optional(), model_id: z.string().nullable(), model_name: z.string().nullable(), nickname: z.string().nullable(), owner_organization_id: z.string().nullable(), ownership_type: z.enum(["shared", "private"]), phone_id: z.string(), phone_name: z.string().nullable(), phone_number: z.string().nullable(), phone_type: z.enum(["android", "iphone", "unknown"]).nullable(), rental_expires_at: z.string().nullable(), status: z.enum(["active", "inactive", "maintenance", "suspended", "recovering", "out_of_service"]), updated_at: z.string()}); export const phonePrivateListResponse = z.object({limit: z.number().int(), offset: z.number().int(), phones: z.array(phoneSummary).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 the org's phones * * GET /phones */ export const getApiPhones = async (args: GetApiPhonesArgs, options?: GetApiPhonesOptions) => { const {baseUrl = "https://api.axilio.ai/api/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.ownership != null) query.append("ownership", String(args.ownership)) if (args.include_expired != null) query.append("include_expired", String(args.include_expired)) 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.status != null) query.append("status", args.status.join(',')) if (args.phone_type != null) query.append("phone_type", args.phone_type.join(',')) if (args.rental_expires_after != null) query.append("rental_expires_after", String(args.rental_expires_after)) if (args.rental_expires_before != null) query.append("rental_expires_before", String(args.rental_expires_before)) if (args.last_heartbeat_after != null) query.append("last_heartbeat_after", String(args.last_heartbeat_after)) if (args.last_heartbeat_before != null) query.append("last_heartbeat_before", String(args.last_heartbeat_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${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(phonePrivateListResponse, await res.json()) };