@apihawk/help-center-sdk
Version:
The ApiHawk Help Center SDK
182 lines (181 loc) • 6.97 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
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) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const qs = require("qs");
const help_center_sdk_service_base_1 = require("../lib/help-center-sdk-service-base");
const help_center_sdk_articles_1 = require("./help-center-sdk-articles");
class HelpCenterSDKCategories extends help_center_sdk_service_base_1.HelpCenterSDKServiceBase {
constructor(api) {
super(api);
this.helpCenterSDKArticles = new help_center_sdk_articles_1.HelpCenterSDKArticles(api);
}
/**
* Fetches the tree of categories.
* @param {string|number} query.rootId - the category ID from which the tree will start
* @param {number} query.nestingLevel - how deep the three will be walked over (-1 => no limits)
* @param {string} query.lang - the language for the content
* @param query
* @param rootCategoryUrl - the root of the categories
*/
getTree(query = {}, rootCategoryUrl) {
return __awaiter(this, void 0, void 0, function* () {
let queryString = `nesting_level=${query.nestingLevel || -1}`;
if (query.lang) {
queryString += `&lang=${query.lang}`;
}
// By default build the tree from the root category
if (!query.rootId && rootCategoryUrl) {
query.rootId = yield this.getRoot(rootCategoryUrl).then((category) => +category.id);
}
queryString += `&root_id=${query.rootId}`;
return this.api.call({
url: `/content/categories/tree?${queryString}`,
method: 'GET'
});
});
}
/**
* Get category
* @param {number} id
* @returns {Promise<IHelpCenterCategory>}
*/
getCategory(id) {
return __awaiter(this, void 0, void 0, function* () {
const category = yield this.api.call({
url: `/content/categories/${id}`,
method: 'GET'
});
return category;
});
}
/**
* Fetches the "root" category.
* The root category is like a scope for the help center - only
* categories and articles which belong to it are visible.
*
* @returns {Promise} - the root category
*/
getRoot(rootCategoryUrl) {
return __awaiter(this, void 0, void 0, function* () {
return yield this.getBySeoUrl(rootCategoryUrl);
});
}
/**
* Get
* @param {string} categorySeoUrl
* @returns {Promise<IHelpCenterCategory>}
*/
getBySeoUrl(categorySeoUrl) {
return __awaiter(this, void 0, void 0, function* () {
const query = qs.stringify({
where: [
{
type: 'equalTo',
where: 'and',
field: 'seo_url',
value: categorySeoUrl
}
]
});
const result = yield this.api.call({
url: `/content/categories?${query}`,
method: 'GET'
});
if (result.total_items === 0) {
return Promise.reject(new Error('Not found'));
}
return result._embedded.categories[0];
});
}
/**
*
* @param {number} categoryId
* @param {number} page
* @param {number} pageSize
* @returns {Promise<void>}
*/
getArticleIdsFromCategory(categoryId, page = 1, pageSize = 25) {
return __awaiter(this, void 0, void 0, function* () {
const category = yield this.getCategory(categoryId);
const categoryTree = yield this.getTree({
rootId: categoryId,
nestingLevel: -1
});
const subcategories = categoryTree.result || [];
// query for article IDs from the current category
const categoriesToFetchFrom = [categoryId];
// query for category children categories' articles (recursive)
if (category.fetch_subarticles === '1') {
const additionalArticleIds = this.collectArticleIdsToFetch(subcategories);
categoriesToFetchFrom.push(...additionalArticleIds);
}
return yield this.helpCenterSDKArticles.getContent2categories(page, pageSize, { categoryId: categoriesToFetchFrom });
});
}
/**
* Get category breadcrumbs
* @param {number} categoryId
* @returns {Promise<ICategoryBreadcrumbResult>}
*/
getBreadcrumbs(categoryId) {
return __awaiter(this, void 0, void 0, function* () {
return this.api
.call({
url: `/content/categories/breadcrumb/${categoryId}`,
method: 'GET'
})
.then((response) => response.result);
});
}
/**
* Collects article IDs to be fetched by walking the
* tree of categories and checking whether a category's
* subcategory articles should be fetched as well.
*
* (yes) -> fetch subarticles
* (no) -> do not fetch subarticles
*
* -- Products (yes) <--- STARTING CATEGORY
* ---- Syme (no)
* ------ Syme Linux (no)
* ------ Syme Windows (no)
* ---- Billia (yes)
* ------ HelpCenter (no)
* -------- Creating Articles (no)
* -------- Creating Categories (no)
* ------ Payments (yes)
* -------- Card Payments (no)
* -------- Bank Payments (no)
*
* Should get the IDs for the following categories:
*
* Products, Syme, Billia, HelpCenter, Payments, Card Payments, Bank Payments
*
* @param {Array} categoryTree - tree of categories
*
* @returns {Array<string>} - list of article IDs
*/
collectArticleIdsToFetch(categoryTree = []) {
return categoryTree.reduce((ids, category) => {
if (category.fetch_subarticles === '1') {
return [
...ids,
category.id,
...this.collectArticleIdsToFetch(category.children)
];
}
else {
return [...ids, category.id];
}
}, []);
}
}
exports.HelpCenterSDKCategories = HelpCenterSDKCategories;