node-alarm-dot-com 2.0.0-beta.3 → 2.0.0-beta.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.
- package/dist/index.js +35 -3
- package/package.json +1 -1
package/dist/index.js
CHANGED
@@ -176,12 +176,44 @@ exports.getCurrentState = getCurrentState;
|
|
176
176
|
* @param {Object} authOpts Authentication object returned from the login.
|
177
177
|
* @returns {Promise}
|
178
178
|
*/
|
179
|
-
function getComponents(url, componentIDs, authOpts) {
|
179
|
+
async function getComponents(url, componentIDs, authOpts) {
|
180
180
|
const IDs = Array.isArray(componentIDs) ? componentIDs : [componentIDs];
|
181
|
-
let
|
182
|
-
|
181
|
+
let requests = [];
|
182
|
+
if (IDs.length <= 50) {
|
183
|
+
const getUrl = `${url}?${IDs.map(id => `ids%5B%5D=${id}`).join('&')}`;
|
184
|
+
requests.push(authenticatedGet(getUrl, authOpts));
|
185
|
+
}
|
186
|
+
else {
|
187
|
+
// We have found that the Alarm.com API will return a 404 error when there is an excessive number of query parameters.
|
188
|
+
// We get around this by breaking up our GET calls into shorter URIs.
|
189
|
+
const shortenedUrls = [];
|
190
|
+
while (IDs.length > 50) {
|
191
|
+
const currentArray = IDs.splice(0, 50);
|
192
|
+
shortenedUrls.push(`${url}?${currentArray.map(id => `ids%5B%5D=${id}`).join('&')}`);
|
193
|
+
}
|
194
|
+
shortenedUrls.push(`${url}?${IDs.map(id => `ids%5B%5D=${id}`).join('&')}`);
|
195
|
+
requests = shortenedUrls.map(u => authenticatedGet(u, authOpts));
|
196
|
+
}
|
197
|
+
return await CombineAPIDeviceAPICalls(requests);
|
183
198
|
}
|
184
199
|
exports.getComponents = getComponents;
|
200
|
+
async function CombineAPIDeviceAPICalls(ApiCalls) {
|
201
|
+
const apiStateCalls = await Promise.all(ApiCalls);
|
202
|
+
const stateToReturn = {
|
203
|
+
data: [],
|
204
|
+
included: []
|
205
|
+
};
|
206
|
+
for (let apiCall of apiStateCalls) {
|
207
|
+
for (const apiData of (apiCall.data)) {
|
208
|
+
stateToReturn.data.push(apiData);
|
209
|
+
}
|
210
|
+
for (const apiInclude of apiCall.included) {
|
211
|
+
stateToReturn.included.push(apiInclude);
|
212
|
+
}
|
213
|
+
}
|
214
|
+
stateToReturn.meta = apiStateCalls[0].meta;
|
215
|
+
return stateToReturn;
|
216
|
+
}
|
185
217
|
// Partition methods ///////////////////////////////////////////////////////////
|
186
218
|
/**
|
187
219
|
* Perform partition actions, e.g., armAway, armStay, disarm.
|