import {z} from 'zod' export type GetApiPhonesAvailabilityArgs = {phone_type?: ('android' | 'iphone') | undefined, location?: string | undefined}; export type GetApiPhonesAvailabilityOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const phoneDedicatedTypeAvailabilityEntry = z.object({idle: z.number().int(), phone_type: z.enum(["android", "iphone", "unknown"]), total: z.number().int()}); export const phoneDedicatedAvailability = z.object({by_type: z.array(phoneDedicatedTypeAvailabilityEntry).nullable(), idle: z.number().int(), total: z.number().int()}); export const phoneLocationAvailabilityEntry = z.object({available: z.number().int(), city: z.string(), location: z.string(), region: z.string(), state: z.string(), total: z.number().int()}); export const phoneTypeAvailabilityEntry = z.object({available: z.number().int(), phone_type: z.enum(["android", "iphone", "unknown"]), total: z.number().int()}); export const phoneSharedAvailability = z.object({available: z.number().int(), by_location: z.array(phoneLocationAvailabilityEntry).nullable(), by_type: z.array(phoneTypeAvailabilityEntry).nullable(), total: z.number().int()}); export const phoneAvailabilityResponse = z.object({dedicated: phoneDedicatedAvailability, shared: phoneSharedAvailability}); 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 }; /** * Phone availability summary * * GET /phones/availability */ export const getApiPhonesAvailability = async (args: GetApiPhonesAvailabilityArgs, options?: GetApiPhonesAvailabilityOptions) => { 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.phone_type != null) query.append("phone_type", String(args.phone_type)) if (args.location != null) query.append("location", String(args.location)) if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/phones/availability${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(phoneAvailabilityResponse, await res.json()) };