설치 및 구성
-
해당 문서는 SON50에 적용된 openapi-generator를 기준으로 작성하였습니다.
-
OpenAPI Generator은 Swagger UI의 Json, yaml 문서를 자동으로 Front-End 코드로 변환하는 작업입니다.
설치
Java jdk가 미리 설치된 상태에서 진행하여야 한다. Microsoft Build of OpenJDK 다운로드
설치 후, java 환경 변수 세팅도 필요하다. [Mac OS] 맥북 JAVA 환경 변수 설정하는 방법
- openAPI generator 설치
npm install @openapitools/openapi-generator-cli -D
- nodejs 스크립트 작성 (
update_api.js)
import { execSync } from 'child_process';
import fs from 'fs';
const swaggerUrl = {
maestro: 'http://10.78.0.151:12190/v3/api-docs/maestro',
}; // 환경 변수 가져오기
const saveJsonFile = {
maestro: './maestro.json',
};
const outPath = {
maestro: '../src/api/maestro',
}; // 변환 위치
const configPath = './openapi-generator-config.json';
// 명령 실행
Object.keys(swaggerUrl).forEach(async key => {
await fetch(swaggerUrl[key], { method: 'get' })
.then(res => res.json())
.then(json => {
Object.keys(json.paths).forEach(path => {
Object.keys(json.paths[path]).forEach(method => {
if (json.paths[path][method].operationId) {
delete json.paths[path][method].operationId;
}
});
});
// 새로운 JSON 파일로 저장
fs.writeFile(saveJsonFile[key], JSON.stringify(json, null, 2), 'utf8', () => {
execSync(
`openapi-generator-cli generate -i ${saveJsonFile[key]} \
-g typescript-axios -c ${configPath} \
-o ${outPath[key]} --skip-validate-spec`,
{
stdio: 'inherit',
},
);
if (fs.existsSync(saveJsonFile[key])) {
fs.unlinkSync(saveJsonFile[key]);
}
});
});
});
- config 설정 (
openapi-generator-config.json)
{
"modelPackage": "types", // 생성된 타입 경로
"apiPackage": "api", // 생성된 API 클래스 경로
"supportsES6": true, // ES6 형식
"withSeparateModelsAndApi": true // 모델, API를 분리하여 생성
"generateOperationIdBasedOnUrl": true // URL로 함수명 생성
}
- package.json 에 스크립트 작성 및 실행
{
"scripts": {
"updateApi": "cd ./swagger && node ./update_api.js"
}
}
npm run updateApi