Skip to content

Commit

Permalink
Remove type assertions while generating client types map (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Jul 28, 2024
1 parent 3cbcf56 commit 3411083
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 81 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-bikes-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": minor
---

Remove type assertions while generating client types map
29 changes: 12 additions & 17 deletions scripts/generateClientTypesMap/getClientReqRespTypesMap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import jscodeshift, {
type Identifier,
type TSFunctionType,
type TSQualifiedName,
type TSTypeReference,
} from "jscodeshift";
import jscodeshift from "jscodeshift";

import { getTypesSource } from "./getTypesSource";

Expand All @@ -23,58 +18,58 @@ export const getClientReqRespTypesMap = async (
if (classMethod.key.name === "constructor") return;
if (classMethod.key.name.startsWith("waitFor")) return;

const classMethodKeyName = (classMethod.key as Identifier).name;
const classMethodKeyName = classMethod.key.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;
const params = classMethod.params[0];

if (!params.typeAnnotation) return;
if (!params.typeAnnotation.typeAnnotation) return;
if (params.typeAnnotation.typeAnnotation.type !== "TSTypeReference") return;
const paramsTypeRef = params.typeAnnotation.typeAnnotation as TSTypeReference;
const paramsTypeRef = params.typeAnnotation.typeAnnotation;

if (!paramsTypeRef.typeName) return;
if (paramsTypeRef.typeName.type !== "TSQualifiedName") return;
const paramsTypeRefName = paramsTypeRef.typeName as TSQualifiedName;
const paramsTypeRefName = paramsTypeRef.typeName;

if (!paramsTypeRefName.right) return;
if (paramsTypeRefName.right.type !== "Identifier") return;
const paramsTypeName = paramsTypeRefName.right as Identifier;
const paramsTypeName = paramsTypeRefName.right;
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;
const callback = classMethod.params[1];

if (!callback.typeAnnotation) return;
if (!callback.typeAnnotation.typeAnnotation) return;
if (callback.typeAnnotation.typeAnnotation.type !== "TSFunctionType") return;
const callbackTypeRef = callback.typeAnnotation.typeAnnotation as TSFunctionType;
const callbackTypeRef = callback.typeAnnotation.typeAnnotation;

if (!callbackTypeRef.parameters) return;
if (callbackTypeRef.parameters.length !== 2) return;
if (callbackTypeRef.parameters[1].type !== "Identifier") return;
const responseType = callbackTypeRef.parameters[1] as Identifier;
const responseType = callbackTypeRef.parameters[1];

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;
const responseTypeRef = responseType.typeAnnotation.typeAnnotation;

if (!responseTypeRef.typeName) return;
if (responseTypeRef.typeName.type !== "TSQualifiedName") return;
const responseTypeRefName = responseTypeRef.typeName as TSQualifiedName;
const responseTypeRefName = responseTypeRef.typeName;

if (!responseTypeRefName.right) return;
if (responseTypeRefName.right.type !== "Identifier") return;
const responseTypeName = (responseTypeRefName.right as Identifier).name;
const responseTypeName = responseTypeRefName.right.name;

clientTypesMap[responseTypeName] = `${commandName}CommandOutput`;
});
Expand Down
122 changes: 58 additions & 64 deletions scripts/generateClientTypesMap/getClientTypesMap.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import jscodeshift, {
type Identifier,
type TSArrayType,
type TSTypeLiteral,
type TSTypeReference,
} from "jscodeshift";
import jscodeshift from "jscodeshift";

import { CLIENT_NAMES_MAP, DOCUMENT_CLIENT } from "../../src/transforms/v2-to-v3/config";
import { getClientTypesMapWithKeysRemovedFromValues } from "./getClientTypesMapWithKeysRemovedFromValues";
Expand Down Expand Up @@ -39,20 +34,21 @@ export const getClientTypesMap = async (clientName: string): Promise<Record<stri
});
}

tsTypes
.filter((tsType) => tsType.typeAnnotation.type === "TSTypeReference")
.forEach((tsType) => {
const name = tsType.id.name;
const typeName = ((tsType.typeAnnotation as TSTypeReference).typeName as Identifier).name;
if (typeName === "Date") {
clientTypesMap[name] = typeName;
} else if (typeName === "EventStream") {
// Exception for SelectObjectContentEventStream
clientTypesMap[name] = "AsyncIterable<KEY>";
} else {
console.log("TSTypeReference with unsupported type:", name, typeName);
}
});
tsTypes.forEach((tsType) => {
if (tsType.typeAnnotation.type !== "TSTypeReference") return;
const typeAnnotationName = tsType.typeAnnotation.typeName;
if (typeAnnotationName.type !== "Identifier") return;
const name = tsType.id.name;
const typeName = typeAnnotationName.name;
if (typeName === "Date") {
clientTypesMap[name] = typeName;
} else if (typeName === "EventStream") {
// Exception for SelectObjectContentEventStream
clientTypesMap[name] = "AsyncIterable<KEY>";
} else {
console.log("TSTypeReference with unsupported type:", name, typeName);
}
});

tsTypes
.filter((tsType) => tsType.typeAnnotation.type === "TSUnionType")
Expand All @@ -63,62 +59,60 @@ export const getClientTypesMap = async (clientName: string): Promise<Record<stri
}
});

tsTypes
.filter((tsType) => tsType.typeAnnotation.type === "TSArrayType")
.forEach((tsType) => {
const name = tsType.id.name;
const elementType = (tsType.typeAnnotation as TSArrayType).elementType;
if (elementType.type === "TSTypeReference") {
const typeName = elementType.typeName;
if (typeName.type === "Identifier") {
if (clientTypesMap[typeName.name]) {
clientTypesMap[name] = `Array<${clientTypesMap[typeName.name]}>`;
} else {
// Assume it's an interface which would be available in v3.
clientTypesMap[name] = `Array<${typeName.name}>`;
}
tsTypes.forEach((tsType) => {
if (tsType.typeAnnotation.type !== "TSArrayType") return;
const name = tsType.id.name;
const elementType = tsType.typeAnnotation.elementType;
if (elementType.type === "TSTypeReference") {
const typeName = elementType.typeName;
if (typeName.type === "Identifier") {
if (clientTypesMap[typeName.name]) {
clientTypesMap[name] = `Array<${clientTypesMap[typeName.name]}>`;
} else {
console.log("TSArrayType TSTypeReference without Identifier type:", name);
// Assume it's an interface which would be available in v3.
clientTypesMap[name] = `Array<${typeName.name}>`;
}
} else if (Object.keys(ElementTypeToNativeTypeMap).includes(elementType.type)) {
clientTypesMap[name] = `Array<${ElementTypeToNativeTypeMap[elementType.type]}>`;
} else {
console.log("TSArrayType with unsupported elemental type:", name);
console.log("TSArrayType TSTypeReference without Identifier type:", name);
}
});
} else if (Object.keys(ElementTypeToNativeTypeMap).includes(elementType.type)) {
clientTypesMap[name] = `Array<${ElementTypeToNativeTypeMap[elementType.type]}>`;
} else {
console.log("TSArrayType with unsupported elemental type:", name);
}
});

tsTypes
.filter((tsType) => tsType.typeAnnotation.type === "TSTypeLiteral")
.forEach((tsType) => {
const name = tsType.id.name;
const member = (tsType.typeAnnotation as TSTypeLiteral).members[0];
if (member.type === "TSIndexSignature") {
if (member.typeAnnotation) {
if (member.typeAnnotation.typeAnnotation) {
const typeAnnotation = member.typeAnnotation.typeAnnotation;
if (typeAnnotation.type === "TSTypeReference") {
const typeName = typeAnnotation.typeName;
if (typeName.type === "Identifier") {
if (clientTypesMap[typeName.name]) {
clientTypesMap[name] = `Record<string, ${clientTypesMap[typeName.name]}>`;
} else {
// Assume it's an interface which would be available in v3.
clientTypesMap[name] = `Record<string, ${typeName.name}>`;
}
tsTypes.forEach((tsType) => {
if (tsType.typeAnnotation.type !== "TSTypeLiteral") return;
const name = tsType.id.name;
const member = tsType.typeAnnotation.members[0];
if (member.type === "TSIndexSignature") {
if (member.typeAnnotation) {
if (member.typeAnnotation.typeAnnotation) {
const typeAnnotation = member.typeAnnotation.typeAnnotation;
if (typeAnnotation.type === "TSTypeReference") {
const typeName = typeAnnotation.typeName;
if (typeName.type === "Identifier") {
if (clientTypesMap[typeName.name]) {
clientTypesMap[name] = `Record<string, ${clientTypesMap[typeName.name]}>`;
} else {
console.log("TSTypeLiteral TSTypeReference without Identifier type:", name);
// Assume it's an interface which would be available in v3.
clientTypesMap[name] = `Record<string, ${typeName.name}>`;
}
} else if (Object.keys(ElementTypeToNativeTypeMap).includes(typeAnnotation.type)) {
clientTypesMap[name] = `Record<string, ${
ElementTypeToNativeTypeMap[typeAnnotation.type]
}>`;
} else {
console.log("TSTypeLiteral with unsupported typeAnnotation type:", name);
console.log("TSTypeLiteral TSTypeReference without Identifier type:", name);
}
} else if (Object.keys(ElementTypeToNativeTypeMap).includes(typeAnnotation.type)) {
clientTypesMap[name] = `Record<string, ${
ElementTypeToNativeTypeMap[typeAnnotation.type]
}>`;
} else {
console.log("TSTypeLiteral with unsupported typeAnnotation type:", name);
}
}
}
});
}
});

tsTypes.forEach((tsType) => {
const name = tsType.id.name;
Expand Down

0 comments on commit 3411083

Please sign in to comment.