import {z} from 'zod' export type CountryV2 = 'ch' | 'eu' | 'all' | 'at' | 'ba' | 'be' | 'bg' | 'cy' | 'cz' | 'de' | 'dk' | 'ee' | 'es' | 'fi' | 'fr' | 'ge' | 'gr' | 'hr' | 'hu' | 'ie' | 'it' | 'lt' | 'lu' | 'lv' | 'md' | 'me' | 'mk' | 'nl' | 'no' | 'pl' | 'pt' | 'ro' | 'rs' | 'se' | 'si' | 'sk' | 'ua' | 'uk' | 'xk'; export type TimeStepV2 = 'yearly' | 'monthly'; export type GetApiV2InstalledPowerArgs = {country?: CountryV2 | undefined, time_step?: TimeStepV2 | undefined, installation_decommission?: boolean | undefined}; export type GetApiV2InstalledPowerOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const seriesDescriptorV2 = z.object({id: z.string(), name: z.string(), unit: z.string().nullable().optional(), description: z.string().nullable().optional()}); export const dataPointV2 = z.object({timestamp: z.string(), values: z.record(z.string(), z.union([z.number(), z.number().int()]).nullable())}); export const timeSeriesResponseV2 = z.object({schema_version: z.string(), endpoint: z.string(), country: z.string().nullable().optional(), bidding_zone: z.string().nullable().optional(), timezone: z.string(), resolution: z.string().nullable().optional(), interval_minutes: z.number().int().nullable().optional(), unit: z.string().nullable().optional(), generated_at: z.string(), available_from: z.string().nullable().optional(), available_until: z.string().nullable().optional(), series: z.array(seriesDescriptorV2), data: z.array(dataPointV2), attributes: z.record(z.string(), z.string()).optional(), license: z.string().nullable().optional(), deprecated: z.boolean()}); 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 }; /** * Installed power per production type * * GET /v2/installed_power */ export const getApiV2InstalledPower = async (args: GetApiV2InstalledPowerArgs, options: GetApiV2InstalledPowerOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.country != null) query.append("country", String(args.country)) if (args.time_step != null) query.append("time_step", String(args.time_step)) if (args.installation_decommission != null) query.append("installation_decommission", String(args.installation_decommission)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/installed_power${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(timeSeriesResponseV2, await res.json()) };