Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transformation for s3 createPresignedPost #863

Merged
merged 7 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-timers-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": minor
---

Add transformation for s3 createPresignedPost
Original file line number Diff line number Diff line change
@@ -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);
});
Original file line number Diff line number Diff line change
@@ -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);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import AWS from "aws-sdk";

const client = new AWS.S3();
const response = client.createPresignedPost(params);
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/transforms/v2-to-v3/apis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ export * from "./getClientWaiterStates";
export * from "./getCommandName";
export * from "./getS3SignedUrlApiNames";
export * from "./getV3ClientWaiterApiName";
export * from "./isS3CreatePresignedPostApiUsed";
export * from "./isS3GetSignedUrlApiUsed";
export * from "./isS3UploadApiUsed";
export * from "./removePromiseCalls";
export * from "./renameErrorCodeWithName";
export * from "./replaceAwsEndpoint";
export * from "./replaceAwsError";
export * from "./replaceAwsIdentity";
export * from "./replaceS3CreatePresignedPostApi";
export * from "./replaceS3GetSignedUrlApi";
export * from "./replaceS3UploadApi";
export * from "./replaceWaiterApi";
24 changes: 24 additions & 0 deletions src/transforms/v2-to-v3/apis/isS3CreatePresignedPostApiUsed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { ClientIdentifier } from "../types";

export const isS3CreatePresignedPostApiUsed = (
j: JSCodeshift,
source: Collection<unknown>,
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;
};
24 changes: 24 additions & 0 deletions src/transforms/v2-to-v3/apis/replaceS3CreatePresignedPostApi.ts
Original file line number Diff line number Diff line change
@@ -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<unknown>,
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]],
}),
})
);
}
};
9 changes: 9 additions & 0 deletions src/transforms/v2-to-v3/modules/addClientModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getCommandName,
getS3SignedUrlApiNames,
getV3ClientWaiterApiName,
isS3CreatePresignedPostApiUsed,
isS3GetSignedUrlApiUsed,
isS3UploadApiUsed,
} from "../apis";
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
addEmptyObjectForUndefined,
renameErrorCodeWithName,
addPromiseRemovalComments,
replaceS3CreatePresignedPostApi,
} from "./apis";
import { replaceAwsUtilFunctions } from "./aws-util";
import {
Expand Down Expand Up @@ -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);
Expand Down