import {z} from 'zod' export type GetApiLocationsLocationIdPriceformulasArgs = {locationId: string, fuel?: string | undefined, direction?: string | undefined}; export type GetApiLocationsLocationIdPriceformulasOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const capacityTariff = z.object({charge_tariff: z.number().optional(), minimum_contribution: z.number().optional(), calculation_type: z.string().nullable().optional()}); export const charge = z.object({resolution: z.string().nullable().optional(), charge: z.number().nullable().optional(), type: z.string().nullable().optional(), capacity_tariff: capacityTariff.optional()}); export const fixed = z.object({unit_rate: z.number().nullable().optional()}); export const interval = z.object({from_minute: z.number().int().nullable().optional(), to_minute: z.number().int().nullable().optional()}); export const tou = z.object({register_id: z.string().nullable().optional(), unit_rate: z.number().nullable().optional(), intervals: z.array(interval).nullable().optional(), skip_intervals: z.boolean().nullable().optional()}); export const block = z.object({resolution: z.string().nullable().optional(), register_id: z.string().nullable().optional(), unit_rate: z.number().optional(), from_kwh: z.number().int().nullable().optional(), to_kwh: z.number().int().nullable().optional(), from_m3: z.number().int().nullable().optional(), to_m3: z.number().int().nullable().optional()}); export const spotPrice = z.object({provider: z.string().nullable().optional(), region: z.string().nullable().optional(), resolution: z.string().nullable().optional(), multiplier: z.number().nullable().optional()}); export const unitRate = z.object({type: z.string().nullable().optional(), fixed: fixed.optional(), tou: tou.optional(), block: block.optional(), spot_price: spotPrice.optional(), weekdays: z.array(z.string()).nullable().optional()}); export const element = z.object({name: z.string().nullable().optional(), type: z.string().nullable().optional(), charge: charge.optional(), unit_rate: unitRate.optional()}); export const locationPriceFormula = z.object({fuel: z.string().nullable().optional(), source: z.string().nullable().optional(), name: z.string().nullable().optional(), direction: z.string().nullable().optional(), from: z.string().nullable().optional(), to: z.string().nullable().optional(), currency_code: z.string().nullable().optional(), elements: z.array(element).nullable().optional(), price_multiplier: z.number().nullable().optional()}); export const getApiLocationsLocationIdPriceformulasResponse = z.array(locationPriceFormula); 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 }; /** * Get location price formulas * * GET /locations/{locationId}/priceformulas */ export const getApiLocationsLocationIdPriceformulas = async (args: GetApiLocationsLocationIdPriceformulasArgs, options: GetApiLocationsLocationIdPriceformulasOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.fuel != null) query.append("fuel", String(args.fuel)) if (args.direction != null) query.append("direction", String(args.direction)) const url = `${baseUrl.replace(/\/+$/, '')}/locations/${encodeURIComponent(String(args.locationId))}/priceformulas${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(getApiLocationsLocationIdPriceformulasResponse, await res.json()) };