-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial support for waiter API transformation (#52)
- Loading branch information
Showing
27 changed files
with
311 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"aws-sdk-js-codemod": minor | ||
--- | ||
|
||
Add initial support for waiter API transformation |
7 changes: 7 additions & 0 deletions
7
src/transforms/v2-to-v3/__fixtures__/waiters/global-import-equals.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import AWS = require("aws-sdk"); | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new AWS.S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }).promise(); | ||
await client.waitFor("bucketExists", { Bucket }).promise(); |
15 changes: 15 additions & 0 deletions
15
src/transforms/v2-to-v3/__fixtures__/waiters/global-import-equals.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import AWS_S3 = require("@aws-sdk/client-s3"); | ||
|
||
const { | ||
S3, | ||
waitUntilBucketExists | ||
} = AWS_S3; | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }); | ||
await waitUntilBucketExists({ | ||
client, | ||
maxWaitTime: 180 | ||
}, { Bucket }); |
7 changes: 7 additions & 0 deletions
7
src/transforms/v2-to-v3/__fixtures__/waiters/global-import.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new AWS.S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }).promise(); | ||
await client.waitFor("bucketExists", { Bucket }).promise(); |
10 changes: 10 additions & 0 deletions
10
src/transforms/v2-to-v3/__fixtures__/waiters/global-import.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { S3, waitUntilBucketExists } from "@aws-sdk/client-s3"; | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }); | ||
await waitUntilBucketExists({ | ||
client, | ||
maxWaitTime: 180 | ||
}, { Bucket }); |
7 changes: 7 additions & 0 deletions
7
src/transforms/v2-to-v3/__fixtures__/waiters/global-require-property.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const S3 = require("aws-sdk").S3; | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }).promise(); | ||
await client.waitFor("bucketExists", { Bucket }).promise(); |
13 changes: 13 additions & 0 deletions
13
src/transforms/v2-to-v3/__fixtures__/waiters/global-require-property.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const { | ||
S3, | ||
waitUntilBucketExists | ||
} = require("@aws-sdk/client-s3"); | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }); | ||
await waitUntilBucketExists({ | ||
client, | ||
maxWaitTime: 180 | ||
}, { Bucket }); |
7 changes: 7 additions & 0 deletions
7
src/transforms/v2-to-v3/__fixtures__/waiters/global-require.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const AWS = require("aws-sdk"); | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new AWS.S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }).promise(); | ||
await client.waitFor("bucketExists", { Bucket }).promise(); |
13 changes: 13 additions & 0 deletions
13
src/transforms/v2-to-v3/__fixtures__/waiters/global-require.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const { | ||
S3, | ||
waitUntilBucketExists | ||
} = require("@aws-sdk/client-s3"); | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }); | ||
await waitUntilBucketExists({ | ||
client, | ||
maxWaitTime: 180 | ||
}, { Bucket }); |
7 changes: 7 additions & 0 deletions
7
src/transforms/v2-to-v3/__fixtures__/waiters/service-import-deep.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import S3 from "aws-sdk/clients/s3"; | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }).promise(); | ||
await client.waitFor("bucketExists", { Bucket }).promise(); |
10 changes: 10 additions & 0 deletions
10
src/transforms/v2-to-v3/__fixtures__/waiters/service-import-deep.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { S3, waitUntilBucketExists } from "@aws-sdk/client-s3"; | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }); | ||
await waitUntilBucketExists({ | ||
client, | ||
maxWaitTime: 180 | ||
}, { Bucket }); |
7 changes: 7 additions & 0 deletions
7
src/transforms/v2-to-v3/__fixtures__/waiters/service-import-equals.input.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import S3 = require("aws-sdk/clients/s3"); | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }).promise(); | ||
await client.waitFor("bucketExists", { Bucket }).promise(); |
15 changes: 15 additions & 0 deletions
15
src/transforms/v2-to-v3/__fixtures__/waiters/service-import-equals.output.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import AWS_S3 = require("@aws-sdk/client-s3"); | ||
|
||
const { | ||
S3, | ||
waitUntilBucketExists | ||
} = AWS_S3; | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }); | ||
await waitUntilBucketExists({ | ||
client, | ||
maxWaitTime: 180 | ||
}, { Bucket }); |
7 changes: 7 additions & 0 deletions
7
src/transforms/v2-to-v3/__fixtures__/waiters/service-import.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { S3 } from "aws-sdk"; | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }).promise(); | ||
await client.waitFor("bucketExists", { Bucket }).promise(); |
10 changes: 10 additions & 0 deletions
10
src/transforms/v2-to-v3/__fixtures__/waiters/service-import.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { S3, waitUntilBucketExists } from "@aws-sdk/client-s3"; | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }); | ||
await waitUntilBucketExists({ | ||
client, | ||
maxWaitTime: 180 | ||
}, { Bucket }); |
7 changes: 7 additions & 0 deletions
7
src/transforms/v2-to-v3/__fixtures__/waiters/service-require-deep.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const S3 = require("aws-sdk/clients/s3"); | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }).promise(); | ||
await client.waitFor("bucketExists", { Bucket }).promise(); |
13 changes: 13 additions & 0 deletions
13
src/transforms/v2-to-v3/__fixtures__/waiters/service-require-deep.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const { | ||
S3, | ||
waitUntilBucketExists | ||
} = require("@aws-sdk/client-s3"); | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }); | ||
await waitUntilBucketExists({ | ||
client, | ||
maxWaitTime: 180 | ||
}, { Bucket }); |
7 changes: 7 additions & 0 deletions
7
src/transforms/v2-to-v3/__fixtures__/waiters/service-require.input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const { S3 } = require("aws-sdk"); | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }).promise(); | ||
await client.waitFor("bucketExists", { Bucket }).promise(); |
13 changes: 13 additions & 0 deletions
13
src/transforms/v2-to-v3/__fixtures__/waiters/service-require.output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const { | ||
S3, | ||
waitUntilBucketExists | ||
} = require("@aws-sdk/client-s3"); | ||
|
||
const Bucket = "BUCKET_NAME"; | ||
const client = new S3({ region: "REGION" }); | ||
|
||
await client.createBucket({ Bucket }); | ||
await waitUntilBucketExists({ | ||
client, | ||
maxWaitTime: 180 | ||
}, { Bucket }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
import { getV2ClientIdentifiers } from "./getV2ClientIdentifiers"; | ||
|
||
export interface GetClientWaiterStatesOptions { | ||
v2ClientName: string; | ||
v2ClientLocalName: string; | ||
v2GlobalName?: string; | ||
} | ||
|
||
export const getClientWaiterStates = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
options: GetClientWaiterStatesOptions | ||
): string[] => { | ||
const waiterStates: string[] = []; | ||
|
||
const v2ClientIdentifiers = getV2ClientIdentifiers(j, source, options); | ||
|
||
for (const v2ClientId of v2ClientIdentifiers) { | ||
source | ||
.find(j.CallExpression, { | ||
callee: { | ||
type: "MemberExpression", | ||
object: v2ClientId, | ||
property: { type: "Identifier", name: "waitFor" }, | ||
}, | ||
}) | ||
.forEach((waiterCallExpression) => { | ||
// @ts-expect-error arguments[0] is Literal or StringLiteral | ||
const waiterState = waiterCallExpression.value.arguments[0].value; | ||
waiterStates.push(waiterState); | ||
}); | ||
} | ||
|
||
return waiterStates; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const getV3ClientWaiterApiName = (waiterState: string): string => | ||
`waitUntil${waiterState[0].toUpperCase()}${waiterState.slice(1)}`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
export * from "./getClientWaiterStates"; | ||
export * from "./getV3ClientWaiterApiName"; | ||
export * from "./removePromiseCalls"; | ||
export * from "./replaceWaiterApi"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
import { getClientWaiterStates } from "./getClientWaiterStates"; | ||
import { getV2ClientIdentifiers } from "./getV2ClientIdentifiers"; | ||
import { getV3ClientWaiterApiName } from "./getV3ClientWaiterApiName"; | ||
|
||
export interface ReplaceWaiterApiOptions { | ||
v2ClientName: string; | ||
v2ClientLocalName: string; | ||
v2GlobalName?: string; | ||
} | ||
|
||
// Updates .waitFor() API with waitUntil* API. | ||
export const replaceWaiterApi = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown>, | ||
options: ReplaceWaiterApiOptions | ||
): void => { | ||
const v2ClientIdentifiers = getV2ClientIdentifiers(j, source, options); | ||
|
||
for (const v2ClientId of v2ClientIdentifiers) { | ||
const waiterStates = getClientWaiterStates(j, source, options); | ||
|
||
for (const waiterState of waiterStates) { | ||
const v3WaiterApiName = getV3ClientWaiterApiName(waiterState); | ||
source | ||
.find(j.CallExpression, { | ||
type: "CallExpression", | ||
callee: { | ||
type: "MemberExpression", | ||
object: v2ClientId, | ||
property: { type: "Identifier", name: "waitFor" }, | ||
}, | ||
}) | ||
.replaceWith((callExpression) => { | ||
return j.callExpression(j.identifier(v3WaiterApiName), [ | ||
j.objectExpression([ | ||
j.objectProperty.from({ | ||
key: j.identifier("client"), | ||
value: v2ClientId, | ||
shorthand: true, | ||
}), | ||
// ToDo: Read maxWaitTime from the waiter configuration | ||
j.objectProperty.from({ | ||
key: j.identifier("maxWaitTime"), | ||
value: j.numericLiteral(180), | ||
}), | ||
]), | ||
callExpression.node.arguments[1], | ||
]); | ||
}); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.