-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add input/output types mapping to
CLIENT_REQ_RESP_TYPES_MAP
(#579)
- Loading branch information
Showing
6 changed files
with
27,790 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"aws-sdk-js-codemod": minor | ||
--- | ||
|
||
Create input/output types mapping in CLIENT_REQ_RESP_TYPES_MAP |
89 changes: 89 additions & 0 deletions
89
scripts/generateClientTypesMap/getClientReqRespTypesMap.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import jscodeshift, { | ||
Identifier, | ||
TSFunctionType, | ||
TSQualifiedName, | ||
TSTypeReference, | ||
} from "jscodeshift"; | ||
|
||
import { getTypesSource } from "./getTypesSource"; | ||
|
||
export const getClientReqRespTypesMap = async ( | ||
clientName: string | ||
): Promise<Record<string, string>> => { | ||
const clientTypesMap = {}; | ||
|
||
const j = jscodeshift.withParser("ts"); | ||
const source = getTypesSource(j, clientName); | ||
|
||
source.find(j.ClassDeclaration, { id: { name: clientName } }).forEach((classDeclaration) => { | ||
const classMethods = j(classDeclaration).find(j.TSDeclareMethod).nodes(); | ||
|
||
classMethods.forEach((classMethod) => { | ||
if (classMethod.key.type !== "Identifier") return; | ||
if (classMethod.key.name === "constructor") return; | ||
if (classMethod.key.name.startsWith("waitFor")) return; | ||
|
||
const classMethodKeyName = (classMethod.key as Identifier).name; | ||
const commandName = classMethodKeyName.charAt(0).toUpperCase() + classMethodKeyName.slice(1); | ||
|
||
if (classMethod.params.length !== 2) return; | ||
if (classMethod.params[0].type !== "Identifier") return; | ||
if (classMethod.params[0].name !== "params") return; | ||
|
||
const params = classMethod.params[0] as Identifier; | ||
|
||
if (!params.typeAnnotation) return; | ||
if (!params.typeAnnotation.typeAnnotation) return; | ||
if (params.typeAnnotation.typeAnnotation.type !== "TSTypeReference") return; | ||
const paramsTypeRef = params.typeAnnotation!.typeAnnotation! as TSTypeReference; | ||
|
||
if (!paramsTypeRef.typeName) return; | ||
if (paramsTypeRef.typeName.type !== "TSQualifiedName") return; | ||
const paramsTypeRefName = paramsTypeRef.typeName as TSQualifiedName; | ||
|
||
if (!paramsTypeRefName.right) return; | ||
if (paramsTypeRefName.right.type !== "Identifier") return; | ||
const paramsTypeName = paramsTypeRefName.right as Identifier; | ||
const requestTypeName = paramsTypeName.name; | ||
|
||
clientTypesMap[requestTypeName] = `${commandName}CommandInput`; | ||
|
||
if (classMethod.params[1].type !== "Identifier") return; | ||
if (classMethod.params[1].name !== "callback") return; | ||
const callback = classMethod.params[1] as Identifier; | ||
|
||
if (!callback.typeAnnotation) return; | ||
if (!callback.typeAnnotation.typeAnnotation) return; | ||
if (callback.typeAnnotation.typeAnnotation.type !== "TSFunctionType") return; | ||
const callbackTypeRef = callback.typeAnnotation!.typeAnnotation! as TSFunctionType; | ||
|
||
if (!callbackTypeRef.parameters) return; | ||
if (callbackTypeRef.parameters.length !== 2) return; | ||
if (callbackTypeRef.parameters[1].type !== "Identifier") return; | ||
const responseType = callbackTypeRef.parameters[1] as Identifier; | ||
|
||
if (!responseType.typeAnnotation) return; | ||
if (responseType.typeAnnotation.type !== "TSTypeAnnotation") return; | ||
if (!responseType.typeAnnotation.typeAnnotation) return; | ||
if (responseType.typeAnnotation.typeAnnotation.type !== "TSTypeReference") return; | ||
const responseTypeRef = responseType.typeAnnotation!.typeAnnotation! as TSTypeReference; | ||
|
||
if (!responseTypeRef.typeName) return; | ||
if (responseTypeRef.typeName.type !== "TSQualifiedName") return; | ||
const responseTypeRefName = responseTypeRef.typeName as TSQualifiedName; | ||
|
||
if (!responseTypeRefName.right) return; | ||
if (responseTypeRefName.right.type !== "Identifier") return; | ||
const responseTypeName = (responseTypeRefName.right as Identifier).name; | ||
|
||
clientTypesMap[responseTypeName] = `${commandName}CommandOutput`; | ||
}); | ||
}); | ||
|
||
return Object.entries(clientTypesMap) | ||
.sort(([key1], [key2]) => key1.localeCompare(key2)) | ||
.reduce((obj, [key, value]) => { | ||
obj[key] = value; | ||
return obj; | ||
}, {}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { readFileSync } from "fs"; | ||
import { join } from "path"; | ||
import { JSCodeshift } from "jscodeshift"; | ||
import { DOCUMENT_CLIENT } from "../../src/transforms/v2-to-v3/config"; | ||
|
||
export const getTypesSource = (j: JSCodeshift, clientName: string) => { | ||
const typesPath = | ||
clientName === DOCUMENT_CLIENT | ||
? join("node_modules", "aws-sdk", "lib", "dynamodb", `document_client.d.ts`) | ||
: join("node_modules", "aws-sdk", "clients", `${clientName.toLowerCase()}.d.ts`); | ||
const relativeTypesPath = join(__dirname, "..", "..", typesPath); | ||
|
||
const typesCode = readFileSync(relativeTypesPath, "utf8"); | ||
|
||
return j(typesCode); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.