aws-sdk-js-codemod
Version:
Collection of codemod scripts that help update AWS SDK for JavaScript APIs
53 lines (52 loc) • 2.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.replaceS3UploadApi = void 0;
const getClientApiCallExpression_1 = require("./getClientApiCallExpression");
// Updates `s3.upload()` API with `new Upload()` API.
const replaceS3UploadApi = (j, source, clientIdentifiers) => {
for (const clientId of clientIdentifiers) {
// Replace `.promise()` call with `.done()` if present.
source
.find(j.MemberExpression, {
type: "MemberExpression",
object: (0, getClientApiCallExpression_1.getClientApiCallExpression)(clientId, "upload"),
property: { type: "Identifier", name: "promise" },
})
.forEach((memberExpression) => {
memberExpression.value.property.name = "done";
});
// Replace `.upload()` call with `new Upload()` call.
source
.find(j.CallExpression, (0, getClientApiCallExpression_1.getClientApiCallExpression)(clientId, "upload"))
.replaceWith((callExpression) => {
const params = callExpression.node.arguments[0];
const properties = [];
properties.push(j.objectProperty.from({
key: j.identifier("client"),
value: clientId,
shorthand: true,
}));
if (params) {
properties.push(j.objectProperty.from({
key: j.identifier("params"),
// @ts-expect-error Type 'SpreadElement | ExpressionKind' is not assignable
value: params,
shorthand: true,
}));
}
const options = callExpression.node.arguments[1];
if (options) {
switch (options.type) {
case "ObjectExpression":
properties.push(...options.properties);
break;
case "Identifier":
properties.push(j.spreadElement(options));
break;
}
}
return j.newExpression(j.identifier("Upload"), [j.objectExpression(properties)]);
});
}
};
exports.replaceS3UploadApi = replaceS3UploadApi;