🛠️ Improve modify-openapi-operationids.js (#720)

This commit is contained in:
Alejandra
2024-03-14 12:27:03 +01:00
committed by GitHub
parent 557b44f0e8
commit f5764acd56

View File

@@ -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]
Object.keys(paths).forEach((pathKey) => {
const pathData = paths[pathKey];
Object.keys(pathData).forEach((method) => {
const operation = pathData[method];
if (operation.tags && operation.tags.length > 0) { if (operation.tags && operation.tags.length > 0) {
const tag = operation.tags[0]; const tag = operation.tags[0]
const operationId = operation.operationId; const operationId = operation.operationId
const toRemove = `${tag}-`; const toRemove = `${tag}-`
if (operationId.startsWith(toRemove)) { if (operationId.startsWith(toRemove)) {
const newOperationId = operationId.substring(toRemove.length); const newOperationId = operationId.substring(toRemove.length)
operation.operationId = newOperationId; operation.operationId = newOperationId
} }
} }
}); }
}); }
fs.writeFile(filePath, JSON.stringify(openapiContent, null, 2), (err) => {
if (err) throw err; await fs.promises.writeFile(
}); filePath,
}); JSON.stringify(openapiContent, null, 2),
)
console.log('File successfully modified')
} catch (err) {
console.error('Error:', err)
}
}
const filePath = './openapi.json'
modifyOpenAPIFile(filePath)