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 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 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); 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 diff --git a/src/transforms/v2-to-v3/apis/index.ts b/src/transforms/v2-to-v3/apis/index.ts index 4de897081..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"; @@ -13,6 +14,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/isS3CreatePresignedPostApiUsed.ts b/src/transforms/v2-to-v3/apis/isS3CreatePresignedPostApiUsed.ts new file mode 100644 index 000000000..f002919e1 --- /dev/null +++ b/src/transforms/v2-to-v3/apis/isS3CreatePresignedPostApiUsed.ts @@ -0,0 +1,24 @@ +import { Collection, JSCodeshift } from "jscodeshift"; + +import { ClientIdentifier } from "../types"; + +export const isS3CreatePresignedPostApiUsed = ( + j: JSCodeshift, + source: Collection, + clientIdentifiers: ClientIdentifier[] +) => { + for (const clientId of clientIdentifiers) { + if ( + source.find(j.CallExpression, { + callee: { + type: "MemberExpression", + object: clientId, + property: { type: "Identifier", name: "createPresignedPost" }, + }, + }).length + ) + return true; + } + + return false; +}; 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/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) { 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);