import {z} from 'zod' export type GetApiOutagesPjmEquipmentArgs = {search?: (string | null) | undefined, limit?: number | undefined, equipment_types?: (string | null) | undefined, min_voltage_kv?: (number | null) | undefined, api_key?: (string | null) | undefined, 'x-api-key'?: (string | null) | undefined}; export type GetApiOutagesPjmEquipmentOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const pJMEquipmentSearchHit = z.object({equipment_type: z.string(), equipment_name: z.string(), voltage_kv: z.number().nullable().optional(), latest_seen_utc: z.string().nullable().optional()}); export const pJMEquipmentSearchResponse = z.object({results: z.array(pJMEquipmentSearchHit)}); 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 }; /** * Search Pjm Equipment * * GET /outages/pjm/equipment */ export const getApiOutagesPjmEquipment = async (args: GetApiOutagesPjmEquipmentArgs, options?: GetApiOutagesPjmEquipmentOptions) => { 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.search != null) query.append("search", String(args.search)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.equipment_types != null) query.append("equipment_types", String(args.equipment_types)) if (args.min_voltage_kv != null) query.append("min_voltage_kv", String(args.min_voltage_kv)) 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/pjm/equipment${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(pJMEquipmentSearchResponse, await res.json()) };