edilkamin 1.7.2 → 1.7.4

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.
Files changed (65) hide show
  1. package/.github/workflows/cli-tests.yml +6 -0
  2. package/.github/workflows/publish.yml +1 -1
  3. package/README.md +1 -1
  4. package/dist/cjs/package.json +84 -0
  5. package/dist/cjs/src/browser-bundle.test.js +64 -0
  6. package/dist/cjs/src/buffer-utils.js +78 -0
  7. package/dist/cjs/src/buffer-utils.test.js +186 -0
  8. package/dist/cjs/src/cli.js +253 -0
  9. package/dist/cjs/src/configureAmplify.test.js +42 -0
  10. package/dist/cjs/src/constants.js +9 -0
  11. package/dist/{esm → cjs/src}/index.d.ts +1 -2
  12. package/dist/cjs/src/index.js +22 -0
  13. package/dist/{esm → cjs/src}/library.d.ts +4 -4
  14. package/dist/cjs/src/library.js +324 -0
  15. package/dist/cjs/src/library.test.js +547 -0
  16. package/dist/cjs/src/serial-utils.js +50 -0
  17. package/dist/cjs/src/serial-utils.test.d.ts +1 -0
  18. package/dist/cjs/src/serial-utils.test.js +50 -0
  19. package/dist/cjs/src/token-storage.js +119 -0
  20. package/dist/cjs/src/types.js +2 -0
  21. package/dist/esm/package.json +84 -0
  22. package/dist/esm/src/browser-bundle.test.d.ts +1 -0
  23. package/dist/esm/src/browser-bundle.test.js +29 -0
  24. package/dist/esm/src/buffer-utils.d.ts +25 -0
  25. package/dist/esm/src/buffer-utils.test.d.ts +1 -0
  26. package/dist/esm/src/cli.d.ts +3 -0
  27. package/dist/esm/src/configureAmplify.test.d.ts +1 -0
  28. package/dist/esm/src/configureAmplify.test.js +37 -0
  29. package/dist/esm/src/constants.d.ts +4 -0
  30. package/dist/esm/src/index.d.ts +6 -0
  31. package/dist/esm/{index.js → src/index.js} +0 -1
  32. package/dist/esm/src/library.d.ts +55 -0
  33. package/dist/esm/{library.js → src/library.js} +59 -51
  34. package/dist/esm/src/library.test.d.ts +1 -0
  35. package/dist/esm/{library.test.js → src/library.test.js} +167 -190
  36. package/dist/esm/src/serial-utils.d.ts +33 -0
  37. package/dist/esm/src/serial-utils.test.d.ts +1 -0
  38. package/dist/esm/src/token-storage.d.ts +14 -0
  39. package/dist/esm/src/types.d.ts +73 -0
  40. package/dist/esm/src/types.js +1 -0
  41. package/package.json +19 -7
  42. package/src/browser-bundle.test.ts +21 -0
  43. package/src/configureAmplify.test.ts +47 -0
  44. package/src/index.ts +0 -1
  45. package/src/library.test.ts +195 -197
  46. package/src/library.ts +76 -62
  47. package/tsconfig.cjs.json +2 -2
  48. package/tsconfig.json +2 -3
  49. /package/dist/{esm/buffer-utils.test.d.ts → cjs/src/browser-bundle.test.d.ts} +0 -0
  50. /package/dist/{esm → cjs/src}/buffer-utils.d.ts +0 -0
  51. /package/dist/{esm/library.test.d.ts → cjs/src/buffer-utils.test.d.ts} +0 -0
  52. /package/dist/{esm → cjs/src}/cli.d.ts +0 -0
  53. /package/dist/{esm/serial-utils.test.d.ts → cjs/src/configureAmplify.test.d.ts} +0 -0
  54. /package/dist/{esm → cjs/src}/constants.d.ts +0 -0
  55. /package/dist/{esm/types.js → cjs/src/library.test.d.ts} +0 -0
  56. /package/dist/{esm → cjs/src}/serial-utils.d.ts +0 -0
  57. /package/dist/{esm → cjs/src}/token-storage.d.ts +0 -0
  58. /package/dist/{esm → cjs/src}/types.d.ts +0 -0
  59. /package/dist/esm/{buffer-utils.js → src/buffer-utils.js} +0 -0
  60. /package/dist/esm/{buffer-utils.test.js → src/buffer-utils.test.js} +0 -0
  61. /package/dist/esm/{cli.js → src/cli.js} +0 -0
  62. /package/dist/esm/{constants.js → src/constants.js} +0 -0
  63. /package/dist/esm/{serial-utils.js → src/serial-utils.js} +0 -0
  64. /package/dist/esm/{serial-utils.test.js → src/serial-utils.test.js} +0 -0
  65. /package/dist/esm/{token-storage.js → src/token-storage.js} +0 -0
package/src/library.ts CHANGED
@@ -2,7 +2,6 @@ import { strict as assert } from "assert";
2
2
  import { Amplify } from "aws-amplify";
3
3
  import * as amplifyAuth from "aws-amplify/auth";
4
4
  import { cognitoUserPoolsTokenProvider } from "aws-amplify/auth/cognito";
5
- import axios, { AxiosInstance } from "axios";
6
5
 
7
6
  import { processResponse } from "./buffer-utils";
8
7
  import { API_URL } from "./constants";
@@ -14,6 +13,22 @@ import {
14
13
  EditDeviceAssociationBody,
15
14
  } from "./types";
16
15
 
16
+ /**
17
+ * Makes a fetch request and returns parsed JSON response.
18
+ * Throws an error for non-2xx status codes.
19
+ */
20
+ const fetchJson = async <T>(
21
+ baseURL: string,
22
+ path: string,
23
+ options: RequestInit = {},
24
+ ): Promise<T> => {
25
+ const response = await fetch(`${baseURL}${path}`, options);
26
+ if (!response.ok) {
27
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
28
+ }
29
+ return response.json();
30
+ };
31
+
17
32
  const amplifyconfiguration = {
18
33
  aws_project_region: "eu-central-1",
19
34
  aws_user_pools_id: "eu-central-1_BYmQ2VBlo",
@@ -111,7 +126,7 @@ const createAuthService = (auth: typeof amplifyAuth) => {
111
126
  const { signIn, getSession } = createAuthService(amplifyAuth);
112
127
 
113
128
  const deviceInfo =
114
- (axiosInstance: AxiosInstance) =>
129
+ (baseURL: string) =>
115
130
  /**
116
131
  * Retrieves information about a device by its MAC address.
117
132
  * Automatically decompresses any gzip-compressed Buffer fields in the response.
@@ -121,28 +136,33 @@ const deviceInfo =
121
136
  * @returns {Promise<DeviceInfoType>} - A promise that resolves to the device info.
122
137
  */
123
138
  async (jwtToken: string, macAddress: string): Promise<DeviceInfoType> => {
124
- const response = await axiosInstance.get<DeviceInfoRawType>(
139
+ const data = await fetchJson<DeviceInfoRawType>(
140
+ baseURL,
125
141
  `device/${macAddress}/info`,
126
142
  {
143
+ method: "GET",
127
144
  headers: headers(jwtToken),
128
145
  },
129
146
  );
130
147
  // Process response to decompress any gzipped Buffer fields
131
- return processResponse(response.data) as DeviceInfoType;
148
+ return processResponse(data) as DeviceInfoType;
132
149
  };
133
150
 
134
151
  const mqttCommand =
135
- (axiosInstance: AxiosInstance) =>
152
+ (baseURL: string) =>
136
153
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
137
154
  (jwtToken: string, macAddress: string, payload: any) =>
138
- axiosInstance.put(
139
- "mqtt/command",
140
- { mac_address: macAddress, ...payload },
141
- { headers: headers(jwtToken) },
142
- );
155
+ fetchJson(baseURL, "mqtt/command", {
156
+ method: "PUT",
157
+ headers: {
158
+ "Content-Type": "application/json",
159
+ ...headers(jwtToken),
160
+ },
161
+ body: JSON.stringify({ mac_address: macAddress, ...payload }),
162
+ });
143
163
 
144
164
  const setPower =
145
- (axiosInstance: AxiosInstance) =>
165
+ (baseURL: string) =>
146
166
  /**
147
167
  * Sends a command to set the power state of a device.
148
168
  *
@@ -152,10 +172,10 @@ const setPower =
152
172
  * @returns {Promise<string>} - A promise that resolves to the command response.
153
173
  */
154
174
  (jwtToken: string, macAddress: string, value: number) =>
155
- mqttCommand(axiosInstance)(jwtToken, macAddress, { name: "power", value });
175
+ mqttCommand(baseURL)(jwtToken, macAddress, { name: "power", value });
156
176
 
157
177
  const setPowerOn =
158
- (axiosInstance: AxiosInstance) =>
178
+ (baseURL: string) =>
159
179
  /**
160
180
  * Turns a device ON by setting its power state.
161
181
  *
@@ -168,10 +188,10 @@ const setPowerOn =
168
188
  * console.log(response);
169
189
  */
170
190
  (jwtToken: string, macAddress: string) =>
171
- setPower(axiosInstance)(jwtToken, macAddress, 1);
191
+ setPower(baseURL)(jwtToken, macAddress, 1);
172
192
 
173
193
  const setPowerOff =
174
- (axiosInstance: AxiosInstance) =>
194
+ (baseURL: string) =>
175
195
  /**
176
196
  * Turns a device OFF by setting its power state.
177
197
  *
@@ -184,10 +204,10 @@ const setPowerOff =
184
204
  * console.log(response);
185
205
  */
186
206
  (jwtToken: string, macAddress: string) =>
187
- setPower(axiosInstance)(jwtToken, macAddress, 0);
207
+ setPower(baseURL)(jwtToken, macAddress, 0);
188
208
 
189
209
  const getPower =
190
- (axiosInstance: AxiosInstance) =>
210
+ (baseURL: string) =>
191
211
  /**
192
212
  * Retrieves the power status of the device.
193
213
  *
@@ -196,12 +216,12 @@ const getPower =
196
216
  * @returns {Promise<boolean>} - A promise that resolves to the power status.
197
217
  */
198
218
  async (jwtToken: string, macAddress: string): Promise<boolean> => {
199
- const info = await deviceInfo(axiosInstance)(jwtToken, macAddress);
219
+ const info = await deviceInfo(baseURL)(jwtToken, macAddress);
200
220
  return info.status.commands.power;
201
221
  };
202
222
 
203
223
  const getEnvironmentTemperature =
204
- (axiosInstance: AxiosInstance) =>
224
+ (baseURL: string) =>
205
225
  /**
206
226
  * Retrieves the environment temperature from the device's sensors.
207
227
  *
@@ -210,12 +230,12 @@ const getEnvironmentTemperature =
210
230
  * @returns {Promise<number>} - A promise that resolves to the temperature value.
211
231
  */
212
232
  async (jwtToken: string, macAddress: string): Promise<number> => {
213
- const info = await deviceInfo(axiosInstance)(jwtToken, macAddress);
233
+ const info = await deviceInfo(baseURL)(jwtToken, macAddress);
214
234
  return info.status.temperatures.enviroment;
215
235
  };
216
236
 
217
237
  const getTargetTemperature =
218
- (axiosInstance: AxiosInstance) =>
238
+ (baseURL: string) =>
219
239
  /**
220
240
  * Retrieves the target temperature value set on the device.
221
241
  *
@@ -224,12 +244,12 @@ const getTargetTemperature =
224
244
  * @returns {Promise<number>} - A promise that resolves to the target temperature (degree celsius).
225
245
  */
226
246
  async (jwtToken: string, macAddress: string): Promise<number> => {
227
- const info = await deviceInfo(axiosInstance)(jwtToken, macAddress);
247
+ const info = await deviceInfo(baseURL)(jwtToken, macAddress);
228
248
  return info.nvm.user_parameters.enviroment_1_temperature;
229
249
  };
230
250
 
231
251
  const setTargetTemperature =
232
- (axiosInstance: AxiosInstance) =>
252
+ (baseURL: string) =>
233
253
  /**
234
254
  * Sends a command to set the target temperature (degree celsius) of a device.
235
255
  *
@@ -239,13 +259,13 @@ const setTargetTemperature =
239
259
  * @returns {Promise<string>} - A promise that resolves to the command response.
240
260
  */
241
261
  (jwtToken: string, macAddress: string, temperature: number) =>
242
- mqttCommand(axiosInstance)(jwtToken, macAddress, {
262
+ mqttCommand(baseURL)(jwtToken, macAddress, {
243
263
  name: "enviroment_1_temperature",
244
264
  value: temperature,
245
265
  });
246
266
 
247
267
  const registerDevice =
248
- (axiosInstance: AxiosInstance) =>
268
+ (baseURL: string) =>
249
269
  /**
250
270
  * Registers a device with the user's account.
251
271
  * This must be called before other device operations will work on the new API.
@@ -270,16 +290,18 @@ const registerDevice =
270
290
  deviceRoom,
271
291
  serialNumber,
272
292
  };
273
- const response = await axiosInstance.post<DeviceAssociationResponse>(
274
- "device",
275
- body,
276
- { headers: headers(jwtToken) },
277
- );
278
- return response.data;
293
+ return fetchJson<DeviceAssociationResponse>(baseURL, "device", {
294
+ method: "POST",
295
+ headers: {
296
+ "Content-Type": "application/json",
297
+ ...headers(jwtToken),
298
+ },
299
+ body: JSON.stringify(body),
300
+ });
279
301
  };
280
302
 
281
303
  const editDevice =
282
- (axiosInstance: AxiosInstance) =>
304
+ (baseURL: string) =>
283
305
  /**
284
306
  * Updates a device's name and room.
285
307
  *
@@ -300,12 +322,18 @@ const editDevice =
300
322
  deviceName,
301
323
  deviceRoom,
302
324
  };
303
- const response = await axiosInstance.put<DeviceAssociationResponse>(
325
+ return fetchJson<DeviceAssociationResponse>(
326
+ baseURL,
304
327
  `device/${normalizedMac}`,
305
- body,
306
- { headers: headers(jwtToken) },
328
+ {
329
+ method: "PUT",
330
+ headers: {
331
+ "Content-Type": "application/json",
332
+ ...headers(jwtToken),
333
+ },
334
+ body: JSON.stringify(body),
335
+ },
307
336
  );
308
- return response.data;
309
337
  };
310
338
 
311
339
  /**
@@ -319,32 +347,18 @@ const editDevice =
319
347
  * const api = configure();
320
348
  * const power = await api.getPower(jwtToken, macAddress);
321
349
  */
322
- const configure = (baseURL: string = API_URL) => {
323
- const axiosInstance = axios.create({ baseURL });
324
- const deviceInfoInstance = deviceInfo(axiosInstance);
325
- const registerDeviceInstance = registerDevice(axiosInstance);
326
- const editDeviceInstance = editDevice(axiosInstance);
327
- const setPowerInstance = setPower(axiosInstance);
328
- const setPowerOffInstance = setPowerOff(axiosInstance);
329
- const setPowerOnInstance = setPowerOn(axiosInstance);
330
- const getPowerInstance = getPower(axiosInstance);
331
- const getEnvironmentTemperatureInstance =
332
- getEnvironmentTemperature(axiosInstance);
333
- const getTargetTemperatureInstance = getTargetTemperature(axiosInstance);
334
- const setTargetTemperatureInstance = setTargetTemperature(axiosInstance);
335
- return {
336
- deviceInfo: deviceInfoInstance,
337
- registerDevice: registerDeviceInstance,
338
- editDevice: editDeviceInstance,
339
- setPower: setPowerInstance,
340
- setPowerOff: setPowerOffInstance,
341
- setPowerOn: setPowerOnInstance,
342
- getPower: getPowerInstance,
343
- getEnvironmentTemperature: getEnvironmentTemperatureInstance,
344
- getTargetTemperature: getTargetTemperatureInstance,
345
- setTargetTemperature: setTargetTemperatureInstance,
346
- };
347
- };
350
+ const configure = (baseURL: string = API_URL) => ({
351
+ deviceInfo: deviceInfo(baseURL),
352
+ registerDevice: registerDevice(baseURL),
353
+ editDevice: editDevice(baseURL),
354
+ setPower: setPower(baseURL),
355
+ setPowerOff: setPowerOff(baseURL),
356
+ setPowerOn: setPowerOn(baseURL),
357
+ getPower: getPower(baseURL),
358
+ getEnvironmentTemperature: getEnvironmentTemperature(baseURL),
359
+ getTargetTemperature: getTargetTemperature(baseURL),
360
+ setTargetTemperature: setTargetTemperature(baseURL),
361
+ });
348
362
 
349
363
  export {
350
364
  configure,
package/tsconfig.cjs.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "extends": "./tsconfig.json",
3
3
  "compilerOptions": {
4
- "outDir": "dist/esm",
5
- "module": "es6"
4
+ "outDir": "dist/cjs",
5
+ "module": "commonjs"
6
6
  }
7
7
  }
package/tsconfig.json CHANGED
@@ -1,10 +1,9 @@
1
1
  {
2
2
  "compilerOptions": {
3
- "target": "es6",
4
3
  "target": "es6",
5
4
  "moduleResolution": "node",
6
5
  "outDir": "dist",
7
- "rootDir": "src",
6
+ "rootDir": ".",
8
7
  "strict": true,
9
8
  "declaration": true,
10
9
  "esModuleInterop": true,
@@ -13,5 +12,5 @@
13
12
  "noUnusedParameters": true,
14
13
  "preserveConstEnums": true
15
14
  },
16
- "include": ["src/**/*.ts"]
15
+ "include": ["src/**/*.ts", "package.json"]
17
16
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes