Swagger - boostcamp-2020/Project03-A-TOTP GitHub Wiki
๋ฐฐ๊ฒฝ
๊ฐ๋ฐ์ด ๋ฐ๋น ์ api ๋ฌธ์ ์์ฑ์ ์ํํ ํ๊ฒ ๋์๊ณ , ์ด์ ๋ค๋ฅธ api์ ์ ๋ณด๋ฅผ ๋ณด๋ ค๋ฉด ์ฝ๋๋ฅผ ๋ณด๋ฉด์ ํ์ธํด์ผ ํ๋ ์ํฉ์ด ๋์๋ค.
์ด๋ฐ ๋ฌธ์ ๋๋ฌธ์ api ๋ฌธ์๋ฅผ ๋ ํธํ๊ฒ ์์ฑํ ์ ์๋๋ก Swagger๋ฅผ ์ฌ์ฉํด ๋ณด๊ธฐ๋ก ํ๋ค.
์ค์
์ฃผ์์ผ๋ก api๋ฌธ์๋ฅผ ์์ฑํ๋ฉด Swagger๊ฐ ํ์ฑํด์ ๋ณด์ฌ์ฃผ๋ ๋ฐฉ์์ธ ๊ฒ ๊ฐ๋ค.
ํ์ํ ํจํค์ง ์ค์น
npm i swagger-ui-express swagger-jsdoc
ํ๊ฒฝ์ค์
// config/swagger.js
const swaggerDefinition = {
info: {
title: 'TOTP App',
version: '1.0.0',
description: 'TOTP App API',
},
host: 'https://dadaikseon.com',
basePath: '/api',
servers: ['http://localhost:3000'],
};
// app.js
const swaggerUi = require('swagger-ui-express');
const swaggerJSDoc = require('swagger-jsdoc');
const swaggerDefinition = require('@config/swagger');
const swaggerOptions = {
swaggerDefinition,
apis: ['./routes/**/*.js'],
};
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerJSDoc(swaggerOptions)));
์ด๋ ๊ฒ ์ค์ ํ๋ฉด routes ํด๋ ๋ด์ ์๋ ๋ชจ๋ jsํ์ผ์์ ์ฃผ์์ ํ์ฑํ๋ ๊ฒ ๊ฐ๊ณ ,
localhost:3000/api-docs
๋ก ์ ์ํ๋ฉด Swagger๊ฐ ๋ง๋ api ๋ฌธ์๋ฅผ ๋ณผ ์ ์๋ค.
API ๋ฌธ์ ์์ฑ
jsdoc ๋ฐฉ์์ ์ฃผ์์ผ๋ก ์์ฑํ๋ฉฐ yaml ํฌ๋งท์ผ๋ก ์์ฑํ๋ค.
/**
* @swagger
* /auth/dup-id:
* post:
* tags:
* - auth
* description: ์์ด๋ ์ค๋ณต ์ฒดํฌ๋ฅผ ํ ํ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํํ๋ค
* consumes:
* - "application/json"
* produces:
* - "application/json"
* parameters:
* - name: id
* in: body
* description: ์ค๋ณต์ฒดํฌํ ์์ด๋
* type: string
* required: true
* responses:
* 200:
* description: ์ค๋ณต์ฒดํฌ ๊ฒฐ๊ณผ
* schema:
* type: object
* properties:
* result:
* type: boolean
* 401:
* description: csrf์๋ฌ
* 500:
* description: ๊ธฐํ ์ค๋ฅ
*/
router.post('/dup-id', authController.dupId);
์์ฑํ๋ฉด ์ฃผ์์ด ๊ต์ฅํ ๊ธธ์ด์ง๋ค. ์คํค๋ง๋ฅผ ๋ฏธ๋ฆฌ ์ ์ํด๋์ ์๋ ์๋ ๊ฒ ๊ฐ์๋ฐ ์ข ๋ ์์๋ด์ผ ํ ๋ฏ