import {z} from 'zod' export type GetApiApiV4SystemsSystemIdDevicesArgs = {system_id: number}; export type GetApiApiV4SystemsSystemIdDevicesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV4SystemsSystemIdDevicesResponse = z.object({system_id: z.number().int().optional(), total_devices: z.number().int().optional(), items: z.string().optional(), devices: z.record(z.string(), z.array(z.object({id: z.number().int().optional(), last_report_at: z.number().int().optional(), name: z.string().optional(), serial_number: z.string().optional(), part_number: z.string().optional(), emu_sw_version: z.string().optional(), sku: z.string().optional(), model: z.string().optional(), status: z.string().optional(), active: z.string().optional(), state: z.string().optional(), config_type: z.string().optional(), config_phase: z.string().optional(), product_name: z.string().optional(), feoc_compliant: z.boolean().optional(), dom_compliant: z.boolean().optional(), warranty_expiration_date: z.string().optional(), warranty_status: z.string().optional()}))).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 }; /** * Retrieves devices for a given system * * GET /api/v4/systems/{system_id}/devices */ export const getApiApiV4SystemsSystemIdDevices = async (args: GetApiApiV4SystemsSystemIdDevicesArgs, options?: GetApiApiV4SystemsSystemIdDevicesOptions) => { const {baseUrl = "https://api-qa2.enphaseenergy.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/systems/${encodeURIComponent(String(args.system_id))}/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(getApiApiV4SystemsSystemIdDevicesResponse, await res.json()) };