We use cookies to enhance your experience on the site
CodeWorlds

Main Project: Complete Imperium API Documentation

The time has come for the grand opus - creating the complete Chronicles of the Empire! In this project, you will combine all the knowledge from this module to create a fully documented API for managing the Roman Empire.

Project Requirements

Your Chronicles of the Empire must include:

1. Swagger Configuration in main.ts

1import { NestFactory } from '@nestjs/core';
2import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
3import { ValidationPipe } from '@nestjs/common';
4import { AppModule } from './app.module';
5
6async function bootstrap() {
7  const app = await NestFactory.create(AppModule);
8
9  app.useGlobalPipes(new ValidationPipe({ whitelist: true, transform: true }));
10
11  const config = new DocumentBuilder()
12    .setTitle('Imperium Romanum API')
13    .setDescription('Complete API documentation for managing the Roman Empire')
14    .setVersion('1.0')
15    .addTag('Legiones', 'Managing legions and soldiers')
16    .addTag('Provinciae', 'Managing provinces of the Empire')
17    .addTag('Tributum', 'Tax system and treasury')
18    .addBearerAuth(
19      { type: 'http', scheme: 'bearer', bearerFormat: 'JWT' },
20      'JWT-auth',
21    )
22    .setContact('Consul Maximus', 'https://imperium.rome', 'consul@rome.gov')
23    .setLicense('Lex Romana', 'https://imperium.rome/lex')
24    .build();
25
26  const document = SwaggerModule.createDocument(app, config);
27
28  SwaggerModule.setup('api/docs', app, document, {
29    customSiteTitle: 'Chronicles of the Imperium API',
30    swaggerOptions: {
31      persistAuthorization: true,
32      filter: true,
33      showRequestDuration: true,
34    },
35  });
36
37  await app.listen(3000);
38}
39bootstrap();

2. DTOs with Full Documentation

1import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
2
3export enum LegionaryRank {
4  MILES = 'Miles',
5  OPTIO = 'Optio',
6  CENTURIO = 'Centurio',
7  TRIBUNUS = 'Tribunus',
8  LEGATUS = 'Legatus',
9}
10
11export class CreateLegionaryDto {
12  @ApiProperty({ description: 'Name of the legionary', example: 'Marcus Aurelius', minLength: 2 })
13  name: string;
14
15  @ApiProperty({ description: 'Rank', enum: LegionaryRank, example: LegionaryRank.MILES })
16  rank: LegionaryRank;
17
18  @ApiProperty({ description: 'Assigned legion', example: 'Legio X Gemina' })
19  legio: string;
20
21  @ApiPropertyOptional({ description: 'Years of experience', example: 3, minimum: 0, maximum: 40 })
22  experience?: number;
23}
24
25export class LegionaryResponseDto {
26  @ApiProperty({ description: 'Legionary ID', example: '64a1b2c3d4e5f6a7b8c9d0e1' })
27  id: string;
28
29  @ApiProperty({ description: 'Name', example: 'Marcus Aurelius' })
30  name: string;
31
32  @ApiProperty({ description: 'Rank', enum: LegionaryRank })
33  rank: LegionaryRank;
34
35  @ApiProperty({ description: 'Legion', example: 'Legio X Gemina' })
36  legio: string;
37
38  @ApiProperty({ description: 'Experience', example: 5 })
39  experience: number;
40
41  @ApiProperty({ description: 'Recruitment date' })
42  recruitedAt: Date;
43}

3. Controller with Full Documentation

1import { Controller, Get, Post, Put, Delete, Param, Body, Query, UseGuards } from '@nestjs/common';
2import { ApiTags, ApiOperation, ApiParam, ApiQuery, ApiBearerAuth,
3  ApiOkResponse, ApiCreatedResponse, ApiNotFoundResponse,
4  ApiBadRequestResponse, ApiUnauthorizedResponse } from '@nestjs/swagger';
5
6@ApiTags('Legiones')
7@Controller('legiones')
8export class LegionController {
9  @ApiOperation({ summary: 'Get all legions' })
10  @ApiOkResponse({ description: 'List of legions', type: [LegionaryResponseDto] })
11  @ApiQuery({ name: 'rank', required: false, enum: LegionaryRank })
12  @Get()
13  findAll(@Query('rank') rank?: LegionaryRank) {}
14
15  @ApiOperation({ summary: 'Get legionary by ID' })
16  @ApiParam({ name: 'id', description: 'Legionary ID' })
17  @ApiOkResponse({ type: LegionaryResponseDto })
18  @ApiNotFoundResponse({ description: 'Legionary not found' })
19  @Get(':id')
20  findOne(@Param('id') id: string) {}
21
22  @ApiBearerAuth('JWT-auth')
23  @ApiOperation({ summary: 'Recruit a legionary' })
24  @ApiCreatedResponse({ type: LegionaryResponseDto })
25  @ApiBadRequestResponse({ description: 'Invalid data' })
26  @ApiUnauthorizedResponse({ description: 'Unauthorized' })
27  @Post()
28  create(@Body() dto: CreateLegionaryDto) {}
29}

Module Summary

In this module, you learned:

  1. What OpenAPI/Swagger is - the API documentation standard
  2. Installation and configuration - DocumentBuilder and SwaggerModule
  3. Controller decorators - @ApiTags, @ApiOperation, @ApiResponse, @ApiParam, @ApiQuery
  4. DTO documentation - @ApiProperty with descriptions, examples, and validation
  5. Advanced responses - dedicated decorators for each status code
  6. Customization - personalizing Swagger UI
  7. CLI Plugin - automatic documentation generation
  8. Versioning - multiple docs for different API versions

Now your Chronicles of the Empire are complete! Every endpoint is documented like an imperial decree, and Swagger UI serves as the Forum of Annals for all developers.

Practical Exercise

Create the complete API documentation for Imperium Romanum:

Go to CodeWorlds