import {z} from 'zod' export type CapacityTariff = {charge_tariff?: number | undefined, minimum_contribution?: number | undefined, calculation_type?: (string | null) | undefined}; export type Charge = {resolution?: (string | null) | undefined, charge?: (number | null) | undefined, type?: (string | null) | undefined, capacity_tariff?: CapacityTariff | undefined}; export type Fixed = {unit_rate?: (number | null) | undefined}; export type Interval = {from_minute?: (number | null) | undefined, to_minute?: (number | null) | undefined}; export type Tou = {register_id?: (string | null) | undefined, unit_rate?: (number | null) | undefined, intervals?: (Array | null) | undefined, skip_intervals?: (boolean | null) | undefined}; export type Block = {resolution?: (string | null) | undefined, register_id?: (string | null) | undefined, unit_rate?: number | undefined, from_kwh?: (number | null) | undefined, to_kwh?: (number | null) | undefined, from_m3?: (number | null) | undefined, to_m3?: (number | null) | undefined}; export type SpotPrice = {provider?: (string | null) | undefined, region?: (string | null) | undefined, resolution?: (string | null) | undefined, multiplier?: (number | null) | undefined}; export type UnitRate = {type?: (string | null) | undefined, fixed?: Fixed | undefined, tou?: Tou | undefined, block?: Block | undefined, spot_price?: SpotPrice | undefined, weekdays?: (Array | null) | undefined}; export type Element = {name?: (string | null) | undefined, type?: (string | null) | undefined, charge?: Charge | undefined, unit_rate?: UnitRate | undefined}; export type LocationPriceFormula = {fuel?: (string | null) | undefined, source?: (string | null) | undefined, name?: (string | null) | undefined, direction?: (string | null) | undefined, from?: (string | null) | undefined, to?: (string | null) | undefined, currency_code?: (string | null) | undefined, elements?: (Array | null) | undefined, price_multiplier?: (number | null) | undefined}; export type CreateApiLocationsLocationIdPriceformulasArgs = {locationId: string, body: LocationPriceFormula}; export type CreateApiLocationsLocationIdPriceformulasOptions = {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 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 }; /** * Create or updates a location price formula * * POST /locations/{locationId}/priceformulas */ export const createApiLocationsLocationIdPriceformulas = async (args: CreateApiLocationsLocationIdPriceformulasArgs, options: CreateApiLocationsLocationIdPriceformulasOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/locations/${encodeURIComponent(String(args.locationId))}/priceformulas${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(locationPriceFormula, await res.json()) };