import {z} from 'zod' export type GetApiGbElexonDynamicMnztArgs = {date_from: string, date_to: string, bm_unit?: string | undefined, settlement_period?: number | undefined, limit?: number | undefined, offset?: number | undefined, timezone?: ('UTC' | 'Europe/Berlin' | 'Europe/London' | 'America/New_York' | 'America/Chicago' | 'America/Los_Angeles') | undefined}; export type GetApiGbElexonDynamicMnztOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const elexonMNZT = z.object({dataset: z.string().optional(), settlementDate: z.string().optional(), settlementPeriod: z.number().int().gte(-2147483648).lte(2147483647).optional(), time: z.string().optional(), periodMin: z.number().int().gte(-2147483648).lte(2147483647).optional(), nationalGridBmUnit: z.string().optional(), bmUnit: z.string().optional()}); export const getApiGbElexonDynamicMnztResponse = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(elexonMNZT)}); 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 }; /** * Minimum Non-Zero Time * * GET /gb/elexon/dynamic/mnzt */ export const getApiGbElexonDynamicMnzt = async (args: GetApiGbElexonDynamicMnztArgs, options?: GetApiGbElexonDynamicMnztOptions) => { const {baseUrl = "https://api.modoenergy.com/pub/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.date_from != null) query.append("date_from", String(args.date_from)) if (args.date_to != null) query.append("date_to", String(args.date_to)) if (args.bm_unit != null) query.append("bm_unit", String(args.bm_unit)) if (args.settlement_period != null) query.append("settlement_period", String(args.settlement_period)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.timezone != null) query.append("timezone", String(args.timezone)) if (apiKey !== undefined) headers.set("X-Token", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/gb/elexon/dynamic/mnzt${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(getApiGbElexonDynamicMnztResponse, await res.json()) };