node-hue-api
Version:
Philips Hue API Library for Node.js
146 lines (145 loc) • 4.66 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deprecatedFunction = exports.extractUpdatedAttributes = exports.wasSuccessful = exports.parseErrors = void 0;
const HueError_1 = require("./HueError");
const ApiError_1 = require("./ApiError");
const suppressDeprecationWarnings = process.env.NODE_HUE_API_SUPPRESS_DEPRICATION_WARNINGS || false;
/**
* Parses a JSON response looking for the errors in the result(s) returned.
* @param results The results to look for errors in.
* @returns {Array} Of errors found.
*/
function parseErrors(results) {
let errors = [];
if (Array.isArray(results)) {
results.forEach(result => {
if (!result.success) {
const error = parseErrors(result);
if (error) {
errors = errors.concat(error);
}
}
});
}
else {
if (results.error) {
// Due to the handling of remote and local errors, we need to differentiate description and message in the errors,
// as the remote API uses both, whilst local uses only description. -- TODO need to review this
if (results.error.description && !results.error.message) {
const payload = Object.assign({ message: results.error.description }, results.error);
errors.push(new HueError_1.HueError(payload));
}
else {
errors.push(new HueError_1.HueError(results.error));
}
}
}
return errors.length > 0 ? errors : undefined;
}
exports.parseErrors = parseErrors;
/**
* Parses a JSON response checking for success on all changes.
* @param result The JSON object to parse for success messages.
* @returns true if all changes were successful.
*/
function wasSuccessful(result) {
let success = true, idx, len;
if (Array.isArray(result)) {
for (idx = 0, len = result.length; idx < len; idx++) {
success = success && wasSuccessful(result[idx]);
}
}
else {
success = result.success !== undefined;
}
return success;
}
exports.wasSuccessful = wasSuccessful;
function extractUpdatedAttributes(result) {
if (wasSuccessful(result)) {
const values = {};
const updatedAttribute = function (update) {
const success = update.success;
Object.keys(success).forEach(key => {
const matched = /.*\/(.*)$/.exec(key);
if (matched) {
const attribute = matched[1];
values[attribute] = true; //success[key];
}
});
};
if (result instanceof Array) {
result.forEach(update => {
updatedAttribute(update);
});
}
else {
updatedAttribute(result);
}
return values;
}
else {
throw new ApiError_1.ApiError('Error in response'); //TODO extract the error
}
}
exports.extractUpdatedAttributes = extractUpdatedAttributes;
// //TODO the type system could replace this function now
// function asStringArray(value) {
// if (!value) {
// return null;
// }
//
// if (Array.isArray(value)) {
// const result = [];
//
// value.forEach(val => {
// result.push(`${val}`);
// });
//
// return result;
// } else {
// return [`${value}`];
// }
// }
// function getValueforKey(key, data) {
// //Use dot notation to get nested values
// const path = key.split('.');
//
// let target = data
// , value = null
// ;
//
// path.forEach(part => {
// if (target != null) {
// value = target[part];
// target = value;
// } else {
// target = null;
// }
// });
//
// return value;
// }
// function mergeArrays() {
// // TODO this can be replaced with [[], [], ...].flat under Node.js 12+
// let result = [];
//
// Array.from(arguments).forEach(arg => {
// if (arg) {
// result = result.concat(arg);
// }
// });
//
// return result;
// }
function deprecatedFunction(version, func, message) {
if (suppressDeprecationWarnings) {
return;
}
console.log(`**************************************************************************************************`);
console.log(`Deprecated Function Usage: ${func}\n`);
console.log(` ${message}\n`);
console.log(` Function will be removed from node-hue-api in version ${version}`);
console.log(`**************************************************************************************************`);
}
exports.deprecatedFunction = deprecatedFunction;