Create wt-odlux directory
[ccsdk/features.git] / sdnr / wt-odlux / odlux / apps / helpApp / src / services / helpService.ts
1 /**
2  * ============LICENSE_START========================================================================
3  * ONAP : ccsdk feature sdnr wt odlux
4  * =================================================================================================
5  * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6  * =================================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8  * in compliance with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License
13  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14  * or implied. See the License for the specific language governing permissions and limitations under
15  * the License.
16  * ============LICENSE_END==========================================================================
17  */
18 import { requestRest } from '../../../../framework/src/services/restService';
19 import { TocTreeNode, TocNodeCollection } from '../models/tocNode';
20
21 class HelpService {
22
23   private tocNodeCollection: TocTreeNode[] | null = null;
24   private documents: { [path: string]: string | null } = {};
25
26   public async getDocument(path: string): Promise<string | null> {
27     // check if the result is allready in the cache
28     if (this.documents[path]) return Promise.resolve(this.documents[path]);
29
30     // request the document
31     const result = await requestRest<string>(`/help/${path}`.replace(/\/{2,}/i, '/'));
32     if (result) {
33       this.documents[path] = result;
34     }
35     return this.documents[path] || null;
36   }
37
38   public async getTableOfContents(): Promise<TocTreeNode[] | null> {
39     // check if the result is allready in the cache
40     if (this.tocNodeCollection) return Promise.resolve(this.tocNodeCollection);
41
42     // request the table of contents
43     const result = await requestRest<TocNodeCollection>('/help/?meta', undefined, false);
44     if (result !== null) {
45       const mapNodesCollection = (col: TocNodeCollection): TocTreeNode[] => {
46         return Object.keys(col).reduce<TocTreeNode[]>((acc, key) => {
47           const current = col[key];
48           acc.push({
49             id: key,
50             label: current.label,
51             uri: current.versions.current.path,
52             nodes: current.nodes && mapNodesCollection(current.nodes) || undefined
53           });
54           return acc;
55         }, []);
56       }
57
58       this.tocNodeCollection = result && mapNodesCollection(result) || null;
59     }
60     return this.tocNodeCollection || null;
61   }
62 }
63
64 export const helpService = new HelpService();
65 export default helpService;