UNPKG

@app-masters/js-lib

Version:
211 lines (204 loc) 10.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _moment = require("moment"); function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); } function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } var minActualClientVersion = null; var _actualVersionSatisfies = true; var VersionCheck = /*#__PURE__*/function () { function VersionCheck() { _classCallCheck(this, VersionCheck); } _createClass(VersionCheck, null, [{ key: "setCurrentVersion", value: function setCurrentVersion(client, platform, version) { VersionCheck.client = client; VersionCheck.platform = platform; VersionCheck.currentClientVersion = version; } /** * Tell if the current client version are ok with the min version setted on api * @param {object} req - Pure HTTP request * @param {string} client - "web" or "mobile" * @returns {boolean} * @author Tiago Gouvêa / Max William */ }, { key: "minVersionSatisfies", value: function minVersionSatisfies(req) { var client = VersionCheck.client; var platform = VersionCheck.platform; if (client !== 'admin' && client !== 'web' && client !== 'mobile') { throw new Error('Invalid client calling on minVersionSatisfies'); } // Check Client version var minClientVersionSatisfies = true; var param = 'min-' + client + '-version'; var minClientVersion = req.headers.get(param); if (minClientVersion) minClientVersionSatisfies = this.actualVersionSatisfies(minClientVersion, param, req); // Check platform (ios/android) version var minPlatformVersionSatisfies = true; if (platform !== 'web') { var _param = 'min-' + platform + '-version'; var minPlatformVersion = req.headers.get(_param); if (minPlatformVersion) minPlatformVersionSatisfies = this.actualVersionSatisfies(minPlatformVersion, _param, req); } return minPlatformVersionSatisfies && minClientVersionSatisfies; } }, { key: "actualVersionSatisfies", value: function actualVersionSatisfies(minClientVersion, param, req) { if (!minClientVersion) { console.error(param + ' should not be null'); console.log('showing all headers received:'); var headers = req.headers.entries(); var _iterator = _createForOfIteratorHelper(req.headers.entries()), _step; try { for (_iterator.s(); !(_step = _iterator.n()).done;) { var pair = _step.value; console.log(pair[0] + ': ' + pair[1]); } } catch (err) { _iterator.e(err); } finally { _iterator.f(); } return true; } if (!minActualClientVersion || minActualClientVersion !== minClientVersion) { // console.log(param, minClientVersion); // console.log('Version now', VersionCheck.currentClientVersion); // The min version changed. Check it our version satisfies. var currentVersion = VersionCheck.currentClientVersion; var comp = VersionCheck.versionCompare(minClientVersion, currentVersion); minActualClientVersion = minClientVersion; // console.log('comp', comp); _actualVersionSatisfies = comp <= 0; } return _actualVersionSatisfies; } /** * Listener to tell you when your version not fit the min version defined on API * @param Http * @param callback * @author Tiago Gouvêa / Max William */ }, { key: "onMinVersionNotSatisfies", value: function onMinVersionNotSatisfies(Http, callback) { if (!VersionCheck.currentClientVersion) { throw new Error('VersionCheck.currentClientVersion null. You must call setCurrentVersion before all other VersionCheck calls'); } Http.setRequestListener(function (req) { if (!VersionCheck.minVersionSatisfies(req)) { // Duplicated code var client = VersionCheck.client; var param = 'min-' + client + '-version'; var minClientVersion = req.headers.get(param); console.log("version client", client); console.log("version param", param); console.log("version minClientVersion", minClientVersion); callback(minClientVersion); } }); } /** * Listener to tell you when your client was updated * @param storage * @author Tiago Gouvêa */ }, { key: "onNewVersion", value: function onNewVersion(storage) { return new Promise(function (resolve, reject) { if (!VersionCheck.currentClientVersion) { reject(new Error('VersionCheck.currentClientVersion null. You must call setCurrentVersion before all other VersionCheck calls')); } // console.log('callback', callback); storage.getItem('last-app-version').then(function (data) { // console.log('storaged version', data); if (data === null) { storage.setItem('last-app-version', VersionCheck.currentClientVersion).then(function (data) { // console.log('will proceed after setItem'); resolve(VersionCheck.currentClientVersion, false); }); } else { // console.log(VersionCheck.currentClientVersion); var comp = VersionCheck.versionCompare(VersionCheck.currentClientVersion, data); // console.log('comp', comp, VersionCheck.currentClientVersion, data); var fromBeta = VersionCheck.currentClientVersion[0] !== '0' && data[0] === '0'; // comp = 1; fromBeta = true; if (comp > 0) { storage.setItem('last-app-version', VersionCheck.currentClientVersion).then(function (data) { // console.log('will proceed after setItem'); resolve(VersionCheck.currentClientVersion, fromBeta); }); } } }).catch(function (storageError) { reject(storageError); }); }); } /** * Compare two versions string to tell if are diferent * @param v1 * @param v2 * @param options * @returns {int} 0 are equal, 1 when v1 is greater than v2, -1 if not */ }, { key: "versionCompare", value: function versionCompare(v1, v2, options) { // console.log('compare v1', v1); // console.log('compare v2', v2); var lexicographical = options && options.lexicographical, zeroExtend = options && options.zeroExtend, v1parts = v1.split('.'), v2parts = v2.split('.'); function isValidPart(x) { return (lexicographical ? /^\d+[A-Za-z]*$/ : /^\d+$/).test(x); } if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) { return NaN; } if (zeroExtend) { while (v1parts.length < v2parts.length) v1parts.push('0'); while (v2parts.length < v1parts.length) v2parts.push('0'); } if (!lexicographical) { v1parts = v1parts.map(Number); v2parts = v2parts.map(Number); } for (var i = 0; i < v1parts.length; ++i) { if (v2parts.length == i) { return 1; } if (v1parts[i] == v2parts[i]) { continue; } else if (v1parts[i] > v2parts[i]) { return 1; } else { return -1; } } if (v1parts.length != v2parts.length) { return -1; } return 0; } }]); return VersionCheck; }(); exports.default = VersionCheck;