Skip to content

Commit

Permalink
Remove .promise() calls from APIs called from new client (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Apr 11, 2024
1 parent ec7f487 commit 13dc42a
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/rotten-ears-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Remove .promise() calls from APIs called from new client
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from "aws-sdk";

const data = await new AWS.DynamoDB().listTables().promise();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { DynamoDB } from "@aws-sdk/client-dynamodb";

const data = await new DynamoDB().listTables();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { DynamoDB } from "aws-sdk";

const data = await new DynamoDB().listTables().promise();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { DynamoDB } from "@aws-sdk/client-dynamodb";

const data = await new DynamoDB().listTables();
59 changes: 58 additions & 1 deletion src/transforms/v2-to-v3/apis/removePromiseCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,69 @@ import { Collection, Identifier, JSCodeshift } from "jscodeshift";
import { ClientIdentifier } from "../types";
import { removePromiseForCallExpression } from "./removePromiseForCallExpression";

export interface RemovePromiseCallsOptions {
v2GlobalName?: string;
v2ClientName: string;
v2ClientLocalName: string;
clientIdentifiers: ClientIdentifier[];
}

// Removes .promise() from client API calls.
export const removePromiseCalls = (
j: JSCodeshift,
source: Collection<unknown>,
clientIdentifiers: ClientIdentifier[]
{ v2GlobalName, v2ClientName, v2ClientLocalName, clientIdentifiers }: RemovePromiseCallsOptions
): void => {
// Remove .promise() for API calls from client creation from global name.
if (v2GlobalName) {
source
.find(j.CallExpression, {
callee: {
type: "MemberExpression",
object: {
type: "CallExpression",
callee: {
type: "MemberExpression",
object: {
type: "NewExpression",
callee: {
type: "MemberExpression",
object: { type: "Identifier", name: v2GlobalName },
property: { type: "Identifier", name: v2ClientName },
},
},
},
},
property: { type: "Identifier", name: "promise" },
},
})
.forEach((callExpression) => {
removePromiseForCallExpression(j, callExpression);
});
}

// Remove .promise() for API calls client creation from local name.
source
.find(j.CallExpression, {
callee: {
type: "MemberExpression",
object: {
type: "CallExpression",
callee: {
type: "MemberExpression",
object: {
type: "NewExpression",
callee: { type: "Identifier", name: v2ClientLocalName },
},
},
},
property: { type: "Identifier", name: "promise" },
},
})
.forEach((callExpression) => {
removePromiseForCallExpression(j, callExpression);
});

for (const clientId of clientIdentifiers) {
// Remove .promise() from client API calls.
source
Expand Down
2 changes: 1 addition & 1 deletion src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ const transformer = async (file: FileInfo, api: API) => {
replaceS3UploadApi(j, source, clientIdentifiers);
}

removePromiseCalls(j, source, clientIdentifiers);
removePromiseCalls(j, source, { ...v2Options, clientIdentifiers });
addEmptyObjectForUndefined(j, source, clientIdentifiers);
renameErrorCodeWithName(j, source, clientIdentifiers);

Expand Down

0 comments on commit 13dc42a

Please sign in to comment.