import {z} from 'zod' export type CreateApiApiV1GetResidentialLoadProfileArgs = {body: {zipCode?: string | undefined, weatherZone?: ('COAST' | 'EAST' | 'FWEST' | 'NCENT' | 'NORTH' | 'SCENT' | 'SOUTH' | 'WEST') | undefined, profileType?: ('RESHIWR' | 'RESHIDG' | 'RESHIPV' | 'RESHIWD') | undefined, year?: number | undefined, granularity?: ('monthly_hourly' | 'monthly_totals' | 'annual_hourly') | undefined}}; export type CreateApiApiV1GetResidentialLoadProfileOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1GetResidentialLoadProfileResponse = z.object({resolvedWeatherZone: z.string(), profileType: z.string(), year: z.number(), granularity: z.string(), annualTotalKwh: z.number(), peakHourlyKw: z.number(), monthlyTotalsKwh: z.array(z.number()), intervals: z.union([z.array(z.object({month: z.number(), hour: z.number(), avgKwh: z.number()})), z.array(z.object({month: z.number(), totalKwh: z.number()})), z.array(z.object({hourOfYear: z.number(), date: z.string(), hour: z.number(), kwh: z.number()}))]), source: z.object({provider: z.string(), url: z.string(), notes: z.string()})}); 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 }; /** * Texas Residential Load Profile (ERCOT) * * POST /api/v1/get-residential-load-profile */ export const createApiApiV1GetResidentialLoadProfile = async (args: CreateApiApiV1GetResidentialLoadProfileArgs, options?: CreateApiApiV1GetResidentialLoadProfileOptions) => { const {baseUrl = "https://meterplan.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/get-residential-load-profile${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiApiV1GetResidentialLoadProfileResponse, await res.json()) };