import {z} from 'zod' export type Intent = {energy_required_kwh: number, ready_by: string, ready_by_local_time?: string | undefined, energy_estimated?: boolean | undefined, may_charge_peak?: boolean | undefined}; export type ChargingHoursIntent = {required_charging_hours: number, ready_by: string}; export type Vehicle = {charging_rate_kw: number}; export type TariffType = 'single_rate' | 'dual_rate' | 'dynamic' | 'smart' | 'unknown'; export type Tariff = {off_peak_times: string, timezone: string, tariff_type?: TariffType | undefined}; export type PlugInEventRequest = {occurred_at?: string | undefined, external_session_id?: string | undefined, intent?: (Intent | ChargingHoursIntent) | undefined, vehicle?: Vehicle | undefined, tariff?: Tariff | undefined}; export type CreateApiEntitiesAssetAssetIdEventPlugInArgs = {asset_id: string, body: PlugInEventRequest}; export type CreateApiEntitiesAssetAssetIdEventPlugInOptions = {baseUrl?: string | undefined, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiEntitiesAssetAssetIdEventPlugInResponse = z.unknown(); 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 }; /** * Asset Plug In Event * * POST /entities/asset/{asset_id}/event/plug-in */ export const createApiEntitiesAssetAssetIdEventPlugIn = async (args: CreateApiEntitiesAssetAssetIdEventPlugInArgs, options?: CreateApiEntitiesAssetAssetIdEventPlugInOptions) => { const {baseUrl = "https://api-sandbox.axle.energy", fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/entities/asset/${encodeURIComponent(String(args.asset_id))}/event/plug-in${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(createApiEntitiesAssetAssetIdEventPlugInResponse, await res.json()) };