homey-api 3.0.12 → 3.0.14

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.
@@ -4451,8 +4451,6 @@ export class Util {
4451
4451
 
4452
4452
  static getSearchParameter(name: string): string | null;
4453
4453
 
4454
- static stringifyQuery(object: object): string;
4455
-
4456
4454
  static base64(input: string): string;
4457
4455
 
4458
4456
  static uuid(): string;
@@ -4461,6 +4459,8 @@ export class Util {
4461
4459
 
4462
4460
  static promiseAny(promises: Array<Promise>): Promise<any>;
4463
4461
 
4462
+ static serializeQueryObject(queryObject: object): string;
4463
+
4464
4464
  static fetch(args: any): Promise;
4465
4465
 
4466
4466
  static wait(ms: number): Promise<void>;
@@ -4485,8 +4485,6 @@ export class Util {
4485
4485
 
4486
4486
  static getSearchParameter(name: string): string | null;
4487
4487
 
4488
- static stringifyQuery(object: object): string;
4489
-
4490
4488
  static base64(input: string): string;
4491
4489
 
4492
4490
  static uuid(): string;
@@ -4494,6 +4492,8 @@ export class Util {
4494
4492
  static env(): string;
4495
4493
 
4496
4494
  static promiseAny(promises: Array<Promise>): Promise<any>;
4495
+
4496
+ static serializeQueryObject(queryObject: object): string;
4497
4497
  }
4498
4498
 
4499
4499
  export class APIDefinition {}
@@ -7407,8 +7407,6 @@ export class Util {
7407
7407
 
7408
7408
  static getSearchParameter(name: string): string | null;
7409
7409
 
7410
- static stringifyQuery(object: object): string;
7411
-
7412
7410
  static base64(input: string): string;
7413
7411
 
7414
7412
  static uuid(): string;
@@ -7417,6 +7415,8 @@ export class Util {
7417
7415
 
7418
7416
  static promiseAny(promises: Array<Promise>): Promise<any>;
7419
7417
 
7418
+ static serializeQueryObject(queryObject: object): string;
7419
+
7420
7420
  static fetch(args: any): Promise;
7421
7421
 
7422
7422
  static wait(ms: number): Promise<void>;
@@ -7441,8 +7441,6 @@ export class Util {
7441
7441
 
7442
7442
  static getSearchParameter(name: string): string | null;
7443
7443
 
7444
- static stringifyQuery(object: object): string;
7445
-
7446
7444
  static base64(input: string): string;
7447
7445
 
7448
7446
  static uuid(): string;
@@ -7450,6 +7448,8 @@ export class Util {
7450
7448
  static env(): string;
7451
7449
 
7452
7450
  static promiseAny(promises: Array<Promise>): Promise<any>;
7451
+
7452
+ static serializeQueryObject(queryObject: object): string;
7453
7453
  }
7454
7454
 
7455
7455
  export namespace HomeyAPIV3Cloud {
package/lib/API.js CHANGED
@@ -97,7 +97,7 @@ class API {
97
97
  }
98
98
 
99
99
  if (typeof value !== 'undefined') {
100
- if (parameter.type === 'string' && typeof value !== 'string') {
100
+ if (parameter.in !== 'query' && parameter.type === 'string' && typeof value !== 'string') {
101
101
  throw new Error(`Invalid Parameter Type: ${parameterId}. Got: ${typeof value}. Expected: string`);
102
102
  }
103
103
 
@@ -201,7 +201,7 @@ class API {
201
201
 
202
202
  if (request.query) {
203
203
  if (Object.keys(request.query).length) {
204
- request.url += `?${Util.stringifyQuery(request.query)}`;
204
+ request.url += `?${Util.serializeQueryObject(request.query)}`;
205
205
  }
206
206
  }
207
207
 
@@ -280,7 +280,8 @@ class API {
280
280
 
281
281
  if (responseContentType && responseContentType.toLowerCase().includes('application/json')) {
282
282
  try {
283
- return JSON.parse(responseText);
283
+ const parsed = JSON.parse(responseText);
284
+ return parsed;
284
285
  } catch (err) {
285
286
  throw new Error(`Failed to parse response body as JSON: ${err.message}`);
286
287
  }
@@ -298,6 +299,21 @@ class API {
298
299
  // eslint-disable-next-line no-unused-vars
299
300
  statusText,
300
301
  }) {
302
+ if (typeof body !== 'object' || body === null) {
303
+ return body;
304
+ }
305
+
306
+ // For ?
307
+ if (body.status != null && body.result != null) {
308
+ return body.result;
309
+ }
310
+
311
+ // For setup API. https://github.com/athombv/athom-cloud-setup/blob/e7764021d33415f21a1e27ce6a874aa41dc7cfdc/lib/Server/index.js#L30
312
+ if (body.success != null && body.message != null) {
313
+ return body.message;
314
+ }
315
+
316
+
301
317
  return body;
302
318
  }
303
319
 
@@ -319,7 +335,10 @@ class API {
319
335
  } else if (typeof body.error === 'string') {
320
336
  message = body.error;
321
337
  } else if (typeof body.message === 'string') {
338
+ // Setup API returns message.
322
339
  message = body.message;
340
+ } else if (typeof body.result === 'string') {
341
+ message = body.result;
323
342
  }
324
343
 
325
344
  throw new APIError(message, statusCode);
@@ -22,7 +22,7 @@ class ManagerFlow extends ManagerFlowV3 {
22
22
  flow,
23
23
  ...props
24
24
  }) {
25
- return this.__super__.createFlow({
25
+ return this.__super__createFlow({
26
26
  flow: Flow.transformSet(flow),
27
27
  ...props,
28
28
  });
@@ -32,7 +32,7 @@ class ManagerFlow extends ManagerFlowV3 {
32
32
  flow,
33
33
  ...props
34
34
  }) {
35
- return this.__super__.updateFlow({
35
+ return this.__super__updateFlow({
36
36
  flow: Flow.transformSet(flow),
37
37
  ...props,
38
38
  });
@@ -42,7 +42,7 @@ class ManagerFlow extends ManagerFlowV3 {
42
42
  advancedflow,
43
43
  ...props
44
44
  }) {
45
- return this.__super__.createAdvancedFlow({
45
+ return this.__super__createAdvancedFlow({
46
46
  advancedflow: AdvancedFlow.transformSet(advancedflow),
47
47
  ...props,
48
48
  });
@@ -52,7 +52,7 @@ class ManagerFlow extends ManagerFlowV3 {
52
52
  advancedflow,
53
53
  ...props
54
54
  }) {
55
- return this.__super__.updateAdvancedFlow({
55
+ return this.__super__updateAdvancedFlow({
56
56
  advancedflow: AdvancedFlow.transformSet(advancedflow),
57
57
  ...props,
58
58
  });
@@ -122,6 +122,17 @@ class ManagerFlow extends ManagerFlowV3 {
122
122
  });
123
123
  }
124
124
 
125
+ async getFlowCardAutocomplete({
126
+ id,
127
+ ...props
128
+ }) {
129
+ return this.__super__getFlowCardAutocomplete({
130
+ id: id.split(':').reverse()[0],
131
+ uri: id.split(':', 3).join(':'),
132
+ ...props,
133
+ });
134
+ }
135
+
125
136
  }
126
137
 
127
138
  module.exports = ManagerFlow;
@@ -130,7 +130,7 @@ class Manager extends EventEmitter {
130
130
  }
131
131
 
132
132
  if (typeof value !== 'undefined') {
133
- if (parameter.type === 'string' && typeof value !== 'string') {
133
+ if (parameter.in !== 'query' && parameter.type === 'string' && typeof value !== 'string') {
134
134
  throw new Error(`Invalid Parameter Type: ${parameterId}. Got: ${typeof value}. Expected: string`);
135
135
  }
136
136
 
@@ -160,10 +160,6 @@ class Manager extends EventEmitter {
160
160
  if (typeof value !== 'undefined') {
161
161
  switch (parameter.in) {
162
162
  case 'path': {
163
- if (typeof value !== 'string') {
164
- throw new Error(`Invalid Parameter Type: ${parameterId}. Got: ${typeof value}. Expected: string`);
165
- }
166
-
167
163
  path = path.replace(`:${parameterId}`, value);
168
164
  break;
169
165
  }
@@ -176,10 +172,6 @@ class Manager extends EventEmitter {
176
172
  break;
177
173
  }
178
174
  case 'query': {
179
- if (typeof value !== 'string') {
180
- throw new Error(`Invalid Parameter Type: ${parameterId}. Got: ${typeof value}. Expected: string`);
181
- }
182
-
183
175
  query[parameterId] = value;
184
176
  break;
185
177
  }
@@ -193,9 +185,7 @@ class Manager extends EventEmitter {
193
185
 
194
186
  // Append query to path
195
187
  if (Object.keys(query).length > 0) {
196
- const queryString = Object.entries(query).map(([key, value]) => {
197
- return `${key}=${encodeURIComponent(value)}`;
198
- }).join('&');
188
+ const queryString = Util.serializeQueryObject(query);
199
189
  path = `${path}?${queryString}`;
200
190
  }
201
191
 
@@ -25,11 +25,6 @@ class FlowToken extends Item {
25
25
  return item;
26
26
  }
27
27
 
28
- get uri() {
29
- console.warn('FlowToken.uri is deprecated. Use FlowToken.ownerUri instead.');
30
- return undefined;
31
- }
32
-
33
28
  get uriObj() {
34
29
  console.warn('FlowToken.uriObj is deprecated.');
35
30
  return undefined;
package/lib/Util.js CHANGED
@@ -123,19 +123,6 @@ class Util {
123
123
  return searchParams.get(name) || null;
124
124
  }
125
125
 
126
- /**
127
- * Converts an object to a query string
128
- * @param {object} object - Query parameter object
129
- * @returns {string}
130
- */
131
- static stringifyQuery(obj) {
132
- const searchParams = new URLSearchParams();
133
- for (const [key, value] of Object.entries(obj)) {
134
- searchParams.append(key, value);
135
- }
136
- return searchParams.toString();
137
- }
138
-
139
126
  /**
140
127
  * This method encodes a string into a base64 string.
141
128
  * It's provided as Util because Node.js uses `Buffer`,
@@ -256,6 +243,64 @@ class Util {
256
243
  });
257
244
  }
258
245
 
246
+ /**
247
+ * Converts an object to a query string
248
+ * @param {object} queryObject - Query parameter object
249
+ * @returns {string}
250
+ */
251
+ static serializeQueryObject(queryObject) {
252
+ let prefix;
253
+ let querystring = [];
254
+ let rbracket = /\[\]$/;
255
+
256
+ function add(key, value) {
257
+ // If value is a function, invoke it and return its value.
258
+ value = (typeof value === 'function') ?
259
+ value() :
260
+ value === null ?
261
+ '' :
262
+ value;
263
+ querystring[querystring.length] = encodeURIComponent(key) +
264
+ '=' + encodeURIComponent(value);
265
+ }
266
+
267
+ function buildParams(prefix, obj, add) {
268
+ let name;
269
+ if (Array.isArray(obj)) {
270
+ // Serialize array item.
271
+ for (let index = 0; index < obj.length; index++) {
272
+ if (rbracket.test(prefix)) {
273
+ // Treat each array item as a scalar.
274
+ add(prefix, obj[index]);
275
+ } else {
276
+ // Item is non-scalar (array or object), encode its numeric index.
277
+ buildParams(prefix + '[' + (typeof (obj[index]) === 'object' ?
278
+ index :
279
+ ''
280
+ ) + ']', obj[index], add);
281
+ }
282
+ }
283
+ } else if (typeof obj === 'object') {
284
+ // Serialize object item.
285
+ for (name in obj) {
286
+ buildParams(prefix + '[' + name + ']', obj[name], add);
287
+ }
288
+ } else {
289
+ // Serialize scalar item.
290
+ add(prefix, obj);
291
+ }
292
+ }
293
+
294
+ // Encode params recursively.
295
+ for (prefix in queryObject) {
296
+ if (typeof queryObject[prefix] === 'undefined') continue;
297
+ buildParams(prefix, queryObject[prefix], add);
298
+ }
299
+
300
+ // Return the resulting serialization.
301
+ return querystring.join('&');
302
+ }
303
+
259
304
  }
260
305
 
261
306
  module.exports = Util;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homey-api",
3
- "version": "3.0.12",
3
+ "version": "3.0.14",
4
4
  "description": "Homey API",
5
5
  "main": "index.js",
6
6
  "files": [