티스토리 뷰

반응형

[nestjs/swagger] swagger를 활용한 document(문서) 자동화

 

nestjs로 api 서버를 개발하다보면 문서화의 번거로움을 이뤄 다 말할 수 없다, 고민 끝에 자동화 툴인 swagger를 써보기로했는데 이게 아주아주 매력적인 라이브러리이다.

 

다음과 같이 제법 깔끔한 UI로 문서가 제공된다.

 

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

먼저 설치를 진행하자

 

$ touch ./src/utils/swagger.ts

설치가 완료된 후엔 swagger.ts 파일을 생성하자

 

import { INestApplication } from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';

/**
 * Swagger 세팅
 *
 * @param {INestApplication} app
 */
export function setupSwagger(app: INestApplication): void {
  const options = new DocumentBuilder()
    .setTitle('Gofogo API Docs')
    .setDescription('Gofogo API 정의서')
    .setVersion('1.0.0')
    .build();

  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api-docs', app, document);
}

파일을 생성 후에 swagger initialize 함수를 만들자.

 

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { setupSwagger } from './util/swagger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  setupSwagger(app);
  await app.listen(3000);
}
bootstrap();

./src/main.ts에 swagger를 등록하여주자.

그 후 실행하여 /api-docs로 이동하면 기본페이지가 뜬다.

이것으로 기본적인 설치가 완료되었다.

 

#nestjs #swagger #swaggerUI #문서화 #문서자동화 #api #api 문서화 #api 문서 자동화 #swagger 문서 자동화

반응형
댓글
반응형