import {z} from 'zod' export type GetApiApiV4SystemsSystemIdTelemetryBatteryArgs = {system_id: number, start_at?: number | undefined, granularity?: ('5mins' | '15mins' | 'day' | 'week') | undefined}; export type GetApiApiV4SystemsSystemIdTelemetryBatteryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV4SystemsSystemIdTelemetryBatteryResponse = z.object({system_id: z.number().int().optional(), granularity: z.string().optional(), total_devices: z.number().int().optional(), start_at: z.number().int().optional(), end_at: z.number().int().optional(), items: z.string().optional(), intervals: z.array(z.object({end_at: z.number().int().optional(), charge: z.object({enwh: z.number().int().optional(), devices_reporting: z.number().int().optional()}).optional(), discharge: z.object({enwh: z.number().int().optional(), devices_reporting: z.number().int().optional()}).optional(), soc: z.object({percent: z.number().int().optional(), devices_reporting: z.number().int().optional()}).optional()})).optional(), last_reported_aggregate_soc: 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 }; /** * Retrieves telemetry for all batteries for a system * * GET /api/v4/systems/{system_id}/telemetry/battery */ export const getApiApiV4SystemsSystemIdTelemetryBattery = async (args: GetApiApiV4SystemsSystemIdTelemetryBatteryArgs, options?: GetApiApiV4SystemsSystemIdTelemetryBatteryOptions) => { const {baseUrl = "https://api-qa2.enphaseenergy.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_at != null) query.append("start_at", String(args.start_at)) if (args.granularity != null) query.append("granularity", String(args.granularity)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/systems/${encodeURIComponent(String(args.system_id))}/telemetry/battery${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(getApiApiV4SystemsSystemIdTelemetryBatteryResponse, await res.json()) };