UNPKG

@webex/webex-core

Version:

Plugin handling for Cisco Webex

95 lines (92 loc) 3.24 kB
"use strict"; var _Object$defineProperty = require("@babel/runtime-corejs2/core-js/object/define-property"); var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault"); _Object$defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _ampersandState = _interopRequireDefault(require("ampersand-state")); /** * @class */ var ServiceDetail = _ampersandState.default.extend({ namespace: 'ServiceDetail', props: { serviceUrls: ['array', false, function () { return []; }], serviceName: ['string', true, undefined], id: ['string', true, undefined] }, /** * Generate a host url based on the host * uri provided. * @param {ServiceUrl} serviceUrl * @returns {string} */ _generateHostUrl: function _generateHostUrl(serviceUrl) { var url = new URL(serviceUrl.baseUrl); // setting url.hostname will not apply during Url.format(), set host via // a string literal instead. url.host = "".concat(serviceUrl.host).concat(url.port ? ":".concat(url.port) : ''); return url.href; }, /** * Get the current host url with the highest priority. This will only return a URL with a filtered host that has the * `homeCluster` value set to `true`. * @returns {string} - The priority host url. */ _getPriorityHostUrl: function _getPriorityHostUrl() { // format of catalog ensures that array is sorted by highest priority var priorityServiceUrl = this._searchForValidPriorityHost(); if (!priorityServiceUrl) { this.serviceUrls = this.serviceUrls.map(function (serviceUrl) { serviceUrl.failed = false; return serviceUrl; }); priorityServiceUrl = this._searchForValidPriorityHost(); } return priorityServiceUrl ? this._generateHostUrl(priorityServiceUrl) : ''; }, /** * Searches for a valid service URL with a priority greater than 0 that has not failed. * @returns {ServiceUrl | undefined} - The first valid service URL found, or undefined if none exist. */ _searchForValidPriorityHost: function _searchForValidPriorityHost() { return this.serviceUrls.find(function (serviceUrl) { return serviceUrl.priority > 0 && !serviceUrl.failed; }); }, /** * Attempt to mark a host from this `ServiceDetail` as failed and return true * if the provided url has a host that could be successfully marked as failed. * * @param {string} url * @returns {boolean} */ failHost: function failHost(url) { var failedUrl = new URL(url); var foundHost = this.serviceUrls.find(function (serviceUrl) { return serviceUrl.host === failedUrl.host; }); if (foundHost) { foundHost.failed = true; } return foundHost !== undefined; }, /** * Generate a url using the host with the * highest priority via host rendering. * * @returns {string} - The full service url. */ get: function get() { // return empty string to indicate that no service url is available if (!this.serviceUrls || this.serviceUrls.length === 0) { return ''; } return this._getPriorityHostUrl(); } }); var _default = exports.default = ServiceDetail; //# sourceMappingURL=service-detail.js.map