Skip to content

Commit 4df3162

Browse files
authored
chore(config-resolver): refactor regionInfo types to improve readability (#2934)
1 parent 5e1d297 commit 4df3162

11 files changed

+84
-133
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* The hash of partition with the information specific to that partition.
3+
* The information includes the list of regions belonging to that partition,
4+
* and the hostname to be used for the partition.
5+
*/
6+
export type PartitionHash = {
7+
[key: string]: { regions: string[]; regionRegex: string; hostname?: string; endpoint?: string };
8+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { RegionInfo } from "@aws-sdk/types";
2+
3+
/**
4+
* The hash of region with the information specific to that region.
5+
* The information can include hostname, signingService and signingRegion.
6+
*/
7+
export type RegionHash = { [key: string]: Partial<Omit<RegionInfo, "partition" | "path">> };
Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,18 @@
11
import { getHostnameTemplate } from "./getHostnameTemplate";
2-
import { getResolvedPartition, PartitionHash } from "./getResolvedPartition";
3-
4-
jest.mock("./getResolvedPartition");
52

63
const AWS_TEMPLATE = "{signingService}.{region}.amazonaws.com";
74

85
describe(getHostnameTemplate.name, () => {
9-
const mockRegion = "mockRegion";
10-
const mockPartition = "mockPartition";
11-
const mockRegionRegex = "mockRegionRegex";
126
const mockHostname = "{region}.mockHostname.com";
137
const mockSigningService = "mockSigningService";
148

15-
beforeEach(() => {
16-
(getResolvedPartition as jest.Mock).mockReturnValue(mockPartition);
17-
});
18-
19-
afterEach(() => {
20-
expect(getResolvedPartition).toHaveBeenCalledTimes(1);
21-
jest.clearAllMocks();
9+
it("returns partitionHostname if present", () => {
10+
expect(getHostnameTemplate(mockSigningService, { partitionHostname: mockHostname })).toEqual(mockHostname);
2211
});
2312

24-
it("returns hostname template if present in partitionHash", () => {
25-
const mockPartitionHash: PartitionHash = {
26-
[mockPartition]: {
27-
regions: [mockRegion, `${mockRegion}2`, `${mockRegion}3`],
28-
regionRegex: mockRegionRegex,
29-
hostname: mockHostname,
30-
},
31-
};
32-
33-
expect(
34-
getHostnameTemplate(mockRegion, { signingService: mockSigningService, partitionHash: mockPartitionHash })
35-
).toEqual(mockHostname);
36-
expect(getResolvedPartition).toHaveBeenCalledWith(mockRegion, { partitionHash: mockPartitionHash });
37-
});
38-
39-
it("returns default hostname template if not present in partitionHash", () => {
40-
const mockPartitionHash: PartitionHash = {};
41-
42-
expect(
43-
getHostnameTemplate(mockRegion, { signingService: mockSigningService, partitionHash: mockPartitionHash })
44-
).toEqual(AWS_TEMPLATE.replace("{signingService}", mockSigningService));
45-
expect(getResolvedPartition).toHaveBeenCalledWith(mockRegion, { partitionHash: mockPartitionHash });
13+
it("returns default hostname template if partitionHostname is not present", () => {
14+
expect(getHostnameTemplate(mockSigningService, {})).toEqual(
15+
AWS_TEMPLATE.replace("{signingService}", mockSigningService)
16+
);
4617
});
4718
});
Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
import { getResolvedPartition, GetResolvedPartitionOptions } from "./getResolvedPartition";
2-
31
const AWS_TEMPLATE = "{signingService}.{region}.amazonaws.com";
42

5-
export interface GetHostnameTemplateOptions extends GetResolvedPartitionOptions {
6-
/**
7-
* The name to be used while signing the service request.
8-
*/
9-
signingService: string;
3+
export interface GetHostnameTemplateOptions {
4+
partitionHostname?: string;
105
}
116

12-
export const getHostnameTemplate = (region: string, { signingService, partitionHash }: GetHostnameTemplateOptions) =>
13-
partitionHash[getResolvedPartition(region, { partitionHash })]?.hostname ??
14-
AWS_TEMPLATE.replace("{signingService}", signingService);
7+
export const getHostnameTemplate = (signingService: string, { partitionHostname }: GetHostnameTemplateOptions) =>
8+
partitionHostname ?? AWS_TEMPLATE.replace("{signingService}", signingService);

packages/config-resolver/src/regionInfo/getRegionInfo.spec.ts

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { getRegionInfo } from "./getRegionInfo";
2-
import { getResolvedHostname, RegionHash } from "./getResolvedHostname";
3-
import { getResolvedPartition, PartitionHash } from "./getResolvedPartition";
2+
import { getResolvedHostname } from "./getResolvedHostname";
3+
import { getResolvedPartition } from "./getResolvedPartition";
4+
import { PartitionHash } from "./PartitionHash";
5+
import { RegionHash } from "./RegionHash";
46

57
jest.mock("./getResolvedHostname");
68
jest.mock("./getResolvedPartition");
@@ -52,7 +54,7 @@ describe(getRegionInfo.name, () => {
5254

5355
const getMockResolvedPartitionOptions = (partitionHash) => ({ partitionHash });
5456

55-
const getMockResolvedHostnameOptions = (regionHash, getResolvedPartitionOptions) => ({
57+
const getMockRegionInfoOptions = (regionHash, getResolvedPartitionOptions) => ({
5658
...getResolvedPartitionOptions,
5759
signingService: mockSigningService,
5860
regionHash,
@@ -75,21 +77,20 @@ describe(getRegionInfo.name, () => {
7577
const mockPartitionHash = getMockPartitionHash(regionCase);
7678

7779
const mockGetResolvedPartitionOptions = getMockResolvedPartitionOptions(mockPartitionHash);
78-
const mockGetResolvedHostnameOptions = getMockResolvedHostnameOptions(
79-
mockRegionHash,
80-
mockGetResolvedPartitionOptions
81-
);
80+
const mockGetRegionInfoOptions = getMockRegionInfoOptions(mockRegionHash, mockGetResolvedPartitionOptions);
8281

83-
expect(getRegionInfo(mockRegion, mockGetResolvedHostnameOptions)).toEqual({
82+
expect(getRegionInfo(mockRegion, mockGetRegionInfoOptions)).toEqual({
8483
signingService: mockSigningService,
8584
hostname: mockHostname,
8685
partition: mockPartition,
8786
});
8887

89-
expect(getResolvedHostname).toHaveBeenCalledWith(
90-
getMockResolvedRegion(regionCase),
91-
mockGetResolvedHostnameOptions
92-
);
88+
const mockResolvedRegion = getMockResolvedRegion(regionCase);
89+
expect(getResolvedHostname).toHaveBeenCalledWith(mockResolvedRegion, {
90+
signingService: mockSigningService,
91+
regionHostname: mockGetRegionInfoOptions.regionHash[mockResolvedRegion]?.hostname,
92+
partitionHostname: mockGetRegionInfoOptions.partitionHash[mockPartition]?.hostname,
93+
});
9394
expect(getResolvedPartition).toHaveBeenCalledWith(mockRegion, mockGetResolvedPartitionOptions);
9495
});
9596
});
@@ -121,10 +122,7 @@ describe(getRegionInfo.name, () => {
121122
const mockPartitionHash = getMockPartitionHash(regionCase);
122123

123124
const mockGetResolvedPartitionOptions = getMockResolvedPartitionOptions(mockPartitionHash);
124-
const mockGetResolvedHostnameOptions = getMockResolvedHostnameOptions(
125-
mockRegionHash,
126-
mockGetResolvedPartitionOptions
127-
);
125+
const mockGetRegionInfoOptions = getMockRegionInfoOptions(mockRegionHash, mockGetResolvedPartitionOptions);
128126

129127
const mockRegionHashWithSigningRegion = getMockRegionHashWithSigningRegion(
130128
regionCase,
@@ -133,17 +131,19 @@ describe(getRegionInfo.name, () => {
133131
);
134132

135133
expect(
136-
getRegionInfo(mockRegion, { ...mockGetResolvedHostnameOptions, regionHash: mockRegionHashWithSigningRegion })
134+
getRegionInfo(mockRegion, { ...mockGetRegionInfoOptions, regionHash: mockRegionHashWithSigningRegion })
137135
).toEqual({
138136
signingService: mockSigningService,
139137
hostname: mockHostname,
140138
partition: mockPartition,
141139
signingRegion: mockSigningRegion,
142140
});
143141

144-
expect(getResolvedHostname).toHaveBeenCalledWith(getMockResolvedRegion(regionCase), {
145-
...mockGetResolvedHostnameOptions,
146-
regionHash: mockRegionHashWithSigningRegion,
142+
const mockResolvedRegion = getMockResolvedRegion(regionCase);
143+
expect(getResolvedHostname).toHaveBeenCalledWith(mockResolvedRegion, {
144+
signingService: mockSigningService,
145+
regionHostname: mockGetRegionInfoOptions.regionHash[mockResolvedRegion]?.hostname,
146+
partitionHostname: mockGetRegionInfoOptions.partitionHash[mockPartition]?.hostname,
147147
});
148148
expect(getResolvedPartition).toHaveBeenCalledWith(mockRegion, mockGetResolvedPartitionOptions);
149149
});
@@ -176,10 +176,7 @@ describe(getRegionInfo.name, () => {
176176
const mockPartitionHash = getMockPartitionHash(regionCase);
177177

178178
const mockGetResolvedPartitionOptions = getMockResolvedPartitionOptions(mockPartitionHash);
179-
const mockGetResolvedHostnameOptions = getMockResolvedHostnameOptions(
180-
mockRegionHash,
181-
mockGetResolvedPartitionOptions
182-
);
179+
const mockGetRegionInfoOptions = getMockRegionInfoOptions(mockRegionHash, mockGetResolvedPartitionOptions);
183180

184181
const mockRegionHashWithSigningRegion = getMockRegionHashWithSigningService(
185182
regionCase,
@@ -188,16 +185,18 @@ describe(getRegionInfo.name, () => {
188185
);
189186

190187
expect(
191-
getRegionInfo(mockRegion, { ...mockGetResolvedHostnameOptions, regionHash: mockRegionHashWithSigningRegion })
188+
getRegionInfo(mockRegion, { ...mockGetRegionInfoOptions, regionHash: mockRegionHashWithSigningRegion })
192189
).toEqual({
193190
signingService: mockSigningServiceInRegionHash,
194191
hostname: mockHostname,
195192
partition: mockPartition,
196193
});
197194

198-
expect(getResolvedHostname).toHaveBeenCalledWith(getMockResolvedRegion(regionCase), {
199-
...mockGetResolvedHostnameOptions,
200-
regionHash: mockRegionHashWithSigningRegion,
195+
const mockResolvedRegion = getMockResolvedRegion(regionCase);
196+
expect(getResolvedHostname).toHaveBeenCalledWith(mockResolvedRegion, {
197+
signingService: mockSigningService,
198+
regionHostname: mockGetRegionInfoOptions.regionHash[mockResolvedRegion]?.hostname,
199+
partitionHostname: mockGetRegionInfoOptions.partitionHash[mockPartition]?.hostname,
201200
});
202201
expect(getResolvedPartition).toHaveBeenCalledWith(mockRegion, mockGetResolvedPartitionOptions);
203202
});

packages/config-resolver/src/regionInfo/getRegionInfo.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import { RegionInfo } from "@aws-sdk/types";
22

3-
import { getResolvedHostname, GetResolvedHostnameOptions, RegionHash } from "./getResolvedHostname";
4-
import { getResolvedPartition, PartitionHash } from "./getResolvedPartition";
3+
import { getResolvedHostname } from "./getResolvedHostname";
4+
import { getResolvedPartition } from "./getResolvedPartition";
5+
import { PartitionHash } from "./PartitionHash";
6+
import { RegionHash } from "./RegionHash";
57

6-
export { RegionHash, PartitionHash };
7-
8-
export interface GetRegionInfoOptions extends GetResolvedHostnameOptions {}
8+
export interface GetRegionInfoOptions {
9+
signingService: string;
10+
regionHash: RegionHash;
11+
partitionHash: PartitionHash;
12+
}
913

1014
export const getRegionInfo = (
1115
region: string,
@@ -16,7 +20,11 @@ export const getRegionInfo = (
1620
return {
1721
partition,
1822
signingService,
19-
hostname: getResolvedHostname(resolvedRegion, { signingService, regionHash, partitionHash }),
23+
hostname: getResolvedHostname(resolvedRegion, {
24+
signingService,
25+
regionHostname: regionHash[resolvedRegion]?.hostname,
26+
partitionHostname: partitionHash[partition]?.hostname,
27+
}),
2028
...(regionHash[resolvedRegion]?.signingRegion && {
2129
signingRegion: regionHash[resolvedRegion].signingRegion,
2230
}),
Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,40 @@
11
import { getHostnameTemplate } from "./getHostnameTemplate";
2-
import { getResolvedHostname, RegionHash } from "./getResolvedHostname";
3-
import { PartitionHash } from "./getResolvedPartition";
2+
import { getResolvedHostname } from "./getResolvedHostname";
43

54
jest.mock("./getHostnameTemplate");
65

76
describe(getResolvedHostname.name, () => {
87
const mockSigningService = "mockSigningService";
98
const mockRegion = "mockRegion";
10-
const mockPartition = "mockPartition";
11-
const mockRegionRegex = "mockRegionRegex";
129
const mockHostname = "{region}.mockHostname.com";
1310

1411
afterEach(() => {
1512
jest.clearAllMocks();
1613
});
1714

1815
it("returns hostname if available in regionHash", () => {
19-
const mockRegionHash: RegionHash = {
20-
[mockRegion]: {
21-
hostname: mockHostname,
22-
},
23-
};
24-
const mockPartitionHash: PartitionHash = {};
25-
2616
expect(
2717
getResolvedHostname(mockRegion, {
2818
signingService: mockSigningService,
29-
regionHash: mockRegionHash,
30-
partitionHash: mockPartitionHash,
19+
regionHostname: mockHostname,
3120
})
3221
).toBe(mockHostname);
3322
expect(getHostnameTemplate).not.toHaveBeenCalled();
3423
});
3524

3625
it("returns hostname from hostname template when not available in regionHash", () => {
37-
const mockRegionHash: RegionHash = {};
38-
3926
(getHostnameTemplate as jest.Mock).mockReturnValue(mockHostname);
4027

41-
const mockPartitionHash: PartitionHash = {
42-
[mockPartition]: {
43-
regions: [mockRegion, `${mockRegion}2`, `${mockRegion}3`],
44-
regionRegex: mockRegionRegex,
45-
hostname: mockHostname,
46-
},
47-
};
48-
4928
expect(
5029
getResolvedHostname(mockRegion, {
5130
signingService: mockSigningService,
52-
regionHash: mockRegionHash,
53-
partitionHash: mockPartitionHash,
31+
partitionHostname: mockHostname,
5432
})
5533
).toBe(mockHostname.replace("{region}", mockRegion));
5634

5735
expect(getHostnameTemplate).toHaveBeenCalledTimes(1);
58-
expect(getHostnameTemplate).toHaveBeenCalledWith(mockRegion, {
59-
signingService: mockSigningService,
60-
partitionHash: mockPartitionHash,
36+
expect(getHostnameTemplate).toHaveBeenCalledWith(mockSigningService, {
37+
partitionHostname: mockHostname,
6138
});
6239
});
6340
});
Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
import { RegionInfo } from "@aws-sdk/types";
1+
import { getHostnameTemplate } from "./getHostnameTemplate";
22

3-
import { getHostnameTemplate, GetHostnameTemplateOptions } from "./getHostnameTemplate";
4-
import { GetResolvedPartitionOptions } from "./getResolvedPartition";
5-
6-
export type RegionHash = { [key: string]: Partial<Omit<RegionInfo, "partition" | "path">> };
7-
8-
export interface GetResolvedHostnameOptions extends GetHostnameTemplateOptions, GetResolvedPartitionOptions {
9-
/**
10-
* The hash of region with the information specific to that region.
11-
* The information can include hostname, signingService and signingRegion.
12-
*/
13-
regionHash: RegionHash;
3+
export interface GetResolvedHostnameOptions {
4+
signingService: string;
5+
regionHostname?: string;
6+
partitionHostname?: string;
147
}
158

169
export const getResolvedHostname = (
17-
region: string,
18-
{ signingService, regionHash, partitionHash }: GetResolvedHostnameOptions
19-
) =>
20-
regionHash[region]?.hostname ??
21-
getHostnameTemplate(region, { signingService, partitionHash }).replace("{region}", region);
10+
resolvedRegion: string,
11+
{ signingService, regionHostname, partitionHostname }: GetResolvedHostnameOptions
12+
) => regionHostname ?? getHostnameTemplate(signingService, { partitionHostname }).replace("{region}", resolvedRegion);

packages/config-resolver/src/regionInfo/getResolvedPartition.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { getResolvedPartition, PartitionHash } from "./getResolvedPartition";
1+
import { getResolvedPartition } from "./getResolvedPartition";
2+
import { PartitionHash } from "./PartitionHash";
23

34
describe(getResolvedPartition.name, () => {
45
const mockRegion = "mockRegion";

packages/config-resolver/src/regionInfo/getResolvedPartition.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
export type PartitionHash = {
2-
[key: string]: { regions: string[]; regionRegex: string; hostname?: string; endpoint?: string };
3-
};
1+
import { PartitionHash } from "./PartitionHash";
42

53
export interface GetResolvedPartitionOptions {
6-
/**
7-
* The hash of partition with the information specific to that partition.
8-
* The information includes the list of regions belonging to that partition,
9-
* and the hostname to be used for the partition.
10-
*/
114
partitionHash: PartitionHash;
125
}
136

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1+
export * from "./PartitionHash";
2+
export * from "./RegionHash";
13
export * from "./getRegionInfo";

0 commit comments

Comments
 (0)