UNPKG

unifi-client

Version:

NodeJs client for Unifi products (https://www.ui.com/)

153 lines (152 loc) 6.42 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.formatMacAddress = exports.checkNeedVersion = exports.checkNeeds = exports.convertTimestampSecondsToDate = exports.axiosUrlParams = exports.removeTrailingSlash = exports.getUrlRepresentation = exports.createDebugger = void 0; var debug_1 = __importDefault(require("debug")); var url_1 = __importStar(require("url")); var Validate_1 = require("./commons/Validate"); var semver_1 = __importDefault(require("semver")); var Errors_1 = require("./Errors"); var debug = (0, debug_1.default)("unifi-client"); /** * create a debugger extending the default debugger * @param name - name for the debugger */ var createDebugger = function (name) { if (!name) { throw new Error('name is mandatory'); } return debug.extend(name); }; exports.createDebugger = createDebugger; /** * used to log an url * @param req - the RawAxiosRequestConfig object from axios * @param hidePassword - to hide "auth" part of the url */ var getUrlRepresentation = function (req, hidePassword) { if (hidePassword === void 0) { hidePassword = true; } var urlParsed = new url_1.URL((req.baseURL || 'http://localhost') + (req.url || '')); var params = new url_1.URLSearchParams(urlParsed.search); if (req.auth) { if (!urlParsed.username) { urlParsed.username = req.auth.username; } if (!urlParsed.password) { urlParsed.password = req.auth.password; } } if (req.params) { Object.entries(req.params).forEach(function (_a) { var k = _a[0], v = _a[1]; params.append(k, v); }); } urlParsed.search = params.toString(); // @ts-ignore return url_1.default.format(urlParsed, { auth: !hidePassword }); }; exports.getUrlRepresentation = getUrlRepresentation; var removeTrailingSlash = function (stringUrl) { return stringUrl.replace(/\/$/, ''); }; exports.removeTrailingSlash = removeTrailingSlash; var axiosUrlParams = function (instance) { instance.interceptors.request.use(function (config) { if (!config || !config.url) { return config; } var currentUrl = new url_1.URL("".concat((0, exports.removeTrailingSlash)(config.baseURL || '')).concat(config.url)); // parse pathName to implement variables Object.entries(config.urlParams || {}).forEach(function (_a) { var k = _a[0], v = _a[1]; currentUrl.pathname = currentUrl.pathname.replace(":".concat(k), encodeURIComponent(v)); }); var retUrl = currentUrl.pathname; currentUrl.pathname = ''; return __assign(__assign({}, config), { baseURL: (0, exports.removeTrailingSlash)(currentUrl.toString()), url: retUrl }); }); return instance; }; exports.axiosUrlParams = axiosUrlParams; var convertTimestampSecondsToDate = function (time) { return new Date(Validate_1.Validate.isNumber(time) ? time * 1000 : time); }; exports.convertTimestampSecondsToDate = convertTimestampSecondsToDate; var checkNeeds = function (controller, minVersion, unifiOs) { return ((Validate_1.Validate.isBoolean(unifiOs) && controller.unifiOs === unifiOs) || (!!minVersion && semver_1.default.gte(controller.version || '', minVersion))); }; exports.checkNeeds = checkNeeds; /** * * @param controller - the controller * @param minVersion - the minimal semver version for this object * @param unifiOs - need to be unifiOs ? or Unifi Controller ? if no need, pass undefined * @param parameterName - a name for the parameter */ var checkNeedVersion = function (controller, minVersion, unifiOs, parameterName) { if (parameterName === void 0) { parameterName = ''; } if ((0, exports.checkNeeds)(controller, minVersion, unifiOs)) { return; } var str = parameterName ? "".concat(parameterName, " ") : ''; var code; if (Validate_1.Validate.isBoolean(unifiOs)) { str += "need ".concat(unifiOs ? '' : 'non-', "UnifiOs controller"); code = Errors_1.EErrorsCodes.UNIFI_CONTROLLER_TYPE_MISMATCH; } else { str += "need minimal controller version ".concat(minVersion); code = Errors_1.EErrorsCodes.NEED_MORE_RECENT_CONTROLLER; } throw new Errors_1.ClientError(str, code); }; exports.checkNeedVersion = checkNeedVersion; var formatMacAddress = function (macAddress, separator) { var macAddressRegex = /^([0-9A-F]{2}[:-]?){5}([0-9A-F]{2})$/i; if (!macAddressRegex.test(macAddress)) { throw new Error('"macAddress" is not a valid MAC address'); } var mac = macAddress.replace(/[:-]/g, ''); return Array.from({ length: mac.length / 2 }, function (_, i) { return i; }) .map(function (i) { return mac.slice(i * 2, i * 2 + 2); }) .join(separator); }; exports.formatMacAddress = formatMacAddress;