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

Merged
merged 3 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/parameters/src/appconfig/AppConfigProvider.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getServiceName } from '@aws-lambda-powertools/commons/utils/env';
import type { StartConfigurationSessionCommandInput } from '@aws-sdk/client-appconfigdata';
import {
AppConfigDataClient,
Expand Down Expand Up @@ -206,7 +207,7 @@ class AppConfigProvider extends BaseProvider {
});

const { application, environment } = options;
this.application = application ?? this.envVarsService.getServiceName();
this.application = application ?? getServiceName();
if (!this.application || this.application.trim().length === 0) {
throw new Error(
'Application name is not defined or POWERTOOLS_SERVICE_NAME is not set'
Expand Down
7 changes: 2 additions & 5 deletions packages/parameters/src/base/BaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
isSdkClient,
isString,
} from '@aws-lambda-powertools/commons';
import { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
import { GetParameterError, TransformParameterError } from '../errors.js';
import type {
BaseProviderConstructorOptions,
Expand Down Expand Up @@ -38,7 +37,6 @@ import { transformValue } from './transformValue.js';
* this should be an acceptable tradeoff.
*/
abstract class BaseProvider implements BaseProviderInterface {
public envVarsService: EnvironmentVariablesService;
protected client: unknown;
protected store: Map<string, ExpirableValue>;

Expand All @@ -48,7 +46,6 @@ abstract class BaseProvider implements BaseProviderInterface {
awsSdkV3ClientPrototype,
}: BaseProviderConstructorOptions) {
this.store = new Map();
this.envVarsService = new EnvironmentVariablesService();
if (awsSdkV3Client) {
if (!isSdkClient(awsSdkV3Client) && awsSdkV3ClientPrototype) {
console.warn(
Expand Down Expand Up @@ -96,7 +93,7 @@ abstract class BaseProvider implements BaseProviderInterface {
name: string,
options?: GetOptionsInterface
): Promise<unknown | undefined> {
const configs = new GetOptions(this.envVarsService, options);
const configs = new GetOptions(options);
const key = [name, configs.transform].toString();

if (!configs.forceFetch && !this.hasKeyExpiredInCache(key)) {
Expand Down Expand Up @@ -135,7 +132,7 @@ abstract class BaseProvider implements BaseProviderInterface {
path: string,
options?: GetMultipleOptionsInterface
): Promise<unknown> {
const configs = new GetMultipleOptions(this.envVarsService, options);
const configs = new GetMultipleOptions(options);
const key = [path, configs.transform].toString();

if (!configs.forceFetch && !this.hasKeyExpiredInCache(key)) {
Expand Down
8 changes: 2 additions & 6 deletions packages/parameters/src/base/GetMultipleOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
import type { GetMultipleOptionsInterface } from '../types/BaseProvider.js';
import { GetOptions } from './GetOptions.js';

Expand All @@ -13,11 +12,8 @@ class GetMultipleOptions
{
public throwOnTransformError = false;

public constructor(
envVarsService: EnvironmentVariablesService,
options: GetMultipleOptionsInterface = {}
) {
super(envVarsService, options);
public constructor(options: GetMultipleOptionsInterface = {}) {
super(options);

if (options.throwOnTransformError !== undefined) {
this.throwOnTransformError = options.throwOnTransformError;
Expand Down
13 changes: 6 additions & 7 deletions packages/parameters/src/base/GetOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
import { getNumberFromEnv } from '@aws-lambda-powertools/commons/utils/env';
import { DEFAULT_MAX_AGE_SECS } from '../constants.js';
import type {
GetOptionsInterface,
Expand All @@ -16,15 +16,14 @@ class GetOptions implements GetOptionsInterface {
public sdkOptions?: unknown;
public transform?: TransformOptions;

public constructor(
envVarsService: EnvironmentVariablesService,
options: GetOptionsInterface = {}
) {
public constructor(options: GetOptionsInterface = {}) {
Object.assign(this, options);

if (options.maxAge === undefined) {
this.maxAge =
envVarsService.getParametersMaxAge() ?? DEFAULT_MAX_AGE_SECS;
this.maxAge = getNumberFromEnv({
key: 'POWERTOOLS_PARAMETERS_MAX_AGE',
defaultValue: DEFAULT_MAX_AGE_SECS,
});
}
}
}
Expand Down
43 changes: 0 additions & 43 deletions packages/parameters/src/config/EnvironmentVariablesService.ts

This file was deleted.

12 changes: 6 additions & 6 deletions packages/parameters/src/ssm/SSMProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
import { getBooleanFromEnv } from '@aws-lambda-powertools/commons/utils/env';
import type {
GetParameterCommandInput,
GetParametersByPathCommandInput,
Expand Down Expand Up @@ -843,13 +844,12 @@ class SSMProvider extends BaseProvider {
if (options?.decrypt !== undefined) return options.decrypt;
if (sdkOptions?.WithDecryption !== undefined)
return sdkOptions.WithDecryption;
if (this.envVarsService.getSSMDecrypt() !== '') {
return this.envVarsService.isValueTrue(
this.envVarsService.getSSMDecrypt()
);
}

return undefined;
return getBooleanFromEnv({
key: 'POWERTOOLS_PARAMETERS_SSM_DECRYPT',
defaultValue: false,
extendedParsing: true,
});
}

/**
Expand Down
25 changes: 0 additions & 25 deletions packages/parameters/src/types/ConfigServiceInterface.ts

This file was deleted.

9 changes: 2 additions & 7 deletions packages/parameters/tests/unit/BaseProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
GetMultipleOptions,
GetOptions,
} from '../../src/base/index.js';
import type { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js';
import { DEFAULT_MAX_AGE_SECS } from '../../src/constants.js';
import {
clearCaches,
Expand Down Expand Up @@ -550,9 +549,7 @@ describe('Class: GetOptions', () => {
const envVarsService = {
getParametersMaxAge: vi.fn(),
};
const options = new GetOptions(
envVarsService as unknown as EnvironmentVariablesService
);
const options = new GetOptions();

// Assess
expect(options.maxAge).toBe(DEFAULT_MAX_AGE_SECS);
Expand All @@ -565,9 +562,7 @@ describe('Class: GetMultipleOptions', () => {
const envVarsService = {
getParametersMaxAge: vi.fn(),
};
const options = new GetMultipleOptions(
envVarsService as unknown as EnvironmentVariablesService
);
const options = new GetMultipleOptions();

// Assess
expect(options.throwOnTransformError).toBe(false);
Expand Down
69 changes: 0 additions & 69 deletions packages/parameters/tests/unit/EnvironmentVariablesService.test.ts

This file was deleted.