|
1 | 1 | import { Collection, JSCodeshift } from "jscodeshift";
|
2 | 2 |
|
3 |
| -import { addClientImportEquals } from "./addClientImportEquals"; |
4 |
| -import { addClientImports } from "./addClientImports"; |
5 |
| -import { addClientRequires } from "./addClientRequires"; |
| 3 | +import { |
| 4 | + getClientWaiterStates, |
| 5 | + getCommandName, |
| 6 | + getS3SignedUrlApiNames, |
| 7 | + getV3ClientWaiterApiName, |
| 8 | + isS3GetSignedUrlApiUsed, |
| 9 | + isS3UploadApiUsed, |
| 10 | +} from "../apis"; |
| 11 | +import { getV3ClientTypesCount } from "../ts-type"; |
| 12 | +import { getClientTSTypeRefCount } from "./getClientTSTypeRefCount"; |
| 13 | +import { getDocClientNewExpressionCount } from "./getDocClientNewExpressionCount"; |
| 14 | +import { getNewExpressionCount } from "./getNewExpressionCount"; |
6 | 15 | import { hasImportEquals } from "./hasImportEquals";
|
7 | 16 | import { hasRequire } from "./hasRequire";
|
| 17 | + |
| 18 | +import * as importEqualsModule from "./importEqualsModule"; |
| 19 | +import * as importModule from "./importModule"; |
| 20 | +import * as requireModule from "./requireModule"; |
8 | 21 | import { ClientModulesOptions } from "./types";
|
9 | 22 |
|
10 | 23 | export const addClientModules = (
|
11 | 24 | j: JSCodeshift,
|
12 | 25 | source: Collection<unknown>,
|
13 | 26 | options: ClientModulesOptions
|
14 |
| -): void => |
15 |
| - hasRequire(j, source) |
16 |
| - ? addClientRequires(j, source, options) |
| 27 | +): void => { |
| 28 | + const { addClientDefaultModule, addClientNamedModule } = hasRequire(j, source) |
| 29 | + ? requireModule |
17 | 30 | : hasImportEquals(j, source)
|
18 |
| - ? addClientImportEquals(j, source, options) |
19 |
| - : addClientImports(j, source, options); |
| 31 | + ? importEqualsModule |
| 32 | + : importModule; |
| 33 | + |
| 34 | + const v3ClientTypesCount = getV3ClientTypesCount(j, source, options); |
| 35 | + const newExpressionCount = getNewExpressionCount(j, source, options); |
| 36 | + const clientTSTypeRefCount = getClientTSTypeRefCount(j, source, options); |
| 37 | + const waiterStates = getClientWaiterStates(j, source, options); |
| 38 | + |
| 39 | + // Add default import for types, if needed. |
| 40 | + if (v3ClientTypesCount > 0) { |
| 41 | + addClientDefaultModule(j, source, options); |
| 42 | + } |
| 43 | + |
| 44 | + if (newExpressionCount > 0 || clientTSTypeRefCount > 0) { |
| 45 | + addClientNamedModule(j, source, { |
| 46 | + ...options, |
| 47 | + importedName: options.v3ClientName, |
| 48 | + localName: options.v2ClientLocalName, |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + for (const waiterState of waiterStates) { |
| 53 | + const v3WaiterApiName = getV3ClientWaiterApiName(waiterState); |
| 54 | + addClientNamedModule(j, source, { |
| 55 | + ...options, |
| 56 | + importedName: v3WaiterApiName, |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + if (isS3UploadApiUsed(j, source, options)) { |
| 61 | + addClientNamedModule(j, source, { |
| 62 | + ...options, |
| 63 | + importedName: "Upload", |
| 64 | + v3ClientPackageName: "@aws-sdk/lib-storage", |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + if (isS3GetSignedUrlApiUsed(j, source, options)) { |
| 69 | + addClientNamedModule(j, source, { |
| 70 | + ...options, |
| 71 | + importedName: "getSignedUrl", |
| 72 | + v3ClientPackageName: "@aws-sdk/s3-request-presigner", |
| 73 | + }); |
| 74 | + for (const apiName of getS3SignedUrlApiNames(j, source, options)) { |
| 75 | + addClientNamedModule(j, source, { |
| 76 | + ...options, |
| 77 | + importedName: getCommandName(apiName), |
| 78 | + }); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + const docClientNewExpressionCount = getDocClientNewExpressionCount(j, source, options); |
| 83 | + if (docClientNewExpressionCount > 0) { |
| 84 | + addClientNamedModule(j, source, { |
| 85 | + ...options, |
| 86 | + importedName: "DynamoDBDocument", |
| 87 | + v3ClientPackageName: "@aws-sdk/lib-dynamodb", |
| 88 | + }); |
| 89 | + } |
| 90 | +}; |
0 commit comments