🛠️ Improve modify-openapi-operationids.js (#720)
This commit is contained in:
@@ -1,29 +1,36 @@
|
|||||||
import * as fs from "fs";
|
import * as fs from 'fs'
|
||||||
|
|
||||||
const filePath = "./openapi.json";
|
async function modifyOpenAPIFile(filePath) {
|
||||||
|
try {
|
||||||
|
const data = await fs.promises.readFile(filePath)
|
||||||
|
const openapiContent = JSON.parse(data)
|
||||||
|
|
||||||
fs.readFile(filePath, (err, data) => {
|
const paths = openapiContent.paths
|
||||||
const openapiContent = JSON.parse(data);
|
for (const pathKey of Object.keys(paths)) {
|
||||||
if (err) throw err;
|
const pathData = paths[pathKey]
|
||||||
|
for (const method of Object.keys(pathData)) {
|
||||||
const paths = openapiContent.paths;
|
const operation = pathData[method]
|
||||||
|
if (operation.tags && operation.tags.length > 0) {
|
||||||
Object.keys(paths).forEach((pathKey) => {
|
const tag = operation.tags[0]
|
||||||
const pathData = paths[pathKey];
|
const operationId = operation.operationId
|
||||||
Object.keys(pathData).forEach((method) => {
|
const toRemove = `${tag}-`
|
||||||
const operation = pathData[method];
|
if (operationId.startsWith(toRemove)) {
|
||||||
if (operation.tags && operation.tags.length > 0) {
|
const newOperationId = operationId.substring(toRemove.length)
|
||||||
const tag = operation.tags[0];
|
operation.operationId = newOperationId
|
||||||
const operationId = operation.operationId;
|
}
|
||||||
const toRemove = `${tag}-`;
|
|
||||||
if (operationId.startsWith(toRemove)) {
|
|
||||||
const newOperationId = operationId.substring(toRemove.length);
|
|
||||||
operation.operationId = newOperationId;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
});
|
|
||||||
fs.writeFile(filePath, JSON.stringify(openapiContent, null, 2), (err) => {
|
await fs.promises.writeFile(
|
||||||
if (err) throw err;
|
filePath,
|
||||||
});
|
JSON.stringify(openapiContent, null, 2),
|
||||||
});
|
)
|
||||||
|
console.log('File successfully modified')
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error:', err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const filePath = './openapi.json'
|
||||||
|
modifyOpenAPIFile(filePath)
|
||||||
|
Reference in New Issue
Block a user