Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dd74b08

Browse files
committedDec 28, 2022
refactor: moved SSM specific logic from BaseProvider to SSMProvider
1 parent 5410348 commit dd74b08

File tree

3 files changed

+22
-36
lines changed

3 files changed

+22
-36
lines changed
 

‎packages/parameters/src/BaseProvider.ts

+2-14
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { ExpirableValue } from './ExpirableValue';
55
import { TRANSFORM_METHOD_BINARY, TRANSFORM_METHOD_JSON } from './constants';
66
import { GetParameterError, TransformParameterError } from './Exceptions';
77
import type { BaseProviderInterface, GetMultipleOptionsInterface, GetOptionsInterface, TransformOptions } from './types';
8-
import { isSSMGetOptionsInterface, isSSMGetMultipleOptionsInterface } from './types/SSMProvider';
98
import type { SSMGetOptionsInterface, SSMGetMultipleOptionsInterface } from './types/SSMProvider';
109

1110
// These providers will be dynamically initialized on first use of the helper functions
@@ -55,12 +54,7 @@ abstract class BaseProvider implements BaseProviderInterface {
5554

5655
let value;
5756
try {
58-
// Type assertion is needed because the SSM provider has a different signature for the get method
59-
if (options && isSSMGetOptionsInterface(options) && options.decrypt) {
60-
options.sdkOptions = options.sdkOptions || {};
61-
options.sdkOptions.WithDecryption = true;
62-
}
63-
value = await this._get(name, options?.sdkOptions);
57+
value = await this._get(name, options);
6458
} catch (error) {
6559
throw new GetParameterError((error as Error).message);
6660
}
@@ -90,13 +84,7 @@ abstract class BaseProvider implements BaseProviderInterface {
9084

9185
let values: Record<string, unknown> = {};
9286
try {
93-
// Type assertion is needed because the SSM provider has a different signature for the get method
94-
if (options && isSSMGetMultipleOptionsInterface(options)) {
95-
options.sdkOptions = options.sdkOptions || {};
96-
if (options.hasOwnProperty('decrypt')) options.sdkOptions.WithDecryption = options.decrypt;
97-
if (options.hasOwnProperty('recursive')) options.sdkOptions.Recursive = options.recursive;
98-
}
99-
values = await this._getMultiple(path, options?.sdkOptions);
87+
values = await this._getMultiple(path, options);
10088
} catch (error) {
10189
throw new GetParameterError((error as Error).message);
10290
}

‎packages/parameters/src/SSMProvider.ts

+20-13
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,41 @@ class SSMProvider extends BaseProvider {
1212
this.client = new SSMClient(config);
1313
}
1414

15-
protected async _get(name: string, sdkOptions?: Partial<GetParameterCommandInput>): Promise<string | undefined> {
16-
const options: GetParameterCommandInput = {
15+
protected async _get(name: string, options?: SSMGetOptionsInterface): Promise<string | undefined> {
16+
const sdkOptions: GetParameterCommandInput = {
1717
Name: name,
1818
};
19-
if (sdkOptions) {
20-
Object.assign(options, sdkOptions);
19+
if (options) {
20+
if (options.hasOwnProperty('decrypt')) sdkOptions.WithDecryption = options.decrypt;
21+
if (options.hasOwnProperty('sdkOptions')) {
22+
Object.assign(sdkOptions, options.sdkOptions);
23+
}
2124
}
22-
const result = await this.client.send(new GetParameterCommand(options));
25+
const result = await this.client.send(new GetParameterCommand(sdkOptions));
2326

2427
return result.Parameter?.Value;
2528
}
2629

27-
protected async _getMultiple(path: string, sdkOptions?: Partial<GetParametersByPathCommandInput>): Promise<Record<string, string | undefined>> {
28-
const options: GetParametersByPathCommandInput = {
30+
protected async _getMultiple(path: string, options?: SSMGetMultipleOptionsInterface): Promise<Record<string, string | undefined>> {
31+
const sdkOptions: GetParametersByPathCommandInput = {
2932
Path: path,
3033
};
3134
const paginationOptions: PaginationConfiguration = {
3235
client: this.client
3336
};
34-
if (sdkOptions) {
35-
Object.assign(options, sdkOptions);
36-
if (sdkOptions.MaxResults) {
37-
paginationOptions.pageSize = sdkOptions.MaxResults;
37+
if (options) {
38+
if (options.hasOwnProperty('decrypt')) sdkOptions.WithDecryption = options.decrypt;
39+
if (options.hasOwnProperty('recursive')) sdkOptions.Recursive = options.recursive;
40+
if (options.hasOwnProperty('sdkOptions')) {
41+
Object.assign(sdkOptions, options.sdkOptions);
42+
if (sdkOptions.MaxResults) {
43+
paginationOptions.pageSize = sdkOptions.MaxResults;
44+
}
3845
}
3946
}
40-
47+
4148
const parameters: Record<string, string | undefined> = {};
42-
for await (const page of paginateGetParametersByPath(paginationOptions, options)) {
49+
for await (const page of paginateGetParametersByPath(paginationOptions, sdkOptions)) {
4350
for (const parameter of page.Parameters || []) {
4451
/**
4552
* Standardize the parameter name

‎packages/parameters/src/types/SSMProvider.ts

-9
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ interface SSMGetOptionsInterface {
1717
transform?: TransformOptions
1818
}
1919

20-
const isSSMGetOptionsInterface = (options: unknown): options is SSMGetOptionsInterface => (options as SSMGetOptionsInterface).decrypt !== undefined;
21-
2220
interface SSMGetMultipleOptionsInterface {
2321
maxAge?: number
2422
forceFetch?: boolean
@@ -29,14 +27,7 @@ interface SSMGetMultipleOptionsInterface {
2927
throwOnTransformError?: boolean
3028
}
3129

32-
const isSSMGetMultipleOptionsInterface =
33-
(options: unknown): options is SSMGetMultipleOptionsInterface =>
34-
(options as SSMGetMultipleOptionsInterface).decrypt !== undefined ||
35-
(options as SSMGetMultipleOptionsInterface).recursive !== undefined;
36-
3730
export {
3831
SSMGetOptionsInterface,
39-
isSSMGetOptionsInterface,
4032
SSMGetMultipleOptionsInterface,
41-
isSSMGetMultipleOptionsInterface,
4233
};

0 commit comments

Comments
 (0)
Please sign in to comment.