@kohost/api-client 3.0.0-beta.22 → 3.0.0-beta.25

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.
@@ -185,9 +185,11 @@ var amqp = require("amqplib");
185
185
  var crypto = require("crypto");
186
186
  var isFatalError = require("amqplib/lib/connection").isFatalError;
187
187
  var debug = require("debug")("kohost:amqp-client");
188
- var HEADER_KEY_ORGANIZATION_ID = "organization-id";
189
- var HEADER_KEY_PROPERTY_ID = "property-id";
190
- var HEADER_KEY_DRIVER = "driver";
188
+ var HEADER_KEY_ORGANIZATION_ID = "X-Organization-Id";
189
+ var HEADER_KEY_PROPERTY_ID = "X-Property-Id";
190
+ var HEADER_KEY_DRIVER = "X-Driver";
191
+ var HEADER_KEY_COMMAND_NAME = "X-Command-Name";
192
+ var HEADER_KEY_EVENT_NAME = "X-Event-Name";
191
193
  var exchanges = {
192
194
  // routes commands based on `command-name` header and in many cases `property-id` header
193
195
  Commands: {
@@ -272,21 +274,19 @@ var KohostAMQPClient = class {
272
274
  debug("parseMessage input %o", message);
273
275
  let error = null;
274
276
  let data = {};
275
- let params = {};
276
277
  let query = {};
277
278
  let context = {};
278
279
  let headers = {};
279
280
  const isCommand = ((_a = message == null ? void 0 : message.properties) == null ? void 0 : _a.type) === "Command";
280
281
  const isEvent = ((_b = message == null ? void 0 : message.properties) == null ? void 0 : _b.type) === "Event";
281
282
  const messageHeaders = ((_c = message == null ? void 0 : message.properties) == null ? void 0 : _c.headers) || {};
282
- const commandName = messageHeaders["command-name"] || null;
283
- const eventName = messageHeaders["event-name"] || null;
283
+ const commandName = messageHeaders[HEADER_KEY_COMMAND_NAME] || null;
284
+ const eventName = messageHeaders[HEADER_KEY_EVENT_NAME] || null;
284
285
  if (message.content) {
285
286
  try {
286
287
  const payload = ((_d = message.properties) == null ? void 0 : _d.contentType) === "application/json" ? JSON.parse(message.content.toString()) : message.content.toString();
287
288
  data = (payload == null ? void 0 : payload.data) || {};
288
289
  error = payload == null ? void 0 : payload.error;
289
- params = (payload == null ? void 0 : payload.params) || {};
290
290
  query = (payload == null ? void 0 : payload.query) || {};
291
291
  context = (payload == null ? void 0 : payload.context) || {};
292
292
  } catch (error2) {
@@ -314,7 +314,6 @@ var KohostAMQPClient = class {
314
314
  if (error)
315
315
  parsed.error = this.parseError(error);
316
316
  parsed.data = data;
317
- parsed.params = params;
318
317
  parsed.query = query;
319
318
  parsed.context = context;
320
319
  parsed.headers = headers;
@@ -5100,6 +5100,7 @@ var KohostApiClient = class extends EventEmitter {
5100
5100
  throw new Error("options.property is required");
5101
5101
  this.options = options;
5102
5102
  this.isBrowser = typeof window !== "undefined";
5103
+ this.isRefreshingToken = false;
5103
5104
  const config = {
5104
5105
  baseURL: options.url,
5105
5106
  responseType: "json",
@@ -5163,19 +5164,22 @@ var KohostApiClient = class extends EventEmitter {
5163
5164
  return Promise.reject(error);
5164
5165
  }
5165
5166
  if (expectedError && newTokensNeeded) {
5166
- return this.RefreshToken().then(() => {
5167
- return this._http(originalReq);
5168
- });
5167
+ while (!this.isRefreshingToken) {
5168
+ this.isRefreshingToken = true;
5169
+ return this.RefreshToken().then(() => {
5170
+ this.isRefreshingToken = false;
5171
+ return this._http(originalReq);
5172
+ }).catch((err) => {
5173
+ this.isRefreshingToken = false;
5174
+ return Promise.reject(err);
5175
+ });
5176
+ }
5169
5177
  }
5170
5178
  } catch (error2) {
5171
5179
  console.log(error2);
5172
5180
  }
5173
5181
  return Promise.reject(error);
5174
5182
  }
5175
- /*
5176
- @param {String} token - The token to set
5177
- @returns undefined
5178
- */
5179
5183
  _onLoginRequired() {
5180
5184
  this.emit("LoginRequired");
5181
5185
  }
@@ -225,11 +225,11 @@ var require_SystemWindowCoveringUpdatedEvent = __commonJS({
225
225
  }
226
226
  });
227
227
 
228
- // src/Events/SystemSourceUpdatedEvent.js
229
- var require_SystemSourceUpdatedEvent = __commonJS({
230
- "src/Events/SystemSourceUpdatedEvent.js"(exports2, module2) {
228
+ // src/Events/SystemMediaSourceUpdatedEvent.js
229
+ var require_SystemMediaSourceUpdatedEvent = __commonJS({
230
+ "src/Events/SystemMediaSourceUpdatedEvent.js"(exports2, module2) {
231
231
  var Event = require_Event();
232
- var SystemMediaSourceUpdatedEvent = class extends Event {
232
+ var SystemMediaSourceUpdatedEvent2 = class extends Event {
233
233
  constructor(mediaSource) {
234
234
  super(mediaSource);
235
235
  }
@@ -240,8 +240,8 @@ var require_SystemSourceUpdatedEvent = __commonJS({
240
240
  return `mediaSource.${this.keyId}.updated`;
241
241
  }
242
242
  };
243
- __name(SystemMediaSourceUpdatedEvent, "SystemMediaSourceUpdatedEvent");
244
- module2.exports = SystemMediaSourceUpdatedEvent;
243
+ __name(SystemMediaSourceUpdatedEvent2, "SystemMediaSourceUpdatedEvent");
244
+ module2.exports = SystemMediaSourceUpdatedEvent2;
245
245
  }
246
246
  });
247
247
 
@@ -265,6 +265,26 @@ var require_SystemCourtesyUpdatedEvent = __commonJS({
265
265
  }
266
266
  });
267
267
 
268
+ // src/Events/SystemMotionSensorUpdatedEvent.js
269
+ var require_SystemMotionSensorUpdatedEvent = __commonJS({
270
+ "src/Events/SystemMotionSensorUpdatedEvent.js"(exports2, module2) {
271
+ var Event = require_Event();
272
+ var SystemMotionSensorUpdatedEvent2 = class extends Event {
273
+ constructor(mediaSource) {
274
+ super(mediaSource);
275
+ }
276
+ get name() {
277
+ return "SystemMotionSensorUpdated";
278
+ }
279
+ get routingKey() {
280
+ return `mediaSource.${this.keyId}.updated`;
281
+ }
282
+ };
283
+ __name(SystemMotionSensorUpdatedEvent2, "SystemMotionSensorUpdatedEvent");
284
+ module2.exports = SystemMotionSensorUpdatedEvent2;
285
+ }
286
+ });
287
+
268
288
  // src/Events/SystemUserUpdatedEvent.js
269
289
  var require_SystemUserUpdatedEvent = __commonJS({
270
290
  "src/Events/SystemUserUpdatedEvent.js"(exports2, module2) {
@@ -425,6 +445,46 @@ var require_ShortLinkCreatedEvent = __commonJS({
425
445
  }
426
446
  });
427
447
 
448
+ // src/Events/ApplicationInUseEvent.js
449
+ var require_ApplicationInUseEvent = __commonJS({
450
+ "src/Events/ApplicationInUseEvent.js"(exports2, module2) {
451
+ var Event = require_Event();
452
+ var ApplicationInUseEvent2 = class extends Event {
453
+ constructor({ propertyId }) {
454
+ super({ propertyId });
455
+ }
456
+ get name() {
457
+ return "ApplicationInUse";
458
+ }
459
+ get routingKey() {
460
+ return `app.${this.data[0].propertyId}.inUse`;
461
+ }
462
+ };
463
+ __name(ApplicationInUseEvent2, "ApplicationInUseEvent");
464
+ module2.exports = ApplicationInUseEvent2;
465
+ }
466
+ });
467
+
468
+ // src/Events/ApplicationOutOfUseEvent.js
469
+ var require_ApplicationOutOfUseEvent = __commonJS({
470
+ "src/Events/ApplicationOutOfUseEvent.js"(exports2, module2) {
471
+ var Event = require_Event();
472
+ var ApplicationOutOfUseEvent2 = class extends Event {
473
+ constructor({ propertyId }) {
474
+ super({ propertyId });
475
+ }
476
+ get name() {
477
+ return "ApplicationOutOfUse";
478
+ }
479
+ get routingKey() {
480
+ return `app.${this.data[0].propertyId}.notInUse`;
481
+ }
482
+ };
483
+ __name(ApplicationOutOfUseEvent2, "ApplicationOutOfUseEvent");
484
+ module2.exports = ApplicationOutOfUseEvent2;
485
+ }
486
+ });
487
+
428
488
  // src/Events/index.js
429
489
  var SystemGatewayUpdatedEvent = require_SystemGatewayUpdatedEvent();
430
490
  var SystemThermostatUpdatedEvent = require_SystemThermostatUpdatedEvent();
@@ -434,8 +494,9 @@ var SystemLockUpdatedEvent = require_SystemLockUpdatedEvent();
434
494
  var SystemCameraUpdatedEvent = require_SystemCameraUpdatedEvent();
435
495
  var SystemSceneControllerUpdatedEvent = require_SystemSceneControllerUpdatedEvent();
436
496
  var SystemWindowCoveringUpdatedEvent = require_SystemWindowCoveringUpdatedEvent();
437
- var SystemSourceUpdatedEvent = require_SystemSourceUpdatedEvent();
497
+ var SystemMediaSourceUpdatedEvent = require_SystemMediaSourceUpdatedEvent();
438
498
  var SystemCourtesyUpdatedEvent = require_SystemCourtesyUpdatedEvent();
499
+ var SystemMotionSensorUpdatedEvent = require_SystemMotionSensorUpdatedEvent();
439
500
  var SystemUserUpdatedEvent = require_SystemUserUpdatedEvent();
440
501
  var SystemSpaceUpdatedEvent = require_SystemSpaceUpdatedEvent();
441
502
  var SystemSpaceTypeUpdatedEvent = require_SystemSpaceTypeUpdatedEvent();
@@ -444,6 +505,8 @@ var SystemReservationUpdatedEvent = require_SystemReservationUpdatedEvent();
444
505
  var SMSSentEvent = require_SMSSentEvent();
445
506
  var EmailSentEvent = require_EmailSentEvent();
446
507
  var ShortLinkCreatedEvent = require_ShortLinkCreatedEvent();
508
+ var ApplicationInUseEvent = require_ApplicationInUseEvent();
509
+ var ApplicationOutOfUseEvent = require_ApplicationOutOfUseEvent();
447
510
  module.exports = {
448
511
  SystemGatewayUpdatedEvent,
449
512
  SystemThermostatUpdatedEvent,
@@ -453,7 +516,8 @@ module.exports = {
453
516
  SystemCameraUpdatedEvent,
454
517
  SystemSceneControllerUpdatedEvent,
455
518
  SystemWindowCoveringUpdatedEvent,
456
- SystemSourceUpdatedEvent,
519
+ SystemMediaSourceUpdatedEvent,
520
+ SystemMotionSensorUpdatedEvent,
457
521
  SystemCourtesyUpdatedEvent,
458
522
  SystemUserUpdatedEvent,
459
523
  SystemSpaceUpdatedEvent,
@@ -462,5 +526,7 @@ module.exports = {
462
526
  SystemReservationUpdatedEvent,
463
527
  SMSSentEvent,
464
528
  EmailSentEvent,
465
- ShortLinkCreatedEvent
529
+ ShortLinkCreatedEvent,
530
+ ApplicationInUseEvent,
531
+ ApplicationOutOfUseEvent
466
532
  };
@@ -1907,17 +1907,12 @@ var require_mediaSource = __commonJS({
1907
1907
  title: "Media Source",
1908
1908
  description: "Any media source",
1909
1909
  type: "object",
1910
+ required: ["id", "type", "systemData", "audio", "video", "driver"],
1910
1911
  properties: {
1911
1912
  id: {
1912
1913
  $ref: "https://api.kohost.io/schemas/v3/definitions/device.json#/definitions/id"
1913
1914
  },
1914
1915
  type: {
1915
- $ref: "https://api.kohost.io/schemas/v3/definitions/device.json#/definitions/type"
1916
- },
1917
- driver: {
1918
- $ref: "https://api.kohost.io/schemas/v3/definitions/common.json#/definitions/driver"
1919
- },
1920
- subType: {
1921
1916
  type: "string",
1922
1917
  enum: [
1923
1918
  "tv",
@@ -1928,6 +1923,9 @@ var require_mediaSource = __commonJS({
1928
1923
  "uncontrolledDevice"
1929
1924
  ]
1930
1925
  },
1926
+ driver: {
1927
+ $ref: "https://api.kohost.io/schemas/v3/definitions/common.json#/definitions/driver"
1928
+ },
1931
1929
  audio: {
1932
1930
  type: "boolean"
1933
1931
  },
@@ -1940,6 +1938,27 @@ var require_mediaSource = __commonJS({
1940
1938
  volumeFeedback: {
1941
1939
  type: "boolean"
1942
1940
  },
1941
+ muted: {
1942
+ type: "boolean"
1943
+ },
1944
+ volume: {
1945
+ type: "number",
1946
+ minimum: 0,
1947
+ maximum: 100
1948
+ },
1949
+ power: {
1950
+ type: "string",
1951
+ enum: ["on", "off"]
1952
+ },
1953
+ input: {
1954
+ type: "string"
1955
+ },
1956
+ supportedInputs: {
1957
+ type: "array",
1958
+ items: {
1959
+ type: "string"
1960
+ }
1961
+ },
1943
1962
  command: {
1944
1963
  type: ["string", "null"],
1945
1964
  enum: [
@@ -1983,6 +2002,7 @@ var require_mediaSource = __commonJS({
1983
2002
  "input",
1984
2003
  "power",
1985
2004
  "enterChannel",
2005
+ "enterVolume",
1986
2006
  "number10",
1987
2007
  "number11",
1988
2008
  "number12",
@@ -2039,7 +2059,12 @@ var require_mediaSource = __commonJS({
2039
2059
  "hdmi2",
2040
2060
  "hdmi3",
2041
2061
  "cecDeviceList",
2042
- "mtsSap"
2062
+ "mtsSap",
2063
+ "red",
2064
+ "green",
2065
+ "yellow",
2066
+ "blue",
2067
+ "alert"
2043
2068
  ]
2044
2069
  },
2045
2070
  supportedNotifications: {
@@ -2049,11 +2074,10 @@ var require_mediaSource = __commonJS({
2049
2074
  $ref: "https://api.kohost.io/schemas/v3/definitions/device.json#/definitions/notification"
2050
2075
  },
2051
2076
  systemData: {
2052
- $ref: "https://api.kohost.io/schemas/v3/definitions/device.json#/definitions/systemData"
2077
+ $ref: "https://api.kohost.app/schemas/v3/definitions/device.json#/definitions/systemData"
2053
2078
  }
2054
2079
  },
2055
- additionalProperties: false,
2056
- required: ["id", "type", "systemData", "audio", "video", "driver"]
2080
+ additionalProperties: false
2057
2081
  };
2058
2082
  }
2059
2083
  });
@@ -3624,7 +3648,7 @@ var require_property = __commonJS({
3624
3648
  },
3625
3649
  organization: {
3626
3650
  type: "string",
3627
- description: "Reference to the organization that owns this property"
3651
+ description: "Reference (id) to the organization that owns this property"
3628
3652
  },
3629
3653
  address: {
3630
3654
  type: "object",
@@ -3793,7 +3817,7 @@ var require_property = __commonJS({
3793
3817
  type: "string",
3794
3818
  pattern: "^(?!#ffffff)(#[0-9a-fA-F]{6})$"
3795
3819
  },
3796
- minItems: 3,
3820
+ minItems: 2,
3797
3821
  maxItems: 3
3798
3822
  }
3799
3823
  }
package/dist/cjs/defs.js CHANGED
@@ -62,6 +62,7 @@ var require_Client = __commonJS({
62
62
  throw new Error("options.property is required");
63
63
  this.options = options;
64
64
  this.isBrowser = typeof window !== "undefined";
65
+ this.isRefreshingToken = false;
65
66
  const config = {
66
67
  baseURL: options.url,
67
68
  responseType: "json",
@@ -125,19 +126,22 @@ var require_Client = __commonJS({
125
126
  return Promise.reject(error);
126
127
  }
127
128
  if (expectedError && newTokensNeeded) {
128
- return this.RefreshToken().then(() => {
129
- return this._http(originalReq);
130
- });
129
+ while (!this.isRefreshingToken) {
130
+ this.isRefreshingToken = true;
131
+ return this.RefreshToken().then(() => {
132
+ this.isRefreshingToken = false;
133
+ return this._http(originalReq);
134
+ }).catch((err) => {
135
+ this.isRefreshingToken = false;
136
+ return Promise.reject(err);
137
+ });
138
+ }
131
139
  }
132
140
  } catch (error2) {
133
141
  console.log(error2);
134
142
  }
135
143
  return Promise.reject(error);
136
144
  }
137
- /*
138
- @param {String} token - The token to set
139
- @returns undefined
140
- */
141
145
  _onLoginRequired() {
142
146
  this.emit("LoginRequired");
143
147
  }
package/dist/cjs/utils.js CHANGED
@@ -441,33 +441,12 @@ var require_Errors = __commonJS({
441
441
  var require_errorFactory = __commonJS({
442
442
  "src/utils/errorFactory.js"(exports2, module2) {
443
443
  var Errors = require_Errors();
444
- module2.exports = /* @__PURE__ */ __name(function errorFactory2(err) {
445
- switch (err.type) {
446
- case "AppError":
447
- return new Errors.AppError(err.message);
448
- case "AuthenticationError":
449
- return new Errors.AuthenticationError(err.message);
450
- case "AuthorizationError":
451
- return new Errors.AuthorizationError(err.message);
452
- case "DeviceCommError":
453
- return new Errors.DeviceCommError(err.message);
454
- case "LoginError":
455
- return new Errors.LoginError(err.message);
456
- case "NotFoundError":
457
- return new Errors.NotFoundError(err.message);
458
- case "RequestError":
459
- return new Errors.RequestError(err.message);
460
- case "SystemCommError":
461
- return new Errors.SystemCommError(err.message);
462
- case "TokenExpiredError":
463
- return new Errors.TokenExpiredError(err.message);
464
- case "UnprocessableRequestError":
465
- return new Errors.UnprocessableRequestError(err.message);
466
- case "ValidationError":
467
- return new Errors.ValidationError(err.message);
468
- default:
469
- return new Error(err.message);
470
- }
444
+ module2.exports = /* @__PURE__ */ __name(function errorFactory2(errName) {
445
+ const AllErrors = Object.values(Errors);
446
+ const TheError = AllErrors.find((E) => E.prototype.name === errName);
447
+ if (!TheError)
448
+ return new Error("Invalid error name: " + errName);
449
+ return TheError;
471
450
  }, "errorFactory");
472
451
  }
473
452
  });
@@ -7555,6 +7555,7 @@ var require_Client = __commonJS({
7555
7555
  throw new Error("options.property is required");
7556
7556
  this.options = options;
7557
7557
  this.isBrowser = typeof window !== "undefined";
7558
+ this.isRefreshingToken = false;
7558
7559
  const config = {
7559
7560
  baseURL: options.url,
7560
7561
  responseType: "json",
@@ -7616,19 +7617,22 @@ var require_Client = __commonJS({
7616
7617
  return Promise.reject(error);
7617
7618
  }
7618
7619
  if (expectedError && newTokensNeeded) {
7619
- return this.RefreshToken().then(() => {
7620
- return this._http(originalReq);
7621
- });
7620
+ while (!this.isRefreshingToken) {
7621
+ this.isRefreshingToken = true;
7622
+ return this.RefreshToken().then(() => {
7623
+ this.isRefreshingToken = false;
7624
+ return this._http(originalReq);
7625
+ }).catch((err) => {
7626
+ this.isRefreshingToken = false;
7627
+ return Promise.reject(err);
7628
+ });
7629
+ }
7622
7630
  }
7623
7631
  } catch (error2) {
7624
7632
  console.log(error2);
7625
7633
  }
7626
7634
  return Promise.reject(error);
7627
7635
  }
7628
- /*
7629
- @param {String} token - The token to set
7630
- @returns undefined
7631
- */
7632
7636
  _onLoginRequired() {
7633
7637
  this.emit("LoginRequired");
7634
7638
  }