Skip to content

Commit 2e3c5b2

Browse files
committed
refactor: readability
1 parent 31a493f commit 2e3c5b2

File tree

2 files changed

+45
-50
lines changed

2 files changed

+45
-50
lines changed

packages/parameters/src/secrets/SecretsProvider.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { BaseProvider } from '../BaseProvider';
2-
import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
2+
import {
3+
SecretsManagerClient,
4+
GetSecretValueCommand
5+
} from '@aws-sdk/client-secrets-manager';
36
import type { GetSecretValueCommandInput } from '@aws-sdk/client-secrets-manager';
4-
import type { SecretsProviderOptions, SecretsGetOptionsInterface } from 'types/SecretsProvider';
7+
import type {
8+
SecretsProviderOptions,
9+
SecretsGetOptionsInterface
10+
} from '../types/SecretsProvider';
511

612
class SecretsProvider extends BaseProvider {
713
public client: SecretsManagerClient;
@@ -13,18 +19,21 @@ class SecretsProvider extends BaseProvider {
1319
this.client = new SecretsManagerClient(clientConfig);
1420
}
1521

16-
public async get(name: string, options?: SecretsGetOptionsInterface): Promise<undefined | string | Uint8Array | Record<string, unknown>> {
22+
public async get(
23+
name: string,
24+
options?: SecretsGetOptionsInterface
25+
): Promise<undefined | string | Uint8Array | Record<string, unknown>> {
1726
return super.get(name, options);
1827
}
1928

20-
protected async _get(name: string, options?: SecretsGetOptionsInterface): Promise<string | Uint8Array | undefined> {
29+
protected async _get(
30+
name: string,
31+
options?: SecretsGetOptionsInterface
32+
): Promise<string | Uint8Array | undefined> {
2133
const sdkOptions: GetSecretValueCommandInput = {
34+
...(options?.sdkOptions || {}),
2235
SecretId: name,
2336
};
24-
if (options?.sdkOptions) {
25-
this.removeNonOverridableOptions(options.sdkOptions as GetSecretValueCommandInput);
26-
Object.assign(sdkOptions, options.sdkOptions);
27-
}
2837

2938
const result = await this.client.send(new GetSecretValueCommand(sdkOptions));
3039

@@ -36,20 +45,12 @@ class SecretsProvider extends BaseProvider {
3645
/**
3746
* Retrieving multiple parameter values is not supported with AWS Secrets Manager.
3847
*/
39-
protected async _getMultiple(_path: string, _options?: unknown): Promise<Record<string, string | undefined>> {
48+
protected async _getMultiple(
49+
_path: string,
50+
_options?: unknown
51+
): Promise<Record<string, string | undefined>> {
4052
throw new Error('Method not implemented.');
41-
}
42-
43-
/**
44-
* Explicit arguments passed to the constructor will take precedence over ones passed to the method.
45-
* For users who consume the library with TypeScript, this will be enforced by the type system. However,
46-
* for JavaScript users, we need to manually delete the properties that are not allowed to be overridden.
47-
*/
48-
protected removeNonOverridableOptions(options: GetSecretValueCommandInput): void {
49-
if (options.hasOwnProperty('SecretId')) {
50-
delete options.SecretId;
51-
}
52-
}
53+
}
5354
}
5455

5556
export {

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

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,29 @@ describe('Class: SecretsProvider', () => {
8080

8181
});
8282

83+
test('when called with sdkOptions that override arguments passed to the method, it gets the secret using the arguments', async () => {
84+
85+
// Prepare
86+
const provider = new SecretsProvider();
87+
const secretName = 'foo';
88+
client.on(GetSecretValueCommand).resolves({
89+
SecretString: 'bar',
90+
});
91+
92+
// Act
93+
await provider.get(secretName, {
94+
sdkOptions: {
95+
SecretId: 'test-secret',
96+
} as unknown as GetSecretValueCommandInput,
97+
});
98+
99+
// Assess
100+
expect(client).toReceiveCommandWith(GetSecretValueCommand, {
101+
SecretId: secretName,
102+
});
103+
104+
});
105+
83106
});
84107

85108
describe('Method: _getMultiple', () => {
@@ -96,33 +119,4 @@ describe('Class: SecretsProvider', () => {
96119

97120
});
98121

99-
describe('Method: removeNonOverridableOptions', () => {
100-
101-
class DummyProvider extends SecretsProvider {
102-
public removeNonOverridableOptions(options: GetSecretValueCommandInput): void {
103-
super.removeNonOverridableOptions(options);
104-
}
105-
}
106-
107-
test('when called with a valid GetSecretValueCommandInput, it removes the non-overridable options', () => {
108-
109-
// Prepare
110-
const provider = new DummyProvider();
111-
const options: GetSecretValueCommandInput = {
112-
SecretId: 'test-secret',
113-
VersionId: 'test-version',
114-
};
115-
116-
// Act
117-
provider.removeNonOverridableOptions(options);
118-
119-
// Assess
120-
expect(options).toEqual({
121-
VersionId: 'test-version',
122-
});
123-
124-
});
125-
126-
});
127-
128122
});

0 commit comments

Comments
 (0)