resource-watch/rw-api-microservice-node

View on GitHub

Showing 20 of 20 total issues

Similar blocks of code found in 2 locations. Consider refactoring.
Open

        try {
            await this.cloudWatchService.send(createLogGroupCommand);

            this.logger.debug(`Log group '${logGroupName}' created successfully.`);
        } catch (createLogGroupError) {
Severity: Major
Found in src/cloudwatch.service.ts and 1 other location - About 3 hrs to fix
src/cloudwatch.service.ts on lines 88..98

Duplicated Code

Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

Tuning

This issue has a mass of 113.

We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

Refactorings

Further Reading

Similar blocks of code found in 2 locations. Consider refactoring.
Open

        try {
            await this.cloudWatchService.send(createLogStreamCommand);
            this.logger.debug(`Log stream '${logStreamName}' created successfully.`);
        } catch (createLogStreamError) {
            if (createLogStreamError.name === 'ResourceAlreadyExistsException') {
Severity: Major
Found in src/cloudwatch.service.ts and 1 other location - About 3 hrs to fix
src/cloudwatch.service.ts on lines 68..79

Duplicated Code

Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

Tuning

This issue has a mass of 113.

We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

Refactorings

Further Reading

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

                  Identical blocks of code found in 2 locations. Consider refactoring.
                  Open

                                  if (requestConfig.resolveWithFullResponse === true) {
                                      return {
                                          statusCode: err.response.status,
                                          body: err.response.data
                                      };
                  Severity: Minor
                  Found in src/main.ts and 1 other location - About 30 mins to fix
                  src/main.ts on lines 361..366

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 45.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  Identical blocks of code found in 2 locations. Consider refactoring.
                  Open

                                  if (requestConfig.resolveWithFullResponse === true) {
                                      return {
                                          statusCode: err.response.status,
                                          body: err.response.data
                                      };
                  Severity: Minor
                  Found in src/main.ts and 1 other location - About 30 mins to fix
                  src/main.ts on lines 352..357

                  Duplicated Code

                  Duplicated code can lead to software that is hard to understand and difficult to change. The Don't Repeat Yourself (DRY) principle states:

                  Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

                  When you violate DRY, bugs and maintenance problems are sure to follow. Duplicated code has a tendency to both continue to replicate and also to diverge (leaving bugs as two similar implementations differ in subtle ways).

                  Tuning

                  This issue has a mass of 45.

                  We set useful threshold defaults for the languages we support but you may want to adjust these settings based on your project guidelines.

                  The threshold configuration represents the minimum mass a code block must have to be analyzed for duplication. The lower the threshold, the more fine-grained the comparison.

                  If the engine is too easily reporting duplication, try raising the threshold. If you suspect that the engine isn't catching enough duplication, try lowering the threshold. The best setting tends to differ from language to language.

                  See codeclimate-duplication's documentation for more information about tuning the mass threshold in your .codeclimate.yml.

                  Refactorings

                  Further Reading

                  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