diff --git a/.changeset/famous-houses-wave.md b/.changeset/famous-houses-wave.md new file mode 100644 index 000000000..bf1590a67 --- /dev/null +++ b/.changeset/famous-houses-wave.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": minor +--- + +Transform AWS.Endpoint to URL diff --git a/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/global-import-equals.input.ts b/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/global-import-equals.input.ts new file mode 100644 index 000000000..435186daa --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/global-import-equals.input.ts @@ -0,0 +1,3 @@ +import AWS = require("aws-sdk"); + +const endpoint = new AWS.Endpoint("http://localhost:8000/"); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/global-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/global-import-equals.output.ts new file mode 100644 index 000000000..49021c52b --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/global-import-equals.output.ts @@ -0,0 +1 @@ +const endpoint = new URL("http://localhost:8000/"); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/global-import.input.js b/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/global-import.input.js new file mode 100644 index 000000000..58f575986 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/global-import.input.js @@ -0,0 +1,3 @@ +import AWS from "aws-sdk"; + +const endpoint = new AWS.Endpoint("http://localhost:8000/"); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/global-import.output.js b/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/global-import.output.js new file mode 100644 index 000000000..49021c52b --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/global-import.output.js @@ -0,0 +1 @@ +const endpoint = new URL("http://localhost:8000/"); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/named-import.input.js b/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/named-import.input.js new file mode 100644 index 000000000..a40f6938b --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/named-import.input.js @@ -0,0 +1,3 @@ +import { Endpoint } from "aws-sdk"; + +const endpoint = new Endpoint("http://localhost:8000/"); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/named-import.output.js b/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/named-import.output.js new file mode 100644 index 000000000..49021c52b --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/aws-endpoint/named-import.output.js @@ -0,0 +1 @@ +const endpoint = new URL("http://localhost:8000/"); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/config/endpoint.input.js b/src/transforms/v2-to-v3/__fixtures__/config/endpoint.input.js new file mode 100644 index 000000000..139215708 --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/config/endpoint.input.js @@ -0,0 +1,5 @@ +import AWS from "aws-sdk"; + +const config = new AWS.Config({ + endpoint: "http://localhost" +}); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/config/endpoint.output.js b/src/transforms/v2-to-v3/__fixtures__/config/endpoint.output.js new file mode 100644 index 000000000..eb261be4e --- /dev/null +++ b/src/transforms/v2-to-v3/__fixtures__/config/endpoint.output.js @@ -0,0 +1,3 @@ +const config = { + endpoint: "http://localhost" +}; \ No newline at end of file diff --git a/src/transforms/v2-to-v3/apis/index.ts b/src/transforms/v2-to-v3/apis/index.ts index aed68b72d..416cbdc80 100644 --- a/src/transforms/v2-to-v3/apis/index.ts +++ b/src/transforms/v2-to-v3/apis/index.ts @@ -10,6 +10,7 @@ export * from "./isS3GetSignedUrlApiUsed"; export * from "./isS3UploadApiUsed"; export * from "./removePromiseCalls"; export * from "./renameErrorCodeWithName"; +export * from "./replaceAwsEndpoint"; export * from "./replaceAwsError"; export * from "./replaceAwsIdentity"; export * from "./replaceS3GetSignedUrlApi"; diff --git a/src/transforms/v2-to-v3/apis/replaceAwsEndpoint.ts b/src/transforms/v2-to-v3/apis/replaceAwsEndpoint.ts new file mode 100644 index 000000000..77d11571e --- /dev/null +++ b/src/transforms/v2-to-v3/apis/replaceAwsEndpoint.ts @@ -0,0 +1,31 @@ +import { Collection, JSCodeshift } from "jscodeshift"; + +export const replaceAwsEndpoint = ( + j: JSCodeshift, + source: Collection, + v2GlobalName?: string +) => { + const endpointCtr = "Endpoint"; + + if (v2GlobalName) { + source + .find(j.NewExpression, { + callee: { + type: "MemberExpression", + object: { type: "Identifier", name: v2GlobalName }, + property: { type: "Identifier", name: endpointCtr }, + }, + }) + .replaceWith((newExpression) => + j.newExpression(j.identifier("URL"), newExpression.node.arguments) + ); + } + + source + .find(j.NewExpression, { + callee: { type: "Identifier", name: endpointCtr }, + }) + .replaceWith((newExpression) => + j.newExpression(j.identifier("URL"), newExpression.node.arguments) + ); +}; diff --git a/src/transforms/v2-to-v3/config/AWS_CONFIG_KEY_MAP.ts b/src/transforms/v2-to-v3/config/AWS_CONFIG_KEY_MAP.ts index 57e3ac649..504de98b2 100644 --- a/src/transforms/v2-to-v3/config/AWS_CONFIG_KEY_MAP.ts +++ b/src/transforms/v2-to-v3/config/AWS_CONFIG_KEY_MAP.ts @@ -22,6 +22,7 @@ export const AWS_CONFIG_KEY_MAP: Record = { deprecationMessage: "The clock skew correction is applied by default.", }, credentials: {}, + endpoint: {}, endpointCacheSize: {}, endpointDiscoveryEnabled: {}, hostPrefixEnabled: { diff --git a/src/transforms/v2-to-v3/transformer.ts b/src/transforms/v2-to-v3/transformer.ts index 092b4ef64..83f5717ba 100644 --- a/src/transforms/v2-to-v3/transformer.ts +++ b/src/transforms/v2-to-v3/transformer.ts @@ -9,6 +9,7 @@ import { replaceS3GetSignedUrlApi, getClientIdentifiersRecord, replaceAwsIdentity, + replaceAwsEndpoint, replaceAwsError, addEmptyObjectForUndefined, renameErrorCodeWithName, @@ -116,6 +117,7 @@ const transformer = async (file: FileInfo, api: API) => { replaceAwsIdentity(j, source, { v2GlobalName, importType }); replaceAwsUtilFunctions(j, source, v2GlobalName); replaceAwsError(j, source, { v2GlobalName, importType }); + replaceAwsEndpoint(j, source, v2GlobalName); removeModules(j, source, importType); const sourceString = getFormattedSourceString(source.toSource({ quote, useTabs, trailingComma }));