Skip to content

Commit 222c00c

Browse files
authored
Replace named imports for Input/Output types (#216)
1 parent 52d8543 commit 222c00c

File tree

12 files changed

+146
-33
lines changed

12 files changed

+146
-33
lines changed

.changeset/purple-pans-shave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Replace named imports for Input/Output types
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import DynamoDB, { ListTablesInput, ListTablesOutput } from "aws-sdk/clients/dynamodb";
2+
3+
const client = new DynamoDB({ region: "us-west-2" });
4+
5+
const listTablesInput: ListTablesInput = { Limit: 10 };
6+
const listTablesOutput: ListTablesOutput = await client
7+
.listTables(listTablesInput)
8+
.promise();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { DynamoDB, ListTablesCommandInput, ListTablesCommandOutput } from "@aws-sdk/client-dynamodb";
2+
3+
const client = new DynamoDB({ region: "us-west-2" });
4+
5+
const listTablesInput: ListTablesCommandInput = { Limit: 10 };
6+
const listTablesOutput: ListTablesCommandOutput = await client
7+
.listTables(listTablesInput);

src/transforms/v2-to-v3/transformer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ export default function transformer(file: FileInfo, api: API) {
3535
v3ClientPackageName,
3636
v2DefaultModuleName,
3737
});
38-
removeV2ClientModule(j, source, v2ClientName);
39-
removePromiseCalls(j, source, { v2DefaultModuleName, v2ClientName });
40-
replaceClientCreation(j, source, { v2DefaultModuleName, v2ClientName, v3ClientName });
41-
replaceTSTypeReference(j, source, { v2DefaultModuleName, v2ClientName, v3ClientName });
38+
replaceTSTypeReference(j, source, { v2ClientName, v2DefaultModuleName, v3ClientName });
39+
removeV2ClientModule(j, source, { v2ClientName, v2DefaultModuleName });
40+
removePromiseCalls(j, source, { v2ClientName, v2DefaultModuleName });
41+
replaceClientCreation(j, source, { v2ClientName, v2DefaultModuleName, v3ClientName });
4242
}
4343

4444
removeDefaultModuleIfNotUsed(j, source, v2DefaultModuleName);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
export const PACKAGE_NAME = "aws-sdk";
2+
3+
// ToDo: Add Request and Response suffixes in future version.
4+
export const V2_CLIENT_INPUT_SUFFIX_LIST = ["Input"];
5+
export const V2_CLIENT_OUTPUT_SUFFIX_LIST = ["Output"];

src/transforms/v2-to-v3/utils/get/getV2ClientTypeNames.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
import { Collection, Identifier, JSCodeshift, TSQualifiedName, TSTypeReference } from "jscodeshift";
1+
import {
2+
Collection,
3+
Identifier,
4+
ImportDeclaration,
5+
ImportSpecifier,
6+
JSCodeshift,
7+
TSQualifiedName,
8+
TSTypeReference,
9+
} from "jscodeshift";
10+
11+
import { getV2ServiceModulePath } from "./getV2ServiceModulePath";
212

313
export interface GetV2ClientTypeNamesOptions {
414
v2ClientName: string;
@@ -28,6 +38,11 @@ export const getV2ClientTypeNames = (
2838
},
2939
} as TSTypeReference;
3040

41+
const v2ClientTypeFromNamedImport = {
42+
type: "ImportDeclaration",
43+
source: { value: getV2ServiceModulePath(v2ClientName) },
44+
} as ImportDeclaration;
45+
3146
return [
3247
...source
3348
.find(j.TSTypeReference, v2DefaultTypeName)
@@ -37,5 +52,12 @@ export const getV2ClientTypeNames = (
3752
.find(j.TSTypeReference, v2ClientTypeName)
3853
.nodes()
3954
.map((node) => getRightIdentifierName(node)),
55+
...source
56+
.find(j.ImportDeclaration, v2ClientTypeFromNamedImport)
57+
.nodes()
58+
.map((node) => node.specifiers)
59+
.flat()
60+
.filter((node) => (node as ImportSpecifier).type === "ImportSpecifier")
61+
.map((node) => ((node as ImportSpecifier).local as Identifier).name),
4062
];
4163
};

src/transforms/v2-to-v3/utils/get/getV3ClientType.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/transforms/v2-to-v3/utils/get/getV3ClientTypeName.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import { V2_CLIENT_INPUT_SUFFIX_LIST, V2_CLIENT_OUTPUT_SUFFIX_LIST } from "../config";
2+
13
export const getV3ClientTypeName = (v2ClientTypeName: string) => {
2-
if (v2ClientTypeName.endsWith("Input")) {
3-
return v2ClientTypeName.replace(/Input$/, "CommandInput");
4+
for (const inputSuffix of V2_CLIENT_INPUT_SUFFIX_LIST) {
5+
if (v2ClientTypeName.endsWith(inputSuffix)) {
6+
return v2ClientTypeName.replace(new RegExp(`${inputSuffix}$`), "CommandInput");
7+
}
48
}
59

6-
if (v2ClientTypeName.endsWith("Output")) {
7-
return v2ClientTypeName.replace(/Output$/, "CommandOutput");
10+
for (const outputSuffix of V2_CLIENT_OUTPUT_SUFFIX_LIST) {
11+
if (v2ClientTypeName.endsWith(outputSuffix)) {
12+
return v2ClientTypeName.replace(new RegExp(`${outputSuffix}$`), "CommandOutput");
13+
}
814
}
915

1016
return undefined;

src/transforms/v2-to-v3/utils/get/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ export * from "./getRequireVariableDeclaration";
33
export * from "./getV2ClientIdentifiers";
44
export * from "./getV2ClientIdThisExpressions";
55
export * from "./getV2ClientNames";
6+
export * from "./getV2ClientTypeNames";
67
export * from "./getV2DefaultModuleName";
78
export * from "./getV2ServiceModuleNames";
89
export * from "./getV2ServiceModulePath";
9-
export * from "./getV3ClientType";
10+
export * from "./getV3ClientTypeName";
1011
export * from "./getV3ClientTypeNames";
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { V2_CLIENT_INPUT_SUFFIX_LIST, V2_CLIENT_OUTPUT_SUFFIX_LIST } from "./config";
2+
3+
export const isV2ClientInputOutputType = (v2ClientTypeName: string) =>
4+
[...V2_CLIENT_INPUT_SUFFIX_LIST, ...V2_CLIENT_OUTPUT_SUFFIX_LIST].some((suffix) =>
5+
v2ClientTypeName.endsWith(suffix)
6+
);

0 commit comments

Comments
 (0)