import {z} from 'zod' export type GetApiUsErcotRtm60dHdlLdlArgs = {date_from: string, date_to: string, limit?: number | undefined, offset?: number | undefined, timezone?: ('UTC' | 'Europe/Berlin' | 'Europe/London' | 'America/New_York' | 'America/Chicago' | 'America/Los_Angeles') | undefined}; export type GetApiUsErcotRtm60dHdlLdlOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const hDLLDLManOverrideSerializers = z.object({scedTimeStamp: z.string(), repeatedHourFlag: z.string().min(1).max(255), participantName: z.string().min(1).max(255), resourceName: z.string().min(1).max(255), hdlOriginal: z.number(), hdlManual: z.number(), hdlFinal: z.number(), ldlOriginal: z.number(), ldlManual: z.number(), ldlFinal: z.number(), reasonCode: z.string().min(1).max(255)}); export const getApiUsErcotRtm60dHdlLdlResponse = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(hDLLDLManOverrideSerializers)}); 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 }; /** * HDL and LDL manual override summary - RTM * * GET /us/ercot/rtm/60d-hdl-ldl */ export const getApiUsErcotRtm60dHdlLdl = async (args: GetApiUsErcotRtm60dHdlLdlArgs, options?: GetApiUsErcotRtm60dHdlLdlOptions) => { const {baseUrl = "https://api.modoenergy.com/pub/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date_from != null) query.append("date_from", String(args.date_from)) if (args.date_to != null) query.append("date_to", String(args.date_to)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (apiKey !== undefined) headers.set("X-Token", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/us/ercot/rtm/60d-hdl-ldl${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(getApiUsErcotRtm60dHdlLdlResponse, await res.json()) };