์ค์น ๋ฐ ๊ตฌ์ฑ
-
ํด๋น ๋ฌธ์๋ 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