0.0.3 • Published 2 years ago

nestjs-swagger-utils v0.0.3

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

NestJs Swagger Utils

--

Installation

$ npm i nestjs-swagger-utils

Usage

import { Controller, Get } from '@nestjs/common';
import { ApiResponse } from '@nestjs/swagger';
import { IHealthService } from './adapter';
import { Swagger } from 'nestjs-swagger-utils';

@Controller()
export class HealthController {
  constructor(private readonly healthService: IHealthService) {}

  @Get('/health')
  @ApiResponse(Swagger.defaultResponseJSON({ json: { "info": "App UP" }, status: 200, description: 'App UP' }))
  @ApiResponse(Swagger.defaultResponseError({ error: { "message": "App Down" }, status: 500, description: 'App Down' }))
  async getHealth(): Promise<string> {
    return this.healthService.getText();
  }
}

If you prefer to centralized swagger documentation

  • Create a Swagger class on each module, exemple

health
     ├── adapter.ts
     ├── controller.ts
     ├── module.ts
     ├── service.ts
     ├── swagger.ts
--swagger.ts
import { Swagger } from 'nestjs-swagger-utils';

export class SwagggerResponse {
  static getHealth = {
    200: Swagger.defaultResponseJSON({ json: { 'info': 'App UP' }, status: 200, description: 'App UP' }),
    500: Swagger.defaultResponseError({
      error: { 'message': 'App Down' },
      status: 500,
      description: 'App Down',
    }),
  };
}

export class SwagggerRequest {
  /** If requesters has a body.  */
}
--controller.ts
import { Controller, Get } from '@nestjs/common';
import { ApiResponse, ApiTags } from '@nestjs/swagger';

import { IHealthService } from './adapter';
import { SwagggerResponse } from './swagger';

@Controller()
@ApiTags('health')
export class HealthController {
  constructor(private readonly healthService: IHealthService) {}

  @Get('/health')
  @ApiResponse(SwagggerResponse.getHealth[200])
  @ApiResponse(SwagggerResponse.getHealth[500])
  async getHealth(): Promise<string> {
    return this.healthService.getText();
  }
}

The following is a list of all the people that have contributed to nest-boilerplate. Thanks for your contributions!

License

It is available under the MIT license. License