-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(middleware-endpoint): add getEndpointUrlConfig
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/middleware-endpoint/src/adaptors/getEndpointUrlConfig.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
packages/middleware-endpoint/src/adaptors/getEndpointUrlConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters