Nestjs Swagger Docs With Examples

2023-06-01

NestJS has a great integration with OpenAPI/Swagger, leveraging decorators to add request and response information to endpoints. For more information on how to use it: https://docs.nestjs.com/openapi/introduction. A simple response with example could look like this: import { ApiOperation, ApiProperty, ApiResponse } from "@nestjs/swagger"; import { Controller, Get, HttpStatus, NotFoundException, Param, } from "@nestjs/common"; class Cat { @ApiProperty({ example: "Ada" }) name: string; @ApiProperty({ example: 10 }) age: number; @ApiProperty({ example: true }) adoptable: boolean; @ApiProperty({ example: false }) isAdopted: boolean; } const catStore: Map<string,Cat> = new Map<string,Cat>(); catStore["Ada"] = { name: "Ada", age: 10, adoptable: true, isAdopted: false } @Controller({ path: "/cat", }) export class CatController { @Get("/:id") @ApiOperation({ summary: "My example get cat by id endpoint" }) @ApiResponse({ status: HttpStatus.

Continue reading 