-
Notifications
You must be signed in to change notification settings - Fork 161
refactor(parameters): replace EnvironmentVariablesService class with helper functions in Parameters #4168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
refactor(parameters): replace EnvironmentVariablesService class with helper functions in Parameters #4168
Conversation
…helper functions in Parameters
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
|
} catch (error) { | ||
return undefined; | ||
} |
Check notice
Code scanning / SonarCloud
Exceptions should not be ignored Low
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is correct, we probably just want this to fail if the environment variable is not read properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to catch it, since the getNumberFromEnv
throws an error at packages/commons/src/envUtils.ts
at line 110.
When removing the try/catch block from packages/parameters/src/utils/env.ts
, the AppConfigProvider
class can't instantiate the packages/parameters/src/base/GetOptions.ts
in case the env does not exists.
Instead of setting the maxAge
to DEFAULT_MAX_AGE_SECS
, everything just breaks because of that thrown error.
What do you think? Perhaps I could add a console.warn
with the error in the catch block to avoid this sonarqube error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ts in case the env does not exists.
Ah yes, that's a good point. Another possibility is to return the default value as defined in the constants file:
import { DEFAULT_MAX_AGE_SECS } from '../constants.js';
// ...
} catch (error) {
return DEFAULT_MAX_AGE_SECS;
}
I'd like to hear @dreamorosi thoughts first though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, I think it'd be sufficient to do this:
return getNumberFromEnv({
key: 'POWERTOOLS_PARAMETERS_MAX_AGE',
// will return `undefined` and not throw if the env var is not set or empty
defaultValue: undefined,
})
It's ok to return undefined
here because in the place where we're calling it (here) we're already doing this:
this.maxAge = getParametersMaxAge() ?? DEFAULT_MAX_AGE_SECS;
Since this getParametersMaxAge
is called only in this place, I'd even consider/suggest to call the getNumberFromEnv
there directly, so we can save a file and reduce cognitive load.
Thanks for picking this up, it's looking good! I only had one small comment. |
} catch (error) { | ||
return undefined; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this case, I think it'd be sufficient to do this:
return getNumberFromEnv({
key: 'POWERTOOLS_PARAMETERS_MAX_AGE',
// will return `undefined` and not throw if the env var is not set or empty
defaultValue: undefined,
})
It's ok to return undefined
here because in the place where we're calling it (here) we're already doing this:
this.maxAge = getParametersMaxAge() ?? DEFAULT_MAX_AGE_SECS;
Since this getParametersMaxAge
is called only in this place, I'd even consider/suggest to call the getNumberFromEnv
there directly, so we can save a file and reduce cognitive load.
* | ||
* @param value The value to check for truthiness. | ||
*/ | ||
const isValueTrue = (value: string): boolean => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we need to add this one, we can use the existing getBooleanFromEnv
above with extendedParsing
set to true
* @returns {string} | ||
*/ | ||
const getSSMDecrypt = (): string => { | ||
return getStringFromEnv({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest replacing this getStringFromEnv
with a call to getBooleanFromEnv
with extendedParsing
set to true
& defaultValue
to false
.
This would change the return value to a boolean
and we could simplify further the call in the SSMProvider
class, since this is used in a if/else
statement.
Also like discussed in the other comment above, I'd consider/suggest moving the getBooleanFromEnv
directly in the SSMProvider
so we can remove this file entirely.
Summary
Changes
This PR removes the legacy class-based EnvironmentVariablesService from the Parameters package and replaces it with functional local helpers.
packages/parameters/src/config/EnvironmentVariablesService.ts
file.packages/parameters/tests/unit/EnvironmentVariablesService.test.ts
file, since our tests are backed bycommons/tests/unit/envUtils.test.ts
.packages/parameters/src/utils/env.ts
file containing the helpers.packages/parameters/src/base/BaseProvider.ts
.packages/parameters/src/base/GetMultipleOptions.ts
.packages/parameters/src/base/GetOptions.ts
.packages/parameters/src/appconfig/AppConfigProvider.ts
.isValueTrue
function to thepackages/commons/src/envUtils.ts
because it is needed inpackages/parameters/src/ssm/SSMProvider.ts
.isValueTrue
inpackages/commons/tests/unit/envUtils.test.ts
.packages/parameters/src/ssm/SSMProvider.ts
.Issue number: #4138
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.