Skip to content

Commit 63d870b

Browse files
authored
Add transformation for s3 createPresignedPost (#863)
1 parent 2db36c9 commit 63d870b

11 files changed

+98
-0
lines changed

.changeset/poor-timers-brake.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": minor
3+
---
4+
5+
Add transformation for s3 createPresignedPost
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import AWS from "aws-sdk";
2+
3+
const s3 = new AWS.S3();
4+
const params = { Bucket: "bucket", Fields: { key: "key" } };
5+
6+
s3.createPresignedPost(params, function (err, data) {
7+
console.log('The URL is', data.url);
8+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import AWS from "aws-sdk";
2+
3+
const s3 = new AWS.S3();
4+
const params = { Bucket: "bucket", Fields: { key: "key" } };
5+
6+
// S3 createPresignedPost with callbacks is not supported in AWS SDK for JavaScript (v3).
7+
// Please convert to 'client.createPresignedPost(params)', and re-run aws-sdk-js-codemod.
8+
s3.createPresignedPost(params, function (err, data) {
9+
console.log('The URL is', data.url);
10+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import AWS from "aws-sdk";
2+
3+
const client = new AWS.S3();
4+
const response = client.createPresignedPost(params);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createPresignedPost } from "@aws-sdk/s3-presigned-post";
2+
import { S3 } from "@aws-sdk/client-s3";
3+
4+
const client = new S3();
5+
const response = await createPresignedPost(client, params);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ export const addNotSupportedClientComments = (
5454
apiDescription: "S3 getSignedUrl",
5555
apiSuggestion: "client.getSignedUrl(apiName, options)",
5656
},
57+
{
58+
apiName: "createPresignedPost",
59+
apiDescription: "S3 createPresignedPost",
60+
apiSuggestion: "client.createPresignedPost(params)",
61+
},
5762
];
5863
for (const { apiName, apiDescription, apiSuggestion } of apiMetadata) {
5964
source

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ export * from "./getClientWaiterStates";
66
export * from "./getCommandName";
77
export * from "./getS3SignedUrlApiNames";
88
export * from "./getV3ClientWaiterApiName";
9+
export * from "./isS3CreatePresignedPostApiUsed";
910
export * from "./isS3GetSignedUrlApiUsed";
1011
export * from "./isS3UploadApiUsed";
1112
export * from "./removePromiseCalls";
1213
export * from "./renameErrorCodeWithName";
1314
export * from "./replaceAwsEndpoint";
1415
export * from "./replaceAwsError";
1516
export * from "./replaceAwsIdentity";
17+
export * from "./replaceS3CreatePresignedPostApi";
1618
export * from "./replaceS3GetSignedUrlApi";
1719
export * from "./replaceS3UploadApi";
1820
export * from "./replaceWaiterApi";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import { ClientIdentifier } from "../types";
4+
5+
export const isS3CreatePresignedPostApiUsed = (
6+
j: JSCodeshift,
7+
source: Collection<unknown>,
8+
clientIdentifiers: ClientIdentifier[]
9+
) => {
10+
for (const clientId of clientIdentifiers) {
11+
if (
12+
source.find(j.CallExpression, {
13+
callee: {
14+
type: "MemberExpression",
15+
object: clientId,
16+
property: { type: "Identifier", name: "createPresignedPost" },
17+
},
18+
}).length
19+
)
20+
return true;
21+
}
22+
23+
return false;
24+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import { ClientIdentifier } from "../types";
4+
import { getClientApiCallExpression } from "./getClientApiCallExpression";
5+
6+
// Updates `s3.createPresignedPost(params)` API with `await createPresignedPost(s3, params)` API.
7+
export const replaceS3CreatePresignedPostApi = (
8+
j: JSCodeshift,
9+
source: Collection<unknown>,
10+
clientIdentifiers: ClientIdentifier[]
11+
): void => {
12+
for (const clientId of clientIdentifiers) {
13+
source
14+
.find(j.CallExpression, getClientApiCallExpression(clientId, "createPresignedPost"))
15+
.replaceWith((callExpression) =>
16+
j.awaitExpression.from({
17+
argument: j.callExpression.from({
18+
callee: j.identifier("createPresignedPost"),
19+
arguments: [clientId, callExpression.node.arguments[0]],
20+
}),
21+
})
22+
);
23+
}
24+
};

src/transforms/v2-to-v3/modules/addClientModules.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getCommandName,
66
getS3SignedUrlApiNames,
77
getV3ClientWaiterApiName,
8+
isS3CreatePresignedPostApiUsed,
89
isS3GetSignedUrlApiUsed,
910
isS3UploadApiUsed,
1011
} from "../apis";
@@ -96,6 +97,14 @@ export const addClientModules = (
9697
});
9798
}
9899
}
100+
101+
if (isS3CreatePresignedPostApiUsed(j, source, clientIdentifiers)) {
102+
addNamedModule(j, source, {
103+
importType,
104+
localName: "createPresignedPost",
105+
packageName: "@aws-sdk/s3-presigned-post",
106+
});
107+
}
99108
}
100109

101110
if (v2ClientName === DYNAMODB) {

src/transforms/v2-to-v3/transformer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
addEmptyObjectForUndefined,
1414
renameErrorCodeWithName,
1515
addPromiseRemovalComments,
16+
replaceS3CreatePresignedPostApi,
1617
} from "./apis";
1718
import { replaceAwsUtilFunctions } from "./aws-util";
1819
import {
@@ -118,6 +119,7 @@ const transformer = async (file: FileInfo, api: API) => {
118119

119120
if (v2ClientName === S3) {
120121
replaceS3GetSignedUrlApi(j, source, clientIdentifiers);
122+
replaceS3CreatePresignedPostApi(j, source, clientIdentifiers);
121123
}
122124

123125
replaceWaiterApi(j, source, clientIdentifiers);

0 commit comments

Comments
 (0)