export type AssetEnergyConsumption = {asset_id: string, timestamp: number, value: number}; export type CreateApiEnergyConsumptionArgs = {body: Array}; export type CreateApiEnergyConsumptionOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; /** * Save energy consumption * * POST /energy-consumption */ export const createApiEnergyConsumption = async (args: CreateApiEnergyConsumptionArgs, options: CreateApiEnergyConsumptionOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/energy-consumption${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 undefined };