|
| 1 | +import jscodeshift, { |
| 2 | + Identifier, |
| 3 | + TSFunctionType, |
| 4 | + TSQualifiedName, |
| 5 | + TSTypeReference, |
| 6 | +} from "jscodeshift"; |
| 7 | + |
| 8 | +import { getTypesSource } from "./getTypesSource"; |
| 9 | + |
| 10 | +export const getClientReqRespTypesMap = async ( |
| 11 | + clientName: string |
| 12 | +): Promise<Record<string, string>> => { |
| 13 | + const clientTypesMap = {}; |
| 14 | + |
| 15 | + const j = jscodeshift.withParser("ts"); |
| 16 | + const source = getTypesSource(j, clientName); |
| 17 | + |
| 18 | + source.find(j.ClassDeclaration, { id: { name: clientName } }).forEach((classDeclaration) => { |
| 19 | + const classMethods = j(classDeclaration).find(j.TSDeclareMethod).nodes(); |
| 20 | + |
| 21 | + classMethods.forEach((classMethod) => { |
| 22 | + if (classMethod.key.type !== "Identifier") return; |
| 23 | + if (classMethod.key.name === "constructor") return; |
| 24 | + if (classMethod.key.name.startsWith("waitFor")) return; |
| 25 | + |
| 26 | + const classMethodKeyName = (classMethod.key as Identifier).name; |
| 27 | + const commandName = classMethodKeyName.charAt(0).toUpperCase() + classMethodKeyName.slice(1); |
| 28 | + |
| 29 | + if (classMethod.params.length !== 2) return; |
| 30 | + if (classMethod.params[0].type !== "Identifier") return; |
| 31 | + if (classMethod.params[0].name !== "params") return; |
| 32 | + |
| 33 | + const params = classMethod.params[0] as Identifier; |
| 34 | + |
| 35 | + if (!params.typeAnnotation) return; |
| 36 | + if (!params.typeAnnotation.typeAnnotation) return; |
| 37 | + if (params.typeAnnotation.typeAnnotation.type !== "TSTypeReference") return; |
| 38 | + const paramsTypeRef = params.typeAnnotation!.typeAnnotation! as TSTypeReference; |
| 39 | + |
| 40 | + if (!paramsTypeRef.typeName) return; |
| 41 | + if (paramsTypeRef.typeName.type !== "TSQualifiedName") return; |
| 42 | + const paramsTypeRefName = paramsTypeRef.typeName as TSQualifiedName; |
| 43 | + |
| 44 | + if (!paramsTypeRefName.right) return; |
| 45 | + if (paramsTypeRefName.right.type !== "Identifier") return; |
| 46 | + const paramsTypeName = paramsTypeRefName.right as Identifier; |
| 47 | + const requestTypeName = paramsTypeName.name; |
| 48 | + |
| 49 | + clientTypesMap[requestTypeName] = `${commandName}CommandInput`; |
| 50 | + |
| 51 | + if (classMethod.params[1].type !== "Identifier") return; |
| 52 | + if (classMethod.params[1].name !== "callback") return; |
| 53 | + const callback = classMethod.params[1] as Identifier; |
| 54 | + |
| 55 | + if (!callback.typeAnnotation) return; |
| 56 | + if (!callback.typeAnnotation.typeAnnotation) return; |
| 57 | + if (callback.typeAnnotation.typeAnnotation.type !== "TSFunctionType") return; |
| 58 | + const callbackTypeRef = callback.typeAnnotation!.typeAnnotation! as TSFunctionType; |
| 59 | + |
| 60 | + if (!callbackTypeRef.parameters) return; |
| 61 | + if (callbackTypeRef.parameters.length !== 2) return; |
| 62 | + if (callbackTypeRef.parameters[1].type !== "Identifier") return; |
| 63 | + const responseType = callbackTypeRef.parameters[1] as Identifier; |
| 64 | + |
| 65 | + if (!responseType.typeAnnotation) return; |
| 66 | + if (responseType.typeAnnotation.type !== "TSTypeAnnotation") return; |
| 67 | + if (!responseType.typeAnnotation.typeAnnotation) return; |
| 68 | + if (responseType.typeAnnotation.typeAnnotation.type !== "TSTypeReference") return; |
| 69 | + const responseTypeRef = responseType.typeAnnotation!.typeAnnotation! as TSTypeReference; |
| 70 | + |
| 71 | + if (!responseTypeRef.typeName) return; |
| 72 | + if (responseTypeRef.typeName.type !== "TSQualifiedName") return; |
| 73 | + const responseTypeRefName = responseTypeRef.typeName as TSQualifiedName; |
| 74 | + |
| 75 | + if (!responseTypeRefName.right) return; |
| 76 | + if (responseTypeRefName.right.type !== "Identifier") return; |
| 77 | + const responseTypeName = (responseTypeRefName.right as Identifier).name; |
| 78 | + |
| 79 | + clientTypesMap[responseTypeName] = `${commandName}CommandOutput`; |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + return Object.entries(clientTypesMap) |
| 84 | + .sort(([key1], [key2]) => key1.localeCompare(key2)) |
| 85 | + .reduce((obj, [key, value]) => { |
| 86 | + obj[key] = value; |
| 87 | + return obj; |
| 88 | + }, {}); |
| 89 | +}; |
0 commit comments