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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
16 * ============LICENSE_END==========================================================================
18 import { requestRest } from '../../../../framework/src/services/restService';
19 import { TocTreeNode, TocNodeCollection } from '../models/tocNode';
23 private tocNodeCollection: TocTreeNode[] | null = null;
24 private documents: { [path: string]: string | null } = {};
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]);
30 // request the document
31 const result = await requestRest<string>(`/help/${path}`.replace(/\/{2,}/i, '/'));
33 this.documents[path] = result;
35 return this.documents[path] || null;
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);
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];
51 uri: current.versions.current.path,
52 nodes: current.nodes && mapNodesCollection(current.nodes) || undefined
58 this.tocNodeCollection = result && mapNodesCollection(result) || null;
60 return this.tocNodeCollection || null;
64 export const helpService = new HelpService();
65 export default helpService;