Socket
Socket
Sign inDemoInstall

@nestjs/swagger

Package Overview
Dependencies
Maintainers
0
Versions
204
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@nestjs/swagger

Nest - modern, fast, powerful node.js web framework (@swagger)


Version published
Weekly downloads
1.8M
increased by9.11%
Maintainers
0
Weekly downloads
 
Created

What is @nestjs/swagger?

The @nestjs/swagger package is used to create documentation for NestJS applications using the Swagger UI. It provides decorators and functions to define API endpoints, their expected request and response structures, and other metadata that can be used to generate interactive API documentation.

What are @nestjs/swagger's main functionalities?

API Documentation Setup

This code sets up Swagger documentation for a NestJS application. It uses the DocumentBuilder to configure the title, description, version, and other metadata for the API documentation. The SwaggerModule.createDocument function generates the Swagger specification, and SwaggerModule.setup mounts the documentation at the specified path.

const options = new DocumentBuilder()
  .setTitle('Cats example')
  .setDescription('The cats API description')
  .setVersion('1.0')
  .addTag('cats')
  .build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api', app, document);

Decorating Controllers and Endpoints

This code demonstrates how to use decorators to add metadata to a controller and its endpoints. The @ApiTags decorator assigns a tag to all endpoints in the controller, while @ApiOperation and @ApiResponse provide additional details about individual endpoints, such as the summary and expected response status and description.

@ApiTags('cats')
@Controller('cats')
export class CatsController {
  @Get()
  @ApiOperation({ summary: 'Get all cats' })
  @ApiResponse({ status: 200, description: 'Return all cats.' })
  findAll(): Cat[] {
    // logic to return all cats
  }
}

Defining DTOs (Data Transfer Objects)

This code snippet shows how to use the @ApiProperty decorator to define the properties of a DTO. This information is used by Swagger to generate accurate documentation for the expected request body when creating a new cat in the system.

export class CreateCatDto {
  @ApiProperty({ example: 'Whiskers', description: 'The name of the cat' })
  name: string;

  @ApiProperty({ example: 3, description: 'The age of the cat' })
  age: number;

  @ApiProperty({ example: 'Maine Coon', description: 'The breed of the cat' })
  breed: string;
}

Other packages similar to @nestjs/swagger

FAQs

Package last updated on 01 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc