Documenting Endpoints and Auth

Tags, Operations, Responses

src/products/products.controller.ts

import { Body, Controller, Get, Param, ParseIntPipe, Post } from '@nestjs/common';
import {
  ApiBearerAuth, ApiCreatedResponse, ApiForbiddenResponse, ApiNotFoundResponse,
  ApiOkResponse, ApiOperation, ApiTags,
} from '@nestjs/swagger';
import { ProductDto } from './dto/product.dto';
import { CreateProductDto } from './dto/create-product.dto';

@ApiTags('products')
@Controller('products')
export class ProductsController {
  constructor(private readonly products: ProductsService) {}

  @Get(':id')
  @ApiOperation({ summary: 'Get one product' })
  @ApiOkResponse({ type: ProductDto })
  @ApiNotFoundResponse({ description: 'Product does not exist' })
  findOne(@Param('id', ParseIntPipe) id: number) {
    return this.products.findOne(id);
  }

  @Post()
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Create a product (admin or seller)' })
  @ApiCreatedResponse({ type: ProductDto })
  @ApiForbiddenResponse({ description: 'Customer role cannot create products' })
  create(@Body() dto: CreateProductDto) {
    return this.products.create(dto);
  }
}

Tip: Generate a typed frontend client from the spec (openapi-typescript or orval) so the Next.js storefront gets compile errors when the API changes. See the Next.js material.