Skip to content

Commit a4dc900

Browse files
authored
Remove .promise() from client API calls (#29)
1 parent 386fc44 commit a4dc900

File tree

6 files changed

+80
-5
lines changed

6 files changed

+80
-5
lines changed

.changeset/smart-pots-yawn.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": minor
3+
---
4+
5+
Remove .promise() from client API calls
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import AWS from "aws-sdk";
2+
3+
const region = "us-west-2";
4+
const client = new AWS.DynamoDB({ region });
5+
6+
client
7+
.listTables({})
8+
.promise()
9+
.then((data) => console.log(data))
10+
.catch((err) => console.log(err, err.stack));
11+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
3+
const region = "us-west-2";
4+
const client = new DynamoDB({ region });
5+
6+
client
7+
.listTables({})
8+
.then((data) => console.log(data))
9+
.catch((err) => console.log(err, err.stack));
10+

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getV2ClientNames,
77
getV2DefaultImportName,
88
removeDefaultImportIfNotUsed,
9+
removePromiseCalls,
910
replaceClientCreation,
1011
} from "./utils";
1112

@@ -24,11 +25,8 @@ export default function transformer(file: FileInfo, api: API) {
2425
for (const [v2ClientName, v3ClientMetadata] of Object.entries(clientMetadata).reverse()) {
2526
const { v3ClientName, v3ClientPackageName } = v3ClientMetadata;
2627
addV3ClientImport(j, source, { v3ClientName, v3ClientPackageName });
27-
replaceClientCreation(j, source, {
28-
v2DefaultImportName,
29-
v2ClientName,
30-
v3ClientName,
31-
});
28+
removePromiseCalls(j, source, { v2DefaultImportName, v2ClientName });
29+
replaceClientCreation(j, source, { v2DefaultImportName, v2ClientName, v3ClientName });
3230
}
3331

3432
removeDefaultImportIfNotUsed(j, source, v2DefaultImportName);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ export * from "./getClientMetadata";
33
export * from "./getV2ClientNames";
44
export * from "./getV2DefaultImportName";
55
export * from "./removeDefaultImportIfNotUsed";
6+
export * from "./removePromiseCalls";
67
export * from "./replaceClientCreation";
78
export * from "./types";
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Collection, Identifier, JSCodeshift, MemberExpression } from "jscodeshift";
2+
3+
export interface RemovePromiseCallsOptions {
4+
v2ClientName: string;
5+
v2DefaultImportName: string;
6+
}
7+
8+
// Removes .promise() from client API calls.
9+
export const removePromiseCalls = (
10+
j: JSCodeshift,
11+
source: Collection<any>,
12+
{ v2DefaultImportName, v2ClientName }: RemovePromiseCallsOptions
13+
): void => {
14+
source
15+
.find(j.VariableDeclarator, {
16+
id: { type: "Identifier" },
17+
init: {
18+
type: "NewExpression",
19+
callee: {
20+
object: { type: "Identifier", name: v2DefaultImportName },
21+
property: { type: "Identifier", name: v2ClientName },
22+
},
23+
},
24+
})
25+
.forEach((nodePath) => {
26+
const name = (nodePath.value.id as Identifier).name;
27+
source
28+
.find(j.CallExpression, {
29+
callee: {
30+
type: "MemberExpression",
31+
object: {
32+
type: "CallExpression",
33+
callee: {
34+
type: "MemberExpression",
35+
object: {
36+
type: "Identifier",
37+
name,
38+
},
39+
},
40+
},
41+
property: { type: "Identifier", name: "promise" },
42+
},
43+
})
44+
.forEach((callExpressionPath) => {
45+
callExpressionPath.parentPath.value.object = (
46+
callExpressionPath.value.callee as MemberExpression
47+
).object;
48+
});
49+
});
50+
};

0 commit comments

Comments
 (0)