UNPKG

@gooddata/gooddata-js

Version:
126 lines (125 loc) 4.15 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 __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); // (C) 2007-2014 GoodData Corporation var set_1 = __importDefault(require("lodash/set")); var get_1 = __importDefault(require("lodash/get")); /** * Config module holds SDK configuration variables * * Currently its only custom domain - which enabled using * sdk from different domain (using CORS) * * Never set properties directly - always use setter methods * * @module config * @class config */ var URL_REGEXP = "(?:(https)://+|(www\\.)?)\\w[:;,\\.?\\[\\]\\w/~%&=+#-@!]*"; function sanitizeDomain(domain) { if (domain === null) { return undefined; } var sanitizedDomain = domain || ""; var link = sanitizedDomain.match(URL_REGEXP); if (!link) { throw new Error(domain + " is not a valid url"); } // ensure https:// prefix and strip possible trailing / return "https://" + link[0].replace(/^https?:\/\/|\/$/g, ""); } exports.sanitizeDomain = sanitizeDomain; /** * Returns sanitized config * * @method sanitizeConfig * @return {object|undefined} config with sanitized domain */ function sanitizeConfig(config) { var sanitized = __assign({}, config); if (config.domain) { sanitized.domain = sanitizeDomain(config.domain); } return sanitized; } exports.sanitizeConfig = sanitizeConfig; /** * Config factory * * @param {object|null} configStorage config object * @method createModule * @return SDK config module */ var ConfigModule = /** @class */ (function () { function ConfigModule(configStorage) { this.configStorage = configStorage; if (arguments.length !== 1) { throw new Error("Config module has to be called with exactly one argument."); } } /** * Sets custom domain. Parameter is url which has always to be https:// * (if you don't provide it, we will do it for you). * * RegExp inspired taken from * https://github.com/jarib/google-closure-library/blob/master/closure/goog/string/linkify.js * @param {String|null} domain valid domain starting with https:// or null for removing * @method setCustomDomain */ ConfigModule.prototype.setCustomDomain = function (domain) { this.configStorage.domain = sanitizeDomain(domain); }; /** * Returns current domain * * @method getCustomDomain */ ConfigModule.prototype.getCustomDomain = function () { return this.configStorage.domain; }; /** * Sets JS package and version info * * @method setJsPackage * @param {String} name package name * @param {String} version package version (semver) * @private */ ConfigModule.prototype.setJsPackage = function (name, version) { if (!this.configStorage.originPackage) { // only set the first (topmost) package this.configStorage.originPackage = { name: name, version: version }; } }; /** * Returns JS package and version info * * @method getJsPackage * @return {object} with 'name' and 'version' properties * @private */ ConfigModule.prototype.getJsPackage = function () { return this.configStorage.originPackage; }; ConfigModule.prototype.setRequestHeader = function (key, value) { set_1.default(this.configStorage, ["xhrSettings", "headers", key], value); }; ConfigModule.prototype.getRequestHeader = function (key) { return get_1.default(this.configStorage, ["xhrSettings", "headers", key]); }; return ConfigModule; }()); exports.ConfigModule = ConfigModule;