aws / aws-sdk-js-codemod

Codemod scripts to update AWS SDK for JavaScript APIs.
MIT No Attribution
72 stars 10 forks source link

[Feature]: aws-sdk not changed after converted #753

Open mutongwu opened 9 months ago

mutongwu commented 9 months ago

Self-service

Describe the bug

I'd like to update my nodejs version from 16.x to 18.x. And when I run npx aws-sdk-js-codemod -t v2-to-v3, he statement var AWS = require("aws-sdk"); not changed after converted.

Steps to reproduce

source fie

var AWS = require("aws-sdk");
var endpoint = new AWS.Endpoint(domain);
var request = new AWS.HttpRequest(endpoint, region);

request.method = method;
request.path += index;
request.body = "";
request.headers["host"] = domain;
request.headers["Content-Type"] = "application/json";
request.headers["Content-Length"] = Buffer.byteLength(request.body);

var credentials = new AWS.EnvironmentCredentials("AWS");
var signer = new AWS.Signers.V4(request, "es");
signer.addAuthorization(credentials, new Date());

var client = new AWS.HttpClient();
client.handleRequest(
  request,
  null,
  function (response) {
    console.log(response.statusCode + " " + response.statusMessage);
    var responseBody = "";
    response.on("data", function (chunk) {
      responseBody += chunk;
    });
    response.on("end", function (chunk) {
      success(responseBody);
    });
  },
  function (error) {
    console.log("Error: " + error);
  }
);

output

var AWS = require("aws-sdk");

const { fromEnv } = require("@aws-sdk/credential-providers");

var endpoint = new AWS.Endpoint(domain);
var request = new AWS.HttpRequest(endpoint, region);

request.method = method;
request.path += index;
request.body = "";
request.headers["host"] = domain;
request.headers["Content-Type"] = "application/json";
request.headers["Content-Length"] = Buffer.byteLength(request.body);

var credentials = // JS SDK v3 switched credential providers from classes to functions.
// This is the closest approximation from codemod of what your application needs.
// Reference: https://www.npmjs.com/package/@aws-sdk/credential-providers
fromEnv("AWS");
var signer = new AWS.Signers.V4(request, "es");
signer.addAuthorization(credentials, new Date());

var client = new AWS.HttpClient();
client.handleRequest(
  request,
  null,
  function (response) {
    console.log(response.statusCode + " " + response.statusMessage);
    var responseBody = "";
    response.on("data", function (chunk) {
      responseBody += chunk;
    });
    response.on("end", function (chunk) {
      success(responseBody);
    });
  },
  function (error) {
    console.log("Error: " + error);
  }
);

Observed behavior

$ npx aws-sdk-js-codemod -t v2-to-v3 part.js

╔════════════════════════════════════════════════════════╗
║ Please review the code change thoroughly for required  ║
║ functionality before deploying it to production.       ║
║                                                        ║
║ If the transformation is not complete or is incorrect, ║
║ please report the issue on GitHub.                     ║
╚════════════════════════════════════════════════════════╝

Processing 1 files...
Spawning 1 workers...
Sending 1 files to free worker...
All done.
Results:
0 errors
0 unmodified
0 skipped
1 ok
Time elapsed: 0.244seconds

Expected behavior

require("aws-sdk"); should changed to something from '@aws-sdk/XXX' right? And AWS.Signers ,AWS.HttpRequest , AWS.Endpoint should changed relevantly

Environment

npx aws-sdk-js-codemod --version
aws-sdk-js-codemod: 1.0.0
- jscodeshift: 0.15.0
- recast: 0.23.4

Additional context

No response

trivikr commented 8 months ago
var endpoint = new AWS.Endpoint(domain);

Feature Request: https://github.com/aws/aws-sdk-js-codemod/issues/694

trivikr commented 8 months ago

@mutongwu Where is the signer used in your sample code?

I see only the following lines:

var signer = new AWS.Signers.V4(request, "es");
signer.addAuthorization(credentials, new Date());

The Signers.V4 was removed from public interface https://github.com/aws/aws-sdk-js/issues/1247