import {z} from 'zod' export type V3Signals = 'co2_moer' | 'health_damage'; export type V3HistoricalSignals = 'co2_aoer'; export type GetApiV3MyAccessArgs = {region?: (string | null) | undefined, signal_type?: (V3Signals | unknown | V3HistoricalSignals | null) | undefined}; export type GetApiV3MyAccessOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const v3Signals = z.enum(["co2_moer", "health_damage"]); export const v3HistoricalSignals = z.literal("co2_aoer"); export const modelTypes = z.enum(["interchange", "average", "binned_regression", "heatrate", "proxy", "synthetic_proxy", "forecast", "synthetic_vre_potential"]); export const hCHistoricalModel = z.object({model: z.string(), data_start: z.string(), data_end: z.string().nullable().optional(), train_start: z.string(), train_end: z.string(), type: modelTypes}); export const hCForecastModel = z.object({model: z.string(), data_start: z.string(), data_end: z.string().nullable().optional(), train_start: z.string(), train_end: z.string(), historical_model: z.string().nullable()}); export const hCEndpoint = z.object({endpoint: z.enum(["v3/forecast", "v3/historical"]), models: z.array(z.union([hCHistoricalModel, hCForecastModel]))}); export const hCRegion = z.object({region: z.string(), region_full_name: z.string(), parent: z.string().nullable().optional(), data_point_period_seconds: z.union([z.literal(3600), z.literal(300)]), endpoints: z.array(hCEndpoint)}); export const hCSignal = z.object({signal_type: z.union([v3Signals, v3HistoricalSignals, z.unknown()]), regions: z.array(hCRegion)}); export const pYHypercube = z.object({signal_types: z.array(hCSignal)}); 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 }; /** * Get information about what your account can access * * GET /v3/my-access */ export const getApiV3MyAccess = async (args: GetApiV3MyAccessArgs, options?: GetApiV3MyAccessOptions) => { const {baseUrl = "https://api.watttime.org", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.region != null) query.append("region", String(args.region)) if (args.signal_type != null) query.append("signal_type", String(args.signal_type)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v3/my-access${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(pYHypercube, await res.json()) };