# nestjs-consumes-produces

> Accept and Content-Type headers check on NestJS API

Latest version **0.0.40** (published 2022-03-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-consumes-produces
pnpm add nestjs-consumes-produces
yarn add nestjs-consumes-produces
bun add nestjs-consumes-produces
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.40 |
| Published | 2022-03-09 |
| First published | 2022-03-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 28.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | hakancoruhh |
| Maintainers | hakancoruhh |
| Keywords | nestjs, consumes, produces, accept, content-type, api |

## Links

- npm: https://www.npmjs.com/package/nestjs-consumes-produces
- Repository: https://github.com/hakancoruhh/nestjs-consumes-produces
- Homepage: https://github.com/hakancoruhh/nestjs-consumes-produces#readme
- Issues: https://github.com/hakancoruhh/nestjs-consumes-produces/issues
- npm.io page: https://npm.io/package/nestjs-consumes-produces

## Recent versions

- 0.0.40 (latest) — 2022-03-09
- 0.0.37 (next) — 2022-03-09
- 0.0.39 — 2022-03-09
- 0.0.38 — 2022-03-09
- 0.0.36 — 2022-03-09
- 0.0.35 — 2022-03-09
- 0.0.34 — 2022-03-09
- 0.0.33 — 2022-03-08
- 0.0.32 — 2022-03-08
- 0.0.31 — 2022-03-08
- 0.0.30 — 2022-03-08
- 0.0.29 — 2022-03-08
- 0.0.28 — 2022-03-08
- 0.0.27 — 2022-03-08
- 0.0.26 — 2022-03-08
- … 24 more at https://npm.io/package/nestjs-consumes-produces/versions

## README

## Description

API Endpoint Consume and Produces module for [Nest](https://github.com/nestjs/nest).

NOTE:Development is currently  in process

## Installation

```bash
$ npm i nestjs-consumes-produces
```

## Usage

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

```typescript
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. 

```typescript
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

```typescript
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

```typescript
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](./LICENSE).

---
_Source: https://npm.io/package/nestjs-consumes-produces · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
