Skip to content

Commit 514abf8

Browse files
committed
feat: improved types for getParameters
1 parent 29fe7de commit 514abf8

File tree

4 files changed

+73
-22
lines changed

4 files changed

+73
-22
lines changed

Diff for: packages/parameters/src/ssm/SSMProvider.ts

+12-10
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ import type {
1515
} from '@aws-sdk/client-ssm';
1616
import type {
1717
SSMProviderOptions,
18-
SSMGetMultipleOptionsInterface,
1918
SSMGetOptions,
2019
SSMGetOptionsUnion,
2120
SSMGetOutput,
21+
SSMGetMultipleOptions,
22+
SSMGetMultipleOptionsUnion,
23+
SSMGetMultipleOutput,
2224
SSMGetParametersByNameOutputInterface,
2325
SSMGetParametersByNameOptionsInterface,
2426
SSMSplitBatchAndDecryptParametersOutputType,
@@ -314,7 +316,7 @@ class SSMProvider extends BaseProvider {
314316
* For usage examples check {@link SSMProvider}.
315317
*
316318
* @param {string} name - The name of the value to retrieve (i.e. the partition key)
317-
* @param {SSMGetOptionsInterface} options - Options to configure the provider
319+
* @param {SSMGetOptions} options - Options to configure the provider
318320
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/
319321
*/
320322
public async get<O extends SSMGetOptionsUnion | undefined = undefined>(
@@ -351,14 +353,14 @@ class SSMProvider extends BaseProvider {
351353
* For usage examples check {@link SSMProvider}.
352354
*
353355
* @param {string} path - The path of the parameters to retrieve
354-
* @param {SSMGetMultipleOptionsInterface} options - Options to configure the retrieval
356+
* @param {SSMGetMultipleOptions} options - Options to configure the retrieval
355357
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/
356358
*/
357-
public async getMultiple(
359+
public async getMultiple<O extends SSMGetMultipleOptionsUnion | undefined = undefined>(
358360
path: string,
359-
options?: SSMGetMultipleOptionsInterface | undefined
360-
): Promise<undefined | Record<string, unknown>> {
361-
return super.getMultiple(path, options);
361+
options?: O & SSMGetMultipleOptions
362+
): Promise<SSMGetMultipleOutput<O> | undefined> {
363+
return super.getMultiple(path, options) as Promise<SSMGetMultipleOutput<O> | undefined>;
362364
}
363365

364366
/**
@@ -469,7 +471,7 @@ class SSMProvider extends BaseProvider {
469471
* Retrieve a parameter from AWS Systems Manager.
470472
*
471473
* @param {string} name - Name of the parameter to retrieve
472-
* @param {SSMGetOptionsInterface} options - Options to customize the retrieval
474+
* @param {SSMGetOptions} options - Options to customize the retrieval
473475
*/
474476
protected async _get(
475477
name: string,
@@ -490,11 +492,11 @@ class SSMProvider extends BaseProvider {
490492
* Retrieve multiple items from AWS Systems Manager.
491493
*
492494
* @param {string} path - The path of the parameters to retrieve
493-
* @param {SSMGetMultipleOptionsInterface} options - Options to configure the provider
495+
* @param {SSMGetMultipleOptions} options - Options to configure the provider
494496
*/
495497
protected async _getMultiple(
496498
path: string,
497-
options?: SSMGetMultipleOptionsInterface
499+
options?: SSMGetMultipleOptions
498500
): Promise<Record<string, string | undefined>> {
499501
const sdkOptions: GetParametersByPathCommandInput = {
500502
...(options?.sdkOptions || {}),

Diff for: packages/parameters/src/ssm/getParameter.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ import type {
137137
* For more usage examples, see [our documentation](https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/).
138138
*
139139
* @param {string} name - The name of the parameter to retrieve
140-
* @param {SSMGetOptionsInterface} options - Options to configure the provider
140+
* @param {SSMGetOptions} options - Options to configure the provider
141141
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/
142142
*/
143143
const getParameter = <O extends SSMGetOptionsUnion | undefined = undefined>(
@@ -148,7 +148,9 @@ const getParameter = <O extends SSMGetOptionsUnion | undefined = undefined>(
148148
DEFAULT_PROVIDERS.ssm = new SSMProvider();
149149
}
150150

151-
return (DEFAULT_PROVIDERS.ssm as SSMProvider).get(name, options) as Promise<SSMGetOutput<O> | undefined>;
151+
return (
152+
DEFAULT_PROVIDERS.ssm as SSMProvider
153+
).get(name, options) as Promise<SSMGetOutput<O> | undefined>;
152154
};
153155

154156
export {

Diff for: packages/parameters/src/ssm/getParameters.ts

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { SSMProvider, DEFAULT_PROVIDERS } from './SSMProvider';
2-
import type { SSMGetMultipleOptionsInterface } from '../types/SSMProvider';
2+
import type {
3+
SSMGetMultipleOptions,
4+
SSMGetMultipleOptionsUnion,
5+
SSMGetMultipleOutput,
6+
} from '../types/SSMProvider';
37

48
/**
59
* ## Intro
@@ -134,18 +138,20 @@ import type { SSMGetMultipleOptionsInterface } from '../types/SSMProvider';
134138
* For more usage examples, see [our documentation](https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/).
135139
*
136140
* @param {string} path - The path of the parameters to retrieve
137-
* @param {SSMGetMultipleOptionsInterface} options - Options to configure the provider
141+
* @param {SSMGetMultipleOptions} options - Options to configure the provider
138142
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/
139143
*/
140-
const getParameters = (
144+
const getParameters = <O extends SSMGetMultipleOptionsUnion | undefined = undefined>(
141145
path: string,
142-
options?: SSMGetMultipleOptionsInterface
143-
): Promise<undefined | Record<string, unknown>> => {
146+
options?: O & SSMGetMultipleOptions
147+
): Promise<SSMGetMultipleOutput<O> | undefined> => {
144148
if (!DEFAULT_PROVIDERS.hasOwnProperty('ssm')) {
145149
DEFAULT_PROVIDERS.ssm = new SSMProvider();
146150
}
147151

148-
return (DEFAULT_PROVIDERS.ssm as SSMProvider).getMultiple(path, options);
152+
return (
153+
DEFAULT_PROVIDERS.ssm as SSMProvider
154+
).getMultiple(path, options) as Promise<SSMGetMultipleOutput<O> | undefined>;
149155
};
150156

151157
export {

Diff for: packages/parameters/src/types/SSMProvider.ts

+45-4
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,18 @@ type SSMProviderOptions = SSMProviderOptionsWithClientConfig | SSMProviderOption
5050
* @extends {GetOptionsInterface}
5151
* @property {number} maxAge - Maximum age of the value in the cache, in seconds.
5252
* @property {boolean} forceFetch - Force fetch the value from the parameter store, ignoring the cache.
53-
* @property {GetItemCommandInput} [sdkOptions] - Additional options to pass to the AWS SDK v3 client.
53+
* @property {GetItemCommandInput} [sdkOptions] - Additional options to pass to the AWS SDK v3 client. Supports all options from `GetParameterCommandInput`.
5454
* @property {TransformOptions} transform - Transform to be applied, can be 'json' or 'binary'.
55-
* @property {boolean} decrypt - If true, the parameter will be decrypted.
55+
* @property {boolean} decrypt - If true, the parameter will be decrypted. Defaults to `false`.
5656
*/
5757
interface SSMGetOptions extends GetOptionsInterface {
58+
/**
59+
* If true, the parameter will be decrypted. Defaults to `false`.
60+
*/
5861
decrypt?: boolean
62+
/**
63+
* Additional options to pass to the AWS SDK v3 client. Supports all options from `GetParameterCommandInput`.
64+
*/
5965
sdkOptions?: Partial<GetParameterCommandInput>
6066
}
6167

@@ -93,13 +99,46 @@ type SSMGetOutput<O = undefined> =
9399
* @property {boolean} recursive - If true, the parameter will be fetched recursively.
94100
* @property {boolean} throwOnTransformError - If true, the method will throw an error if the transform fails.
95101
*/
96-
interface SSMGetMultipleOptionsInterface extends GetMultipleOptionsInterface {
102+
interface SSMGetMultipleOptions extends GetMultipleOptionsInterface {
103+
/**
104+
* Additional options to pass to the AWS SDK v3 client. Supports all options from `GetParametersByPathCommandInput`.
105+
*/
97106
sdkOptions?: Partial<GetParametersByPathCommandInput>
107+
/**
108+
* If true, the parameters will be decrypted. Defaults to `false`.
109+
*/
98110
decrypt?: boolean
111+
/**
112+
* If true, the parameters will be fetched recursively. Defaults to `false`.
113+
*/
99114
recursive?: boolean
115+
/**
116+
* If true, the method will throw an error if the transform fails.
117+
*/
100118
throwOnTransformError?: boolean
101119
}
102120

121+
interface SSMGetMultipleOptionsTransformJson extends SSMGetMultipleOptions {
122+
transform: 'json'
123+
}
124+
125+
interface SSMGetMultipleOptionsTransformBinary extends SSMGetMultipleOptions {
126+
transform: 'binary'
127+
}
128+
129+
interface SSMGetMultipleOptionsTransformNone extends SSMGetMultipleOptions {
130+
transform?: never
131+
}
132+
133+
type SSMGetMultipleOptionsUnion = SSMGetMultipleOptionsTransformJson | SSMGetMultipleOptionsTransformBinary | SSMGetMultipleOptionsTransformNone | undefined;
134+
135+
type SSMGetMultipleOutput<O = undefined> =
136+
undefined extends O ? Record<string, string> :
137+
O extends SSMGetMultipleOptionsTransformNone | SSMGetMultipleOptionsTransformBinary ? Record<string, string> :
138+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
139+
O extends SSMGetMultipleOptionsTransformJson ? Record<string, Record<string, any>> :
140+
never;
141+
103142
/**
104143
* Options for the SSMProvider getParametersByName method.
105144
*
@@ -145,7 +184,9 @@ export type {
145184
SSMGetOptions,
146185
SSMGetOptionsUnion,
147186
SSMGetOutput,
148-
SSMGetMultipleOptionsInterface,
187+
SSMGetMultipleOptions,
188+
SSMGetMultipleOptionsUnion,
189+
SSMGetMultipleOutput,
149190
SSMGetParametersByNameOptionsInterface,
150191
SSMSplitBatchAndDecryptParametersOutputType,
151192
SSMGetParametersByNameOutputInterface,

0 commit comments

Comments
 (0)