Skip to content

Commit aaf5986

Browse files
authored
chore(config-resolver): refactor RegionConfig into multiple files (#2721)
1 parent c1a7dd5 commit aaf5986

10 files changed

+138
-111
lines changed

packages/config-resolver/src/RegionConfig.spec.ts

-59
This file was deleted.

packages/config-resolver/src/RegionConfig.ts

-51
This file was deleted.

packages/config-resolver/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from "./endpointsConfig";
2-
export * from "./RegionConfig";
2+
export * from "./regionConfig";
33
export * from "./regionInfo";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {
2+
NODE_REGION_CONFIG_FILE_OPTIONS,
3+
NODE_REGION_CONFIG_OPTIONS,
4+
REGION_ENV_NAME,
5+
REGION_INI_NAME,
6+
} from "./config";
7+
8+
describe("config", () => {
9+
describe("NODE_REGION_CONFIG_OPTIONS", () => {
10+
describe("environmentVariableSelector", () => {
11+
const { environmentVariableSelector } = NODE_REGION_CONFIG_OPTIONS;
12+
it.each([undefined, "mockRegion"])(`when env[${REGION_ENV_NAME}]: %s`, (mockEndpoint) => {
13+
expect(environmentVariableSelector({ [REGION_ENV_NAME]: mockEndpoint })).toBe(mockEndpoint);
14+
});
15+
});
16+
17+
describe("configFileSelector", () => {
18+
const { configFileSelector } = NODE_REGION_CONFIG_OPTIONS;
19+
it.each([undefined, "mockRegion"])(`when env[${REGION_INI_NAME}]: %s`, (mockEndpoint) => {
20+
expect(configFileSelector({ [REGION_INI_NAME]: mockEndpoint })).toBe(mockEndpoint);
21+
});
22+
});
23+
24+
it("default throws error", () => {
25+
const { default: defaultKey } = NODE_REGION_CONFIG_OPTIONS;
26+
expect(() => {
27+
(defaultKey as any)();
28+
}).toThrowError(new Error("Region is missing"));
29+
});
30+
});
31+
32+
describe("NODE_REGION_CONFIG_FILE_OPTIONS", () => {
33+
it("preferredFile contains credentials", () => {
34+
const { preferredFile } = NODE_REGION_CONFIG_FILE_OPTIONS;
35+
expect(preferredFile).toBe("credentials");
36+
});
37+
});
38+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { LoadedConfigSelectors, LocalConfigOptions } from "@aws-sdk/node-config-provider";
2+
3+
export const REGION_ENV_NAME = "AWS_REGION";
4+
export const REGION_INI_NAME = "region";
5+
6+
export const NODE_REGION_CONFIG_OPTIONS: LoadedConfigSelectors<string> = {
7+
environmentVariableSelector: (env) => env[REGION_ENV_NAME],
8+
configFileSelector: (profile) => profile[REGION_INI_NAME],
9+
default: () => {
10+
throw new Error("Region is missing");
11+
},
12+
};
13+
14+
export const NODE_REGION_CONFIG_FILE_OPTIONS: LocalConfigOptions = {
15+
preferredFile: "credentials",
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./config";
2+
export * from "./resolveRegionConfig";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { normalizeRegion } from "./normalizeRegion";
2+
3+
describe(normalizeRegion.name, () => {
4+
const mockRegion = "mockRegion";
5+
6+
it("returns Provider if value is string", async () => {
7+
const output = normalizeRegion(mockRegion);
8+
expect(await output()).toEqual(mockRegion);
9+
});
10+
11+
it("returns Provider if it's a Provider", () => {
12+
const mockRegionProvider = () => Promise.resolve(mockRegion);
13+
expect(normalizeRegion(mockRegionProvider)).toBe(mockRegionProvider);
14+
});
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Provider } from "@aws-sdk/types";
2+
3+
export const normalizeRegion = (region: string | Provider<string>): Provider<string> => {
4+
if (typeof region === "string") {
5+
const promisified = Promise.resolve(region);
6+
return () => promisified;
7+
}
8+
return region as Provider<string>;
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { normalizeRegion } from "./normalizeRegion";
2+
import { resolveRegionConfig } from "./resolveRegionConfig";
3+
4+
jest.mock("./normalizeRegion");
5+
6+
describe("RegionConfig", () => {
7+
const mockRegionProvider = () => Promise.resolve("mockRegion");
8+
9+
beforeEach(() => {
10+
(normalizeRegion as jest.Mock).mockReturnValue(mockRegionProvider);
11+
});
12+
13+
afterEach(() => {
14+
jest.clearAllMocks();
15+
});
16+
17+
it("assigns value returned by normalizeRegion to region", async () => {
18+
const region = "mockRegion";
19+
expect(resolveRegionConfig({ region }).region).toBe(mockRegionProvider);
20+
expect(normalizeRegion).toHaveBeenCalledTimes(1);
21+
expect(normalizeRegion).toHaveBeenCalledWith(region);
22+
});
23+
24+
it("throw if region is not supplied", () => {
25+
expect(() => resolveRegionConfig({})).toThrow();
26+
expect(normalizeRegion).not.toHaveBeenCalled();
27+
});
28+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { Provider } from "@aws-sdk/types";
2+
3+
import { normalizeRegion } from "./normalizeRegion";
4+
5+
export interface RegionInputConfig {
6+
/**
7+
* The AWS region to which this client will send requests
8+
*/
9+
region?: string | Provider<string>;
10+
}
11+
12+
interface PreviouslyResolved {}
13+
14+
export interface RegionResolvedConfig {
15+
/**
16+
* Resolved value for input config {@link RegionInputConfig.region}
17+
*/
18+
region: Provider<string>;
19+
}
20+
21+
export const resolveRegionConfig = <T>(input: T & RegionInputConfig & PreviouslyResolved): T & RegionResolvedConfig => {
22+
if (!input.region) {
23+
throw new Error("Region is missing");
24+
}
25+
return {
26+
...input,
27+
region: normalizeRegion(input.region!),
28+
};
29+
};

0 commit comments

Comments
 (0)