edilkamin 1.7.2 → 1.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/publish.yml +1 -1
- package/README.md +1 -1
- package/dist/esm/browser-bundle.test.d.ts +1 -0
- package/dist/esm/browser-bundle.test.js +29 -0
- package/dist/esm/configureAmplify.test.d.ts +1 -0
- package/dist/esm/configureAmplify.test.js +37 -0
- package/dist/esm/index.d.ts +1 -2
- package/dist/esm/index.js +0 -1
- package/dist/esm/library.d.ts +4 -4
- package/dist/esm/library.js +59 -51
- package/dist/esm/library.test.js +167 -190
- package/package.json +14 -2
- package/src/browser-bundle.test.ts +21 -0
- package/src/configureAmplify.test.ts +47 -0
- package/src/index.ts +0 -1
- package/src/library.test.ts +195 -197
- package/src/library.ts +76 -62
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Edilkamin.js
|
|
2
2
|
|
|
3
|
-
[](https://github.com/AndreMiras/edilkamin.js/actions/workflows/tests.yml)
|
|
4
4
|
[](https://github.com/AndreMiras/edilkamin.js/actions/workflows/cli-tests.yml)
|
|
5
5
|
[](https://app.codecov.io/gh/AndreMiras/edilkamin.js/tree/main)
|
|
6
6
|
[](https://github.com/AndreMiras/edilkamin.js/actions/workflows/documentation.yml)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { strict as assert } from "assert";
|
|
11
|
+
import * as esbuild from "esbuild";
|
|
12
|
+
describe("browser-bundle", () => {
|
|
13
|
+
it("should bundle for browser without Node.js built-ins", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
// This test verifies that the library can be bundled for browser environments
|
|
15
|
+
// without requiring Node.js built-in modules (fs, os, path).
|
|
16
|
+
// If this test fails, it means Node.js-only code has leaked into the main exports.
|
|
17
|
+
const result = yield esbuild.build({
|
|
18
|
+
entryPoints: ["dist/esm/index.js"],
|
|
19
|
+
platform: "browser",
|
|
20
|
+
bundle: true,
|
|
21
|
+
write: false,
|
|
22
|
+
// External dependencies that are expected (real deps + assert which is used for validation)
|
|
23
|
+
external: ["aws-amplify", "aws-amplify/*", "pako", "assert"],
|
|
24
|
+
logLevel: "silent",
|
|
25
|
+
});
|
|
26
|
+
// If we get here without error, the bundle succeeded
|
|
27
|
+
assert.ok(result.outputFiles.length > 0, "Bundle should produce output");
|
|
28
|
+
}));
|
|
29
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { strict as assert } from "assert";
|
|
2
|
+
import sinon from "sinon";
|
|
3
|
+
import { configureAmplify } from "../src/library";
|
|
4
|
+
/**
|
|
5
|
+
* This test file specifically tests the configureAmplify function with custom storage.
|
|
6
|
+
* It tests line 61 in library.ts:
|
|
7
|
+
* cognitoUserPoolsTokenProvider.setKeyValueStorage(storage)
|
|
8
|
+
*
|
|
9
|
+
* IMPORTANT: This file is named to run BEFORE library.test.ts (alphabetically)
|
|
10
|
+
* to ensure amplifyConfigured is still false when these tests run.
|
|
11
|
+
*/
|
|
12
|
+
describe("configureAmplify", () => {
|
|
13
|
+
it("should configure Amplify with custom storage", () => {
|
|
14
|
+
const mockStorage = {
|
|
15
|
+
setItem: sinon.stub().resolves(),
|
|
16
|
+
getItem: sinon.stub().resolves(null),
|
|
17
|
+
removeItem: sinon.stub().resolves(),
|
|
18
|
+
clear: sinon.stub().resolves(),
|
|
19
|
+
};
|
|
20
|
+
// Call configureAmplify with custom storage
|
|
21
|
+
// This is the first call in the test suite, so amplifyConfigured is false
|
|
22
|
+
// This should trigger line 61 in library.ts
|
|
23
|
+
configureAmplify(mockStorage);
|
|
24
|
+
// The test passes if no error is thrown
|
|
25
|
+
// Coverage confirms line 61 is executed
|
|
26
|
+
assert.ok(true, "configureAmplify with storage completed without error");
|
|
27
|
+
});
|
|
28
|
+
it("should only configure Amplify once (idempotent)", () => {
|
|
29
|
+
// Call configureAmplify multiple times without storage
|
|
30
|
+
configureAmplify();
|
|
31
|
+
configureAmplify();
|
|
32
|
+
configureAmplify();
|
|
33
|
+
// Should not throw or have any side effects
|
|
34
|
+
// The function returns early if already configured (line 58)
|
|
35
|
+
assert.ok(true, "Multiple calls to configureAmplify completed without error");
|
|
36
|
+
});
|
|
37
|
+
});
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -2,6 +2,5 @@ export { decompressBuffer, isBuffer, processResponse } from "./buffer-utils";
|
|
|
2
2
|
export { API_URL, NEW_API_URL, OLD_API_URL } from "./constants";
|
|
3
3
|
export { configure, getSession, signIn } from "./library";
|
|
4
4
|
export { serialNumberDisplay, serialNumberFromHex, serialNumberToHex, } from "./serial-utils";
|
|
5
|
-
export { clearSession } from "./token-storage";
|
|
6
5
|
export { BufferEncodedType, CommandsType, DeviceAssociationBody, DeviceAssociationResponse, DeviceInfoRawType, DeviceInfoType, EditDeviceAssociationBody, StatusType, TemperaturesType, UserParametersType, } from "./types";
|
|
7
|
-
export declare const deviceInfo: (jwtToken: string, macAddress: string) => Promise<import("./types").DeviceInfoType>, registerDevice: (jwtToken: string, macAddress: string, serialNumber: string, deviceName?: string, deviceRoom?: string) => Promise<import("./types").DeviceAssociationResponse>, editDevice: (jwtToken: string, macAddress: string, deviceName?: string, deviceRoom?: string) => Promise<import("./types").DeviceAssociationResponse>, setPower: (jwtToken: string, macAddress: string, value: number) => Promise<
|
|
6
|
+
export declare const deviceInfo: (jwtToken: string, macAddress: string) => Promise<import("./types").DeviceInfoType>, registerDevice: (jwtToken: string, macAddress: string, serialNumber: string, deviceName?: string, deviceRoom?: string) => Promise<import("./types").DeviceAssociationResponse>, editDevice: (jwtToken: string, macAddress: string, deviceName?: string, deviceRoom?: string) => Promise<import("./types").DeviceAssociationResponse>, setPower: (jwtToken: string, macAddress: string, value: number) => Promise<unknown>, setPowerOff: (jwtToken: string, macAddress: string) => Promise<unknown>, setPowerOn: (jwtToken: string, macAddress: string) => Promise<unknown>, getPower: (jwtToken: string, macAddress: string) => Promise<boolean>, getEnvironmentTemperature: (jwtToken: string, macAddress: string) => Promise<number>, getTargetTemperature: (jwtToken: string, macAddress: string) => Promise<number>, setTargetTemperature: (jwtToken: string, macAddress: string, temperature: number) => Promise<unknown>;
|
package/dist/esm/index.js
CHANGED
|
@@ -3,5 +3,4 @@ export { decompressBuffer, isBuffer, processResponse } from "./buffer-utils";
|
|
|
3
3
|
export { API_URL, NEW_API_URL, OLD_API_URL } from "./constants";
|
|
4
4
|
export { configure, getSession, signIn } from "./library";
|
|
5
5
|
export { serialNumberDisplay, serialNumberFromHex, serialNumberToHex, } from "./serial-utils";
|
|
6
|
-
export { clearSession } from "./token-storage";
|
|
7
6
|
export const { deviceInfo, registerDevice, editDevice, setPower, setPowerOff, setPowerOn, getPower, getEnvironmentTemperature, getTargetTemperature, setTargetTemperature, } = configure();
|
package/dist/esm/library.d.ts
CHANGED
|
@@ -44,12 +44,12 @@ declare const configure: (baseURL?: string) => {
|
|
|
44
44
|
deviceInfo: (jwtToken: string, macAddress: string) => Promise<DeviceInfoType>;
|
|
45
45
|
registerDevice: (jwtToken: string, macAddress: string, serialNumber: string, deviceName?: string, deviceRoom?: string) => Promise<DeviceAssociationResponse>;
|
|
46
46
|
editDevice: (jwtToken: string, macAddress: string, deviceName?: string, deviceRoom?: string) => Promise<DeviceAssociationResponse>;
|
|
47
|
-
setPower: (jwtToken: string, macAddress: string, value: number) => Promise<
|
|
48
|
-
setPowerOff: (jwtToken: string, macAddress: string) => Promise<
|
|
49
|
-
setPowerOn: (jwtToken: string, macAddress: string) => Promise<
|
|
47
|
+
setPower: (jwtToken: string, macAddress: string, value: number) => Promise<unknown>;
|
|
48
|
+
setPowerOff: (jwtToken: string, macAddress: string) => Promise<unknown>;
|
|
49
|
+
setPowerOn: (jwtToken: string, macAddress: string) => Promise<unknown>;
|
|
50
50
|
getPower: (jwtToken: string, macAddress: string) => Promise<boolean>;
|
|
51
51
|
getEnvironmentTemperature: (jwtToken: string, macAddress: string) => Promise<number>;
|
|
52
52
|
getTargetTemperature: (jwtToken: string, macAddress: string) => Promise<number>;
|
|
53
|
-
setTargetTemperature: (jwtToken: string, macAddress: string, temperature: number) => Promise<
|
|
53
|
+
setTargetTemperature: (jwtToken: string, macAddress: string, temperature: number) => Promise<unknown>;
|
|
54
54
|
};
|
|
55
55
|
export { configure, configureAmplify, createAuthService, getSession, headers, signIn, };
|
package/dist/esm/library.js
CHANGED
|
@@ -11,9 +11,19 @@ import { strict as assert } from "assert";
|
|
|
11
11
|
import { Amplify } from "aws-amplify";
|
|
12
12
|
import * as amplifyAuth from "aws-amplify/auth";
|
|
13
13
|
import { cognitoUserPoolsTokenProvider } from "aws-amplify/auth/cognito";
|
|
14
|
-
import axios from "axios";
|
|
15
14
|
import { processResponse } from "./buffer-utils";
|
|
16
15
|
import { API_URL } from "./constants";
|
|
16
|
+
/**
|
|
17
|
+
* Makes a fetch request and returns parsed JSON response.
|
|
18
|
+
* Throws an error for non-2xx status codes.
|
|
19
|
+
*/
|
|
20
|
+
const fetchJson = (baseURL_1, path_1, ...args_1) => __awaiter(void 0, [baseURL_1, path_1, ...args_1], void 0, function* (baseURL, path, options = {}) {
|
|
21
|
+
const response = yield fetch(`${baseURL}${path}`, options);
|
|
22
|
+
if (!response.ok) {
|
|
23
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
24
|
+
}
|
|
25
|
+
return response.json();
|
|
26
|
+
});
|
|
17
27
|
const amplifyconfiguration = {
|
|
18
28
|
aws_project_region: "eu-central-1",
|
|
19
29
|
aws_user_pools_id: "eu-central-1_BYmQ2VBlo",
|
|
@@ -91,7 +101,7 @@ const createAuthService = (auth) => {
|
|
|
91
101
|
};
|
|
92
102
|
// Create the default auth service using amplifyAuth
|
|
93
103
|
const { signIn, getSession } = createAuthService(amplifyAuth);
|
|
94
|
-
const deviceInfo = (
|
|
104
|
+
const deviceInfo = (baseURL) =>
|
|
95
105
|
/**
|
|
96
106
|
* Retrieves information about a device by its MAC address.
|
|
97
107
|
* Automatically decompresses any gzip-compressed Buffer fields in the response.
|
|
@@ -101,16 +111,21 @@ const deviceInfo = (axiosInstance) =>
|
|
|
101
111
|
* @returns {Promise<DeviceInfoType>} - A promise that resolves to the device info.
|
|
102
112
|
*/
|
|
103
113
|
(jwtToken, macAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
104
|
-
const
|
|
114
|
+
const data = yield fetchJson(baseURL, `device/${macAddress}/info`, {
|
|
115
|
+
method: "GET",
|
|
105
116
|
headers: headers(jwtToken),
|
|
106
117
|
});
|
|
107
118
|
// Process response to decompress any gzipped Buffer fields
|
|
108
|
-
return processResponse(
|
|
119
|
+
return processResponse(data);
|
|
109
120
|
});
|
|
110
|
-
const mqttCommand = (
|
|
121
|
+
const mqttCommand = (baseURL) =>
|
|
111
122
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
112
|
-
(jwtToken, macAddress, payload) =>
|
|
113
|
-
|
|
123
|
+
(jwtToken, macAddress, payload) => fetchJson(baseURL, "mqtt/command", {
|
|
124
|
+
method: "PUT",
|
|
125
|
+
headers: Object.assign({ "Content-Type": "application/json" }, headers(jwtToken)),
|
|
126
|
+
body: JSON.stringify(Object.assign({ mac_address: macAddress }, payload)),
|
|
127
|
+
});
|
|
128
|
+
const setPower = (baseURL) =>
|
|
114
129
|
/**
|
|
115
130
|
* Sends a command to set the power state of a device.
|
|
116
131
|
*
|
|
@@ -119,8 +134,8 @@ const setPower = (axiosInstance) =>
|
|
|
119
134
|
* @param {number} value - The desired power state (1 for ON, 0 for OFF).
|
|
120
135
|
* @returns {Promise<string>} - A promise that resolves to the command response.
|
|
121
136
|
*/
|
|
122
|
-
(jwtToken, macAddress, value) => mqttCommand(
|
|
123
|
-
const setPowerOn = (
|
|
137
|
+
(jwtToken, macAddress, value) => mqttCommand(baseURL)(jwtToken, macAddress, { name: "power", value });
|
|
138
|
+
const setPowerOn = (baseURL) =>
|
|
124
139
|
/**
|
|
125
140
|
* Turns a device ON by setting its power state.
|
|
126
141
|
*
|
|
@@ -132,8 +147,8 @@ const setPowerOn = (axiosInstance) =>
|
|
|
132
147
|
* const response = await api.setPowerOn(jwtToken, macAddress);
|
|
133
148
|
* console.log(response);
|
|
134
149
|
*/
|
|
135
|
-
(jwtToken, macAddress) => setPower(
|
|
136
|
-
const setPowerOff = (
|
|
150
|
+
(jwtToken, macAddress) => setPower(baseURL)(jwtToken, macAddress, 1);
|
|
151
|
+
const setPowerOff = (baseURL) =>
|
|
137
152
|
/**
|
|
138
153
|
* Turns a device OFF by setting its power state.
|
|
139
154
|
*
|
|
@@ -145,8 +160,8 @@ const setPowerOff = (axiosInstance) =>
|
|
|
145
160
|
* const response = await api.setPowerOff(jwtToken, macAddress);
|
|
146
161
|
* console.log(response);
|
|
147
162
|
*/
|
|
148
|
-
(jwtToken, macAddress) => setPower(
|
|
149
|
-
const getPower = (
|
|
163
|
+
(jwtToken, macAddress) => setPower(baseURL)(jwtToken, macAddress, 0);
|
|
164
|
+
const getPower = (baseURL) =>
|
|
150
165
|
/**
|
|
151
166
|
* Retrieves the power status of the device.
|
|
152
167
|
*
|
|
@@ -155,10 +170,10 @@ const getPower = (axiosInstance) =>
|
|
|
155
170
|
* @returns {Promise<boolean>} - A promise that resolves to the power status.
|
|
156
171
|
*/
|
|
157
172
|
(jwtToken, macAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
158
|
-
const info = yield deviceInfo(
|
|
173
|
+
const info = yield deviceInfo(baseURL)(jwtToken, macAddress);
|
|
159
174
|
return info.status.commands.power;
|
|
160
175
|
});
|
|
161
|
-
const getEnvironmentTemperature = (
|
|
176
|
+
const getEnvironmentTemperature = (baseURL) =>
|
|
162
177
|
/**
|
|
163
178
|
* Retrieves the environment temperature from the device's sensors.
|
|
164
179
|
*
|
|
@@ -167,10 +182,10 @@ const getEnvironmentTemperature = (axiosInstance) =>
|
|
|
167
182
|
* @returns {Promise<number>} - A promise that resolves to the temperature value.
|
|
168
183
|
*/
|
|
169
184
|
(jwtToken, macAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
170
|
-
const info = yield deviceInfo(
|
|
185
|
+
const info = yield deviceInfo(baseURL)(jwtToken, macAddress);
|
|
171
186
|
return info.status.temperatures.enviroment;
|
|
172
187
|
});
|
|
173
|
-
const getTargetTemperature = (
|
|
188
|
+
const getTargetTemperature = (baseURL) =>
|
|
174
189
|
/**
|
|
175
190
|
* Retrieves the target temperature value set on the device.
|
|
176
191
|
*
|
|
@@ -179,10 +194,10 @@ const getTargetTemperature = (axiosInstance) =>
|
|
|
179
194
|
* @returns {Promise<number>} - A promise that resolves to the target temperature (degree celsius).
|
|
180
195
|
*/
|
|
181
196
|
(jwtToken, macAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
182
|
-
const info = yield deviceInfo(
|
|
197
|
+
const info = yield deviceInfo(baseURL)(jwtToken, macAddress);
|
|
183
198
|
return info.nvm.user_parameters.enviroment_1_temperature;
|
|
184
199
|
});
|
|
185
|
-
const setTargetTemperature = (
|
|
200
|
+
const setTargetTemperature = (baseURL) =>
|
|
186
201
|
/**
|
|
187
202
|
* Sends a command to set the target temperature (degree celsius) of a device.
|
|
188
203
|
*
|
|
@@ -191,11 +206,11 @@ const setTargetTemperature = (axiosInstance) =>
|
|
|
191
206
|
* @param {number} temperature - The desired target temperature (degree celsius).
|
|
192
207
|
* @returns {Promise<string>} - A promise that resolves to the command response.
|
|
193
208
|
*/
|
|
194
|
-
(jwtToken, macAddress, temperature) => mqttCommand(
|
|
209
|
+
(jwtToken, macAddress, temperature) => mqttCommand(baseURL)(jwtToken, macAddress, {
|
|
195
210
|
name: "enviroment_1_temperature",
|
|
196
211
|
value: temperature,
|
|
197
212
|
});
|
|
198
|
-
const registerDevice = (
|
|
213
|
+
const registerDevice = (baseURL) =>
|
|
199
214
|
/**
|
|
200
215
|
* Registers a device with the user's account.
|
|
201
216
|
* This must be called before other device operations will work on the new API.
|
|
@@ -214,10 +229,13 @@ const registerDevice = (axiosInstance) =>
|
|
|
214
229
|
deviceRoom,
|
|
215
230
|
serialNumber,
|
|
216
231
|
};
|
|
217
|
-
|
|
218
|
-
|
|
232
|
+
return fetchJson(baseURL, "device", {
|
|
233
|
+
method: "POST",
|
|
234
|
+
headers: Object.assign({ "Content-Type": "application/json" }, headers(jwtToken)),
|
|
235
|
+
body: JSON.stringify(body),
|
|
236
|
+
});
|
|
219
237
|
});
|
|
220
|
-
const editDevice = (
|
|
238
|
+
const editDevice = (baseURL) =>
|
|
221
239
|
/**
|
|
222
240
|
* Updates a device's name and room.
|
|
223
241
|
*
|
|
@@ -233,8 +251,11 @@ const editDevice = (axiosInstance) =>
|
|
|
233
251
|
deviceName,
|
|
234
252
|
deviceRoom,
|
|
235
253
|
};
|
|
236
|
-
|
|
237
|
-
|
|
254
|
+
return fetchJson(baseURL, `device/${normalizedMac}`, {
|
|
255
|
+
method: "PUT",
|
|
256
|
+
headers: Object.assign({ "Content-Type": "application/json" }, headers(jwtToken)),
|
|
257
|
+
body: JSON.stringify(body),
|
|
258
|
+
});
|
|
238
259
|
});
|
|
239
260
|
/**
|
|
240
261
|
* Configures the library for API interactions.
|
|
@@ -247,29 +268,16 @@ const editDevice = (axiosInstance) =>
|
|
|
247
268
|
* const api = configure();
|
|
248
269
|
* const power = await api.getPower(jwtToken, macAddress);
|
|
249
270
|
*/
|
|
250
|
-
const configure = (baseURL = API_URL) => {
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
return {
|
|
263
|
-
deviceInfo: deviceInfoInstance,
|
|
264
|
-
registerDevice: registerDeviceInstance,
|
|
265
|
-
editDevice: editDeviceInstance,
|
|
266
|
-
setPower: setPowerInstance,
|
|
267
|
-
setPowerOff: setPowerOffInstance,
|
|
268
|
-
setPowerOn: setPowerOnInstance,
|
|
269
|
-
getPower: getPowerInstance,
|
|
270
|
-
getEnvironmentTemperature: getEnvironmentTemperatureInstance,
|
|
271
|
-
getTargetTemperature: getTargetTemperatureInstance,
|
|
272
|
-
setTargetTemperature: setTargetTemperatureInstance,
|
|
273
|
-
};
|
|
274
|
-
};
|
|
271
|
+
const configure = (baseURL = API_URL) => ({
|
|
272
|
+
deviceInfo: deviceInfo(baseURL),
|
|
273
|
+
registerDevice: registerDevice(baseURL),
|
|
274
|
+
editDevice: editDevice(baseURL),
|
|
275
|
+
setPower: setPower(baseURL),
|
|
276
|
+
setPowerOff: setPowerOff(baseURL),
|
|
277
|
+
setPowerOn: setPowerOn(baseURL),
|
|
278
|
+
getPower: getPower(baseURL),
|
|
279
|
+
getEnvironmentTemperature: getEnvironmentTemperature(baseURL),
|
|
280
|
+
getTargetTemperature: getTargetTemperature(baseURL),
|
|
281
|
+
setTargetTemperature: setTargetTemperature(baseURL),
|
|
282
|
+
});
|
|
275
283
|
export { configure, configureAmplify, createAuthService, getSession, headers, signIn, };
|