Skip to content

Commit

Permalink
chore(middleware-endpoint): add getEndpointUrlConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Oct 9, 2023
1 parent 5fb6d4c commit d550919
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/middleware-endpoint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"license": "Apache-2.0",
"dependencies": {
"@smithy/middleware-serde": "workspace:^",
"@smithy/node-config-provider": "workspace:^",
"@smithy/types": "workspace:^",
"@smithy/url-parser": "workspace:^",
"@smithy/util-middleware": "workspace:^",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { getEndpointUrlConfig } from "./getEndpointUrlConfig";

const ENV_ENDPOINT_URL = "AWS_ENDPOINT_URL";
const CONFIG_ENDPOINT_URL = "endpoint_url";

describe(getEndpointUrlConfig.name, () => {
const serviceId = "foo";
const endpointUrlConfig = getEndpointUrlConfig(serviceId);

const mockEndpoint = "https://mock-endpoint.com";
const ORIGINAL_ENV = process.env;

beforeEach(() => {
process.env = {};
});

afterEach(() => {
process.env = ORIGINAL_ENV;
});

describe("environmentVariableSelector", () => {
const serviceMockEndpoint = `${mockEndpoint}/${serviceId}`;

beforeEach(() => {
process.env[ENV_ENDPOINT_URL] = mockEndpoint;
process.env[`${ENV_ENDPOINT_URL}_${serviceId.toUpperCase()}`] = serviceMockEndpoint;
});

it("returns service specific endpoint from environment variable, if available", () => {
expect(endpointUrlConfig.environmentVariableSelector(process.env)).toEqual(serviceMockEndpoint);
});

it("returns endpoint from environment variable, if available", () => {
process.env[`${ENV_ENDPOINT_URL}_${serviceId.toUpperCase()}`] = undefined;
expect(endpointUrlConfig.environmentVariableSelector(process.env)).toEqual(mockEndpoint);
});

it("returns undefined, if endpoint not available in environment variables", () => {
process.env[ENV_ENDPOINT_URL] = undefined;
process.env[`${ENV_ENDPOINT_URL}_${serviceId.toUpperCase()}`] = undefined;
expect(endpointUrlConfig.environmentVariableSelector(process.env)).toBeUndefined();
});
});

describe("configFileSelector", () => {
it("returns service specific endpoint from config file, if available", () => {
// ToDo
});

it("returns endpoint from config file, if available", () => {
const profile = { [CONFIG_ENDPOINT_URL]: mockEndpoint };
expect(endpointUrlConfig.configFileSelector(profile)).toEqual(mockEndpoint);
});

it("returns undefined, if endpoint not available in config", () => {
expect(endpointUrlConfig.environmentVariableSelector({})).toBeUndefined();
});
});

it("returns undefined by default", () => {
expect(endpointUrlConfig.default).toBeUndefined();
});
});
35 changes: 35 additions & 0 deletions packages/middleware-endpoint/src/adaptors/getEndpointUrlConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { LoadedConfigSelectors } from "@smithy/node-config-provider";

const ENV_ENDPOINT_URL = "AWS_ENDPOINT_URL";
const CONFIG_ENDPOINT_URL = "endpoint_url";

export const getEndpointUrlConfig = (serviceId: string): LoadedConfigSelectors<string | undefined> => ({
environmentVariableSelector: (env) => {
// The value provided by a service-specific environment variable.
const serviceEndpointUrlSections = [ENV_ENDPOINT_URL, serviceId.toUpperCase()];
const serviceEndpointUrl = env[serviceEndpointUrlSections.join("_")];
if (serviceEndpointUrl) return serviceEndpointUrl;

// The value provided by the global endpoint environment variable.
const endpointUrl = env[ENV_ENDPOINT_URL];
if (endpointUrl) return endpointUrl;

return undefined;
},

configFileSelector: (profile) => {
// The value provided by a service-specific parameter from a services definition section
// referenced in a profile in the shared configuration file.

// ToDo: profile is selected one. It does not have access to other 'services' section.
// We should call loadSharedConfigFiles directly.

// The value provided by the global parameter from a profile in the shared configuration file.
const endpointUrl = profile[CONFIG_ENDPOINT_URL];
if (endpointUrl) return endpointUrl;

return undefined;
},

default: undefined,
});
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,7 @@ __metadata:
resolution: "@smithy/middleware-endpoint@workspace:packages/middleware-endpoint"
dependencies:
"@smithy/middleware-serde": "workspace:^"
"@smithy/node-config-provider": "workspace:^"
"@smithy/types": "workspace:^"
"@smithy/url-parser": "workspace:^"
"@smithy/util-middleware": "workspace:^"
Expand Down

0 comments on commit d550919

Please sign in to comment.