import {z} from 'zod' export type GetApiForecastAvailabilityWeeklyHistoryArgs = {publishTime: string, level?: ('total' | 'fuelType' | 'bmUnit') | undefined, bmUnit?: Array | undefined, fuelType?: Array | undefined, format?: ('json' | 'xml' | 'csv') | undefined}; export type GetApiForecastAvailabilityWeeklyHistoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const insightsApiModelsResponsesGenerationAvailabilityWeekly = z.object({dataset: z.string().nullable().optional(), publishTime: z.string().nullable().optional(), fuelType: z.string().nullable().optional(), ngcBmUnit: z.string().nullable().optional(), bmUnit: z.string().nullable().optional(), outputUsable: z.number().int().nullable().optional(), year: z.number().int().optional(), calendarWeekNumber: z.number().int().optional()}); export const insightsApiModelsMetadataApiResponseSourceMetadata = z.object({datasets: z.array(z.string()).nullable().optional()}); export const insightsApiModelsResponsesResponseWithMetadata1InsightsApiModelsResponsesGenerationAvailabilityWeekly = z.object({data: z.array(insightsApiModelsResponsesGenerationAvailabilityWeekly).nullable().optional(), metadata: insightsApiModelsMetadataApiResponseSourceMetadata.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 }; /** * History of the three-year generation capacity forecast (FOU2T3YW) * * GET /forecast/availability/weekly/history */ export const getApiForecastAvailabilityWeeklyHistory = async (args: GetApiForecastAvailabilityWeeklyHistoryArgs, options?: GetApiForecastAvailabilityWeeklyHistoryOptions) => { const {baseUrl = "https://data.elexon.co.uk/bmrs/api/v1", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.publishTime != null) query.append("publishTime", String(args.publishTime)) if (args.level != null) query.append("level", String(args.level)) for (const item of args.bmUnit ?? []) query.append("bmUnit", String(item)) for (const item of args.fuelType ?? []) query.append("fuelType", String(item)) if (args.format != null) query.append("format", String(args.format)) const url = `${baseUrl.replace(/\/+$/, '')}/forecast/availability/weekly/history${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(insightsApiModelsResponsesResponseWithMetadata1InsightsApiModelsResponsesGenerationAvailabilityWeekly, await res.json()) };