From 4e8021635d2cec47c8b6612c63f3e8e328a43aeb Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 16 Apr 2024 08:56:27 -0700 Subject: [PATCH 1/7] test: s3-presigned-post/callback --- .../__fixtures__/s3-presigned-post/callback.input.js | 8 ++++++++ .../__fixtures__/s3-presigned-post/callback.output.js | 10 ++++++++++ 2 files changed, 18 insertions(+) create mode 100644 src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/callback.input.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/callback.output.js diff --git a/src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/callback.input.js b/src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/callback.input.js new file mode 100644 index 000000000..ec9ca5d00 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/callback.input.js @@ -0,0 +1,8 @@ +import AWS from "aws-sdk"; + +const s3 = new AWS.S3(); +const params = { Bucket: "bucket", Fields: { key: "key" } }; + +s3.createPresignedPost(params, function (err, data) { + console.log('The URL is', data.url); +}); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/callback.output.js b/src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/callback.output.js new file mode 100644 index 000000000..4583fdf98 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/callback.output.js @@ -0,0 +1,10 @@ +import AWS from "aws-sdk"; + +const s3 = new AWS.S3(); +const params = { Bucket: "bucket", Fields: { key: "key" } }; + +// S3 createPresignedPost with callbacks is not supported in AWS SDK for JavaScript (v3). +// Please convert to 'client.createPresignedPost(params)', and re-run aws-sdk-js-codemod. +s3.createPresignedPost(params, function (err, data) { + console.log('The URL is', data.url); +}); \ No newline at end of file From 9d54e44949983f628afdce863fd8cb40ac40c6b7 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 16 Apr 2024 09:14:50 -0700 Subject: [PATCH 2/7] Add unsupported comment for s3 createPresignedPost callback --- .../v2-to-v3/apis/addNotSupportedClientComments.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/transforms/v2-to-v3/apis/addNotSupportedClientComments.ts b/src/transforms/v2-to-v3/apis/addNotSupportedClientComments.ts index cef5e5ec9..d315e5ca0 100644 --- a/src/transforms/v2-to-v3/apis/addNotSupportedClientComments.ts +++ b/src/transforms/v2-to-v3/apis/addNotSupportedClientComments.ts @@ -54,6 +54,11 @@ export const addNotSupportedClientComments = ( apiDescription: "S3 getSignedUrl", apiSuggestion: "client.getSignedUrl(apiName, options)", }, + { + apiName: "createPresignedPost", + apiDescription: "S3 createPresignedPost", + apiSuggestion: "client.createPresignedPost(params)", + }, ]; for (const { apiName, apiDescription, apiSuggestion } of apiMetadata) { source From 02c43f6fdad91fce0a28ad3ace0fbc1af97869ad Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 16 Apr 2024 09:30:10 -0700 Subject: [PATCH 3/7] test: s3-presigned-post/createPresignedPost --- .../s3-presigned-post/createPresignedPost.input.js | 4 ++++ .../s3-presigned-post/createPresignedPost.output.js | 5 +++++ 2 files changed, 9 insertions(+) create mode 100644 src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/createPresignedPost.input.js create mode 100644 src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/createPresignedPost.output.js diff --git a/src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/createPresignedPost.input.js b/src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/createPresignedPost.input.js new file mode 100644 index 000000000..ddcf3c013 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/createPresignedPost.input.js @@ -0,0 +1,4 @@ +import AWS from "aws-sdk"; + +const client = new AWS.S3(); +const response = client.createPresignedPost(params); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/createPresignedPost.output.js b/src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/createPresignedPost.output.js new file mode 100644 index 000000000..9a50f5ec7 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/s3-presigned-post/createPresignedPost.output.js @@ -0,0 +1,5 @@ +import { createPresignedPost } from "@aws-sdk/s3-presigned-post"; +import { S3 } from "@aws-sdk/client-s3"; + +const client = new S3(); +const response = await createPresignedPost(client, params); From f196d5d49fcaf04054244a454b8e92b361a04db7 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 16 Apr 2024 09:44:18 -0700 Subject: [PATCH 4/7] Add utility replaceS3CreatePresignedPostApi --- src/transforms/v2-to-v3/apis/index.ts | 1 + .../apis/replaceS3CreatePresignedPostApi.ts | 24 +++++++++++++++++++ src/transforms/v2-to-v3/transformer.ts | 2 ++ 3 files changed, 27 insertions(+) create mode 100644 src/transforms/v2-to-v3/apis/replaceS3CreatePresignedPostApi.ts diff --git a/src/transforms/v2-to-v3/apis/index.ts b/src/transforms/v2-to-v3/apis/index.ts index 4de897081..0b3c373e6 100644 --- a/src/transforms/v2-to-v3/apis/index.ts +++ b/src/transforms/v2-to-v3/apis/index.ts @@ -13,6 +13,7 @@ export * from "./renameErrorCodeWithName"; export * from "./replaceAwsEndpoint"; export * from "./replaceAwsError"; export * from "./replaceAwsIdentity"; +export * from "./replaceS3CreatePresignedPostApi"; export * from "./replaceS3GetSignedUrlApi"; export * from "./replaceS3UploadApi"; export * from "./replaceWaiterApi"; diff --git a/src/transforms/v2-to-v3/apis/replaceS3CreatePresignedPostApi.ts b/src/transforms/v2-to-v3/apis/replaceS3CreatePresignedPostApi.ts new file mode 100644 index 000000000..026eb6e4a --- /dev/null +++ b/src/transforms/v2-to-v3/apis/replaceS3CreatePresignedPostApi.ts @@ -0,0 +1,24 @@ +import { Collection, JSCodeshift } from "jscodeshift"; + +import { ClientIdentifier } from "../types"; +import { getClientApiCallExpression } from "./getClientApiCallExpression"; + +// Updates `s3.createPresignedPost(params)` API with `await createPresignedPost(s3, params)` API. +export const replaceS3CreatePresignedPostApi = ( + j: JSCodeshift, + source: Collection, + clientIdentifiers: ClientIdentifier[] +): void => { + for (const clientId of clientIdentifiers) { + source + .find(j.CallExpression, getClientApiCallExpression(clientId, "createPresignedPost")) + .replaceWith((callExpression) => + j.awaitExpression.from({ + argument: j.callExpression.from({ + callee: j.identifier("createPresignedPost"), + arguments: [clientId, callExpression.node.arguments[0]], + }), + }) + ); + } +}; diff --git a/src/transforms/v2-to-v3/transformer.ts b/src/transforms/v2-to-v3/transformer.ts index 3e9e9f8c7..ad982d23f 100644 --- a/src/transforms/v2-to-v3/transformer.ts +++ b/src/transforms/v2-to-v3/transformer.ts @@ -13,6 +13,7 @@ import { addEmptyObjectForUndefined, renameErrorCodeWithName, addPromiseRemovalComments, + replaceS3CreatePresignedPostApi, } from "./apis"; import { replaceAwsUtilFunctions } from "./aws-util"; import { @@ -118,6 +119,7 @@ const transformer = async (file: FileInfo, api: API) => { if (v2ClientName === S3) { replaceS3GetSignedUrlApi(j, source, clientIdentifiers); + replaceS3CreatePresignedPostApi(j, source, clientIdentifiers); } replaceWaiterApi(j, source, clientIdentifiers); From 227e877dc54be3350c1020472f040673b63931be Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 16 Apr 2024 09:48:17 -0700 Subject: [PATCH 5/7] Add import for @aws-sdk/s3-presigned-post --- src/transforms/v2-to-v3/apis/index.ts | 1 + .../apis/isS3CreatePresignedPostApiUsed.ts | 22 +++++++++++++++++++ .../v2-to-v3/modules/addClientModules.ts | 9 ++++++++ 3 files changed, 32 insertions(+) create mode 100644 src/transforms/v2-to-v3/apis/isS3CreatePresignedPostApiUsed.ts diff --git a/src/transforms/v2-to-v3/apis/index.ts b/src/transforms/v2-to-v3/apis/index.ts index 0b3c373e6..84b453f95 100644 --- a/src/transforms/v2-to-v3/apis/index.ts +++ b/src/transforms/v2-to-v3/apis/index.ts @@ -6,6 +6,7 @@ export * from "./getClientWaiterStates"; export * from "./getCommandName"; export * from "./getS3SignedUrlApiNames"; export * from "./getV3ClientWaiterApiName"; +export * from "./isS3CreatePresignedPostApiUsed"; export * from "./isS3GetSignedUrlApiUsed"; export * from "./isS3UploadApiUsed"; export * from "./removePromiseCalls"; diff --git a/src/transforms/v2-to-v3/apis/isS3CreatePresignedPostApiUsed.ts b/src/transforms/v2-to-v3/apis/isS3CreatePresignedPostApiUsed.ts new file mode 100644 index 000000000..724322254 --- /dev/null +++ b/src/transforms/v2-to-v3/apis/isS3CreatePresignedPostApiUsed.ts @@ -0,0 +1,22 @@ +import { Collection, JSCodeshift } from "jscodeshift"; + +import { ClientIdentifier } from "../types"; + +export const isS3CreatePresignedPostApiUsed = ( + j: JSCodeshift, + source: Collection, + clientIdentifiers: ClientIdentifier[] +) => { + for (const clientId of clientIdentifiers) { + const s3GetSignedUrlCallExpressions = source.find(j.CallExpression, { + callee: { + type: "MemberExpression", + object: clientId, + property: { type: "Identifier", name: "createPresignedPost" }, + }, + }); + if (s3GetSignedUrlCallExpressions.length) return true; + } + + return false; +}; diff --git a/src/transforms/v2-to-v3/modules/addClientModules.ts b/src/transforms/v2-to-v3/modules/addClientModules.ts index 46f4fc12e..c9fec178c 100644 --- a/src/transforms/v2-to-v3/modules/addClientModules.ts +++ b/src/transforms/v2-to-v3/modules/addClientModules.ts @@ -5,6 +5,7 @@ import { getCommandName, getS3SignedUrlApiNames, getV3ClientWaiterApiName, + isS3CreatePresignedPostApiUsed, isS3GetSignedUrlApiUsed, isS3UploadApiUsed, } from "../apis"; @@ -96,6 +97,14 @@ export const addClientModules = ( }); } } + + if (isS3CreatePresignedPostApiUsed(j, source, clientIdentifiers)) { + addNamedModule(j, source, { + importType, + localName: "createPresignedPost", + packageName: "@aws-sdk/s3-presigned-post", + }); + } } if (v2ClientName === DYNAMODB) { From eb43a0aa54d89ad168f5490ed0a3e8a4bb88686e Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 16 Apr 2024 09:48:49 -0700 Subject: [PATCH 6/7] chore: yarn changeset --- .changeset/poor-timers-brake.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/poor-timers-brake.md diff --git a/.changeset/poor-timers-brake.md b/.changeset/poor-timers-brake.md new file mode 100644 index 000000000..2458bdbe9 --- /dev/null +++ b/.changeset/poor-timers-brake.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": minor +--- + +Add transformation for s3 createPresignedPost From 577d6530441a86548ec23f25e35bd224a700d853 Mon Sep 17 00:00:00 2001 From: "Kamat, Trivikram" <16024985+trivikr@users.noreply.github.com> Date: Tue, 16 Apr 2024 09:50:13 -0700 Subject: [PATCH 7/7] Update isS3CreatePresignedPostApiUsed --- .../apis/isS3CreatePresignedPostApiUsed.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/transforms/v2-to-v3/apis/isS3CreatePresignedPostApiUsed.ts b/src/transforms/v2-to-v3/apis/isS3CreatePresignedPostApiUsed.ts index 724322254..f002919e1 100644 --- a/src/transforms/v2-to-v3/apis/isS3CreatePresignedPostApiUsed.ts +++ b/src/transforms/v2-to-v3/apis/isS3CreatePresignedPostApiUsed.ts @@ -8,14 +8,16 @@ export const isS3CreatePresignedPostApiUsed = ( clientIdentifiers: ClientIdentifier[] ) => { for (const clientId of clientIdentifiers) { - const s3GetSignedUrlCallExpressions = source.find(j.CallExpression, { - callee: { - type: "MemberExpression", - object: clientId, - property: { type: "Identifier", name: "createPresignedPost" }, - }, - }); - if (s3GetSignedUrlCallExpressions.length) return true; + if ( + source.find(j.CallExpression, { + callee: { + type: "MemberExpression", + object: clientId, + property: { type: "Identifier", name: "createPresignedPost" }, + }, + }).length + ) + return true; } return false;