UNPKG

aws-sdk-js-codemod

Version:

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

104 lines (103 loc) 4.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.addClientModules = void 0; const apis_1 = require("../apis"); const config_1 = require("../config"); const ts_type_1 = require("../ts-type"); const addNamedModule_1 = require("./addNamedModule"); const getClientTSTypeRefCount_1 = require("./getClientTSTypeRefCount"); const getNewExpressionCount_1 = require("./getNewExpressionCount"); const addClientModules = (j, source, options) => { const { clientIdentifiers, v2ClientName, v3ClientName, v3ClientPackageName, v2ClientLocalName, importType, } = options; const v3ClientTypes = (0, ts_type_1.getV3ClientTypes)(j, source, options); const newExpressionCount = (0, getNewExpressionCount_1.getNewExpressionCount)(j, source, options); const clientTSTypeRefCount = (0, getClientTSTypeRefCount_1.getClientTSTypeRefCount)(j, source, options); const waiterStates = (0, apis_1.getClientWaiterStates)(j, source, clientIdentifiers); // Add named import for types, if needed. for (const v3ClientType of v3ClientTypes) { if (v2ClientName === config_1.DYNAMODB && v3ClientType === config_1.DOCUMENT_CLIENT) { // Not a type, but a class. Skip adding import for DocumentClient. continue; } (0, addNamedModule_1.addNamedModule)(j, source, { importType, localName: v3ClientType, packageName: v3ClientPackageName, }); } if (newExpressionCount > 0 || clientTSTypeRefCount > 0) { (0, addNamedModule_1.addNamedModule)(j, source, { importType, importedName: v3ClientName, localName: v2ClientName === v2ClientLocalName ? v3ClientName : v2ClientLocalName, packageName: v3ClientPackageName, }); } for (const waiterState of waiterStates) { const v3WaiterApiName = (0, apis_1.getV3ClientWaiterApiName)(waiterState); (0, addNamedModule_1.addNamedModule)(j, source, { importType, localName: v3WaiterApiName, packageName: v3ClientPackageName, }); } if (v2ClientName === config_1.S3) { if ((0, apis_1.isS3UploadApiUsed)(j, source, clientIdentifiers)) { (0, addNamedModule_1.addNamedModule)(j, source, { importType, localName: "Upload", packageName: "@aws-sdk/lib-storage", }); } if ((0, apis_1.isS3GetSignedUrlApiUsed)(j, source, clientIdentifiers)) { (0, addNamedModule_1.addNamedModule)(j, source, { importType, localName: "getSignedUrl", packageName: "@aws-sdk/s3-request-presigner", }); for (const apiName of (0, apis_1.getS3SignedUrlApiNames)(j, source, clientIdentifiers)) { (0, addNamedModule_1.addNamedModule)(j, source, { importType, localName: (0, apis_1.getCommandName)(apiName), packageName: v3ClientPackageName, }); } } if ((0, apis_1.isS3CreatePresignedPostApiUsed)(j, source, clientIdentifiers)) { (0, addNamedModule_1.addNamedModule)(j, source, { importType, localName: "createPresignedPost", packageName: "@aws-sdk/s3-presigned-post", }); } } if (v2ClientName === config_1.DYNAMODB) { const { v2ClientLocalName } = options; const docClientOptions = { ...options, v2ClientName: config_1.DYNAMODB_DOCUMENT_CLIENT, ...(v2ClientLocalName && { v2ClientLocalName: `${v2ClientLocalName}.${config_1.DOCUMENT_CLIENT}`, }), }; const docClientTypes = (0, ts_type_1.getV3ClientTypes)(j, source, docClientOptions); const docClientNewExpressionCount = (0, getNewExpressionCount_1.getNewExpressionCount)(j, source, docClientOptions); const docClientTSTypeRefCount = (0, getClientTSTypeRefCount_1.getClientTSTypeRefCount)(j, source, docClientOptions); // Add named import for types, if needed. for (const docClientType of docClientTypes) { (0, addNamedModule_1.addNamedModule)(j, source, { importType, localName: docClientType, packageName: "@aws-sdk/lib-dynamodb", }); } if (docClientNewExpressionCount > 0 || docClientTSTypeRefCount > 0) { (0, addNamedModule_1.addNamedModule)(j, source, { importType, localName: config_1.DYNAMODB_DOCUMENT, packageName: "@aws-sdk/lib-dynamodb", }); } } }; exports.addClientModules = addClientModules;