import {z} from 'zod' export type BiddingZoneV2 = 'DE-LU' | 'AT' | 'BE' | 'BG' | 'CH' | 'CZ' | 'DK1' | 'DK2' | 'EE' | 'ES' | 'FI' | 'FR' | 'GR' | 'HR' | 'HU' | 'IT-Calabria' | 'IT-Centre-North' | 'IT-Centre-South' | 'IT-North' | 'IT-SACOAC' | 'IT-SACODC' | 'IT-Sardinia' | 'IT-Sicily' | 'IT-South' | 'LT' | 'LV' | 'ME' | 'NL' | 'NO1' | 'NO2' | 'NO2NSL' | 'NO3' | 'NO4' | 'NO5' | 'PL' | 'PT' | 'RO' | 'RS' | 'SE1' | 'SE2' | 'SE3' | 'SE4' | 'SI' | 'SK' | 'DE-AT-LU' | 'IE(SEM)' | 'UA-IPS' | 'UA-BEI' | 'IT-Brindisi' | 'IT-Foggia' | 'IT-GR' | 'IT-North-AT' | 'IT-North-CH' | 'IT-North-SI' | 'IT-North-FR' | 'IT-Priolo' | 'IT-Rossano'; export type GetApiV2PriceCurrentArgs = {bzn?: BiddingZoneV2 | undefined}; export type GetApiV2PriceCurrentOptions = {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 }; /** * Day-ahead spot market price of the current interval * * GET /v2/price_current */ export const getApiV2PriceCurrent = async (args: GetApiV2PriceCurrentArgs, options: GetApiV2PriceCurrentOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.bzn != null) query.append("bzn", String(args.bzn)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/price_current${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()) };