modify-openapi-operationids.js 1002 B

123456789101112131415161718192021222324252627282930313233343536
  1. import * as fs from "node:fs";
  2. async function modifyOpenAPIFile(filePath) {
  3. try {
  4. const data = await fs.promises.readFile(filePath);
  5. const openapiContent = JSON.parse(data);
  6. const paths = openapiContent.paths;
  7. for (const pathKey of Object.keys(paths)) {
  8. const pathData = paths[pathKey];
  9. for (const method of Object.keys(pathData)) {
  10. const operation = pathData[method];
  11. if (operation.tags && operation.tags.length > 0) {
  12. const tag = operation.tags[0];
  13. const operationId = operation.operationId;
  14. const toRemove = `${tag}-`;
  15. if (operationId.startsWith(toRemove)) {
  16. const newOperationId = operationId.substring(toRemove.length);
  17. operation.operationId = newOperationId;
  18. }
  19. }
  20. }
  21. }
  22. await fs.promises.writeFile(
  23. filePath,
  24. JSON.stringify(openapiContent, null, 2),
  25. );
  26. console.log("File successfully modified");
  27. } catch (err) {
  28. console.error("Error:", err);
  29. }
  30. }
  31. const filePath = "./openapi.json";
  32. modifyOpenAPIFile(filePath);