1 import { requestRest } from '../../../../framework/src/services/restService';
2 import { TocTreeNode, TocNodeCollection } from '../models/tocNode';
6 private tocNodeCollection: TocTreeNode[] | null = null;
7 private documents: { [path: string]: string | null } = {};
9 public async getDocument(path: string): Promise<string | null> {
10 // check if the result is allready in the cache
11 if (this.documents[path]) return Promise.resolve(this.documents[path]);
13 // request the document
14 const result = await requestRest<string>(`/help/${ path }`.replace(/\/{2,}/i, '/'));
16 this.documents[path] = result;
18 return this.documents[path] || null;
21 public async getTableOfContents(): Promise<TocTreeNode[] | null> {
22 // check if the result is allready in the cache
23 if (this.tocNodeCollection) return Promise.resolve(this.tocNodeCollection);
25 // request the table of contents
26 const result = await requestRest<TocNodeCollection>('/help/?meta', undefined, false);
27 if (result !== false) {
28 const mapNodesCollection = (col: TocNodeCollection): TocTreeNode[] => {
29 return Object.keys(col).reduce <TocTreeNode[]>((acc, key) => {
30 const current = col[key];
33 uri: current.versions.current.path,
34 nodes: current.nodes && mapNodesCollection(current.nodes) || undefined
40 this.tocNodeCollection = result && mapNodesCollection(result);
42 return this.tocNodeCollection || null;
46 export const helpService = new HelpService();
47 export default helpService;