@apihawk/help-center-sdk
Version:
The ApiHawk Help Center SDK
260 lines (259 loc) • 9.12 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 errors_1 = require("@apihawk/errors");
const qs = require("qs");
const help_center_sdk_service_base_1 = require("../lib/help-center-sdk-service-base");
const to_rest_resource_1 = require("./common/to-rest-resource");
class HelpCenterSDKArticles extends help_center_sdk_service_base_1.HelpCenterSDKServiceBase {
/**
* Get articles
* @param page
* @param {number} pageSize
* @param query
* @returns {Promise<IRestPaginatedResource<IHelpCenterArticle>>}
*/
getArticles(page = 1, pageSize = 25, query) {
return __awaiter(this, void 0, void 0, function* () {
const filter = {
page,
page_size: pageSize,
where: []
};
if (query && query.columns) {
filter.columns = query.columns;
}
if (query && query.id) {
if (Array.isArray(query.id)) {
filter.where.push({
field: 'id',
where: 'and',
type: 'in',
values: query.id
});
}
else {
filter.where.push({
field: 'id',
where: 'and',
type: 'equalTo',
value: query.id
});
}
}
if (query && query.seo_url) {
filter.where.push({
field: 'seo_url',
where: 'and',
type: 'equalTo',
value: query.seo_url
});
}
const articles = yield this.api.call({
url: `/content/articles?${qs.stringify(filter)}`,
method: 'GET'
});
return to_rest_resource_1.default(articles, 'content');
});
}
/**
* Get article by seo_url
* @returns {IHelpCenterArticle}
* @param seoUrl
*/
getArticleBySeoUrl(seoUrl) {
return __awaiter(this, void 0, void 0, function* () {
const articles = yield this.getArticles(1, 1, { seo_url: seoUrl });
if (articles && articles.items && articles.items.length) {
return articles.items[0];
}
throw new errors_1.NotFoundError('Article not found');
});
}
/**
* Get article by id
* @returns {IHelpCenterArticle}
* @param id
*/
getArticleById(id) {
return __awaiter(this, void 0, void 0, function* () {
const article = yield this.api.call({
url: `/content/articles/${id}`,
method: 'GET'
});
return article;
});
}
// /**
// * Search articles
// * @param page
// * @param {number} pageSize
// * @param query
// * @returns {Promise<IRestPaginatedResource<IHelpCenterArticle>>}
// */
// public async searchArticles(
// page: number = 1,
// pageSize: number = 25,
// query: {
// q: string,
// limit?: number
// } = {
// q: '',
// limit: 10
// }) {
// const articles = this.api
// .call({
// url: `/search/find?resource=content/articles&q=${query.q || ''}&limit=${query.limit || 10}`,
// method: 'GET'
// });
// }
/**
* Get content2categories
* @param {number} page
* @param {number} pageSize
* @param query
* @returns {Promise<IRestPaginatedResource<IContent2Category>>}
*/
getContent2categories(page = 1, pageSize = 25, query) {
return __awaiter(this, void 0, void 0, function* () {
const filter = {
page,
page_size: pageSize,
where: []
};
if (query && query.categoryId) {
if (Array.isArray(query.categoryId)) {
filter.where.push({
field: 'category_id',
where: 'and',
type: 'in',
values: query.categoryId
});
}
else {
filter.where.push({
field: 'category_id',
where: 'and',
type: 'equalTo',
value: query.categoryId
});
}
}
if (query && query.contentId) {
if (Array.isArray(query.contentId)) {
filter.where.push({
field: 'content_id',
where: 'and',
type: 'in',
values: query.contentId
});
}
else {
filter.where.push({
field: 'content_id',
where: 'and',
type: 'equalTo',
value: query.contentId
});
}
filter.group = ['content_id'];
}
const headers = {
Accept: 'application/json'
};
if (query && query.advanced) {
headers['Accept-Response'] = 'Advanced';
}
const content2categories = yield this.api.call({
url: `/content/content2categories${qs.stringify(filter)}`,
method: 'GET',
headers
});
return to_rest_resource_1.default(content2categories, 'content2categories');
});
}
/**
* Get latest articles by category
* @param {number[]} ids
* @returns {Promise<any>}
*/
getLatestByCategory(ids = []) {
return __awaiter(this, void 0, void 0, function* () {
const filter = {
categories: ids
};
const url = `/helpcenter/articles-by-category?${qs.stringify(filter)}`;
return this.api
.call({
url,
method: 'GET',
getErrorBody: true
})
.then((response) => response.categories.categories);
});
}
/**
* Get all latest articles
* @param {number[]} ids
* @returns {Promise<Bluebird<any>>}
*/
getAllLatest(ids = []) {
return __awaiter(this, void 0, void 0, function* () {
return this.getLatestByCategory(ids).then((articlesByCategory) => {
const allArticles = Object.keys(articlesByCategory)
.filter((catId) => {
return !!articlesByCategory[catId].articles;
})
.reduce((accumulated, key) => {
accumulated = [
...accumulated,
...articlesByCategory[key].articles.map((a) => {
a.breadcrumb =
a.primary_category_breadcrumb
.reverse()
.slice(1)
.map((c) => c.seo_url)
.join('/') +
'/' +
a.url;
return a;
})
];
return accumulated;
}, []);
const uniqueArticles = allArticles.filter(function (article) {
const key = article.content_id;
// @ts-ignore
return !this.has(key) && this.add(key);
}, new Set());
return uniqueArticles;
});
});
}
/**
*
* @returns {IRestPaginatedResource<IHelpCenterArticle>}
*/
getEmptyList() {
return to_rest_resource_1.default({
_embedded: {
content: []
},
page_count: 0,
page_size: 25,
total_items: 0,
page: 1,
_links: {},
_language: {}
}, 'content');
}
}
exports.HelpCenterSDKArticles = HelpCenterSDKArticles;