NestJs Swagger

까망거북·2024년 10월 20일
post-thumbnail

토이프로젝트를 진행중 백엔드 API문서를 작성할 필요성이 생겼다.

참조문서

1. 설치

npm i @nestjs/swagger

2. 적용

  • main.ts 수정

import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

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

  await app.listen(process.env.PORT ?? 3000);
}
bootstrap();
  • dto 수정
import { ApiProperty } from '@nestjs/swagger';

export class SignInDto {
  @ApiProperty({
    description: 'user Id',
    minLength: 5,
    maxLength: 20,
    default: '',
  })
  username: string;
  @ApiProperty({
    description: 'user password',
    minLength: 5,
    maxLength: 20,
    default: '',
  })
  password: string;
}

3. 확인

npm run start

0개의 댓글