Skip to content

Commit cacc32c

Browse files
authored
chore(config-resolver): use normalizeProvider from util-middleware (#3457)
1 parent 55f08d5 commit cacc32c

File tree

9 files changed

+86
-128
lines changed

9 files changed

+86
-128
lines changed

packages/config-resolver/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@aws-sdk/signature-v4": "*",
2323
"@aws-sdk/types": "*",
2424
"@aws-sdk/util-config-provider": "*",
25+
"@aws-sdk/util-middleware": "*",
2526
"tslib": "^2.3.1"
2627
},
2728
"devDependencies": {
Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { normalizeProvider } from "@aws-sdk/util-middleware";
2+
13
import { resolveCustomEndpointsConfig } from "./resolveCustomEndpointsConfig";
2-
import { normalizeEndpoint } from "./utils/normalizeEndpoint";
34

4-
jest.mock("./utils/normalizeEndpoint");
5+
jest.mock("@aws-sdk/util-middleware");
56

67
describe(resolveCustomEndpointsConfig.name, () => {
78
const mockEndpoint = {
@@ -10,18 +11,26 @@ describe(resolveCustomEndpointsConfig.name, () => {
1011
path: "/",
1112
};
1213

13-
const mockInput = { endpoint: mockEndpoint } as any;
14+
const mockInput = {
15+
endpoint: mockEndpoint,
16+
urlParser: jest.fn(() => mockEndpoint),
17+
useDualstackEndpoint: () => Promise.resolve(false),
18+
} as any;
1419

1520
beforeEach(() => {
16-
(normalizeEndpoint as jest.Mock).mockReturnValueOnce(() => Promise.resolve(mockEndpoint));
21+
(normalizeProvider as jest.Mock).mockImplementation((input) =>
22+
typeof input === "function" ? input : () => Promise.resolve(input)
23+
);
1724
});
1825

1926
afterEach(() => {
27+
expect(normalizeProvider).toHaveBeenCalledTimes(2);
28+
expect(normalizeProvider).toHaveBeenNthCalledWith(2, mockInput.useDualstackEndpoint);
2029
jest.clearAllMocks();
2130
});
2231

2332
describe("tls", () => {
24-
it.each([true, false])("returns %s when it's %s", (tls) => {
33+
it.each([true, false])("returns %s when the value is passed", (tls) => {
2534
expect(resolveCustomEndpointsConfig({ ...mockInput, tls }).tls).toStrictEqual(tls);
2635
});
2736

@@ -34,10 +43,22 @@ describe(resolveCustomEndpointsConfig.name, () => {
3443
expect(resolveCustomEndpointsConfig(mockInput).isCustomEndpoint).toStrictEqual(true);
3544
});
3645

37-
it("returns normalized endpoint", async () => {
38-
const endpoint = await resolveCustomEndpointsConfig(mockInput).endpoint();
39-
expect(endpoint).toStrictEqual(mockEndpoint);
40-
expect(normalizeEndpoint).toHaveBeenCalledTimes(1);
41-
expect(normalizeEndpoint).toHaveBeenCalledWith(mockInput);
46+
describe("returns normalized endpoint", () => {
47+
afterEach(() => {
48+
expect(normalizeProvider).toHaveBeenNthCalledWith(1, mockInput.endpoint);
49+
});
50+
51+
it("calls urlParser endpoint is of type string", async () => {
52+
const mockEndpointString = "http://localhost/";
53+
const endpoint = await resolveCustomEndpointsConfig({ ...mockInput, endpoint: mockEndpointString }).endpoint();
54+
expect(endpoint).toStrictEqual(mockEndpoint);
55+
expect(mockInput.urlParser).toHaveBeenCalledWith(mockEndpointString);
56+
});
57+
58+
it("passes endpoint to normalize if not string", async () => {
59+
const endpoint = await resolveCustomEndpointsConfig(mockInput).endpoint();
60+
expect(endpoint).toStrictEqual(mockEndpoint);
61+
expect(mockInput.urlParser).not.toHaveBeenCalled();
62+
});
4263
});
4364
});

packages/config-resolver/src/endpointsConfig/resolveCustomEndpointsConfig.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Endpoint, Provider, UrlParser } from "@aws-sdk/types";
2+
import { normalizeProvider } from "@aws-sdk/util-middleware";
23

34
import { EndpointsInputConfig, EndpointsResolvedConfig } from "./resolveEndpointsConfig";
4-
import { normalizeBoolean } from "./utils/normalizeBoolean";
5-
import { normalizeEndpoint } from "./utils/normalizeEndpoint";
65

76
export interface CustomEndpointsInputConfig extends EndpointsInputConfig {
87
/**
@@ -25,10 +24,13 @@ export interface CustomEndpointsResolvedConfig extends EndpointsResolvedConfig {
2524

2625
export const resolveCustomEndpointsConfig = <T>(
2726
input: T & CustomEndpointsInputConfig & PreviouslyResolved
28-
): T & CustomEndpointsResolvedConfig => ({
29-
...input,
30-
tls: input.tls ?? true,
31-
endpoint: normalizeEndpoint(input),
32-
isCustomEndpoint: true,
33-
useDualstackEndpoint: normalizeBoolean(input.useDualstackEndpoint!),
34-
});
27+
): T & CustomEndpointsResolvedConfig => {
28+
const { endpoint, urlParser } = input;
29+
return {
30+
...input,
31+
tls: input.tls ?? true,
32+
endpoint: normalizeProvider(typeof endpoint === "string" ? urlParser(endpoint) : endpoint),
33+
isCustomEndpoint: true,
34+
useDualstackEndpoint: normalizeProvider(input.useDualstackEndpoint!),
35+
};
36+
};
Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
import { normalizeProvider } from "@aws-sdk/util-middleware";
2+
13
import { resolveEndpointsConfig } from "./resolveEndpointsConfig";
24
import { getEndpointFromRegion } from "./utils/getEndpointFromRegion";
3-
import { normalizeEndpoint } from "./utils/normalizeEndpoint";
45

6+
jest.mock("@aws-sdk/util-middleware");
57
jest.mock("./utils/getEndpointFromRegion");
6-
jest.mock("./utils/normalizeEndpoint");
78

89
describe(resolveEndpointsConfig.name, () => {
910
const mockEndpoint = {
@@ -12,14 +13,22 @@ describe(resolveEndpointsConfig.name, () => {
1213
path: "/",
1314
};
1415

15-
const mockInput = { endpoint: mockEndpoint } as any;
16+
const mockInput = {
17+
endpoint: mockEndpoint,
18+
urlParser: jest.fn(() => mockEndpoint),
19+
useDualstackEndpoint: () => Promise.resolve(false),
20+
useFipsEndpoint: () => Promise.resolve(false),
21+
} as any;
1622

1723
beforeEach(() => {
1824
(getEndpointFromRegion as jest.Mock).mockResolvedValueOnce(mockEndpoint);
19-
(normalizeEndpoint as jest.Mock).mockReturnValueOnce(() => Promise.resolve(mockEndpoint));
25+
(normalizeProvider as jest.Mock).mockImplementation((input) =>
26+
typeof input === "function" ? input : () => Promise.resolve(input)
27+
);
2028
});
2129

2230
afterEach(() => {
31+
expect(normalizeProvider).toHaveBeenNthCalledWith(1, mockInput.useDualstackEndpoint);
2332
jest.clearAllMocks();
2433
});
2534

@@ -39,25 +48,40 @@ describe(resolveEndpointsConfig.name, () => {
3948
});
4049

4150
it("returns false when endpoint is not defined", () => {
42-
expect(resolveEndpointsConfig({} as any).isCustomEndpoint).toStrictEqual(false);
51+
const { endpoint, ...mockInputWithoutEndpoint } = mockInput;
52+
expect(resolveEndpointsConfig(mockInputWithoutEndpoint).isCustomEndpoint).toStrictEqual(false);
4353
});
4454
});
4555

4656
describe("endpoint", () => {
47-
it("returns from normalizeEndpoint when endpoint is defined", async () => {
48-
const endpoint = await resolveEndpointsConfig(mockInput).endpoint();
49-
expect(endpoint).toStrictEqual(mockEndpoint);
50-
expect(normalizeEndpoint).toHaveBeenCalledTimes(1);
51-
expect(normalizeEndpoint).toHaveBeenCalledWith(mockInput);
52-
expect(getEndpointFromRegion).not.toHaveBeenCalled();
57+
describe("returns from normalizeProvider when endpoint is defined", () => {
58+
afterEach(() => {
59+
expect(normalizeProvider).toHaveBeenCalledTimes(2);
60+
expect(normalizeProvider).toHaveBeenNthCalledWith(2, mockInput.endpoint);
61+
expect(getEndpointFromRegion).not.toHaveBeenCalled();
62+
});
63+
64+
it("calls urlParser endpoint is of type string", async () => {
65+
const mockEndpointString = "http://localhost/";
66+
const endpoint = await resolveEndpointsConfig({ ...mockInput, endpoint: mockEndpointString }).endpoint();
67+
expect(endpoint).toStrictEqual(mockEndpoint);
68+
expect(mockInput.urlParser).toHaveBeenCalledWith(mockEndpointString);
69+
});
70+
71+
it("passes endpoint to normalize if not string", async () => {
72+
const endpoint = await resolveEndpointsConfig(mockInput).endpoint();
73+
expect(endpoint).toStrictEqual(mockEndpoint);
74+
expect(mockInput.urlParser).not.toHaveBeenCalled();
75+
});
5376
});
5477

5578
it("returns from getEndpointFromRegion when endpoint is not defined", async () => {
56-
const endpoint = await resolveEndpointsConfig({} as any).endpoint();
57-
expect(endpoint).toStrictEqual(mockEndpoint);
58-
expect(normalizeEndpoint).not.toHaveBeenCalled();
79+
const { endpoint, ...mockInputWithoutEndpoint } = mockInput;
80+
const returnedEndpoint = await resolveEndpointsConfig(mockInputWithoutEndpoint).endpoint();
81+
expect(returnedEndpoint).toStrictEqual(mockEndpoint);
82+
expect(normalizeProvider).toHaveBeenCalledTimes(1);
5983
expect(getEndpointFromRegion).toHaveBeenCalledTimes(1);
60-
expect(getEndpointFromRegion).toHaveBeenCalledWith({});
84+
expect(getEndpointFromRegion).toHaveBeenCalledWith(mockInputWithoutEndpoint);
6185
});
6286
});
6387
});

packages/config-resolver/src/endpointsConfig/resolveEndpointsConfig.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { Endpoint, Provider, RegionInfoProvider, UrlParser } from "@aws-sdk/types";
2+
import { normalizeProvider } from "@aws-sdk/util-middleware";
23

34
import { getEndpointFromRegion } from "./utils/getEndpointFromRegion";
4-
import { normalizeBoolean } from "./utils/normalizeBoolean";
5-
import { normalizeEndpoint } from "./utils/normalizeEndpoint";
65

76
export interface EndpointsInputConfig {
87
/**
@@ -49,13 +48,13 @@ export interface EndpointsResolvedConfig extends Required<EndpointsInputConfig>
4948
export const resolveEndpointsConfig = <T>(
5049
input: T & EndpointsInputConfig & PreviouslyResolved
5150
): T & EndpointsResolvedConfig => {
52-
const useDualstackEndpoint = normalizeBoolean(input.useDualstackEndpoint!);
53-
const { endpoint, useFipsEndpoint } = input;
51+
const useDualstackEndpoint = normalizeProvider(input.useDualstackEndpoint!);
52+
const { endpoint, useFipsEndpoint, urlParser } = input;
5453
return {
5554
...input,
5655
tls: input.tls ?? true,
5756
endpoint: endpoint
58-
? normalizeEndpoint({ ...input, endpoint })
57+
? normalizeProvider(typeof endpoint === "string" ? urlParser(endpoint) : endpoint)
5958
: () => getEndpointFromRegion({ ...input, useDualstackEndpoint, useFipsEndpoint }),
6059
isCustomEndpoint: endpoint ? true : false,
6160
useDualstackEndpoint,

packages/config-resolver/src/endpointsConfig/utils/normalizeBoolean.spec.ts

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

packages/config-resolver/src/endpointsConfig/utils/normalizeBoolean.ts

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

packages/config-resolver/src/endpointsConfig/utils/normalizeEndpoint.spec.ts

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

packages/config-resolver/src/endpointsConfig/utils/normalizeEndpoint.ts

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

0 commit comments

Comments
 (0)