import {z} from 'zod' export type OrderBy = 'asc' | 'desc'; export type GetApiConstraintsContingenciesArgs = {api_key?: (string | null) | undefined, isos?: (string | null) | undefined, active_after?: (string | null) | undefined, limit?: (number | null) | undefined, sort_order?: (OrderBy | null) | undefined, search?: (string | null) | undefined, sort_column?: ('contingency_name' | 'market_id' | 'earliest_seen_utc' | 'latest_seen_utc' | null) | undefined, 'x-api-key'?: (string | null) | undefined}; export type GetApiConstraintsContingenciesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const contingencyListResponse = z.object({gs_entity_id: z.string(), contingency_name: z.string(), market_id: z.string().nullable(), earliest_seen_utc: z.string().nullable(), latest_seen_utc: z.string().nullable(), available_datasets: z.array(z.string()).nullable(), gs_created_at_utc: z.string(), gs_updated_at_utc: z.string()}); export const getApiConstraintsContingenciesResponse = z.array(contingencyListResponse); 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 Contingencies * * GET /constraints/contingencies */ export const getApiConstraintsContingencies = async (args: GetApiConstraintsContingenciesArgs, options?: GetApiConstraintsContingenciesOptions) => { 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.api_key != null) query.append("api_key", String(args.api_key)) if (args.isos != null) query.append("isos", String(args.isos)) if (args.active_after != null) query.append("active_after", String(args.active_after)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.sort_order != null) query.append("sort_order", String(args.sort_order)) if (args.search != null) query.append("search", String(args.search)) if (args.sort_column != null) query.append("sort_column", String(args.sort_column)) if (args['x-api-key'] != null) headers.set("x-api-key", String(args['x-api-key'])) const url = `${baseUrl.replace(/\/+$/, '')}/constraints/contingencies${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(getApiConstraintsContingenciesResponse, await res.json()) };