UNPKG

@enonic/mock-xp

Version:

Mock Enonic XP API JavaScript Library

165 lines (164 loc) 7.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LibPortal = void 0; var forceArray_1 = require("@enonic/js-utils/array/forceArray"); var constants_1 = require("../constants"); var ContentConnection_1 = require("../implementation/ContentConnection"); var HASH = '0123456789abcdef'; var FINGERPRINT = '0123456789abcdef'; var LibPortal = (function () { function LibPortal(_a) { var app = _a.app, server = _a.server; this.app = app; this.log = server.log; this.server = server; } LibPortal.prototype._getParentContent = function (_a) { var path = _a.path; var pathParts = path.split('/'); pathParts.pop(); var parentPath = pathParts.join('/'); return this.connect().get({ key: parentPath }); }; LibPortal.prototype._recursiveReturnParentIfSite = function (_a) { var path = _a.path; var parentContent = this._getParentContent({ path: path }); if (!parentContent) { return null; } if (parentContent.type == constants_1.CONTENT_TYPE_PORTAL_SITE) { return parentContent; } return this._recursiveReturnParentIfSite({ path: parentContent._path }); }; LibPortal.prototype.assetUrl = function (params) { if (!this.request) { throw new Error('mock-xp: Portal.assetUrl(): No request set on the Portal object instance!'); } var _a = this.request, host = _a.host, requestPath = _a.path, port = _a.port, scheme = _a.scheme; var requestPathNoEndSlash = requestPath.replace(/\/$/, ''); var currentApplicationKey = this.app.key; var assetPath = params.path, _b = params.application, application = _b === void 0 ? currentApplicationKey : _b, type = params.type, urlParams = params.params; var assetPathWithoutLeadingSlash = assetPath.replace(/^\//, ''); var query = urlParams ? "?".concat(new URLSearchParams(urlParams).toString()) : ''; var urlPathAndQuery = "".concat(requestPathNoEndSlash, "/_/asset/").concat(application, ":").concat(FINGERPRINT, "/").concat(assetPathWithoutLeadingSlash).concat(query); if (type !== 'absolute') { return urlPathAndQuery; } return "".concat(scheme, "://").concat(host).concat(port && port !== 80 ? ":".concat(port) : '').concat(urlPathAndQuery); }; LibPortal.prototype.connect = function () { var repoId = this.server.context.repository; if (!repoId) { throw new Error('mock-xp: LibPortal.connect: No repository set in context!'); } var repo = this.server.repos[repoId]; var branchId = this.server.context.branch; if (!branchId) { throw new Error('mock-xp: LibPortal.connect: No branch set in context!'); } var branch = repo.getBranch(branchId); var contentConnection = new ContentConnection_1.ContentConnection({ branch: branch }); return contentConnection; }; LibPortal.prototype.getComponent = function () { if (!this.component) { this.log.error('mock-xp: Portal.getComponent(): There is no component set on the Portal object instance!'); return null; } return this.component; }; LibPortal.prototype.getContent = function (_a) { var _b = _a === void 0 ? {} : _a, _c = _b._trace, _trace = _c === void 0 ? false : _c; if (!this.request) { throw new Error('mock-xp: Portal.getContent(): Unable to determine current contentId as there is no request set on the Portal object instance!'); } if (_trace) this.log.debug('getContent: request', this.request); var contentPath = this.request.contentPath({ _trace: _trace }); if (_trace) this.log.debug('getContent: contentPath', contentPath); return this.connect().get({ _trace: _trace, key: contentPath }); }; LibPortal.prototype.getSite = function () { if (!this.request) { throw new Error('mock-xp: Portal.getSite(): Unable to determine current contentId as there is no request set on the Portal object instance!'); } var currentContent = this.getContent(); if (!currentContent) { throw new Error('mock-xp: Portal.getSite(): Unable to find current content!'); } if (currentContent.type == constants_1.CONTENT_TYPE_PORTAL_SITE) { return currentContent; } return this._recursiveReturnParentIfSite({ path: currentContent._path }); }; LibPortal.prototype.getSiteConfig = function () { var _this = this; if (!this.request) { throw new Error('mock-xp: Portal.getSiteConfig(): Unable to determine current contentId as there is no request set on the Portal object instance!'); } var site; try { site = this.getSite(); } catch (e) { throw new Error('mock-xp: Portal.getSiteConfig(): Unable to find site content!'); } if (!site.data.siteConfig) { return null; } var siteConfigsObj = (0, forceArray_1.forceArray)(site.data.siteConfig); var filteredSiteConfigs = siteConfigsObj.filter(function (siteConfig) { return siteConfig.applicationKey === _this.app.key; }); if (filteredSiteConfigs.length === 0) { return null; } var aSiteConfig = filteredSiteConfigs[0]; return aSiteConfig.config; }; LibPortal.prototype.imageUrl = function (params) { var scale = params.scale, id = params.id, path = params.path, quality = params.quality, background = params.background, format = params.format, filter = params.filter, urlParams = params.params, type = params.type; var key = id || path; if (!key) { throw new Error('lib-portal.imageUrl(): Either id or path must be set!'); } var imageContent = this.connect().get({ key: key }); if (!imageContent) { throw new Error("lib-portal.imageUrl(): No imageContent with key:".concat(key)); } var _a = this.request, host = _a.host, requestPath = _a.path, port = _a.port, scheme = _a.scheme; var s = scale .replace(/[\(,]/g, '-') .replace(/\)/, ''); var queryParams = urlParams || {}; if (background) { queryParams.background = background; } if (filter) { queryParams.filter = filter; } var f = format ? ".".concat(format) : ''; if (quality) { queryParams.quality = "".concat(quality); } var query = Object.values(queryParams).length ? "?".concat(new URLSearchParams(queryParams).toString()) : ''; var urlPathAndQuery = "".concat(requestPath, "/_/image/").concat(imageContent._id, ":").concat(HASH, "/").concat(s, "/").concat(imageContent._name).concat(f).concat(query); if (type !== 'absolute') { return urlPathAndQuery; } return "".concat(scheme, "://").concat(host).concat(port && port !== 80 ? ":".concat(port) : '').concat(urlPathAndQuery); }; return LibPortal; }()); exports.LibPortal = LibPortal;