You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/07/18 17:54:40 UTC

[GitHub] [airflow] pierrejeambrun commented on a diff in pull request #25123: Generate typescript types from rest API docs

pierrejeambrun commented on code in PR #25123:
URL: https://github.com/apache/airflow/pull/25123#discussion_r923604481


##########
airflow/www/alias-rest-types.js:
##########
@@ -0,0 +1,185 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+const ts = require('typescript');
+const fs = require('fs');
+
+/* This library does three things to make openapi-typescript generation easier to use.
+ * 1. Creates capitalized exports for Paths and Operations
+ * 2. Alias Variables based either on the Path name or the Operation ID, such as:
+ *      export type ListProjectsVariables = operations['listProjects']['parameters']['query'];
+ * 3. Aliases the returned data types, such as:
+ *      export type ConnectionArgument = components['schemas']['ConnectionArgument'];
+ */
+
+/* Finds all words, capitalizes them, and removes all other characters. */
+const toPascalCase = (str) => (
+  (str.match(/[a-zA-Z0-9]+/g) || [])
+    .map((w) => `${w.charAt(0).toUpperCase()}${w.slice(1)}`)
+    .join('')
+);
+
+/* Adds a prefix to a type prop as necessary.
+ * ('', 'components') => 'components'
+ */
+const prefixPath = (rootPrefix, prop) => (rootPrefix ? `${rootPrefix}['${prop}']` : prop);
+
+// Recursively find child nodes by name.
+const findNode = (node, ...names) => {
+  if (!node || names.length === 0) return node;
+
+  const children = node.members || node.type.members;
+
+  if (!children) {
+    return undefined;
+  }
+
+  const child = children.find((c) => c.name?.text === names[0]);
+
+  return findNode(child, ...names.slice(1));
+};
+
+// Generate Variable Type Aliases for a given path or operation
+const generateVariableAliases = (node, operationPath, operationName) => {
+  const variableTypes = [];
+  const hasPath = !!findNode(node, 'parameters', 'path');
+  const hasQuery = !!findNode(node, 'parameters', 'query');
+  const hasBody = !!findNode(node, 'requestBody', 'content', 'application/json');
+
+  if (hasPath) variableTypes.push(`${operationPath}['parameters']['path']`);
+  if (hasQuery) variableTypes.push(`${operationPath}['parameters']['query']`);
+  if (hasBody) variableTypes.push(`${operationPath}['requestBody']['content']['application/json']`);
+
+  if (variableTypes.length === 0) return '';
+  const typeName = `${toPascalCase(operationName)}Variables`;
+  return [typeName, `export type ${typeName} = ${variableTypes.join(' & ')};`];
+};
+
+// Generate Type Aliases
+// This is recurrsivenly called to generate types for Linege's external dependencies.

Review Comment:
   Typo `recurrsivenly`



##########
airflow/www/alias-rest-types.js:
##########
@@ -0,0 +1,185 @@
+/*!
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+const ts = require('typescript');
+const fs = require('fs');
+
+/* This library does three things to make openapi-typescript generation easier to use.
+ * 1. Creates capitalized exports for Paths and Operations
+ * 2. Alias Variables based either on the Path name or the Operation ID, such as:
+ *      export type ListProjectsVariables = operations['listProjects']['parameters']['query'];
+ * 3. Aliases the returned data types, such as:
+ *      export type ConnectionArgument = components['schemas']['ConnectionArgument'];
+ */
+
+/* Finds all words, capitalizes them, and removes all other characters. */
+const toPascalCase = (str) => (
+  (str.match(/[a-zA-Z0-9]+/g) || [])
+    .map((w) => `${w.charAt(0).toUpperCase()}${w.slice(1)}`)
+    .join('')
+);
+
+/* Adds a prefix to a type prop as necessary.
+ * ('', 'components') => 'components'
+ */
+const prefixPath = (rootPrefix, prop) => (rootPrefix ? `${rootPrefix}['${prop}']` : prop);
+
+// Recursively find child nodes by name.
+const findNode = (node, ...names) => {
+  if (!node || names.length === 0) return node;
+
+  const children = node.members || node.type.members;
+
+  if (!children) {
+    return undefined;
+  }
+
+  const child = children.find((c) => c.name?.text === names[0]);
+
+  return findNode(child, ...names.slice(1));
+};
+
+// Generate Variable Type Aliases for a given path or operation
+const generateVariableAliases = (node, operationPath, operationName) => {
+  const variableTypes = [];
+  const hasPath = !!findNode(node, 'parameters', 'path');
+  const hasQuery = !!findNode(node, 'parameters', 'query');
+  const hasBody = !!findNode(node, 'requestBody', 'content', 'application/json');
+
+  if (hasPath) variableTypes.push(`${operationPath}['parameters']['path']`);
+  if (hasQuery) variableTypes.push(`${operationPath}['parameters']['query']`);
+  if (hasBody) variableTypes.push(`${operationPath}['requestBody']['content']['application/json']`);
+
+  if (variableTypes.length === 0) return '';
+  const typeName = `${toPascalCase(operationName)}Variables`;
+  return [typeName, `export type ${typeName} = ${variableTypes.join(' & ')};`];
+};
+
+// Generate Type Aliases
+// This is recurrsivenly called to generate types for Linege's external dependencies.
+const generateAliases = (rootNode, writeText, prefix = '') => {
+  // Loop through the root AST nodes of the file
+  ts.forEachChild(rootNode, (node) => {
+    // Response Data Types
+    if (ts.isInterfaceDeclaration(node) && node.name?.text === 'components') {
+      const schemaMemberNames = findNode(node, 'schemas').type.members.map((n) => n.name?.text);
+
+      const types = schemaMemberNames.map((n) => [
+        `${n}`,
+        `export type ${n} = ${prefixPath(prefix, 'components')}['schemas']['${n}'];`,
+      ]);
+      if (types.length) {
+        writeText.push(['comment', `Types for returned data ${prefix}`]);
+        writeText.push(...types);
+      }
+    }
+
+    /* Fetch Query Variable Types
+     * These are the variables that are passed to the fetch function, comprising variables that
+     * may go into the path (`/namespaces/{namespace}/datasets/{dataset}/metrics`)
+     * and those that are passed in as query parameters.
+     */
+
+    // Paths referencing an operation are skipped
+    if (node.name?.text === 'paths') {
+      if (!prefix) {
+        writeText.push(['comment', 'Alias paths to PascalCase.']);
+        writeText.push(['Paths', 'export type Paths = paths;']);
+      }
+
+      const types = [];
+
+      (node.members || node.type.members).forEach((path) => {
+        const methodNames = path.type.members.map((m) => m.name.text);
+        const methodTypes = methodNames.map((m) => (
+          generateVariableAliases(
+            findNode(path, m),
+            `${prefixPath(prefix, 'paths')}['${path.name?.text}']['${m}']`,
+            `${path.name.text}${toPascalCase(m)}`,
+          )));
+        types.push(...methodTypes.filter((m) => !!m));
+      });
+
+      if (types.length) {
+        writeText.push(['comment', `Types for path operation variables ${prefix}`]);
+        writeText.push(...types);
+      }
+    }
+
+    // operationIds are defined
+    if (node.name?.text === 'operations') {
+      if (!prefix) {
+        writeText.push(['comment', 'Alias operations to PascalCase.']);
+        writeText.push(['Operations', 'export type Operations = operations;']);
+      }
+
+      const types = (node.members || node.type.members).map((operation) => (
+        generateVariableAliases(
+          operation,
+          `${prefixPath(prefix, 'operations')}['${operation.name.text}']`,
+          operation.name.text,
+        )));
+      if (types.length) {
+        writeText.push(['comment', `Types for operation variables ${prefix}`]);
+        writeText.push(...types);
+      }
+    }
+
+    // recursively call this for any externals
+    if (ts.isInterfaceDeclaration(node) && node.name?.text === 'external') {
+      node.members.forEach((external) => {
+        generateAliases(external.type, writeText, `external['${external.name.text}']`);
+      });
+    }
+  });
+};
+
+function generate(file) {

Review Comment:
   Should we also push the licence directly, or are we counting on the pre-commit hook to do that for us ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org