import {z} from 'zod' export type GetApiV2FrequencyArgs = {region?: string | undefined, start?: string | undefined, end?: string | undefined}; export type GetApiV2FrequencyOptions = {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 }; /** * Grid frequency (RG Continental Europe), 1 second resolution * * GET /v2/frequency */ export const getApiV2Frequency = async (args: GetApiV2FrequencyArgs, options: GetApiV2FrequencyOptions) => { const {baseUrl, fetch: fetchFn = fetch} = 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.start != null) query.append("start", String(args.start)) if (args.end != null) query.append("end", String(args.end)) const url = `${baseUrl.replace(/\/+$/, '')}/v2/frequency${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()) };