Skip to content

Commit 3a9465b

Browse files
authored
Make code reusable in addClientModules (#525)
1 parent 31f3ef4 commit 3a9465b

15 files changed

+139
-304
lines changed

src/transforms/v2-to-v3/modules/addClientImportEquals.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.

src/transforms/v2-to-v3/modules/addClientImports.ts

Lines changed: 0 additions & 80 deletions
This file was deleted.
Lines changed: 79 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,90 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
22

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";
615
import { hasImportEquals } from "./hasImportEquals";
716
import { hasRequire } from "./hasRequire";
17+
18+
import * as importEqualsModule from "./importEqualsModule";
19+
import * as importModule from "./importModule";
20+
import * as requireModule from "./requireModule";
821
import { ClientModulesOptions } from "./types";
922

1023
export const addClientModules = (
1124
j: JSCodeshift,
1225
source: Collection<unknown>,
1326
options: ClientModulesOptions
14-
): void =>
15-
hasRequire(j, source)
16-
? addClientRequires(j, source, options)
27+
): void => {
28+
const { addClientDefaultModule, addClientNamedModule } = hasRequire(j, source)
29+
? requireModule
1730
: 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+
};

src/transforms/v2-to-v3/modules/addClientRequires.ts

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { JSCodeshift } from "jscodeshift";
22

3-
import { RequirePropertyOptions } from "./types";
3+
import { ImportSpecifierOptions } from "./types";
44

55
export const getRequireProperty = (
66
j: JSCodeshift,
7-
{ keyName, valueName }: RequirePropertyOptions
7+
{ importedName, localName }: ImportSpecifierOptions
88
) =>
99
j.objectProperty.from({
10-
key: j.identifier(keyName),
11-
value: j.identifier(valueName ?? keyName),
10+
key: j.identifier(importedName),
11+
value: j.identifier(localName ?? importedName),
1212
shorthand: true,
1313
});

src/transforms/v2-to-v3/modules/addClientDefaultImportEquals.ts renamed to src/transforms/v2-to-v3/modules/importEqualsModule/addClientDefaultModule.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
22

3-
import { getDefaultLocalName } from "../utils";
4-
import { getImportEqualsDeclaration } from "./getImportEqualsDeclaration";
5-
import { getImportEqualsDeclarationType } from "./getImportEqualsDeclarationType";
6-
import { getImportEqualsLocalNameSuffix } from "./getImportEqualsLocalNameSuffix";
7-
import { ClientModulesOptions } from "./types";
3+
import { getDefaultLocalName } from "../../utils";
4+
import { getImportEqualsDeclaration } from "../getImportEqualsDeclaration";
5+
import { getImportEqualsDeclarationType } from "../getImportEqualsDeclarationType";
6+
import { getImportEqualsLocalNameSuffix } from "../getImportEqualsLocalNameSuffix";
7+
import { ClientModulesOptions } from "../types";
88

9-
export const addClientDefaultImportEquals = (
9+
export const addClientDefaultModule = (
1010
j: JSCodeshift,
1111
source: Collection<unknown>,
1212
{ v2ClientLocalName, v2ClientName, v2GlobalName, v3ClientPackageName }: ClientModulesOptions

0 commit comments

Comments
 (0)