import {z} from 'zod' export type GetApiOutagesErcotActiveArgs = {start_time: string, end_time: string, start_date?: (string | null) | undefined, limit?: number | undefined, api_key?: (string | null) | undefined, 'x-api-key'?: (string | null) | undefined}; export type GetApiOutagesErcotActiveOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const substationLocation = z.object({latitude: z.number(), longitude: z.number(), name: z.string()}); export const eRCOTActiveOutage = z.object({outage_identifier: z.string(), change_time_utc: z.string(), outage_status: z.string().nullable().optional(), outage_type: z.string().nullable().optional(), nature_of_work: z.string().nullable().optional(), equipment_type: z.string().nullable().optional(), equipment_name: z.string().nullable().optional(), equipment_from_station_name: z.string().nullable().optional(), equipment_to_station_name: z.string().nullable().optional(), group_label: z.string().nullable().optional(), voltage_level: z.number().int().nullable().optional(), requestor_org_name: z.string().nullable().optional(), breaker_switch_normal_status: z.string().nullable().optional(), breaker_switch_outage_status: z.string().nullable().optional(), planned_start_date_utc: z.string().nullable().optional(), planned_end_date_utc: z.string().nullable().optional(), actual_start_date_utc: z.string().nullable().optional(), actual_end_date_utc: z.string().nullable().optional(), current_interval_start_utc: z.string(), current_interval_end_utc: z.string().nullable().optional(), from_substation: substationLocation.nullable().optional(), to_substation: substationLocation.nullable().optional()}); export const eRCOTActiveOutagesResponse = z.object({truncated: z.boolean(), outages: z.array(eRCOTActiveOutage)}); 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 }; /** * List Ercot Active Outages * * GET /outages/ercot/active */ export const getApiOutagesErcotActive = async (args: GetApiOutagesErcotActiveArgs, options?: GetApiOutagesErcotActiveOptions) => { const {baseUrl = "https://api.gridstatus.io/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.start_time != null) query.append("start_time", String(args.start_time)) if (args.end_time != null) query.append("end_time", String(args.end_time)) if (args.start_date != null) query.append("start_date", String(args.start_date)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.api_key != null) query.append("api_key", String(args.api_key)) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/outages/ercot/active${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(eRCOTActiveOutagesResponse, await res.json()) };