]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
🛠️ Improve Node.js script in docs to generate TypeScript clients (#11293)
authorAlejandra <90076947+alejsdev@users.noreply.github.com>
Thu, 14 Mar 2024 11:40:05 +0000 (12:40 +0100)
committerGitHub <noreply@github.com>
Thu, 14 Mar 2024 11:40:05 +0000 (12:40 +0100)
docs_src/generate_clients/tutorial004.js

index 18dc38267bcde81a09be18fa93779555683d90ab..fa222ba6cc88460795b961dd990d0bc0e5c26fee 100644 (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 openapiContent = JSON.parse(data);
-  if (err) throw err;
-
-  const paths = openapiContent.paths;
-
-  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) {
-        const tag = operation.tags[0];
-        const operationId = operation.operationId;
-        const toRemove = `${tag}-`;
-        if (operationId.startsWith(toRemove)) {
-          const newOperationId = operationId.substring(toRemove.length);
-          operation.operationId = newOperationId;
+    const paths = openapiContent.paths
+    for (const pathKey of Object.keys(paths)) {
+      const pathData = paths[pathKey]
+      for (const method of Object.keys(pathData)) {
+        const operation = pathData[method]
+        if (operation.tags && operation.tags.length > 0) {
+          const tag = operation.tags[0]
+          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) => {
-    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)