edilkamin 1.6.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/cli-tests.yml +1 -1
- package/.github/workflows/documentation.yml +1 -1
- package/.github/workflows/publish.yml +6 -4
- package/.github/workflows/tests.yml +1 -1
- package/README.md +31 -7
- package/dist/esm/browser-bundle.test.d.ts +1 -0
- package/dist/esm/browser-bundle.test.js +29 -0
- package/dist/esm/cli.js +69 -10
- package/dist/esm/configureAmplify.test.d.ts +1 -0
- package/dist/esm/configureAmplify.test.js +37 -0
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +1 -1
- package/dist/esm/library.d.ts +18 -6
- package/dist/esm/library.js +87 -55
- package/dist/esm/library.test.js +230 -192
- package/dist/esm/token-storage.d.ts +14 -0
- package/dist/esm/token-storage.js +81 -0
- package/eslint.config.mjs +12 -1
- package/package.json +15 -3
- package/src/browser-bundle.test.ts +21 -0
- package/src/buffer-utils.test.ts +1 -1
- package/src/cli.ts +113 -40
- package/src/configureAmplify.test.ts +47 -0
- package/src/index.ts +1 -1
- package/src/library.test.ts +279 -206
- package/src/library.ts +125 -70
- package/src/token-storage.ts +78 -0
package/dist/esm/library.js
CHANGED
|
@@ -10,9 +10,20 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
import { strict as assert } from "assert";
|
|
11
11
|
import { Amplify } from "aws-amplify";
|
|
12
12
|
import * as amplifyAuth from "aws-amplify/auth";
|
|
13
|
-
import
|
|
13
|
+
import { cognitoUserPoolsTokenProvider } from "aws-amplify/auth/cognito";
|
|
14
14
|
import { processResponse } from "./buffer-utils";
|
|
15
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
|
+
});
|
|
16
27
|
const amplifyconfiguration = {
|
|
17
28
|
aws_project_region: "eu-central-1",
|
|
18
29
|
aws_user_pools_id: "eu-central-1_BYmQ2VBlo",
|
|
@@ -28,11 +39,15 @@ let amplifyConfigured = false;
|
|
|
28
39
|
/**
|
|
29
40
|
* Configures Amplify if not already configured.
|
|
30
41
|
* Uses a local flag to avoid calling getConfig() which prints a warning.
|
|
42
|
+
* @param {object} [storage] - Optional custom storage adapter for token persistence
|
|
31
43
|
*/
|
|
32
|
-
const configureAmplify = () => {
|
|
44
|
+
const configureAmplify = (storage) => {
|
|
33
45
|
if (amplifyConfigured)
|
|
34
46
|
return;
|
|
35
47
|
Amplify.configure(amplifyconfiguration);
|
|
48
|
+
if (storage) {
|
|
49
|
+
cognitoUserPoolsTokenProvider.setKeyValueStorage(storage);
|
|
50
|
+
}
|
|
36
51
|
amplifyConfigured = true;
|
|
37
52
|
};
|
|
38
53
|
/**
|
|
@@ -63,11 +78,30 @@ const createAuthService = (auth) => {
|
|
|
63
78
|
assert.ok(tokens.idToken, "No ID token found");
|
|
64
79
|
return tokens.idToken.toString();
|
|
65
80
|
});
|
|
66
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Retrieves the current session, refreshing tokens if necessary.
|
|
83
|
+
* Requires a prior successful signIn() call.
|
|
84
|
+
* @param {boolean} [forceRefresh=false] - Force token refresh even if valid
|
|
85
|
+
* @param {boolean} [legacy=false] - If true, returns accessToken for legacy API
|
|
86
|
+
* @returns {Promise<string>} - The JWT token (idToken or accessToken)
|
|
87
|
+
* @throws {Error} - If no session exists (user needs to sign in)
|
|
88
|
+
*/
|
|
89
|
+
const getSession = (...args_1) => __awaiter(void 0, [...args_1], void 0, function* (forceRefresh = false, legacy = false) {
|
|
90
|
+
configureAmplify();
|
|
91
|
+
const { tokens } = yield auth.fetchAuthSession({ forceRefresh });
|
|
92
|
+
assert.ok(tokens, "No session found - please sign in first");
|
|
93
|
+
if (legacy) {
|
|
94
|
+
assert.ok(tokens.accessToken, "No access token found");
|
|
95
|
+
return tokens.accessToken.toString();
|
|
96
|
+
}
|
|
97
|
+
assert.ok(tokens.idToken, "No ID token found");
|
|
98
|
+
return tokens.idToken.toString();
|
|
99
|
+
});
|
|
100
|
+
return { signIn, getSession };
|
|
67
101
|
};
|
|
68
102
|
// Create the default auth service using amplifyAuth
|
|
69
|
-
const { signIn } = createAuthService(amplifyAuth);
|
|
70
|
-
const deviceInfo = (
|
|
103
|
+
const { signIn, getSession } = createAuthService(amplifyAuth);
|
|
104
|
+
const deviceInfo = (baseURL) =>
|
|
71
105
|
/**
|
|
72
106
|
* Retrieves information about a device by its MAC address.
|
|
73
107
|
* Automatically decompresses any gzip-compressed Buffer fields in the response.
|
|
@@ -77,16 +111,21 @@ const deviceInfo = (axiosInstance) =>
|
|
|
77
111
|
* @returns {Promise<DeviceInfoType>} - A promise that resolves to the device info.
|
|
78
112
|
*/
|
|
79
113
|
(jwtToken, macAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
80
|
-
const
|
|
114
|
+
const data = yield fetchJson(baseURL, `device/${macAddress}/info`, {
|
|
115
|
+
method: "GET",
|
|
81
116
|
headers: headers(jwtToken),
|
|
82
117
|
});
|
|
83
118
|
// Process response to decompress any gzipped Buffer fields
|
|
84
|
-
return processResponse(
|
|
119
|
+
return processResponse(data);
|
|
85
120
|
});
|
|
86
|
-
const mqttCommand = (
|
|
121
|
+
const mqttCommand = (baseURL) =>
|
|
87
122
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
88
|
-
(jwtToken, macAddress, payload) =>
|
|
89
|
-
|
|
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) =>
|
|
90
129
|
/**
|
|
91
130
|
* Sends a command to set the power state of a device.
|
|
92
131
|
*
|
|
@@ -95,8 +134,8 @@ const setPower = (axiosInstance) =>
|
|
|
95
134
|
* @param {number} value - The desired power state (1 for ON, 0 for OFF).
|
|
96
135
|
* @returns {Promise<string>} - A promise that resolves to the command response.
|
|
97
136
|
*/
|
|
98
|
-
(jwtToken, macAddress, value) => mqttCommand(
|
|
99
|
-
const setPowerOn = (
|
|
137
|
+
(jwtToken, macAddress, value) => mqttCommand(baseURL)(jwtToken, macAddress, { name: "power", value });
|
|
138
|
+
const setPowerOn = (baseURL) =>
|
|
100
139
|
/**
|
|
101
140
|
* Turns a device ON by setting its power state.
|
|
102
141
|
*
|
|
@@ -108,8 +147,8 @@ const setPowerOn = (axiosInstance) =>
|
|
|
108
147
|
* const response = await api.setPowerOn(jwtToken, macAddress);
|
|
109
148
|
* console.log(response);
|
|
110
149
|
*/
|
|
111
|
-
(jwtToken, macAddress) => setPower(
|
|
112
|
-
const setPowerOff = (
|
|
150
|
+
(jwtToken, macAddress) => setPower(baseURL)(jwtToken, macAddress, 1);
|
|
151
|
+
const setPowerOff = (baseURL) =>
|
|
113
152
|
/**
|
|
114
153
|
* Turns a device OFF by setting its power state.
|
|
115
154
|
*
|
|
@@ -121,8 +160,8 @@ const setPowerOff = (axiosInstance) =>
|
|
|
121
160
|
* const response = await api.setPowerOff(jwtToken, macAddress);
|
|
122
161
|
* console.log(response);
|
|
123
162
|
*/
|
|
124
|
-
(jwtToken, macAddress) => setPower(
|
|
125
|
-
const getPower = (
|
|
163
|
+
(jwtToken, macAddress) => setPower(baseURL)(jwtToken, macAddress, 0);
|
|
164
|
+
const getPower = (baseURL) =>
|
|
126
165
|
/**
|
|
127
166
|
* Retrieves the power status of the device.
|
|
128
167
|
*
|
|
@@ -131,10 +170,10 @@ const getPower = (axiosInstance) =>
|
|
|
131
170
|
* @returns {Promise<boolean>} - A promise that resolves to the power status.
|
|
132
171
|
*/
|
|
133
172
|
(jwtToken, macAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
134
|
-
const info = yield deviceInfo(
|
|
173
|
+
const info = yield deviceInfo(baseURL)(jwtToken, macAddress);
|
|
135
174
|
return info.status.commands.power;
|
|
136
175
|
});
|
|
137
|
-
const getEnvironmentTemperature = (
|
|
176
|
+
const getEnvironmentTemperature = (baseURL) =>
|
|
138
177
|
/**
|
|
139
178
|
* Retrieves the environment temperature from the device's sensors.
|
|
140
179
|
*
|
|
@@ -143,10 +182,10 @@ const getEnvironmentTemperature = (axiosInstance) =>
|
|
|
143
182
|
* @returns {Promise<number>} - A promise that resolves to the temperature value.
|
|
144
183
|
*/
|
|
145
184
|
(jwtToken, macAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
146
|
-
const info = yield deviceInfo(
|
|
185
|
+
const info = yield deviceInfo(baseURL)(jwtToken, macAddress);
|
|
147
186
|
return info.status.temperatures.enviroment;
|
|
148
187
|
});
|
|
149
|
-
const getTargetTemperature = (
|
|
188
|
+
const getTargetTemperature = (baseURL) =>
|
|
150
189
|
/**
|
|
151
190
|
* Retrieves the target temperature value set on the device.
|
|
152
191
|
*
|
|
@@ -155,10 +194,10 @@ const getTargetTemperature = (axiosInstance) =>
|
|
|
155
194
|
* @returns {Promise<number>} - A promise that resolves to the target temperature (degree celsius).
|
|
156
195
|
*/
|
|
157
196
|
(jwtToken, macAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
158
|
-
const info = yield deviceInfo(
|
|
197
|
+
const info = yield deviceInfo(baseURL)(jwtToken, macAddress);
|
|
159
198
|
return info.nvm.user_parameters.enviroment_1_temperature;
|
|
160
199
|
});
|
|
161
|
-
const setTargetTemperature = (
|
|
200
|
+
const setTargetTemperature = (baseURL) =>
|
|
162
201
|
/**
|
|
163
202
|
* Sends a command to set the target temperature (degree celsius) of a device.
|
|
164
203
|
*
|
|
@@ -167,11 +206,11 @@ const setTargetTemperature = (axiosInstance) =>
|
|
|
167
206
|
* @param {number} temperature - The desired target temperature (degree celsius).
|
|
168
207
|
* @returns {Promise<string>} - A promise that resolves to the command response.
|
|
169
208
|
*/
|
|
170
|
-
(jwtToken, macAddress, temperature) => mqttCommand(
|
|
209
|
+
(jwtToken, macAddress, temperature) => mqttCommand(baseURL)(jwtToken, macAddress, {
|
|
171
210
|
name: "enviroment_1_temperature",
|
|
172
211
|
value: temperature,
|
|
173
212
|
});
|
|
174
|
-
const registerDevice = (
|
|
213
|
+
const registerDevice = (baseURL) =>
|
|
175
214
|
/**
|
|
176
215
|
* Registers a device with the user's account.
|
|
177
216
|
* This must be called before other device operations will work on the new API.
|
|
@@ -190,10 +229,13 @@ const registerDevice = (axiosInstance) =>
|
|
|
190
229
|
deviceRoom,
|
|
191
230
|
serialNumber,
|
|
192
231
|
};
|
|
193
|
-
|
|
194
|
-
|
|
232
|
+
return fetchJson(baseURL, "device", {
|
|
233
|
+
method: "POST",
|
|
234
|
+
headers: Object.assign({ "Content-Type": "application/json" }, headers(jwtToken)),
|
|
235
|
+
body: JSON.stringify(body),
|
|
236
|
+
});
|
|
195
237
|
});
|
|
196
|
-
const editDevice = (
|
|
238
|
+
const editDevice = (baseURL) =>
|
|
197
239
|
/**
|
|
198
240
|
* Updates a device's name and room.
|
|
199
241
|
*
|
|
@@ -209,8 +251,11 @@ const editDevice = (axiosInstance) =>
|
|
|
209
251
|
deviceName,
|
|
210
252
|
deviceRoom,
|
|
211
253
|
};
|
|
212
|
-
|
|
213
|
-
|
|
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
|
+
});
|
|
214
259
|
});
|
|
215
260
|
/**
|
|
216
261
|
* Configures the library for API interactions.
|
|
@@ -223,29 +268,16 @@ const editDevice = (axiosInstance) =>
|
|
|
223
268
|
* const api = configure();
|
|
224
269
|
* const power = await api.getPower(jwtToken, macAddress);
|
|
225
270
|
*/
|
|
226
|
-
const configure = (baseURL = API_URL) => {
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
deviceInfo: deviceInfoInstance,
|
|
240
|
-
registerDevice: registerDeviceInstance,
|
|
241
|
-
editDevice: editDeviceInstance,
|
|
242
|
-
setPower: setPowerInstance,
|
|
243
|
-
setPowerOff: setPowerOffInstance,
|
|
244
|
-
setPowerOn: setPowerOnInstance,
|
|
245
|
-
getPower: getPowerInstance,
|
|
246
|
-
getEnvironmentTemperature: getEnvironmentTemperatureInstance,
|
|
247
|
-
getTargetTemperature: getTargetTemperatureInstance,
|
|
248
|
-
setTargetTemperature: setTargetTemperatureInstance,
|
|
249
|
-
};
|
|
250
|
-
};
|
|
251
|
-
export { configure, createAuthService, headers, signIn };
|
|
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
|
+
});
|
|
283
|
+
export { configure, configureAmplify, createAuthService, getSession, headers, signIn, };
|