import {z} from 'zod' export type CreateApiApiV1GetSmtDataArgs = {body: {requestId: string, includeIntervals?: boolean | undefined, months?: number | undefined}}; export type CreateApiApiV1GetSmtDataOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiApiV1GetSmtDataResponse = z.object({status: z.enum(["needs_clarification", "pending", "complete", "error"]), requestId: z.string().nullable(), reused: z.boolean(), step: z.string(), checkAfterSeconds: z.number().optional(), esiId: z.string().nullable(), utility: z.string().nullable(), serviceAddress: z.string().nullable(), dataStart: z.string().nullable(), dataEnd: z.string().nullable(), readingCount: z.number(), monthlyAggregates: z.array(z.object({month: z.string(), totalImportsKwh: z.number(), totalExportsKwh: z.number(), netKwh: z.number(), peakDemandKw: z.number(), daysOfData: z.number()})), avgMonthlyImportKwh: z.number().nullable(), avgMonthlyExportKwh: z.number().nullable(), peakDemandKw: z.number().nullable(), hasSolarExports: z.boolean().nullable(), intervals: z.array(z.object({timestamp: z.string(), importKwh: z.number(), exportKwh: z.number()})).optional(), intervalsTruncated: z.boolean().optional(), exports: z.object({csv: z.string(), aurora: z.string(), opensolar: z.string(), solargraf: z.string()}).optional(), errorCode: z.string().optional(), errorMessage: z.string().optional(), candidates: z.array(z.object({esiId: z.string(), formatted: z.string(), utility: z.string().nullable()})).optional(), summary: 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 }; /** * Get Smart Meter Texas Data * * POST /api/v1/get-smt-data */ export const createApiApiV1GetSmtData = async (args: CreateApiApiV1GetSmtDataArgs, options?: CreateApiApiV1GetSmtDataOptions) => { 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-smt-data${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(createApiApiV1GetSmtDataResponse, await res.json()) };