Skip to content

Commit ce4e618

Browse files
JonkaSusakisvozza
andauthored
refactor(parameters): replace EnvironmentVariablesService class with helper functions in Parameters (#4168)
Co-authored-by: Stefano Vozza <[email protected]>
1 parent 204e2d7 commit ce4e618

File tree

9 files changed

+20
-169
lines changed

9 files changed

+20
-169
lines changed

packages/parameters/src/appconfig/AppConfigProvider.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getServiceName } from '@aws-lambda-powertools/commons/utils/env';
12
import type { StartConfigurationSessionCommandInput } from '@aws-sdk/client-appconfigdata';
23
import {
34
AppConfigDataClient,
@@ -206,7 +207,7 @@ class AppConfigProvider extends BaseProvider {
206207
});
207208

208209
const { application, environment } = options;
209-
this.application = application ?? this.envVarsService.getServiceName();
210+
this.application = application ?? getServiceName();
210211
if (!this.application || this.application.trim().length === 0) {
211212
throw new Error(
212213
'Application name is not defined or POWERTOOLS_SERVICE_NAME is not set'

packages/parameters/src/base/BaseProvider.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
isSdkClient,
66
isString,
77
} from '@aws-lambda-powertools/commons';
8-
import { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
98
import { GetParameterError, TransformParameterError } from '../errors.js';
109
import type {
1110
BaseProviderConstructorOptions,
@@ -38,7 +37,6 @@ import { transformValue } from './transformValue.js';
3837
* this should be an acceptable tradeoff.
3938
*/
4039
abstract class BaseProvider implements BaseProviderInterface {
41-
public envVarsService: EnvironmentVariablesService;
4240
protected client: unknown;
4341
protected store: Map<string, ExpirableValue>;
4442

@@ -48,7 +46,6 @@ abstract class BaseProvider implements BaseProviderInterface {
4846
awsSdkV3ClientPrototype,
4947
}: BaseProviderConstructorOptions) {
5048
this.store = new Map();
51-
this.envVarsService = new EnvironmentVariablesService();
5249
if (awsSdkV3Client) {
5350
if (!isSdkClient(awsSdkV3Client) && awsSdkV3ClientPrototype) {
5451
console.warn(
@@ -96,7 +93,7 @@ abstract class BaseProvider implements BaseProviderInterface {
9693
name: string,
9794
options?: GetOptionsInterface
9895
): Promise<unknown | undefined> {
99-
const configs = new GetOptions(this.envVarsService, options);
96+
const configs = new GetOptions(options);
10097
const key = [name, configs.transform].toString();
10198

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

141138
if (!configs.forceFetch && !this.hasKeyExpiredInCache(key)) {

packages/parameters/src/base/GetMultipleOptions.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
21
import type { GetMultipleOptionsInterface } from '../types/BaseProvider.js';
32
import { GetOptions } from './GetOptions.js';
43

@@ -13,11 +12,8 @@ class GetMultipleOptions
1312
{
1413
public throwOnTransformError = false;
1514

16-
public constructor(
17-
envVarsService: EnvironmentVariablesService,
18-
options: GetMultipleOptionsInterface = {}
19-
) {
20-
super(envVarsService, options);
15+
public constructor(options: GetMultipleOptionsInterface = {}) {
16+
super(options);
2117

2218
if (options.throwOnTransformError !== undefined) {
2319
this.throwOnTransformError = options.throwOnTransformError;

packages/parameters/src/base/GetOptions.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js';
1+
import { getNumberFromEnv } from '@aws-lambda-powertools/commons/utils/env';
22
import { DEFAULT_MAX_AGE_SECS } from '../constants.js';
33
import type {
44
GetOptionsInterface,
@@ -16,15 +16,14 @@ class GetOptions implements GetOptionsInterface {
1616
public sdkOptions?: unknown;
1717
public transform?: TransformOptions;
1818

19-
public constructor(
20-
envVarsService: EnvironmentVariablesService,
21-
options: GetOptionsInterface = {}
22-
) {
19+
public constructor(options: GetOptionsInterface = {}) {
2320
Object.assign(this, options);
2421

2522
if (options.maxAge === undefined) {
26-
this.maxAge =
27-
envVarsService.getParametersMaxAge() ?? DEFAULT_MAX_AGE_SECS;
23+
this.maxAge = getNumberFromEnv({
24+
key: 'POWERTOOLS_PARAMETERS_MAX_AGE',
25+
defaultValue: DEFAULT_MAX_AGE_SECS,
26+
});
2827
}
2928
}
3029
}

packages/parameters/src/config/EnvironmentVariablesService.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

packages/parameters/src/ssm/SSMProvider.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
2+
import { getBooleanFromEnv } from '@aws-lambda-powertools/commons/utils/env';
23
import type {
34
GetParameterCommandInput,
45
GetParametersByPathCommandInput,
@@ -843,13 +844,12 @@ class SSMProvider extends BaseProvider {
843844
if (options?.decrypt !== undefined) return options.decrypt;
844845
if (sdkOptions?.WithDecryption !== undefined)
845846
return sdkOptions.WithDecryption;
846-
if (this.envVarsService.getSSMDecrypt() !== '') {
847-
return this.envVarsService.isValueTrue(
848-
this.envVarsService.getSSMDecrypt()
849-
);
850-
}
851847

852-
return undefined;
848+
return getBooleanFromEnv({
849+
key: 'POWERTOOLS_PARAMETERS_SSM_DECRYPT',
850+
defaultValue: false,
851+
extendedParsing: true,
852+
});
853853
}
854854

855855
/**

packages/parameters/src/types/ConfigServiceInterface.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/parameters/tests/unit/BaseProvider.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
GetMultipleOptions,
88
GetOptions,
99
} from '../../src/base/index.js';
10-
import type { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js';
1110
import { DEFAULT_MAX_AGE_SECS } from '../../src/constants.js';
1211
import {
1312
clearCaches,
@@ -550,9 +549,7 @@ describe('Class: GetOptions', () => {
550549
const envVarsService = {
551550
getParametersMaxAge: vi.fn(),
552551
};
553-
const options = new GetOptions(
554-
envVarsService as unknown as EnvironmentVariablesService
555-
);
552+
const options = new GetOptions();
556553

557554
// Assess
558555
expect(options.maxAge).toBe(DEFAULT_MAX_AGE_SECS);
@@ -565,9 +562,7 @@ describe('Class: GetMultipleOptions', () => {
565562
const envVarsService = {
566563
getParametersMaxAge: vi.fn(),
567564
};
568-
const options = new GetMultipleOptions(
569-
envVarsService as unknown as EnvironmentVariablesService
570-
);
565+
const options = new GetMultipleOptions();
571566

572567
// Assess
573568
expect(options.throwOnTransformError).toBe(false);

packages/parameters/tests/unit/EnvironmentVariablesService.test.ts

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)