Skip to content

Commit 13dc42a

Browse files
authored
Remove .promise() calls from APIs called from new client (#857)
1 parent ec7f487 commit 13dc42a

File tree

7 files changed

+76
-2
lines changed

7 files changed

+76
-2
lines changed

.changeset/rotten-ears-move.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+
Remove .promise() calls from APIs called from new client
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import AWS from "aws-sdk";
2+
3+
const data = await new AWS.DynamoDB().listTables().promise();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
3+
const data = await new DynamoDB().listTables();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { DynamoDB } from "aws-sdk";
2+
3+
const data = await new DynamoDB().listTables().promise();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
3+
const data = await new DynamoDB().listTables();

src/transforms/v2-to-v3/apis/removePromiseCalls.ts

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,69 @@ import { Collection, Identifier, JSCodeshift } from "jscodeshift";
33
import { ClientIdentifier } from "../types";
44
import { removePromiseForCallExpression } from "./removePromiseForCallExpression";
55

6+
export interface RemovePromiseCallsOptions {
7+
v2GlobalName?: string;
8+
v2ClientName: string;
9+
v2ClientLocalName: string;
10+
clientIdentifiers: ClientIdentifier[];
11+
}
12+
613
// Removes .promise() from client API calls.
714
export const removePromiseCalls = (
815
j: JSCodeshift,
916
source: Collection<unknown>,
10-
clientIdentifiers: ClientIdentifier[]
17+
{ v2GlobalName, v2ClientName, v2ClientLocalName, clientIdentifiers }: RemovePromiseCallsOptions
1118
): void => {
19+
// Remove .promise() for API calls from client creation from global name.
20+
if (v2GlobalName) {
21+
source
22+
.find(j.CallExpression, {
23+
callee: {
24+
type: "MemberExpression",
25+
object: {
26+
type: "CallExpression",
27+
callee: {
28+
type: "MemberExpression",
29+
object: {
30+
type: "NewExpression",
31+
callee: {
32+
type: "MemberExpression",
33+
object: { type: "Identifier", name: v2GlobalName },
34+
property: { type: "Identifier", name: v2ClientName },
35+
},
36+
},
37+
},
38+
},
39+
property: { type: "Identifier", name: "promise" },
40+
},
41+
})
42+
.forEach((callExpression) => {
43+
removePromiseForCallExpression(j, callExpression);
44+
});
45+
}
46+
47+
// Remove .promise() for API calls client creation from local name.
48+
source
49+
.find(j.CallExpression, {
50+
callee: {
51+
type: "MemberExpression",
52+
object: {
53+
type: "CallExpression",
54+
callee: {
55+
type: "MemberExpression",
56+
object: {
57+
type: "NewExpression",
58+
callee: { type: "Identifier", name: v2ClientLocalName },
59+
},
60+
},
61+
},
62+
property: { type: "Identifier", name: "promise" },
63+
},
64+
})
65+
.forEach((callExpression) => {
66+
removePromiseForCallExpression(j, callExpression);
67+
});
68+
1269
for (const clientId of clientIdentifiers) {
1370
// Remove .promise() from client API calls.
1471
source

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ const transformer = async (file: FileInfo, api: API) => {
107107
replaceS3UploadApi(j, source, clientIdentifiers);
108108
}
109109

110-
removePromiseCalls(j, source, clientIdentifiers);
110+
removePromiseCalls(j, source, { ...v2Options, clientIdentifiers });
111111
addEmptyObjectForUndefined(j, source, clientIdentifiers);
112112
renameErrorCodeWithName(j, source, clientIdentifiers);
113113

0 commit comments

Comments
 (0)