Skip to content

Commit

Permalink
Support transformation of waiter API on a client class member (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Mar 1, 2023
1 parent bfae4dc commit 692b989
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-snails-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Support transformation of waiter API on a client class member
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import AWS from "aws-sdk";

// Client as class member
class ClientClassMember {
constructor(clientInCtr = new AWS.S3()) {
this.clientInClass = clientInCtr;
}

async listTables() {
return this.clientInClass.waitFor("bucketExists", { Bucket: "BUCKET" }).promise();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import AWS from "aws-sdk";

// Client as class member
class ClientClassMember {
private clientInClass: AWS.S3;

constructor(clientInCtr: AWS.S3) {
this.clientInClass = clientInCtr;
}

async listTables() {
return this.clientInClass.waitFor("bucketExists", { Bucket: "BUCKET" }).promise();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { S3, waitUntilBucketExists } from "@aws-sdk/client-s3";

// Client as class member
class ClientClassMember {
constructor(clientInCtr = new S3()) {
this.clientInClass = clientInCtr;
}

async listTables() {
return waitUntilBucketExists({
client: this.clientInClass,
maxWaitTime: 200
}, { Bucket: "BUCKET" });
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { S3, waitUntilBucketExists } from "@aws-sdk/client-s3";

// Client as class member
class ClientClassMember {
private clientInClass: S3;

constructor(clientInCtr: S3) {
this.clientInClass = clientInCtr;
}

async listTables() {
return waitUntilBucketExists({
client: this.clientInClass,
maxWaitTime: 200
}, { Bucket: "BUCKET" });
}
}
12 changes: 10 additions & 2 deletions src/transforms/v2-to-v3/apis/getV2ClientIdentifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Collection, Identifier, JSCodeshift } from "jscodeshift";

import { getV2ClientIdNamesFromNewExpr } from "./getV2ClientIdNamesFromNewExpr";
import { getV2ClientIdNamesFromTSTypeRef } from "./getV2ClientIdNamesFromTSTypeRef";
import { getV2ClientIdThisExpressions, ThisMemberExpression } from "./getV2ClientIdThisExpressions";

export interface GetV2ClientIdentifiersOptions {
v2ClientName: string;
Expand All @@ -13,9 +14,16 @@ export const getV2ClientIdentifiers = (
j: JSCodeshift,
source: Collection<unknown>,
options: GetV2ClientIdentifiersOptions
): Identifier[] => {
): (Identifier | ThisMemberExpression)[] => {
const namesFromNewExpr = getV2ClientIdNamesFromNewExpr(j, source, options);
const namesFromTSTypeRef = getV2ClientIdNamesFromTSTypeRef(j, source, options);
const clientIdNames = [...new Set([...namesFromNewExpr, ...namesFromTSTypeRef])];
return clientIdNames.map((clientidName) => ({ type: "Identifier", name: clientidName }));

const v2ClientIdentifiers: Identifier[] = clientIdNames.map((clientidName) => ({
type: "Identifier",
name: clientidName,
}));
const v2ClientIdThisExpressions = getV2ClientIdThisExpressions(j, source, v2ClientIdentifiers);

return [...v2ClientIdentifiers, ...v2ClientIdThisExpressions];
};
4 changes: 1 addition & 3 deletions src/transforms/v2-to-v3/apis/removePromiseCalls.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Collection, Identifier, JSCodeshift } from "jscodeshift";

import { getV2ClientIdentifiers } from "./getV2ClientIdentifiers";
import { getV2ClientIdThisExpressions } from "./getV2ClientIdThisExpressions";
import { removePromiseForCallExpression } from "./removePromiseForCallExpression";

export interface RemovePromiseCallsOptions {
Expand All @@ -17,9 +16,8 @@ export const removePromiseCalls = (
options: RemovePromiseCallsOptions
): void => {
const v2ClientIdentifiers = getV2ClientIdentifiers(j, source, options);
const v2ClientIdThisExpressions = getV2ClientIdThisExpressions(j, source, v2ClientIdentifiers);

for (const v2ClientId of [...v2ClientIdentifiers, ...v2ClientIdThisExpressions]) {
for (const v2ClientId of v2ClientIdentifiers) {
// Remove .promise() from client API calls.
source
.find(j.CallExpression, {
Expand Down

0 comments on commit 692b989

Please sign in to comment.