import {z} from 'zod' export type GetApiApiDevicesArgs = {platform?: ('ANDROID' | 'IOS') | undefined, availableOnly?: boolean | undefined}; export type GetApiApiDevicesOptions = {baseUrl: string, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const device = z.object({arn: z.string().optional(), name: z.string().optional(), manufacturer: z.string().optional(), model: z.string().optional(), modelId: z.string().optional(), platform: z.enum(["ANDROID", "IOS"]).optional(), os: z.string().optional(), formFactor: z.enum(["PHONE", "TABLET"]).optional(), availability: z.string().optional(), heapSize: z.number().int().nullable().optional(), memory: z.number().int().nullable().optional(), resolution: z.object({width: z.number().int().optional(), height: z.number().int().optional()}).nullable().optional()}); export const getApiApiDevicesResponse = z.object({devices: z.array(device)}); 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 available devices * * GET /api/devices */ export const getApiApiDevices = async (args: GetApiApiDevicesArgs, options: GetApiApiDevicesOptions) => { const {baseUrl, fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.platform != null) query.append("platform", String(args.platform)) if (args.availableOnly != null) query.append("availableOnly", String(args.availableOnly)) if (ApiKeyAuth !== undefined) headers.set("x-api-key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api/devices${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(getApiApiDevicesResponse, await res.json()) };