@enonic/mock-xp
Version:
Mock Enonic XP API JavaScript Library
124 lines (123 loc) • 5.32 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Request = void 0;
var tslib_1 = require("tslib");
var sortKeys_1 = require("@enonic/js-utils/object/sortKeys");
var fast_uri_1 = require("fast-uri");
var SCHEME_DEFAULT = 'http';
var HOST_DEFAULT = 'localhost';
var PORT_DEFAULT = 80;
var PATH_DEFAULT = '';
var Request = (function () {
function Request(constructorParams) {
if (constructorParams === void 0) { constructorParams = {}; }
var _this = this;
var _a, _b, _c, _d;
this.branch = 'draft';
this.method = 'GET';
this.mode = 'preview';
this.body = '';
this.contextPath = '';
this.contentType = '';
this.followRedirects = true;
this.pathParams = {};
this.rawPath = '';
this.repositoryId = '';
this.webSocket = false;
this.getHeader = function (headerName) {
var _a, _b;
return (_b = (_a = _this.headers) === null || _a === void 0 ? void 0 : _a[headerName]) !== null && _b !== void 0 ? _b : null;
};
Object.assign(this, constructorParams);
var scheme = constructorParams.scheme, host = constructorParams.host, port = constructorParams.port, path = constructorParams.path, params = constructorParams.params, url = constructorParams.url;
if (url) {
var _e = (0, fast_uri_1.parse)(url), urlScheme = _e.scheme, urlHost = _e.host, urlPort = _e.port, urlPath = _e.path, query = _e.query;
if (scheme && scheme !== urlScheme) {
throw new Error("scheme !== url.scheme: ".concat(scheme, " !== ").concat(urlScheme));
}
if (host && host !== urlHost) {
throw new Error("host !== url.host: ".concat(host, " !== ").concat(urlHost));
}
if (port && port !== urlPort) {
throw new Error("port !== url.port: ".concat(port, " !== ").concat(urlPort));
}
if (path && path !== urlPath) {
throw new Error("path !== url.path: ".concat(path, " !== ").concat(urlPath));
}
if (query) {
var queryParams_1 = Request.queryToParams(query);
if (params && Object.entries(params).some(function (_a) {
var _b = tslib_1.__read(_a, 2), key = _b[0], value = _b[1];
return queryParams_1[key] !== value;
})) {
throw new Error("params !== query: ".concat(JSON.stringify(params), " !== ").concat(JSON.stringify(queryParams_1)));
}
this.params = queryParams_1;
}
}
if (!this.params) {
this.params = (_a = constructorParams.params) !== null && _a !== void 0 ? _a : {};
}
if (this.params) {
this.params = (0, sortKeys_1.sortKeys)(this.params);
}
this.scheme = scheme || SCHEME_DEFAULT;
this.host = host || HOST_DEFAULT;
this.port = port || PORT_DEFAULT;
this.path = path || PATH_DEFAULT;
this.url = this.serialize();
this.cookies = (_b = constructorParams.cookies) !== null && _b !== void 0 ? _b : {};
this.headers = (_c = constructorParams.headers) !== null && _c !== void 0 ? _c : {};
this.remoteAddress = (_d = constructorParams.remoteAddress) !== null && _d !== void 0 ? _d : '';
}
Request.queryToParams = function (query) {
var queryParams = {};
query.split('&').forEach(function (pair) {
var _a = tslib_1.__read(pair.split('='), 2), key = _a[0], _b = _a[1], value = _b === void 0 ? '' : _b;
if (!queryParams.hasOwnProperty(key)) {
queryParams[key] = value;
}
else {
var currentValue = queryParams[key];
if (Array.isArray(currentValue)) {
currentValue.push(value);
}
else {
queryParams[key] = [currentValue, value];
}
}
});
return queryParams;
};
Request.prototype.contentPath = function () {
return this.path
.replace(/^\/admin/, '')
.replace(/^\/site/, '')
.replace(new RegExp("^/".concat(this.mode)), '')
.replace(new RegExp("^/".concat(this.project())), '')
.replace(new RegExp("^/".concat(this.branch)), '');
};
Request.prototype.query = function () {
return Object.entries(this.params).map(function (_a) {
var _b = tslib_1.__read(_a, 2), key = _b[0], value = _b[1];
return "".concat(key, "=").concat(value);
}).join('&');
};
Request.prototype.project = function () {
if (!this.repositoryId) {
throw new Error('Unable to get project from request without repositoryId!');
}
return this.repositoryId.replace(/^com\.enonic\.cms\./, '');
};
Request.prototype.serialize = function () {
return (0, fast_uri_1.serialize)({
scheme: this.scheme,
host: this.host,
port: this.port,
path: this.path,
query: this.query()
}).replace(/\?$/, '');
};
return Request;
}());
exports.Request = Request;