@jorsek/ezd-client
Version:
152 lines • 6.66 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const class_transformer_validator_1 = require("class-transformer-validator");
const Types_1 = require("../Types");
const Resource_1 = require("./Resource");
class Content extends Resource_1.Resource {
constructor() {
super(...arguments);
this.nav_tree_cache = {};
}
getPageTitle() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.axios.get("/content/v1/pageTitle");
return response.data;
});
}
getBannerText() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.axios.get("/content/v1/bannerText");
const data = response.data.banner;
return Array.isArray(data) ? data : [data];
});
}
getSectionsAndVersions() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.axios.get("/content/v1/sections");
return response.data;
});
}
getSections(path) {
return __awaiter(this, void 0, void 0, function* () {
const params = {};
if (path) {
params["for-path"] = path;
}
const response = yield this.axios.get(`structure`, { params });
const sections = response.data.children;
return sections.map(obj => {
obj = this.fixMetadata(obj);
class_transformer_validator_1.transformAndValidateSync(Types_1.ISection, obj);
return obj;
});
});
}
getAllGroups() {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.axios.get(`groups`);
const groups = response.data.groups;
for (const key of Object.keys(groups)) {
groups[key] = this.fixMetadata(groups[key]);
class_transformer_validator_1.transformAndValidateSync(Types_1.ISection, groups[key]);
}
return groups;
});
}
getNamedGroup(group_name) {
return __awaiter(this, void 0, void 0, function* () {
const groups = yield this.getAllGroups();
if (groups[group_name] === undefined) {
return null;
}
return groups[group_name].children.map(child => this.fixMetadata(child));
});
}
getContentForResourceId(resource_id) {
return __awaiter(this, void 0, void 0, function* () {
const params = { "for-resourceid": resource_id };
const response = yield this.axios.get(`content`, { params });
const data = response.data;
data.versions = data.otherVersions || [];
data.current_version = data.thisVersion || "";
data.page_type = data.outputclasses && data.outputclasses.length > 0 && data.outputclasses[0] ? "landing_page" : "content_page";
class_transformer_validator_1.transformAndValidateSync(Types_1.IPageContent, data);
return response.data;
});
}
getRedirect(path) {
return __awaiter(this, void 0, void 0, function* () {
const response = yield this.axios.get(`content`, { params: { "for-path": path } });
return response.data.redirect ? response.data.redirect.href : null;
});
}
getContent(path, audiences = []) {
return __awaiter(this, void 0, void 0, function* () {
const params = {
"audiences": audiences.join(","),
"include-metadata": true,
};
if (path) {
params["for-path"] = path;
}
const response = yield this.axios.get(`content`, { params });
const data = response.data;
data.versions = data.otherVersions || [];
data.current_version = data.thisVersion || "";
data.page_type = data.outputclasses && data.outputclasses.length > 0 && data.outputclasses[0] ? "landing_page" : "content_page";
class_transformer_validator_1.transformAndValidateSync(Types_1.IPageContent, data);
return response.data;
});
}
getPrintContent(path, audiences = []) {
return __awaiter(this, void 0, void 0, function* () {
const params = {
"audiences": audiences.join(","),
"include-metadata": true,
"full": true,
};
if (path) {
params["for-path"] = path;
}
const response = yield this.axios.get(`contentForPrint`, { params });
return response.data.content;
});
}
clearNavTreeCache() {
this.nav_tree_cache = {};
}
getNavTree(path) {
return __awaiter(this, void 0, void 0, function* () {
const fixed_path = path.startsWith("/") ? path.slice(1) : path;
const cache_key = fixed_path.split("/").slice(0, 2).join("/");
if (this.nav_tree_cache[cache_key]) {
return this.nav_tree_cache[cache_key];
}
console.log(`NO CACHED TREE FOR ${cache_key}`);
const tree = (yield this.axios.get("structure", { params: { "for-path": cache_key } })).data;
class_transformer_validator_1.transformAndValidateSync(Types_1.INavTree, tree);
this.nav_tree_cache[cache_key] = tree;
// return this.nav_tree_cache[cache_key];
return tree;
});
}
fixMetadata(section) {
// TECH DEBT: m should never be null, but we still have to check in the conditional?
const thumbnails = section.meta.filter(m => m && m.name === "thumbnail");
if (thumbnails.length > 0) {
section.thumbnail = thumbnails[0].href;
section.meta = section.meta.filter(m => m && m.name !== "thumbnail");
}
return section;
}
}
exports.Content = Content;
//# sourceMappingURL=Content.js.map