import {z} from 'zod' export type GetApiSitesSiteIdPricesArgs = {siteId: string, startDate?: string | undefined, endDate?: string | undefined, resolution?: (5 | 30) | undefined}; export type GetApiSitesSiteIdPricesOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const channelType = z.enum(["general", "controlledLoad", "feedIn"]); export const tariffInformation = z.object({period: z.enum(["offPeak", "shoulder", "solarSponge", "peak"]).optional(), season: z.enum(["default", "summer", "autumn", "winter", "spring", "nonSummer", "holiday", "weekend", "weekendHoliday", "weekday"]).optional(), block: z.number().gte(1).lte(2).optional(), demandWindow: z.boolean().optional()}); export const spikeStatus = z.enum(["none", "potential", "spike"]); export const priceDescriptor = z.enum(["negative", "extremelyLow", "veryLow", "low", "neutral", "high", "spike"]); export const actualInterval = z.object({duration: z.union([z.literal(5), z.literal(15), z.literal(30)]), spotPerKwh: z.number(), perKwh: z.number(), date: z.string(), nemTime: z.string(), startTime: z.string(), endTime: z.string(), renewables: z.number().gte(0).lte(100), channelType: channelType, tariffInformation: tariffInformation.nullable().optional(), spikeStatus: spikeStatus, descriptor: priceDescriptor, type: z.literal("ActualInterval")}); export const range = z.object({min: z.number(), max: z.number()}); export const currentInterval = z.object({duration: z.union([z.literal(5), z.literal(15), z.literal(30)]), spotPerKwh: z.number(), perKwh: z.number(), date: z.string(), nemTime: z.string(), startTime: z.string(), endTime: z.string(), renewables: z.number().gte(0).lte(100), channelType: channelType, tariffInformation: tariffInformation.nullable().optional(), spikeStatus: spikeStatus, descriptor: priceDescriptor, type: z.literal("CurrentInterval"), range: range.nullable().optional(), estimate: z.boolean()}); export const forecastInterval = z.object({duration: z.union([z.literal(5), z.literal(15), z.literal(30)]), spotPerKwh: z.number(), perKwh: z.number(), date: z.string(), nemTime: z.string(), startTime: z.string(), endTime: z.string(), renewables: z.number().gte(0).lte(100), channelType: channelType, tariffInformation: tariffInformation.nullable().optional(), spikeStatus: spikeStatus, descriptor: priceDescriptor, type: z.literal("ForecastInterval"), range: range.nullable().optional()}); export const getApiSitesSiteIdPricesResponse = z.array(z.discriminatedUnion("type", [actualInterval, currentInterval, forecastInterval])); 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 }; /** * Returns all the prices between the start and end dates * * GET /sites/{siteId}/prices */ export const getApiSitesSiteIdPrices = async (args: GetApiSitesSiteIdPricesArgs, options?: GetApiSitesSiteIdPricesOptions) => { const {baseUrl = "https://api.amber.com.au/v1", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.startDate != null) query.append("startDate", String(args.startDate)) if (args.endDate != null) query.append("endDate", String(args.endDate)) if (args.resolution != null) query.append("resolution", String(args.resolution)) if (apiKey !== undefined) headers.set('authorization', `Bearer ${apiKey}`) const url = `${baseUrl.replace(/\/+$/, '')}/sites/${encodeURIComponent(String(args.siteId))}/prices${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(getApiSitesSiteIdPricesResponse, await res.json()) };