import {z} from 'zod' export type CreateApiApiV1EstimateBatteryValueArgs = {body: {zipCode: string, monthlyKwh: number, monthlyExportKwh?: number | undefined, currentMonthlyCostDollars?: number | undefined, solarSizeKw?: number | undefined, batteryBrand?: ('tesla' | 'solaredge' | 'enphase' | 'qcells' | 'franklin' | 'other') | undefined, batteryCapacityKwh?: number | undefined}}; export type CreateApiApiV1EstimateBatteryValueOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1EstimateBatteryValueResponse = z.object({status: z.literal("early_access"), message: z.string(), requestId: z.string(), requestedFor: z.object({zipCode: z.string(), monthlyKwh: z.number(), monthlyExportKwh: z.number().nullable(), currentMonthlyCostDollars: z.number().nullable(), solarSizeKw: z.number().nullable(), batteryBrand: z.string().nullable(), batteryCapacityKwh: z.number().nullable()}), publicAlternatives: z.object({batterySurvey: z.string(), batteryAnalysis: z.string(), installerQuotes: z.string()}), expectedResponseShape: z.object({recommendation: z.object({action: z.string(), reason: z.string()}), vppEligibility: z.object({eligible: z.boolean(), customerCreditDollarsPerKwhMonth: z.number(), estimatedAnnualCreditDollars: z.number()}), payback: z.object({estimatedInstalledCostDollars: z.number(), annualBenefitDollars: z.number(), simplePaybackYears: z.number()}), recommendedBattery: z.object({brand: z.string(), capacityKwh: z.number(), rationale: z.string()}), partnerInstaller: z.object({name: z.string(), quoteUrl: z.string()}).nullable(), planPairingSuggestion: z.object({planName: z.string(), reason: z.string()}).nullable()}), estimatedAvailability: 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 Battery Value Estimator * * POST /api/v1/estimate-battery-value */ export const createApiApiV1EstimateBatteryValue = async (args: CreateApiApiV1EstimateBatteryValueArgs, options?: CreateApiApiV1EstimateBatteryValueOptions) => { 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/estimate-battery-value${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(createApiApiV1EstimateBatteryValueResponse, await res.json()) };