import {z} from 'zod' export type GetApiApiV4SystemsConfigSystemIdLoadControlArgs = {system_id: string}; export type GetApiApiV4SystemsConfigSystemIdLoadControlOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV4SystemsConfigSystemIdLoadControlResponse = z.object({system_id: z.number().int().optional(), load_control_data: z.array(z.object({name: z.string().optional(), load_name: z.string().optional(), owner_can_override: z.string().optional(), mode: z.string().optional(), soc_low: z.number().int().optional(), soc_high: z.number().int().optional(), status: z.string().optional(), essential_start_time: z.number().int().optional(), essential_end_time: z.number().int().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 }; /** * Returns the current load control settings of a system * * GET /api/v4/systems/config/{system_id}/load_control */ export const getApiApiV4SystemsConfigSystemIdLoadControl = async (args: GetApiApiV4SystemsConfigSystemIdLoadControlArgs, options?: GetApiApiV4SystemsConfigSystemIdLoadControlOptions) => { 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/config/${encodeURIComponent(String(args.system_id))}/load_control${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(getApiApiV4SystemsConfigSystemIdLoadControlResponse, await res.json()) };