import {z} from 'zod' export type GetApiForecastDemandTotalWeekAheadArgs = {from: string, to: string, format?: ('json' | 'xml' | 'csv') | undefined}; export type GetApiForecastDemandTotalWeekAheadOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const insightsApiModelsResponsesTransparencyWeekAheadTotalLoadPerBiddingZone = z.object({publishTime: z.string().optional(), forecastDate: z.string().optional(), forecastWeek: z.number().int().optional(), minimumPossible: z.number().optional(), maximumAvailable: z.number().optional()}); export const insightsApiModelsMetadataApiResponseSourceMetadata = z.object({datasets: z.array(z.string()).nullable().optional()}); export const insightsApiModelsResponsesResponseWithMetadata1InsightsApiModelsResponsesTransparencyWeekAheadTotalLoadPerBiddingZone = z.object({data: z.array(insightsApiModelsResponsesTransparencyWeekAheadTotalLoadPerBiddingZone).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 }; /** * Week-ahead total load forecast (WATL/B0630) * * GET /forecast/demand/total/week-ahead */ export const getApiForecastDemandTotalWeekAhead = async (args: GetApiForecastDemandTotalWeekAheadArgs, options?: GetApiForecastDemandTotalWeekAheadOptions) => { 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.from != null) query.append("from", String(args.from)) if (args.to != null) query.append("to", String(args.to)) if (args.format != null) query.append("format", String(args.format)) const url = `${baseUrl.replace(/\/+$/, '')}/forecast/demand/total/week-ahead${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(insightsApiModelsResponsesResponseWithMetadata1InsightsApiModelsResponsesTransparencyWeekAheadTotalLoadPerBiddingZone, await res.json()) };