Skip to content

Commit 36a1628

Browse files
committed
chore(commons): simplify config service interface
1 parent 5d38256 commit 36a1628

16 files changed

+206
-78
lines changed

packages/commons/src/config/EnvironmentVariablesService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ConfigService } from './ConfigService.js';
1+
import type { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';
22

33
/**
44
* Class EnvironmentVariablesService
@@ -13,7 +13,7 @@ import { ConfigService } from './ConfigService.js';
1313
* @see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime
1414
* @see https://docs.powertools.aws.dev/lambda-typescript/latest/#environment-variables
1515
*/
16-
class EnvironmentVariablesService implements ConfigService {
16+
class EnvironmentVariablesService implements ConfigServiceInterface {
1717
/**
1818
* @see https://docs.powertools.aws.dev/lambda-typescript/latest/#environment-variables
1919
* @protected

packages/commons/src/config/ConfigService.ts renamed to packages/commons/src/types/ConfigServiceInterface.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,22 @@
33
*
44
* This class defines common methods and variables that can be set by the developer
55
* in the runtime.
6-
*
7-
* @class
8-
* @abstract
96
*/
10-
abstract class ConfigService {
7+
interface ConfigServiceInterface {
118
/**
129
* It returns the value of an environment variable that has given name.
1310
*
1411
* @param {string} name
1512
* @returns {string}
1613
*/
17-
public abstract get(name: string): string;
14+
get(name: string): string;
1815

1916
/**
2017
* It returns the value of the POWERTOOLS_SERVICE_NAME environment variable.
2118
*
2219
* @returns {string}
2320
*/
24-
public abstract getServiceName(): string;
21+
getServiceName(): string;
2522

2623
/**
2724
* It returns the value of the _X_AMZN_TRACE_ID environment variable.
@@ -33,22 +30,22 @@ abstract class ConfigService {
3330
*
3431
* @returns {string|undefined}
3532
*/
36-
public abstract getXrayTraceId(): string | undefined;
33+
getXrayTraceId(): string | undefined;
3734

3835
/**
3936
* It returns true if the `POWERTOOLS_DEV` environment variable is set to truthy value.
4037
*
4138
* @returns {boolean}
4239
*/
43-
public abstract isDevMode(): boolean;
40+
isDevMode(): boolean;
4441

4542
/**
4643
* It returns true if the string value represents a boolean true value.
4744
*
4845
* @param {string} value
4946
* @returns boolean
5047
*/
51-
public abstract isValueTrue(value: string): boolean;
48+
isValueTrue(value: string): boolean;
5249
}
5350

54-
export { ConfigService };
51+
export type { ConfigServiceInterface };

packages/commons/src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ export type {
1616
LambdaInterface,
1717
HandlerMethodDecorator,
1818
} from './LambdaInterface.js';
19+
export type { ConfigServiceInterface } from './ConfigServiceInterface.js';

packages/idempotency/src/types/ConfigServiceInterface.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1-
interface ConfigServiceInterface {
2-
get(name: string): string;
3-
4-
getServiceName(): string;
1+
import type { ConfigServiceInterface as ConfigServiceBaseInterface } from '@aws-lambda-powertools/commons/types';
52

3+
/**
4+
* Interface ConfigServiceInterface
5+
*
6+
* @interface
7+
* @see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime
8+
* @see https://docs.powertools.aws.dev/lambda-typescript/latest/#environment-variables
9+
*/
10+
interface ConfigServiceInterface extends ConfigServiceBaseInterface {
11+
/**
12+
* It returns the value of the AWS_LAMBDA_FUNCTION_NAME environment variable.
13+
*
14+
* @returns {string}
15+
*/
616
getFunctionName(): string;
717

18+
/**
19+
* It returns whether the idempotency feature is enabled or not.
20+
*
21+
* Reads the value of the POWERTOOLS_IDEMPOTENCY_DISABLED environment variable.
22+
*
23+
* @returns {boolean}
24+
*/
825
getIdempotencyEnabled(): boolean;
926
}
1027

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1+
import type { ConfigServiceInterface as ConfigServiceBaseInterface } from '@aws-lambda-powertools/commons/types';
2+
13
/**
24
* Interface ConfigServiceInterface
35
*
46
* @interface
57
* @see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime
68
* @see https://docs.powertools.aws.dev/lambda-typescript/latest/#environment-variables
79
*/
8-
interface ConfigServiceInterface {
9-
/**
10-
* It returns the value of an environment variable that has given name.
11-
*
12-
* @param {string} name
13-
* @returns {string}
14-
*/
15-
get(name: string): string;
16-
10+
interface ConfigServiceInterface extends ConfigServiceBaseInterface {
1711
/**
1812
* It returns the value of the ENVIRONMENT environment variable.
1913
*
@@ -41,28 +35,6 @@ interface ConfigServiceInterface {
4135
* @returns {string|undefined}
4236
*/
4337
getSampleRateValue(): number | undefined;
44-
45-
/**
46-
* It returns the value of the POWERTOOLS_SERVICE_NAME environment variable.
47-
*
48-
* @returns {string}
49-
*/
50-
getServiceName(): string;
51-
52-
/**
53-
* It returns the value of the POWERTOOLS_DEV environment variable.
54-
*
55-
* @returns {boolean}
56-
*/
57-
isDevMode(): boolean;
58-
59-
/**
60-
* It returns true if the string value represents a boolean true value.
61-
*
62-
* @param {string} value
63-
* @returns boolean
64-
*/
65-
isValueTrue(value: string): boolean;
6638
}
6739

6840
export type { ConfigServiceInterface };

packages/logger/tests/unit/Logger.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ describe('Class: Logger', () => {
369369
getServiceName(): string {
370370
return 'my-backend-service';
371371
},
372+
getXrayTraceId(): string | undefined {
373+
return undefined;
374+
},
372375
isDevMode(): boolean {
373376
return false;
374377
},

packages/logger/tests/unit/formatter/PowertoolsLogFormatter.test.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,12 @@ describe('Class: PowertoolsLogFormatter', () => {
113113
const formattedError = formatter.formatError(new Error('Ouch!'));
114114
expect(formattedError).toEqual({
115115
location: expect.stringMatching(
116-
/PowertoolLogFormatter.test.ts:[0-9]+$/
116+
/PowertoolsLogFormatter.test.ts:[0-9]+$/
117117
),
118118
message: 'Ouch!',
119119
name: 'Error',
120120
stack: expect.stringMatching(
121-
/PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/
121+
/PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/
122122
),
123123
});
124124
});
@@ -133,12 +133,12 @@ describe('Class: PowertoolsLogFormatter', () => {
133133
);
134134
expect(formattedReferenceError).toEqual({
135135
location: expect.stringMatching(
136-
/PowertoolLogFormatter.test.ts:[0-9]+$/
136+
/PowertoolsLogFormatter.test.ts:[0-9]+$/
137137
),
138138
message: 'doesNotExist is not defined',
139139
name: 'ReferenceError',
140140
stack: expect.stringMatching(
141-
/PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/
141+
/PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/
142142
),
143143
});
144144
});
@@ -163,7 +163,7 @@ describe('Class: PowertoolsLogFormatter', () => {
163163
message: expect.stringMatching(/Expected values to be strictly equal/),
164164
name: 'AssertionError',
165165
stack: expect.stringMatching(
166-
/PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/
166+
/PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/
167167
),
168168
});
169169
});
@@ -177,11 +177,13 @@ describe('Class: PowertoolsLogFormatter', () => {
177177
new RangeError('The argument must be between 10 and 20')
178178
);
179179
expect(formattedRangeError).toEqual({
180-
location: expect.stringMatching(/PowertoolLogFormatter.test.ts:[0-9]+/),
180+
location: expect.stringMatching(
181+
/PowertoolsLogFormatter.test.ts:[0-9]+/
182+
),
181183
message: 'The argument must be between 10 and 20',
182184
name: 'RangeError',
183185
stack: expect.stringMatching(
184-
/PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/
186+
/PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/
185187
),
186188
});
187189
});
@@ -195,11 +197,13 @@ describe('Class: PowertoolsLogFormatter', () => {
195197
new ReferenceError('foo is not defined')
196198
);
197199
expect(formattedError).toEqual({
198-
location: expect.stringMatching(/PowertoolLogFormatter.test.ts:[0-9]+/),
200+
location: expect.stringMatching(
201+
/PowertoolsLogFormatter.test.ts:[0-9]+/
202+
),
199203
message: 'foo is not defined',
200204
name: 'ReferenceError',
201205
stack: expect.stringMatching(
202-
/PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/
206+
/PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/
203207
),
204208
});
205209
});
@@ -213,11 +217,13 @@ describe('Class: PowertoolsLogFormatter', () => {
213217
new SyntaxError(`Unexpected identifier 'bar'`)
214218
);
215219
expect(formattedSyntaxError).toEqual({
216-
location: expect.stringMatching(/PowertoolLogFormatter.test.ts:[0-9]+/),
220+
location: expect.stringMatching(
221+
/PowertoolsLogFormatter.test.ts:[0-9]+/
222+
),
217223
message: `Unexpected identifier 'bar'`,
218224
name: 'SyntaxError',
219225
stack: expect.stringMatching(
220-
/PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/
226+
/PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/
221227
),
222228
});
223229
});
@@ -231,11 +237,13 @@ describe('Class: PowertoolsLogFormatter', () => {
231237
new TypeError(`Cannot read property 'foo' of null`)
232238
);
233239
expect(formattedTypeError).toEqual({
234-
location: expect.stringMatching(/PowertoolLogFormatter.test.ts:[0-9]+/),
240+
location: expect.stringMatching(
241+
/PowertoolsLogFormatter.test.ts:[0-9]+/
242+
),
235243
message: expect.stringMatching(/Cannot read propert/),
236244
name: 'TypeError',
237245
stack: expect.stringMatching(
238-
/PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/
246+
/PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/
239247
),
240248
});
241249
});
@@ -249,11 +257,13 @@ describe('Class: PowertoolsLogFormatter', () => {
249257
new URIError('URI malformed')
250258
);
251259
expect(formattedURIError).toEqual({
252-
location: expect.stringMatching(/PowertoolLogFormatter.test.ts:[0-9]+/),
260+
location: expect.stringMatching(
261+
/PowertoolsLogFormatter.test.ts:[0-9]+/
262+
),
253263
message: 'URI malformed',
254264
name: 'URIError',
255265
stack: expect.stringMatching(
256-
/PowertoolLogFormatter.test.ts:[0-9]+:[0-9]+/
266+
/PowertoolsLogFormatter.test.ts:[0-9]+:[0-9]+/
257267
),
258268
});
259269
});

packages/logger/tests/unit/middleware/middy.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,9 @@ describe('Middy middleware', () => {
289289
getServiceName(): string {
290290
return 'my-backend-service';
291291
},
292+
getXrayTraceId(): string | undefined {
293+
return undefined;
294+
},
292295
isDevMode(): boolean {
293296
return false;
294297
},

packages/metrics/src/config/EnvironmentVariablesService.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ class EnvironmentVariablesService
77
{
88
private namespaceVariable = 'POWERTOOLS_METRICS_NAMESPACE';
99

10+
/**
11+
* It returns the value of the POWERTOOLS_METRICS_NAMESPACE environment variable.
12+
*
13+
* @returns {string}
14+
*/
1015
public getNamespace(): string {
1116
return this.get(this.namespaceVariable);
1217
}
Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
interface ConfigServiceInterface {
2-
get?(name: string): string;
3-
getNamespace(): string;
4-
getServiceName(): string;
1+
import type { ConfigServiceInterface as ConfigServiceBaseInterface } from '@aws-lambda-powertools/commons/types';
2+
3+
/**
4+
* Interface ConfigServiceInterface
5+
*
6+
* @interface
7+
* @see https://docs.aws.amazon.com/lambda/latest/dg/configuration-envvars.html#configuration-envvars-runtime
8+
* @see https://docs.powertools.aws.dev/lambda-typescript/latest/#environment-variables
9+
*/
10+
interface ConfigServiceInterface extends ConfigServiceBaseInterface {
511
/**
6-
* It returns the value of the POWERTOOLS_DEV environment variable.
12+
* It returns the value of the POWERTOOLS_METRICS_NAMESPACE environment variable.
713
*
8-
* @returns {boolean}
14+
* @returns {string}
915
*/
10-
isDevMode(): boolean;
16+
getNamespace(): string;
1117
}
1218

1319
export type { ConfigServiceInterface };

packages/metrics/tests/unit/Metrics.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,15 @@ describe('Class: Metrics', () => {
242242
getServiceName(): string {
243243
return 'test-service';
244244
},
245+
getXrayTraceId(): string | undefined {
246+
return 'test-trace-id';
247+
},
245248
isDevMode(): boolean {
246249
return false;
247250
},
251+
isValueTrue(value: string): boolean {
252+
return value === 'true';
253+
},
248254
};
249255
const metricsOptions: MetricsOptions = {
250256
customConfigService: configService,

packages/parameters/src/config/EnvironmentVariablesService.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ class EnvironmentVariablesService
1010
private parametersMaxAgeVariable = 'POWERTOOLS_PARAMETERS_MAX_AGE';
1111
private ssmDecryptVariable = 'POWERTOOLS_PARAMETERS_SSM_DECRYPT';
1212

13+
/**
14+
* It returns the value of the POWERTOOLS_PARAMETERS_MAX_AGE environment variable.
15+
*
16+
* @returns {number|undefined}
17+
*/
1318
public getParametersMaxAge(): number | undefined {
1419
const maxAge = this.get(this.parametersMaxAgeVariable);
1520

@@ -25,6 +30,11 @@ class EnvironmentVariablesService
2530
}
2631
}
2732

33+
/**
34+
* It returns the value of the POWERTOOLS_PARAMETERS_SSM_DECRYPT environment variable.
35+
*
36+
* @returns {string}
37+
*/
2838
public getSSMDecrypt(): string {
2939
return this.get(this.ssmDecryptVariable);
3040
}

0 commit comments

Comments
 (0)