0.0.40 • Published 2 years ago

nestjs-consumes-produces v0.0.40

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Description

API Endpoint Consume and Produces module for Nest.

NOTE:Development is currently in process

Installation

$ npm i nestjs-consumes-produces

Usage

Once the installation process is complete, we can import the NestjsConsumesProducesModule into the root AppModule.

import { Module } from '@nestjs/common';
import { NestjsConsumesProducesModule } from "nestjs-consumes-produces";

@Module({
  imports: [
   NestjsConsumesProducesModule.forRoot()
  ],
})
export class AppModule {}

Then we can import Guards to our app. You can use one of them.

import { Module } from '@nestjs/common';
import { NestjsConsumesProducesModule,
        ConsumesContentTypeGuard,
        ProducesContentTypeGuard } from "nestjs-consumes-produces";

@Module({
  imports: [
   NestjsConsumesProducesModule.forRoot()
  ],
  providers: [
  {
      provide:APP_GUARD,
      useClass:ConsumesContentTypeGuard
    },
    {
      provide:APP_GUARD,
      useClass:ProducesContentTypeGuard
    },
  ]
})
export class AppModule {}

Once this is done,You can protect your end point by Content-Type and Accept header

import { Controller, Post,Put } from '@nestjs/common';
import { ApiConsumes, ApiProduces } from "nestjs-consumes-produces";



@Controller('cats')
export class CatsController {
  @Post()
  @ApiConsumes("application/json","application/xml")
  @ApiProduces("application/json")
  create(){
    return 'This action returns all cats';
  }

  @Put()
  @ApiConsumes("application/json")
  @ApiProduces("*/*")
  update(){
    return 'This action returns all cats';
  }
}

Also You can change default values and add extra conditions

import { Module } from '@nestjs/common';
import { NestjsConsumesProducesModule,
        ConsumesContentTypeGuard,
        ProducesContentTypeGuard,
        ContentTypes } from "nestjs-consumes-produces";

@Injectable()
export class CustomNestjsConsumesProducesService implements INestjsConsumesProducesService{
  isConsumesExtraConditionOk(expected: ContentTypes[], received: ContentTypes, request: Request): boolean {
    return true
  }
  isProducesExtraConditionOk(expected: ContentTypes[], received: ContentTypes[]): boolean {
      return  true
  }


  getConsumesErrorText(expected: ContentTypes[], received: ContentTypes): string {
    return "Custom  text";
  }

  getHttpCode(): HttpStatus {
    return HttpStatus.BAD_REQUEST;
  }

  getProducesErrorText(expected: ContentTypes[], received: ContentTypes[]): string {
    return "Custom  text";
  }

  getTitle(): string {
    return "Custom Title";
  }
}


@Module({
  imports: [
    NestjsConsumesProducesModule.forRootAsync({useClass:NestjsConsumesProducesService}),
  ],
  providers: [
  {
      provide:APP_GUARD,
      useClass:ConsumesContentTypeGuard
    },
    {
      provide:APP_GUARD,
      useClass:ProducesContentTypeGuard
    },
  ]
})
export class AppModule {}

License

nestjs-consumes-produces is MIT licensed.

0.0.40

2 years ago

0.0.39

2 years ago

0.0.38

2 years ago

0.0.37

2 years ago

0.0.36

2 years ago

0.0.35

2 years ago

0.0.34

2 years ago

0.0.33

2 years ago

0.0.32

2 years ago

0.0.31

2 years ago

0.0.30

2 years ago

0.0.29

2 years ago

0.0.28

2 years ago

0.0.27

2 years ago

0.0.26

2 years ago

0.0.25

2 years ago

0.0.24

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago