import {z} from 'zod' export type GetApiApiV4SystemsArgs = {page?: number | undefined, size?: number | undefined, sort_by?: ('id' | '-id') | undefined}; export type GetApiApiV4SystemsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiApiV4SystemsResponse = z.object({total: z.number().int().optional(), current_page: z.number().int().optional(), size: z.number().int().optional(), count: z.number().int().optional(), items: z.string().optional(), systems: z.array(z.object({system_id: z.number().int().optional(), name: z.string().optional(), public_name: z.string().optional(), timezone: z.string().optional(), address: z.object({city: z.string().optional(), state: z.string().optional(), country: z.string().optional(), postal_code: z.string().optional()}).optional(), connection_type: z.string().optional(), energy_lifetime: z.number().int().optional(), energy_today: z.number().int().optional(), system_size: z.number().optional(), status: z.string().optional(), stage: z.number().int().optional(), last_report_at: z.number().int().optional(), last_energy_at: z.number().int().optional(), operational_at: z.number().int().optional(), attachment_type: z.string().optional(), interconnect_date: z.number().int().optional(), reference: z.string().optional(), other_references: z.array(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 }; /** * Fetch systems * * GET /api/v4/systems */ export const getApiApiV4Systems = async (args: GetApiApiV4SystemsArgs, options?: GetApiApiV4SystemsOptions) => { 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.page != null) query.append("page", String(args.page)) if (args.size != null) query.append("size", String(args.size)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v4/systems${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(getApiApiV4SystemsResponse, await res.json()) };