import {z} from 'zod' export type GetApiForecastMarginWeeklyHistoryArgs = {publishTime: string, range?: ('52W' | '156W') | undefined, format?: ('json' | 'xml' | 'csv') | undefined}; export type GetApiForecastMarginWeeklyHistoryOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const insightsApiModelsResponsesForecastMarginForecastMarginWeekly = z.object({publishTime: z.string().optional(), margin: z.number().int().nullable().optional(), week: z.number().int().optional(), year: z.number().int().optional(), weekStartDate: z.string().optional()}); export const insightsApiModelsMetadataApiResponseSourceMetadata = z.object({datasets: z.array(z.string()).nullable().optional()}); export const insightsApiModelsResponsesResponseWithMetadata1InsightsApiModelsResponsesForecastMarginForecastMarginWeekly = z.object({data: z.array(insightsApiModelsResponsesForecastMarginForecastMarginWeekly).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 }; /** * Historical weekly margin forecast (OCNMFW2, OCNMF3Y2) * * GET /forecast/margin/weekly/history */ export const getApiForecastMarginWeeklyHistory = async (args: GetApiForecastMarginWeeklyHistoryArgs, options?: GetApiForecastMarginWeeklyHistoryOptions) => { 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.range != null) query.append("range", String(args.range)) if (args.format != null) query.append("format", String(args.format)) const url = `${baseUrl.replace(/\/+$/, '')}/forecast/margin/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(insightsApiModelsResponsesResponseWithMetadata1InsightsApiModelsResponsesForecastMarginForecastMarginWeekly, await res.json()) };