aws / aws-sdk-js-codemod

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

[Feature]: The v2-to-v3 transform should convert to v3 style of making API calls #497

Open trivikr opened 1 year ago

trivikr commented 1 year ago

Self-service

Template name

v2-to-v3

Input code

import AWS from "aws-sdk";

const region = "us-west-2";
const client = new AWS.DynamoDB({ region });
const response = await client.listTables().promise();

Expected Output

Existing output

import { DynamoDB } from "@aws-sdk/client-dynamodb";

const region = "us-west-2";
const client = new DynamoDB({ region });
const response = await client.listTables();

Recommended output

import { DynamoDB, ListTablesCommand } from "@aws-sdk/client-dynamodb";

const region = "us-west-2";
const client = new DynamoDB({ region });
const response = await client.send(new ListTablesCommand({}));

Additional context

Reference blog post: https://www.10printiamcool.com/updating-to-nodejs-18-and-aws-javascript-sdk-v3-part-1

trivikr commented 1 year ago

The codemod defaults to making minimal changes to the customer code to move from v2 to v3. And the code written in v2 will never use command style of calling APIs, as it was introduced in v3.

trivikr commented 1 year ago

A better solution could be to write a different transform which converts application code which uses v3 aggregated client to v3 barebones client. That transform can be written separately, but used by default in v2-to-v3.

Example (transform name is assumed to be to-bare-bones-client):

$ cat example.ts
import { DynamoDB } from "@aws-sdk/client-dynamodb";

const region = "us-west-2";
const client = new DynamoDB({ region });
const response = await client.listTables();

$ npx aws-sdk-js-codemod -t to-bare-bones-client example.ts

$ cat example.ts
import { DynamoDB, ListTablesCommand } from "@aws-sdk/client-dynamodb";

const region = "us-west-2";
const client = new DynamoDB({ region });
const response = await client.send(new ListTablesCommand({}));
trivikr commented 1 year ago

In https://github.com/awslabs/aws-sdk-js-codemod/issues/480#issuecomment-1480352172, the name api-to-command was recommended for transformer which updates JS SDK v3 code to use commands.

trivikr commented 1 year ago

We discussed the naming during scrum on 2023-08-15, we decided to stick with to-command-syntax name for the transformer.

BHunter2889 commented 7 months ago

We discussed the naming during scrum on 2023-08-15, we decided to stick with to-command-syntax name for the transformer.

Are there any updates to this? Are there any known community provided transformers out there? We're actively migrating now so this would be a huge win.