import {z} from 'zod' export type OrderBy = 'asc' | 'desc'; export type GetApiConstraintsArgs = {include_absolute_costs?: boolean | undefined, include_timestamps?: boolean | undefined, include_dataset_metadata?: boolean | undefined, absolute_cost_start_time?: (string | null) | undefined, absolute_cost_end_time?: (string | null) | undefined, 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?: ('constraint_name' | 'market_id' | 'earliest_seen_utc' | 'latest_seen_utc' | null) | undefined, 'x-api-key'?: (string | null) | undefined}; export type GetApiConstraintsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const constraintResponse = z.object({gs_entity_id: z.string(), constraint_name: z.string(), market_id: z.string(), earliest_seen_utc: z.string().nullable(), latest_seen_utc: z.string().nullable(), available_datasets: z.array(z.string()).nullable(), rt_absolute_cost: z.number().nullable().optional(), da_absolute_cost: z.number().nullable().optional(), gs_created_at_utc: z.string().nullable().optional(), gs_updated_at_utc: z.string().nullable().optional()}); export const constraintPricingDatasetSpecResponse = z.object({dataset_id: z.string(), shadow_price_column: z.string()}); export const constraintsListResponse = z.object({data: z.array(constraintResponse), dataset_metadata: z.record(z.string(), z.record(z.string(), z.array(constraintPricingDatasetSpecResponse))).nullable().optional()}); 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 Constraints * * GET /constraints */ export const getApiConstraints = async (args: GetApiConstraintsArgs, options?: GetApiConstraintsOptions) => { 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.include_absolute_costs != null) query.append("include_absolute_costs", String(args.include_absolute_costs)) if (args.include_timestamps != null) query.append("include_timestamps", String(args.include_timestamps)) if (args.include_dataset_metadata != null) query.append("include_dataset_metadata", String(args.include_dataset_metadata)) if (args.absolute_cost_start_time != null) query.append("absolute_cost_start_time", String(args.absolute_cost_start_time)) if (args.absolute_cost_end_time != null) query.append("absolute_cost_end_time", String(args.absolute_cost_end_time)) 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${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(constraintsListResponse, await res.json()) };