Home Swagger(OpenAPI)
Post
X

Swagger(OpenAPI)

Swagger

Swagger는 OpenAPI Specification(OAS)를 기반으로 동작하는 REST API 문서화 도구입니다.

NestJS에서는 @nestjs/swagger 패키지를 통해 Swagger를 쉽게 설정하고 사용 가능합니다.

  • 설치

    1
    
    npm install --save @nestjs/swagger swagger-ui-express
    

사용하는 이유

  • API 문서를 자동 생성하여 생산성과 협업 효율 증가

  • 요청(Request), 응답(Response) 구조를 명확하게 정의

  • Swagger UI로 바로 API 테스트 가능

  • DTO와 데코레이터 기반으로 유지보수 용이


설정

1
2
3
4
5
6
7
8
9
10
11
12
// main.ts
import { SwaggerModule, DocumentBuilder } from "@nestjs/swagger";

const config = new DocumentBuilder()
  .setTitle("My API")
  .setDescription("API 문서 설명")
  .setVersion("1.0")
  .addCookieAuth("access_token") // JWT 쿠키 인증 예시
  .build();

const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup("api-docs", app, document);
이 기사는 저작권자의 CC BY 4.0 라이센스를 따릅니다.