Merge "SDN-R ws notifications"
[ccsdk/features.git] / sdnr / wt / odlux / apps / helpApp / src / services / helpService.ts
1 import { requestRest } from '../../../../framework/src/services/restService';
2 import { TocTreeNode, TocNodeCollection } from '../models/tocNode';
3
4 class HelpService {
5
6   private tocNodeCollection: TocTreeNode[] | null = null;
7   private documents: { [path: string]: string | null } = {};
8
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]);
12
13     // request the document
14     const result = await requestRest<string>(`/help/${ path }`.replace(/\/{2,}/i, '/'));
15     if (result) {
16       this.documents[path] = result;
17     }
18     return this.documents[path] || null;
19   }
20
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);
24
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];
31           acc.push({
32             label: current.label,
33             uri: current.versions.current.path,
34             nodes: current.nodes && mapNodesCollection(current.nodes) || undefined
35           });
36           return acc;
37         }, []);
38       }
39
40       this.tocNodeCollection = result && mapNodesCollection(result);
41     }
42     return this.tocNodeCollection  || null;
43   }
44 }
45
46 export const helpService = new HelpService();
47 export default helpService;