Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

JonkaSusaki
Copy link

Summary

Changes

This PR removes the legacy class-based EnvironmentVariablesService from the Parameters package and replaces it with functional local helpers.

  • Removed the packages/parameters/src/config/EnvironmentVariablesService.ts file.
  • Removed the packages/parameters/tests/unit/EnvironmentVariablesService.test.ts file, since our tests are backed by commons/tests/unit/envUtils.test.ts.
  • Created the packages/parameters/src/utils/env.ts file containing the helpers.
  • Changed the contents of packages/parameters/src/base/BaseProvider.ts.
  • Changed the contents of packages/parameters/src/base/GetMultipleOptions.ts.
  • Changed the contents of packages/parameters/src/base/GetOptions.ts.
  • Changed the contents of packages/parameters/src/appconfig/AppConfigProvider.ts.
  • Added the isValueTrue function to the packages/commons/src/envUtils.ts because it is needed in packages/parameters/src/ssm/SSMProvider.ts.
  • Added the tests for isValueTrue in packages/commons/tests/unit/envUtils.test.ts.
  • Changed the contents of 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.

@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label Jul 14, 2025
@boring-cyborg boring-cyborg bot added commons This item relates to the Commons Utility parameters This item relates to the Parameters Utility tests PRs that add or change tests labels Jul 14, 2025
Copy link

boring-cyborg bot commented Jul 14, 2025

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #typescript channel on our Powertools for AWS Lambda Discord: Invite link

Copy link

Comment on lines +16 to +18
} catch (error) {
return undefined;
}

Check notice

Code scanning / SonarCloud

Exceptions should not be ignored Low

Handle this exception or don't catch it at all. See more on SonarQube Cloud
Copy link
Contributor

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.

Copy link
Author

@JonkaSusaki JonkaSusaki Jul 14, 2025

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.

Copy link
Contributor

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.

Copy link
Contributor

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.

@svozza
Copy link
Contributor

svozza commented Jul 14, 2025

Thanks for picking this up, it's looking good! I only had one small comment.

@dreamorosi dreamorosi self-requested a review July 14, 2025 20:57
Comment on lines +16 to +18
} catch (error) {
return undefined;
}
Copy link
Contributor

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 => {
Copy link
Contributor

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({
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
commons This item relates to the Commons Utility parameters This item relates to the Parameters Utility size/L PRs between 100-499 LOC tests PRs that add or change tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants