UNPKG

aws-sdk-js-codemod

Version:

Collection of codemod scripts that help update AWS SDK for JavaScript APIs

79 lines (78 loc) 3.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.replaceTSTypeReference = void 0; const config_1 = require("../config"); const getClientTypeNames_1 = require("./getClientTypeNames"); const getTSQualifiedNameFromClientName_1 = require("./getTSQualifiedNameFromClientName"); const getV3ClientType_1 = require("./getV3ClientType"); // Replace v2 client type reference with v3 client type reference. const replaceTSTypeReference = (j, source, options) => { const { v2ClientName, v2ClientLocalName, v2GlobalName, v3ClientName } = options; const clientTypeOptions = { v2ClientName, v2ClientLocalName }; // DynamoDb DocumentClient types need to be updated first. if (v2ClientName === config_1.DYNAMODB) { const ddbClientLocalName = `${v2ClientLocalName}.${config_1.DOCUMENT_CLIENT}`; (0, exports.replaceTSTypeReference)(j, source, { ...options, v2ClientName: config_1.DYNAMODB_DOCUMENT_CLIENT, v2ClientLocalName: ddbClientLocalName, v3ClientName: config_1.DYNAMODB_DOCUMENT, }); source .find(j.TSTypeReference, { typeName: (0, getTSQualifiedNameFromClientName_1.getTSQualifiedNameFromClientName)(ddbClientLocalName) }) .replaceWith(() => j.tsTypeReference(j.identifier(config_1.DYNAMODB_DOCUMENT))); } if (v2GlobalName) { // Replace type reference to client created with global name. source .find(j.TSTypeReference, { typeName: (0, getTSQualifiedNameFromClientName_1.getTSQualifiedNameFromClientName)(v2ClientName, v2GlobalName), }) .replaceWith((v2ClientType) => j.tsTypeReference(j.identifier(v3ClientName), v2ClientType.node.typeParameters)); // Replace reference to client types created with global name. source .find(j.TSTypeReference, { typeName: { left: (0, getTSQualifiedNameFromClientName_1.getTSQualifiedNameFromClientName)(v2ClientName, v2GlobalName), }, }) .replaceWith((v2ClientType) => { const tSQualifiedName = v2ClientType.node.typeName; if (tSQualifiedName.right.type !== "Identifier") { return v2ClientType; } const v2ClientTypeName = tSQualifiedName.right.name; return (0, getV3ClientType_1.getV3ClientType)(j, { ...clientTypeOptions, v2ClientTypeName }); }); } source .find(j.TSTypeReference, { typeName: (0, getTSQualifiedNameFromClientName_1.getTSQualifiedNameFromClientName)(v2ClientName) }) .replaceWith(() => j.tsTypeReference(j.identifier(v3ClientName))); source .find(j.TSTypeReference, { typeName: { left: (0, getTSQualifiedNameFromClientName_1.getTSQualifiedNameFromClientName)(v2ClientLocalName) }, }) .replaceWith((v2ClientType) => { const tSQualifiedName = v2ClientType.node.typeName; if (tSQualifiedName.right.type !== "Identifier") { return v2ClientType; } const v2ClientTypeName = tSQualifiedName.right.name; return (0, getV3ClientType_1.getV3ClientType)(j, { ...clientTypeOptions, v2ClientTypeName }); }); // Replace type reference to client type with modules. const clientTypeNames = (0, getClientTypeNames_1.getClientTypeNames)(j, source, { v2ClientLocalName, v2ClientName, v2GlobalName, }); for (const clientTypeName of clientTypeNames) { source .find(j.TSTypeReference, { typeName: { type: "Identifier", name: clientTypeName } }) .replaceWith((v2ClientType) => { const v2ClientTypeName = v2ClientType.node.typeName.name; return (0, getV3ClientType_1.getV3ClientType)(j, { ...clientTypeOptions, v2ClientTypeName }); }); } }; exports.replaceTSTypeReference = replaceTSTypeReference;