UNPKG

@enonic/mock-xp

Version:

Mock Enonic XP API JavaScript Library

241 lines (240 loc) 9.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Server = void 0; var tslib_1 = require("tslib"); var memfs_1 = require("memfs"); var constants_1 = require("../constants"); var Auth_1 = require("./Auth"); var ContentConnection_1 = require("./ContentConnection"); var Context_1 = require("./Context"); var Log_1 = require("./Log"); var Project_1 = require("./Project"); var Repo_1 = require("./Repo"); var RepoConnection_1 = require("./RepoConnection"); var RepositoryNotFoundException_1 = require("./repo/RepositoryNotFoundException"); var setupSystemRepo_1 = require("./server/setupSystemRepo"); var Version_1 = require("./Version"); var Server = (function () { function Server(_a) { var _b = _a === void 0 ? {} : _a, _c = _b.indexWarnings, indexWarnings = _c === void 0 ? false : _c, log = _b.log, loglevel = _b.loglevel, version = _b.version; this.applications = []; this.indexWarnings = false; this.log = Log_1.Log.createLogger({ loglevel: 'info' }); this.projects = {}; this.repos = {}; this.version = '7.14.0'; this.vol = memfs_1.vol; if (indexWarnings) { this.indexWarnings = indexWarnings; } if (log) { this.log = log; } else if (loglevel) { this.log = Log_1.Log.createLogger({ loglevel: loglevel }); } if (version) { this.version = version; } this.vol.fromJSON({}, '/'); this.createRepo({ id: constants_1.SYSTEM_REPO }); this.systemRepoConnection = this.connect({ branchId: 'master', repoId: constants_1.SYSTEM_REPO }); this.auth = new Auth_1.Auth({ server: this }); (0, setupSystemRepo_1.setupSystemRepo)({ server: this }); this.context = new Context_1.Context({ branch: 'master', repository: constants_1.SYSTEM_REPO, server: this, principals: [ constants_1.USER_SYSTEM_ANONYMOUS, constants_1.ROLE_SYSTEM_EVERYONE ], }); } Server.prototype.connect = function (_a) { var repoId = _a.repoId, branchId = _a.branchId; if (!repoId) { throw new Error('connect: No repoId provided!'); } if (!branchId) { throw new Error('connect: No branchId provided!'); } var repo = this.getRepo(repoId); var branch = repo.getBranch(branchId); return new RepoConnection_1.RepoConnection({ branch: branch }); }; Server.prototype.contentConnect = function (_a) { var branchId = _a.branchId, projectId = _a.projectId; if (!projectId) { throw new Error('Server: contentConnect: No projectId provided!'); } if (!branchId) { throw new Error('Server: contentConnect: No branchId provided!'); } return new ContentConnection_1.ContentConnection({ branch: this.getBranch({ branchId: branchId, repoId: Project_1.Project.repoIdFromProjectName(projectId) }) }); }; Server.prototype.createBranch = function (_a) { var branchId = _a.branchId, repoId = _a.repoId; return this.getRepo(repoId).createBranch(branchId); }; Server.prototype.createNode = function (_a) { var _b = _a._trace, _trace = _b === void 0 ? false : _b, branchId = _a.branchId, repoId = _a.repoId, node = _a.node; return this.connect({ branchId: branchId, repoId: repoId }).create(tslib_1.__assign({ _trace: _trace }, node)); }; Server.prototype.createRepo = function (_a) { var id = _a.id, settings = _a.settings; var repo = new Repo_1.Repo({ id: id, server: this, settings: settings }); this.repos[id] = repo; return this; }; Server.prototype.createProject = function (_a) { var projectName = _a.projectName, _b = _a.settings, settings = _b === void 0 ? {} : _b; if (!projectName) { throw new Error('Server: createProject: No projectName provided!'); } if (this.projects[projectName]) { this.log.info("Project ".concat(projectName, " already exists.")); return this; } this.projects[projectName] = new Project_1.Project({ projectName: projectName, server: this, settings: settings }); return this; }; Server.prototype.createUser = function (params) { this.auth.createUser(params); return this; }; Server.prototype.deleteRepo = function (_a) { var repoId = _a.repoId; if (!this.repos[repoId]) { throw new RepositoryNotFoundException_1.RepositoryNotFoundException(repoId); } delete this.repos[repoId]; return this; }; Server.prototype.getBranch = function (_a) { var branchId = _a.branchId, repoId = _a.repoId; return this.getRepo(repoId).getBranch(branchId); }; Server.prototype.getNode = function (_a) { var branchId = _a.branchId, key = _a.key, repoId = _a.repoId; return this.connect({ branchId: branchId, repoId: repoId })._getSingle(key); }; Server.prototype.getProject = function (projectName) { if (!projectName) { throw new Error('Server: getProject: No projectName provided!'); } var project = this.projects[projectName]; if (!project) { throw new Error("Server: getProject: Project ".concat(projectName, " not found!")); } return project; }; Server.prototype.getRepo = function (repoId) { var repo = this.repos[repoId]; if (!repo) { throw new RepositoryNotFoundException_1.RepositoryNotFoundException(repoId); } return repo; }; Server.prototype.install = function (app) { var systemVersion = new Version_1.Version(this.version); if (app.minSystemVersion && systemVersion.lessThan(new Version_1.Version(app.minSystemVersion))) { throw new Error("System version ".concat(this.version, " is lower than App minSystemVersion ").concat(app.minSystemVersion, "!")); } if (app.maxSystemVersion && systemVersion.greaterThan(new Version_1.Version(app.maxSystemVersion))) { throw new Error("System version ".concat(this.version, " is higher than App maxSystemVersion ").concat(app.maxSystemVersion, "!")); } var index = this.applications.findIndex(function (a) { return a.key === app.key; }); if (index !== -1) { this.log.info("Application ".concat(app.key, " already installed, replacing it.")); this.applications[index] = app; } else { this.applications.push(app); this.log.debug("Application ".concat(app.key, " installed.")); } return this; }; Server.prototype.listRepos = function () { return Object.values(this.repos); }; Server.prototype.login = function (params) { this.auth.login(params); return this; }; Server.prototype.logout = function () { this.auth.logout(); return this; }; Server.prototype.modifyNode = function (_a) { var branchId = _a.branchId, editor = _a.editor, key = _a.key, repoId = _a.repoId; this.connect({ branchId: branchId, repoId: repoId }).modify({ editor: editor, key: key, }); return this; }; Server.prototype.setContext = function (_a) { var attributes = _a.attributes, principals = _a.principals, projectName = _a.projectName, _b = _a.branch, branch = _b === void 0 ? projectName ? 'draft' : 'master' : _b, _c = _a.repository, repository = _c === void 0 ? "com.enonic.cms.".concat(projectName) : _c, _d = _a.idProvider, idProvider = _d === void 0 ? 'system' : _d, login = _a.login, _e = _a.user, user = _e === void 0 ? login ? { login: login, idProvider: idProvider } : undefined : _e; if (!user) { var currentUser = this.auth.getUser(); if (currentUser) { user = { idProvider: currentUser.idProvider, login: currentUser.login, }; } } this.context = new Context_1.Context({ attributes: attributes, branch: branch, principals: principals, repository: repository, server: this, user: user, }); return this; }; Server.prototype.su = function () { this.login({ idProvider: 'system', user: 'su', }); return this; }; return Server; }()); exports.Server = Server;