homey-api 1.5.30 → 1.6.0

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.
@@ -579,7 +579,7 @@
579
579
 
580
580
  }
581
581
 
582
- export class StorageAdapterNodeJS extends StorageAdapter {
582
+ export class StorageAdapterMemory extends StorageAdapter {
583
583
 
584
584
 
585
585
 
@@ -5637,6 +5637,15 @@
5637
5637
 
5638
5638
 
5639
5639
 
5640
+ ):
5641
+ boolean;
5642
+
5643
+ isReactNative(
5644
+
5645
+
5646
+
5647
+
5648
+
5640
5649
  ):
5641
5650
  boolean;
5642
5651
 
@@ -5844,7 +5853,7 @@
5844
5853
 
5845
5854
  }
5846
5855
 
5847
- export class StorageAdapterNodeJS extends StorageAdapter {
5856
+ export class StorageAdapterMemory extends StorageAdapter {
5848
5857
 
5849
5858
 
5850
5859
 
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  /**
4
- * Abstract storage adapter. To be extended by your own, or {@link AthomCloudAPI.StorageAdapterBrowser} or {@link AthomCloudAPI.StorageAdapterNodeJS}.
4
+ * Abstract storage adapter. To be extended by your own, or {@link AthomCloudAPI.StorageAdapterBrowser} or {@link AthomCloudAPI.StorageAdapterMemory}.
5
5
  * @class
6
6
  * @memberof AthomCloudAPI
7
7
  */
@@ -3,12 +3,12 @@
3
3
  const StorageAdapter = require('./StorageAdapter');
4
4
 
5
5
  /**
6
- * In-memory storage adapter for Node.js.
6
+ * In-memory storage adapter for Node.js or React Native.
7
7
  * @class
8
8
  * @extends StorageAdapter
9
9
  * @memberof AthomCloudAPI
10
10
  */
11
- class StorageAdapterNodeJS extends StorageAdapter {
11
+ class StorageAdapterMemory extends StorageAdapter {
12
12
 
13
13
  constructor() {
14
14
  super();
@@ -34,4 +34,4 @@ class StorageAdapterNodeJS extends StorageAdapter {
34
34
 
35
35
  }
36
36
 
37
- module.exports = StorageAdapterNodeJS;
37
+ module.exports = StorageAdapterMemory;
@@ -9,7 +9,7 @@ const Homey = require('./AthomCloudAPI/Homey');
9
9
  const Token = require('./AthomCloudAPI/Token');
10
10
  const StorageAdapter = require('./AthomCloudAPI/StorageAdapter');
11
11
  const StorageAdapterBrowser = require('./AthomCloudAPI/StorageAdapterBrowser');
12
- const StorageAdapterNodeJS = require('./AthomCloudAPI/StorageAdapterNodeJS');
12
+ const StorageAdapterMemory = require('./AthomCloudAPI/StorageAdapterMemory');
13
13
 
14
14
  class AthomCloudAPI extends API {
15
15
 
@@ -18,7 +18,7 @@ class AthomCloudAPI extends API {
18
18
  static Token = Token;
19
19
  static StorageAdapter = StorageAdapter;
20
20
  static StorageAdapterBrowser = StorageAdapterBrowser;
21
- static StorageAdapterNodeJS = StorageAdapterNodeJS;
21
+ static StorageAdapterMemory = StorageAdapterMemory;
22
22
 
23
23
  static SPECIFICATION = require('../assets/specifications/AthomCloudAPI.json');
24
24
  static SPECIFICATION_URL = 'https://api.athom.com/specification.json';
@@ -75,7 +75,7 @@ for(const device of Object.values(devices)) {
75
75
  @param {string} opts.redirectUrl
76
76
  @param {boolean} [opts.autoRefreshTokens=true]
77
77
  @param {AthomCloudAPI.Token} [opts.token=null]
78
- @param {AthomCloudAPI.StorageAdapter} [opts.store={@link AthomCloudAPI.StorageAdapterBrowser} or {@link AthomCloudAPI.StorageAdapterNodeJS}]`;
78
+ @param {AthomCloudAPI.StorageAdapter} [opts.store={@link AthomCloudAPI.StorageAdapterBrowser} or {@link AthomCloudAPI.StorageAdapterMemory}]`;
79
79
 
80
80
  constructor({
81
81
  clientId,
@@ -85,7 +85,7 @@ for(const device of Object.values(devices)) {
85
85
  token = null,
86
86
  store = Util.isBrowser()
87
87
  ? new StorageAdapterBrowser()
88
- : new StorageAdapterNodeJS(),
88
+ : new StorageAdapterMemory(),
89
89
  ...args
90
90
  } = {}) {
91
91
  super({ ...args });
@@ -1,9 +1,8 @@
1
1
  'use strict';
2
2
 
3
- const fetch = require('node-fetch');
4
-
5
3
  const API = require('./API');
6
4
  const APIError = require('./APIError');
5
+ const Util = require('./Util');
7
6
 
8
7
  class HomeyCloudAPI extends API {
9
8
 
@@ -26,7 +25,7 @@ class HomeyCloudAPI extends API {
26
25
  * @returns {string} result.region - e.g. `eu-central-1`
27
26
  */
28
27
  static async getClosestRegion() {
29
- const res = await fetch(`https://${HomeyCloudAPI.SPECIFICATION.host}`);
28
+ const res = await Util.fetch(`https://${HomeyCloudAPI.SPECIFICATION.host}`);
30
29
  if (!res.ok) {
31
30
  throw new Error(res.statusText || 'Unknown Error');
32
31
  }
@@ -56,7 +55,7 @@ class HomeyCloudAPI extends API {
56
55
  * @returns {Promise<object>} result
57
56
  */
58
57
  async getSystemStatus({ secret }) {
59
- const res = await fetch(`${this.baseUrl}/api/system/status`, {
58
+ const res = await Util.fetch(`${this.baseUrl}/api/system/status`, {
60
59
  headers: {
61
60
  'X-Homey-Secret': secret,
62
61
  },
package/lib/Util.js CHANGED
@@ -18,13 +18,23 @@ class Util {
18
18
  * @returns {Promise}
19
19
  */
20
20
  static async fetch(...args) {
21
+ if (this.isReactNative()) {
22
+ return fetch(...args);
23
+ }
24
+
21
25
  // If in a browser
22
26
  if (this.isBrowser()) {
23
27
  return window.fetch(...args);
24
28
  }
25
29
 
26
- const fetch = require('node-fetch');
27
- return fetch(...args);
30
+ if (this.isNodeJS()) {
31
+ const fetch = require('node-fetch');
32
+ return fetch(...args);
33
+ }
34
+
35
+ if (typeof fetch !== 'undefined') {
36
+ return fetch(...args);
37
+ }
28
38
  }
29
39
 
30
40
  /**
@@ -81,17 +91,26 @@ class Util {
81
91
  return window.location.protocol === 'http:';
82
92
  }
83
93
 
94
+ /**
95
+ * @returns {boolean}
96
+ */
97
+ static isReactNative() {
98
+ return (typeof navigator !== 'undefined' && navigator.product === 'ReactNative');
99
+ }
100
+
84
101
  /**
85
102
  * @returns {boolean}
86
103
  */
87
104
  static isBrowser() {
88
- return (typeof window !== 'undefined');
105
+ if (this.isReactNative()) return false;
106
+ return (typeof document !== 'undefined' && typeof document.window !== 'undefined');
89
107
  }
90
108
 
91
109
  /**
92
110
  * @returns {boolean}
93
111
  */
94
112
  static isNodeJS() {
113
+ if (this.isReactNative()) return false;
95
114
  return (typeof process !== 'undefined');
96
115
  }
97
116
 
@@ -161,6 +180,10 @@ class Util {
161
180
  return process.env[key] || null;
162
181
  }
163
182
 
183
+ if (this.isReactNative()) {
184
+ return null;
185
+ }
186
+
164
187
  return null;
165
188
  }
166
189
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "homey-api",
3
- "version": "1.5.30",
3
+ "version": "1.6.0",
4
4
  "description": "Homey API",
5
5
  "main": "index.js",
6
6
  "types": "assets/types/homey-api.d.ts",