All files / src sequence.ts

92.3% Statements 48/52
50% Branches 4/8
80% Functions 4/5
95.74% Lines 45/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 771x 1x 1x 1x 1x 1x 1x 1x 1x       1x       1x 1x           92x     92x 92x   92x 92x 92x 92x 92x       92x 92x 92x 92x 92x 92x 92x   92x 90x 78x 78x 78x 78x 78x   14x 14x         92x 92x   92x 92x 92x       78x   78x 78x 78x        
require("dotenv").config()
import {AuthenticateFn, AuthenticationBindings} from '@loopback/authentication';
import {inject} from '@loopback/context';
import {ExpressRequestHandler, FindRoute, InvokeMethod, InvokeMiddleware, ParseParams, Reject, RequestContext, RestBindings, Send, SequenceHandler} from '@loopback/rest';
import cors from 'cors';
import requestIp from 'request-ip';
import * as winston from 'winston';
import {failureHandler} from './controllers/components';
import {LoggerServiceBindings} from './keys';
// import * as winston from 'winston';
// import {LoggerServiceBindings} from './keys';
 
const middlewareList: ExpressRequestHandler[] = [
  cors(),
  requestIp.mw()
];
const SequenceActions = RestBindings.SequenceActions;
export class MySequence implements SequenceHandler {
  /**
   * Optional invoker for registered middleware in a chain.
   * To be injected via SequenceActions.INVOKE_MIDDLEWARE.
   */
  @inject(SequenceActions.INVOKE_MIDDLEWARE, {optional: true})
  protected invokeMiddleware: InvokeMiddleware = () => true;
 
  constructor(
    @inject(SequenceActions.FIND_ROUTE) protected findRoute: FindRoute,
    @inject(SequenceActions.PARSE_PARAMS) protected parseParams: ParseParams,
 
    @inject(SequenceActions.INVOKE_METHOD) protected invoke: InvokeMethod,
    @inject(SequenceActions.SEND) public send: Send,
    @inject(SequenceActions.REJECT) public reject: Reject,
    @inject(AuthenticationBindings.AUTH_ACTION) protected authenticateRequest: AuthenticateFn,
    @inject(LoggerServiceBindings.LOGGER_SERVICE) public logger: winston.Logger,
 
  ) { }
  async handle(context: RequestContext) {
    const {request, response, name} = context;
    try {
      const entryTime = Date.now();
      const finished = await this.invokeMiddleware(context, middlewareList);
      Iif (finished) return;
      await this.trackEntry(request, name);
      const route = this.findRoute(request);
      // call authentication action
      await this.authenticateRequest(request);
      const args = await this.parseParams(request, route);
      const result = await this.invoke(route, args);
      const exitTime = Date.now();
      const responseTime = exitTime - entryTime;
      await this.trackExit(request, name, responseTime);
      this.send(response, result);
    } catch (err: any) {
      let code = err.code || 422
      failureHandler(response, code, err)
    }
  }
 
  async trackEntry(request: any, name: String,): Promise<void | boolean> {
    console.log(request.url)
    Iif (request.url === "/")
      return true
    const logData = {ip: request.clientIp, api: request.url, message: `Track Entry Log`};
    this.logger.info(logData);
    return true
 
  }
  async trackExit(request: any, name: String, responseTime: Number,): Promise<void | boolean> {
    Iif (request.url === "/")
      return true
    const logData = {ip: request.clientIp, api: request.url, responseTime, message: `Track Exit Log`, };
    this.logger.info(logData);
    return true
  }
}