resource-watch/rw-api-microservice-node

View on GitHub

Showing 16 of 20 total issues

File main.ts has 329 lines of code (exceeds 250 allowed). Consider refactoring.
Open

// @ts-ignore
import Fastly from '@tiagojsag/fastly-promises';
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
import type { Context, Middleware, Next, Request } from "koa";
import cors from "@koa/cors";
Severity: Minor
Found in src/main.ts - About 3 hrs to fix

    Function convertAndValidateBootstrapOptions has a Cognitive Complexity of 22 (exceeds 5 allowed). Consider refactoring.
    Open

        private static convertAndValidateBootstrapOptions(options: BootstrapArguments): ConfigurationOptions {
            if (
                options.fastlyEnabled !== true
                && options.fastlyEnabled !== false
                && options.fastlyEnabled !== "true"
    Severity: Minor
    Found in src/main.ts - About 3 hrs to fix

    Cognitive Complexity

    Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

    A method's cognitive complexity is based on a few simple rules:

    • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
    • Code is considered more complex for each "break in the linear flow of the code"
    • Code is considered more complex when "flow breaking structures are nested"

    Further reading

    Function convertAndValidateBootstrapOptions has 71 lines of code (exceeds 25 allowed). Consider refactoring.
    Open

        private static convertAndValidateBootstrapOptions(options: BootstrapArguments): ConfigurationOptions {
            if (
                options.fastlyEnabled !== true
                && options.fastlyEnabled !== false
                && options.fastlyEnabled !== "true"
    Severity: Major
    Found in src/main.ts - About 2 hrs to fix

      Function bootstrap has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
      Open

          public bootstrap(opts: BootstrapArguments): Middleware<{}, {}> {
              this.options = Microservice.convertAndValidateBootstrapOptions(opts);
              this.options.logger.info('RW API integration middleware registered');
              if (this.options.awsCloudWatchLoggingEnabled) {
                  this.cloudWatchService = CloudWatchService.init(this.options.logger, this.options.awsRegion, this.options.awsCloudWatchLogGroupName, this.options.awsCloudWatchLogStreamName);
      Severity: Minor
      Found in src/main.ts - About 2 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function fastlyIntegrationHandler has a Cognitive Complexity of 17 (exceeds 5 allowed). Consider refactoring.
      Open

          private static async fastlyIntegrationHandler(ctx: Context, fastlyServiceId: string, fastlyAPIKey: string, logger: Logger): Promise<void> {
              const fastly: Fastly = Fastly(fastlyAPIKey, fastlyServiceId);
      
              if (ctx.status >= 200 && ctx.status < 400) {
                  // Non-GET, anonymous requests with the `uncache` header can purge the cache
      Severity: Minor
      Found in src/main.ts - About 2 hrs to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function requestToMicroservice has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring.
      Open

          public async requestToMicroservice(requestConfig: RequestToMicroserviceOptions): Promise<Record<string, any>> {
              this.options.logger.info('Adding authorization header');
              const axiosRequestConfig: AxiosRequestConfig = {
                  baseURL: this.options.gatewayURL,
                  data: requestConfig.body,
      Severity: Minor
      Found in src/main.ts - About 1 hr to fix

      Cognitive Complexity

      Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

      A method's cognitive complexity is based on a few simple rules:

      • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
      • Code is considered more complex for each "break in the linear flow of the code"
      • Code is considered more complex when "flow breaking structures are nested"

      Further reading

      Function logRequestToCloudWatch has 47 lines of code (exceeds 25 allowed). Consider refactoring.
      Open

          private async logRequestToCloudWatch(logger: Logger, request: Request, requestValidationData: RequestValidationResponse): Promise<void> {
              logger.debug('[logRequestToCloudWatch] Logging request to CloudWatch');
      
              const logQuery: Record<string, any> = { ...request.query };
              delete logQuery.loggedUser;
      Severity: Minor
      Found in src/main.ts - About 1 hr to fix

        Function bootstrap has 43 lines of code (exceeds 25 allowed). Consider refactoring.
        Open

            public bootstrap(opts: BootstrapArguments): Middleware<{}, {}> {
                this.options = Microservice.convertAndValidateBootstrapOptions(opts);
                this.options.logger.info('RW API integration middleware registered');
                if (this.options.awsCloudWatchLoggingEnabled) {
                    this.cloudWatchService = CloudWatchService.init(this.options.logger, this.options.awsRegion, this.options.awsCloudWatchLogGroupName, this.options.awsCloudWatchLogStreamName);
        Severity: Minor
        Found in src/main.ts - About 1 hr to fix

          Function requestToMicroservice has 43 lines of code (exceeds 25 allowed). Consider refactoring.
          Open

              public async requestToMicroservice(requestConfig: RequestToMicroserviceOptions): Promise<Record<string, any>> {
                  this.options.logger.info('Adding authorization header');
                  const axiosRequestConfig: AxiosRequestConfig = {
                      baseURL: this.options.gatewayURL,
                      data: requestConfig.body,
          Severity: Minor
          Found in src/main.ts - About 1 hr to fix

            Function getRequestValidationData has 42 lines of code (exceeds 25 allowed). Consider refactoring.
            Open

                private async getRequestValidationData(logger: Logger, baseURL: string, request: Request): Promise<RequestValidationResponse> {
                    logger.debug('[getLoggedUser] Validating request');
                    if (!request.header.authorization) {
                        logger.debug('[getLoggedUser] No authorization header found');
                    }
            Severity: Minor
            Found in src/main.ts - About 1 hr to fix

              Function getRequestValidationData has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring.
              Open

                  private async getRequestValidationData(logger: Logger, baseURL: string, request: Request): Promise<RequestValidationResponse> {
                      logger.debug('[getLoggedUser] Validating request');
                      if (!request.header.authorization) {
                          logger.debug('[getLoggedUser] No authorization header found');
                      }
              Severity: Minor
              Found in src/main.ts - About 1 hr to fix

              Cognitive Complexity

              Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

              A method's cognitive complexity is based on a few simple rules:

              • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
              • Code is considered more complex for each "break in the linear flow of the code"
              • Code is considered more complex when "flow breaking structures are nested"

              Further reading

              Function bootstrapMiddleware has 31 lines of code (exceeds 25 allowed). Consider refactoring.
              Open

                      const bootstrapMiddleware: Middleware = async (ctx: Context, next: Next) => {
                          const { logger, gatewayURL } = this.options;
              
                          if (!this.shouldSkipAPIKeyValidation(ctx)) {
                              try {
              Severity: Minor
              Found in src/main.ts - About 1 hr to fix

                Function injectRequestValidationData has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                    private async injectRequestValidationData(logger: Logger, requestValidationData: RequestValidationResponse, ctx: Context): Promise<void> {
                        logger.debug('[injectRequestValidationData] Obtaining loggedUser for microserviceToken');
                        if (requestValidationData.application) {
                            ctx.state.requestApplication = requestValidationData.application
                        }
                Severity: Minor
                Found in src/main.ts - About 35 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Function logRequestToCloudWatch has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring.
                Open

                    private async logRequestToCloudWatch(logger: Logger, request: Request, requestValidationData: RequestValidationResponse): Promise<void> {
                        logger.debug('[logRequestToCloudWatch] Logging request to CloudWatch');
                
                        const logQuery: Record<string, any> = { ...request.query };
                        delete logQuery.loggedUser;
                Severity: Minor
                Found in src/main.ts - About 35 mins to fix

                Cognitive Complexity

                Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.

                A method's cognitive complexity is based on a few simple rules:

                • Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
                • Code is considered more complex for each "break in the linear flow of the code"
                • Code is considered more complex when "flow breaking structures are nested"

                Further reading

                Avoid too many return statements within this function.
                Open

                                    return {
                                        statusCode: err.response.status,
                                        body: err.response.data
                                    };
                Severity: Major
                Found in src/main.ts - About 30 mins to fix

                  Avoid too many return statements within this function.
                  Open

                              return err;
                  Severity: Major
                  Found in src/main.ts - About 30 mins to fix
                    Severity
                    Category
                    Status
                    Source
                    Language