2.3.1 • Published 4 months ago

response-normalizer v2.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

Table of Contents

Response Normalizer

Response Normalizer is a package which handle NestJS responses normalization.

Responses format:

{
  "message": "...", // Auto generated message
  "data":"service returned datas", // ORM / DB datas
  "statusCode": 200, // Request associated success status code
}

Installation & Bootstraping

To install Response Normalizer it's quite easy, just type the following command in your shell:

npm install response-normalizer

To use Response Normalizer, proceed as following:

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { init } from 'response-normalizer'; //Import bootstrap function

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  init(app); // Use bootstrap function which need an 'INestApplication' object (Here 'app')
  await app.listen(3000);
}

With this setup, Response Normalizer will hook every API Endpoints and normalize responses before return them to client.

Personalization

There's many way to personalize Response Normalizer returns, let's dig througth them.

Messages

You can obviously personalize format of builded messages.

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { init } from 'response-normalizer';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  init(app, {
    messages: {
      success: {
        createdMessage: '::subjectModuleName has been registered', //Default value, set is as you want
      },
      errors: {
        alreadyRegistered: '::subjectModuleName was already registered', //Default value, set is as you want
      },
    },
  });
  await app.listen(3000);
}

By setting up Response Normalizer like that, you'll be able to overwrite default messages patterns by your owns.

Identifiers for messages patterns injection

There's 2 ways to tell to module, which value do you want to put into your messages.

Here's the list of keys to inject real values into your message:

  • subjectModuleName: This represents the name of handler module.

    import { AwesomeService } from './awesome-service.service';
    import { CreateAwesomeRessourceDto } from './dto/create-awesome-ressource.dto';
    
    @Controller()
    export class AwesomeController {
      constructor(
        private readonly awesomeService: AwesomeService,
      ) {}
    
      @Post()
      public create(@Body() createAwesomeRessourceDto : CreateAwesomeRessourceDto) {
        return this.awesomeService.create(createAwesomeRessourceDto);
      }
    }

    ::subjectModuleName will be Awesome (or Awesomes depending on returned data)

  • stringifiedQueryParams: This represents list of query params with handler was invoked or supposed to be invoked.

    import { AwesomeService } from './awesome-service.service';
    import { CreateAwesomeRessourceDto } from './dto/create-awesome-ressource.dto';
    
    @Controller()
    export class AwesomeController {
      constructor(
        private readonly awesomeService: AwesomeService,
      ) {}
    
      @Post()
      public create(@Body() createAwesomeRessourceDto : CreateAwesomeRessourceDto) {
        return this.awesomeService.create(createAwesomeRessourceDto);
      }
    
      @Get(':uuid') // <-        ↓      ↓   This query param
      public getByUUID(@Param('uuid') uuid: string) {
        return this.awesomeService.getByUUID(uuid);
      }
    }

    ::stringifiedQueryParams will be for '5b890609-f862-4a6e-b1dd-89467c2de36b' Uuid (There's some way to personalize this format, see below)

  • statusCode: ::statusCode will represents the response status code (Not used in default templates).

But 'cause it can be long and tricky to remember, the other way to inject values is aliases:

  • subjectModuleName: 'subjectmodulename', 'modulename', 'submodulename', 'mn', 'smn', 'module', 'submodule'.
  • stringifiedQueryParams: 'stringifiedqueryparams', 'queryparams', 'qp'.
  • statusCode: 'statuscode', 'sc', 'status', 'stcd', 'stc', 'code'.

Stringified Query Params formating

It's possible to personalize the format of query params.

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { init } from 'response-normalizer';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  init(app, {
    queryParamsOptions: {
      joinedBy: ', ',
      formattingRules: [
        {
          subStringSequence: 'uuid',
          casing: WordCasing.UPPERED,
        },
      ],
    },
  });
  await app.listen(3000);
}

By adding queryParamsOptions object, it's possible to dig into options, there's 2 keys to personalize format:

  • joinedBy: This represents the operator of joining (DEFAULT: 'and')

    import { AwesomeService } from './awesome-service.service';
    import { CreateAwesomeRessourceDto } from './dto/create-awesome-ressource.dto';
    
    @Controller()
    export class AwesomeController {
      constructor(
        private readonly awesomeService: AwesomeService,
      ) {}
    
      @Get(':uuid/:anotherCriteria')
      public getByUUIDAndAnotherCriteria(
        @Param('uuid') uuid: string, 
        @Param('anotherCriteria') anotherCriteria: string) {
        return this.awesomeService.getByUUIDAndAnotherCriteria(uuid, anotherCriteria);
      }
    }

    ::stringifiedQueryParams will be for '5b890609-f862-4a6e-b1dd-89467c2de36b' Uuid and 'value_here' Another Criteria

  • formattingRules: This is an object that permeet to format specifically rules for a query params term.

    import { AwesomeService } from './awesome-service.service';
    import { CreateAwesomeRessourceDto } from './dto/create-awesome-ressource.dto';
    
    @Controller()
    export class AwesomeController {
      constructor(
        private readonly awesomeService: AwesomeService,
      ) {}
    
      @Get(':uuid')
      public getByUUID(@Param('uuid') uuid: string) {
        return this.awesomeService.getByUUID(uuid);
      }
    }

    Formatting rules definition:

    init(app, {
        queryParamsOptions: {
          formattingRules: [
            {
              subStringSequence: 'uuid',
              casing: WordCasing.UPPERED,
            },
          ],
        },
      });

    Will make return of getByUUID handler invokation looks like : for '5b890609-f862-4a6e-b1dd-89467c2de36b' UUID.

    Formatting rules objects has a replaceBy key which make you able to replace the subStringSequence by something totally different (like: for 'uuid' replace by 'Universally Unique Identifier')

Decorators

Also, package provides many decorators, here's a list:

  • CustomResponseMessage(message): This decorator has to be applied above handler declaration.

    import { AwesomeService } from './awesome-service.service';
    import { CreateAwesomeRessourceDto } from './dto/create-awesome-ressource.dto';
    import { CustomResponseMessage } from 'response-normalizer';
    
    @Controller()
    export class AwesomeController {
      constructor(
        private readonly awesomeService: AwesomeService,
      ) {}
    
      @Get(':uuid')
      @CustomResponseMessage('My custom message here') // <- Decorator
      public getByUUID(@Param('uuid') uuid: string) { // <-- Handler declaration
        return this.awesomeService.getByUUID(uuid);
      }
    }

    Using this decorator means "Message pattern for this route is this", it also takes injectable identifiers (::subjectModuleName, ...)

  • ExternalService(type): This decorator has to be applied above handler declaration, and has to be used when your logic is in another module.

    import { AwesomeService } from './awesome-service.service';
    import { AnotherAwesomeService } from '../another-awesome-module/another-awesome-service.service'; 
    // ↑ ⚠️ Not the same module which is responsible of service ⚠️ ↑
    import { CreateAwesomeRessourceDto } from './dto/create-awesome-ressource.dto';
    import { ExternalService } from 'response-normalizer';
    
    @Controller()
    export class AwesomeController {
      constructor(
        private readonly awesomeService: AwesomeService,
        private readonly anotherAwesomeService: AnotherAwesomeService, // Another service
      ) {}
    
      @Get(':uuid')
      @ExternalService(AnotherAwesomeService) // <- Decorator (⚠️ with the type of the service, not the name of properties, the type of service)
      public getByUUID(@Param('uuid') uuid: string) { // <-- Handler declaration
        return this.anotherAwesomeService.getByUUID(uuid);
      }
    }

    Using this decorator means "The subject module isn't the same as handler".

  • IgnoreFormattingRules(string[]): This decorator has to be applied above handler declaration, and has to be used if you want to do not apply specific (or all) rules.

    import { AwesomeService } from './awesome-service.service';
    import { CreateAwesomeRessourceDto } from './dto/create-awesome-ressource.dto';
    import { IgnoreFormattingRules } from 'response-normalizer';
    
    @Controller()
    export class AwesomeController {
      constructor(
        private readonly awesomeService: AwesomeService,
      ) {}
    
      @Get(':uuid')
      @IgnoreFormattingRules(['uuid']) // <- Decorator
      public getByUUID(@Param('uuid') uuid: string) { // <-- Handler declaration
        return this.awesomeService.getByUUID(uuid);
      }
    }

    Using this decorator means "For the formatting rule where subStringSequence is contained in table, do not apply formatting". If you do not specify any rule, all formatting rules will be ignored.

  • IgnoreNormalization(): This decorator has to be applied above handler declaration. It's used to do not submit handler response to any form of normalization.

Response Format

Also, package provides a way to include or not the statusCode field into responses, it can be modified only globally.

main.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { init } from 'response-normalizer';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  init(app, {
    includeStatusCode: false, 
    // ↑ This will remove 'statusCode' field from responses
  });
  await app.listen(3000);
}

Conclusion

Every suggestions / help is most welcome, enjoy package.

2.3.0

4 months ago

2.3.1

4 months ago

2.1.0

5 months ago

2.0.1

5 months ago

1.12.0

5 months ago

2.0.0

5 months ago

1.11.0

5 months ago

1.10.0

5 months ago

1.9.0

5 months ago

1.8.0

6 months ago

1.7.0

6 months ago

1.6.0

6 months ago

1.5.0

6 months ago

1.4.2

6 months ago

1.4.1

6 months ago

1.4.0

6 months ago

1.3.0

6 months ago

1.2.0

6 months ago

1.1.0

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago