Skip to content

Commit 692b989

Browse files
authored
Support transformation of waiter API on a client class member (#431)
1 parent bfae4dc commit 692b989

File tree

7 files changed

+74
-5
lines changed

7 files changed

+74
-5
lines changed

.changeset/unlucky-snails-grab.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+
Support transformation of waiter API on a client class member
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import AWS from "aws-sdk";
2+
3+
// Client as class member
4+
class ClientClassMember {
5+
constructor(clientInCtr = new AWS.S3()) {
6+
this.clientInClass = clientInCtr;
7+
}
8+
9+
async listTables() {
10+
return this.clientInClass.waitFor("bucketExists", { Bucket: "BUCKET" }).promise();
11+
}
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import AWS from "aws-sdk";
2+
3+
// Client as class member
4+
class ClientClassMember {
5+
private clientInClass: AWS.S3;
6+
7+
constructor(clientInCtr: AWS.S3) {
8+
this.clientInClass = clientInCtr;
9+
}
10+
11+
async listTables() {
12+
return this.clientInClass.waitFor("bucketExists", { Bucket: "BUCKET" }).promise();
13+
}
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { S3, waitUntilBucketExists } from "@aws-sdk/client-s3";
2+
3+
// Client as class member
4+
class ClientClassMember {
5+
constructor(clientInCtr = new S3()) {
6+
this.clientInClass = clientInCtr;
7+
}
8+
9+
async listTables() {
10+
return waitUntilBucketExists({
11+
client: this.clientInClass,
12+
maxWaitTime: 200
13+
}, { Bucket: "BUCKET" });
14+
}
15+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { S3, waitUntilBucketExists } from "@aws-sdk/client-s3";
2+
3+
// Client as class member
4+
class ClientClassMember {
5+
private clientInClass: S3;
6+
7+
constructor(clientInCtr: S3) {
8+
this.clientInClass = clientInCtr;
9+
}
10+
11+
async listTables() {
12+
return waitUntilBucketExists({
13+
client: this.clientInClass,
14+
maxWaitTime: 200
15+
}, { Bucket: "BUCKET" });
16+
}
17+
}

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Collection, Identifier, JSCodeshift } from "jscodeshift";
22

33
import { getV2ClientIdNamesFromNewExpr } from "./getV2ClientIdNamesFromNewExpr";
44
import { getV2ClientIdNamesFromTSTypeRef } from "./getV2ClientIdNamesFromTSTypeRef";
5+
import { getV2ClientIdThisExpressions, ThisMemberExpression } from "./getV2ClientIdThisExpressions";
56

67
export interface GetV2ClientIdentifiersOptions {
78
v2ClientName: string;
@@ -13,9 +14,16 @@ export const getV2ClientIdentifiers = (
1314
j: JSCodeshift,
1415
source: Collection<unknown>,
1516
options: GetV2ClientIdentifiersOptions
16-
): Identifier[] => {
17+
): (Identifier | ThisMemberExpression)[] => {
1718
const namesFromNewExpr = getV2ClientIdNamesFromNewExpr(j, source, options);
1819
const namesFromTSTypeRef = getV2ClientIdNamesFromTSTypeRef(j, source, options);
1920
const clientIdNames = [...new Set([...namesFromNewExpr, ...namesFromTSTypeRef])];
20-
return clientIdNames.map((clientidName) => ({ type: "Identifier", name: clientidName }));
21+
22+
const v2ClientIdentifiers: Identifier[] = clientIdNames.map((clientidName) => ({
23+
type: "Identifier",
24+
name: clientidName,
25+
}));
26+
const v2ClientIdThisExpressions = getV2ClientIdThisExpressions(j, source, v2ClientIdentifiers);
27+
28+
return [...v2ClientIdentifiers, ...v2ClientIdThisExpressions];
2129
};

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Collection, Identifier, JSCodeshift } from "jscodeshift";
22

33
import { getV2ClientIdentifiers } from "./getV2ClientIdentifiers";
4-
import { getV2ClientIdThisExpressions } from "./getV2ClientIdThisExpressions";
54
import { removePromiseForCallExpression } from "./removePromiseForCallExpression";
65

76
export interface RemovePromiseCallsOptions {
@@ -17,9 +16,8 @@ export const removePromiseCalls = (
1716
options: RemovePromiseCallsOptions
1817
): void => {
1918
const v2ClientIdentifiers = getV2ClientIdentifiers(j, source, options);
20-
const v2ClientIdThisExpressions = getV2ClientIdThisExpressions(j, source, v2ClientIdentifiers);
2119

22-
for (const v2ClientId of [...v2ClientIdentifiers, ...v2ClientIdThisExpressions]) {
20+
for (const v2ClientId of v2ClientIdentifiers) {
2321
// Remove .promise() from client API calls.
2422
source
2523
.find(j.CallExpression, {

0 commit comments

Comments
 (0)