Fix npm dependency issue for server
[ccsdk/cds.git] / cds-ui / server / src / sequence.ts
1 /*
2 ============LICENSE_START==========================================
3 ===================================================================
4 Copyright (C) 2018-19 IBM Intellectual Property. All rights reserved.
5 ===================================================================
6
7 Unless otherwise specified, all software contained herein is licensed
8 under the Apache License, Version 2.0 (the License);
9 you may not use this software except in compliance with the License.
10 You may obtain a copy of the License at
11
12     http://www.apache.org/licenses/LICENSE-2.0
13
14 Unless required by applicable law or agreed to in writing, software
15 distributed under the License is distributed on an "AS IS" BASIS,
16 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 See the License for the specific language governing permissions and
18 limitations under the License.
19 ============LICENSE_END============================================
20 */
21
22 import { inject } from '@loopback/context';
23 import {
24   FindRoute,
25   InvokeMethod,
26   ParseParams,
27   Reject,
28   RequestContext,
29   RestBindings,
30   Send,
31   SequenceHandler,
32 } from '@loopback/rest';
33 import { logger } from './logger/logger';
34 import { v4 as uuid } from 'uuid';
35
36 const SequenceActions = RestBindings.SequenceActions;
37
38 export class MySequence implements SequenceHandler {
39   constructor(
40     @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute,
41     @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams,
42     @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
43     @inject(SequenceActions.SEND) public send: Send,
44     @inject(SequenceActions.REJECT) public reject: Reject,
45   ) { }
46
47   async handle(context: RequestContext) {
48     const { request, response } = context;
49     try {
50       if (!('X-ONAP-RequestID' in request.headers || 'x-onap-requestid' in request.headers)) {
51         request.headers = { 'X-ONAP-RequestID': uuid(), ...request.headers}
52         logger.info(JSON.stringify(request.headers))
53       }
54       const route = this.findRoute(request);
55       const args = await this.parseParams(request, route);
56       const result = await this.invoke(route, args);
57       this.send(response, result);
58     } catch (err: unknown) {
59       if (err instanceof Error) {
60         this.reject(context, err);
61       }
62     } finally {
63       const { authorization, ...headers} = request.headers;
64       logger.info("Incoming request from %s %s and with header %s query %s params %s and response code: %s",
65         request.method, request.url, JSON.stringify(headers), JSON.stringify(request.query), JSON.stringify(request.params), JSON.stringify(response.statusCode))
66     }
67   }
68 }