Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parameters): stronger types for SSM getParameter #1387

Merged
merged 5 commits into from
Apr 5, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: added explicit type override
  • Loading branch information
dreamorosi committed Mar 29, 2023
commit 971ce1af4a69765b5195fbdbde9bf910a5cda6a6
20 changes: 10 additions & 10 deletions packages/parameters/src/ssm/SSMProvider.ts
Original file line number Diff line number Diff line change
@@ -16,11 +16,11 @@ import type {
import type {
SSMProviderOptions,
SSMGetOptions,
SSMGetOptionsUnion,
SSMGetOutput,
SSMGetMultipleOptions,
SSMGetMultipleOptionsUnion,
SSMGetMultipleOutput,
SSMGetParametersByNameOutput,
SSMGetParametersByNameOutputInterface,
SSMGetParametersByNameOptionsInterface,
SSMSplitBatchAndDecryptParametersOutputType,
@@ -319,11 +319,11 @@ class SSMProvider extends BaseProvider {
* @param {SSMGetOptions} options - Options to configure the provider
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/
*/
public async get<O extends SSMGetOptionsUnion | undefined = undefined>(
public async get<T = undefined, O extends SSMGetOptions | undefined = SSMGetOptions>(
name: string,
options?: O & SSMGetOptions
): Promise<SSMGetOutput<O> | undefined> {
return super.get(name, options) as Promise<SSMGetOutput<O> | undefined>;
): Promise<SSMGetOutput<T, O> | undefined> {
return super.get(name, options) as Promise<SSMGetOutput<T, O> | undefined>;
}

/**
@@ -356,11 +356,11 @@ class SSMProvider extends BaseProvider {
* @param {SSMGetMultipleOptions} options - Options to configure the retrieval
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/
*/
public async getMultiple<O extends SSMGetMultipleOptionsUnion | undefined = undefined>(
public async getMultiple<T = undefined, O extends SSMGetMultipleOptionsUnion | undefined = undefined>(
path: string,
options?: O & SSMGetMultipleOptions
): Promise<SSMGetMultipleOutput<O> | undefined> {
return super.getMultiple(path, options) as Promise<SSMGetMultipleOutput<O> | undefined>;
): Promise<SSMGetMultipleOutput<T, O> | undefined> {
return super.getMultiple(path, options) as Promise<SSMGetMultipleOutput<T, O> | undefined>;
}

/**
@@ -413,10 +413,10 @@ class SSMProvider extends BaseProvider {
* @param {SSMGetParametersByNameOptionsInterface} options - Options to configure the retrieval
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/
*/
public async getParametersByName(
public async getParametersByName<T = undefined>(
parameters: Record<string, SSMGetParametersByNameOptionsInterface>,
options?: SSMGetParametersByNameOptionsInterface
): Promise<Record<string, unknown>> {
): Promise<SSMGetParametersByNameOutput<T>> {
const configs = { ...{
decrypt: this.resolveDecryptionConfigValue({}) || false,
maxAge: DEFAULT_MAX_AGE_SECS,
@@ -464,7 +464,7 @@ class SSMProvider extends BaseProvider {
}
}

return response;
return response as unknown as Promise<SSMGetParametersByNameOutput<T>>;
}

/**
7 changes: 3 additions & 4 deletions packages/parameters/src/ssm/getParameter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { SSMProvider, DEFAULT_PROVIDERS } from './SSMProvider';
import type {
SSMGetOptions,
SSMGetOptionsUnion,
SSMGetOutput,
} from '../types/SSMProvider';

@@ -140,17 +139,17 @@ import type {
* @param {SSMGetOptions} options - Options to configure the provider
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/
*/
const getParameter = <O extends SSMGetOptionsUnion | undefined = undefined>(
const getParameter = <T = undefined, O extends SSMGetOptions | undefined = SSMGetOptions>(
name: string,
options?: O & SSMGetOptions
): Promise<SSMGetOutput<O> | undefined> => {
): Promise<SSMGetOutput<T, O> | undefined> => {
if (!DEFAULT_PROVIDERS.hasOwnProperty('ssm')) {
DEFAULT_PROVIDERS.ssm = new SSMProvider();
}

return (
DEFAULT_PROVIDERS.ssm as SSMProvider
).get(name, options) as Promise<SSMGetOutput<O> | undefined>;
).get(name, options) as Promise<SSMGetOutput<T, O> | undefined>;
};

export {
6 changes: 3 additions & 3 deletions packages/parameters/src/ssm/getParameters.ts
Original file line number Diff line number Diff line change
@@ -141,17 +141,17 @@ import type {
* @param {SSMGetMultipleOptions} options - Options to configure the provider
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/
*/
const getParameters = <O extends SSMGetMultipleOptionsUnion | undefined = undefined>(
const getParameters = <T = undefined, O extends SSMGetMultipleOptionsUnion | undefined = SSMGetMultipleOptionsUnion>(
path: string,
options?: O & SSMGetMultipleOptions
): Promise<SSMGetMultipleOutput<O> | undefined> => {
): Promise<SSMGetMultipleOutput<T, O> | undefined> => {
if (!DEFAULT_PROVIDERS.hasOwnProperty('ssm')) {
DEFAULT_PROVIDERS.ssm = new SSMProvider();
}

return (
DEFAULT_PROVIDERS.ssm as SSMProvider
).getMultiple(path, options) as Promise<SSMGetMultipleOutput<O> | undefined>;
).getMultiple(path, options) as Promise<SSMGetMultipleOutput<T, O> | undefined>;
};

export {
11 changes: 7 additions & 4 deletions packages/parameters/src/ssm/getParametersByName.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SSMProvider, DEFAULT_PROVIDERS } from './SSMProvider';
import type {
SSMGetParametersByNameOptionsInterface
SSMGetParametersByNameOptionsInterface,
SSMGetParametersByNameOutput,
} from '../types/SSMProvider';

/**
@@ -160,15 +161,17 @@ import type {
* @param {SSMGetParametersByNameOptionsInterface} options - Options to configure the provider
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/
*/
const getParametersByName = (
const getParametersByName = <T = undefined>(
parameters: Record<string, SSMGetParametersByNameOptionsInterface>,
options?: SSMGetParametersByNameOptionsInterface
): Promise<Record<string, unknown> & { _errors?: string[] }> => {
): Promise<SSMGetParametersByNameOutput<T>> => {
if (!DEFAULT_PROVIDERS.hasOwnProperty('ssm')) {
DEFAULT_PROVIDERS.ssm = new SSMProvider();
}

return (DEFAULT_PROVIDERS.ssm as SSMProvider).getParametersByName(parameters, options);
return (
DEFAULT_PROVIDERS.ssm as SSMProvider
).getParametersByName(parameters, options) as Promise<SSMGetParametersByNameOutput<T>>;
};

export {
8 changes: 6 additions & 2 deletions packages/parameters/src/types/BaseProvider.ts
Original file line number Diff line number Diff line change
@@ -25,9 +25,9 @@ interface GetOptionsInterface {
*/
sdkOptions?: unknown
/**
* Transform to be applied, can be 'json', 'binary', or 'auto'.
* Transform to be applied, can be `json` or `binary`.
*/
transform?: TransformOptions
transform?: Omit<TransformOptions, 'auto'>
}

/**
@@ -40,6 +40,10 @@ interface GetOptionsInterface {
* @property {boolean} throwOnTransformError - Whether to throw an error if a value cannot be transformed.
*/
interface GetMultipleOptionsInterface extends GetOptionsInterface {
/**
* Transform to be applied, can be `json`, `binary`, or `auto`.
*/
transform?: TransformOptions
/**
* Whether to throw an error if a value cannot be transformed.
*/
49 changes: 36 additions & 13 deletions packages/parameters/src/types/SSMProvider.ts
Original file line number Diff line number Diff line change
@@ -63,6 +63,8 @@ interface SSMGetOptions extends GetOptionsInterface {
* Additional options to pass to the AWS SDK v3 client. Supports all options from `GetParameterCommandInput`.
*/
sdkOptions?: Partial<GetParameterCommandInput>

transform?: Exclude<TransformOptions, 'auto'>
}

interface SSMGetOptionsTransformJson extends SSMGetOptions {
@@ -77,14 +79,16 @@ interface SSMGetOptionsTransformNone extends SSMGetOptions {
transform?: never
}

type SSMGetOptionsUnion = SSMGetOptionsTransformJson | SSMGetOptionsTransformBinary | SSMGetOptionsTransformNone | undefined;

type SSMGetOutput<O = undefined> =
/**
* Generic output type for the SSMProvider get method.
*/
type SSMGetOutput<T = undefined, O = undefined> =
undefined extends T ?
undefined extends O ? string :
O extends SSMGetOptionsTransformNone | SSMGetOptionsTransformBinary ? string :
// eslint-disable-next-line @typescript-eslint/no-explicit-any
O extends SSMGetOptionsTransformJson ? Record<string, any> :
never;
O extends SSMGetOptionsTransformJson ? Record<string, unknown> :
never
: T;

/**
* Options for the SSMProvider getMultiple method.
@@ -126,18 +130,32 @@ interface SSMGetMultipleOptionsTransformBinary extends SSMGetMultipleOptions {
transform: 'binary'
}

interface SSMGetMultipleOptionsTransformAuto extends SSMGetMultipleOptions {
transform: 'auto'
}

interface SSMGetMultipleOptionsTransformNone extends SSMGetMultipleOptions {
transform?: never
}

type SSMGetMultipleOptionsUnion = SSMGetMultipleOptionsTransformJson | SSMGetMultipleOptionsTransformBinary | SSMGetMultipleOptionsTransformNone | undefined;
type SSMGetMultipleOptionsUnion =
SSMGetMultipleOptionsTransformJson |
SSMGetMultipleOptionsTransformBinary |
SSMGetMultipleOptionsTransformAuto |
SSMGetMultipleOptionsTransformNone |
undefined;

type SSMGetMultipleOutput<O = undefined> =
/**
* Generic output type for the SSMProvider getMultiple method.
*/
type SSMGetMultipleOutput<T = undefined, O = undefined> =
undefined extends T ?
undefined extends O ? Record<string, string> :
O extends SSMGetMultipleOptionsTransformNone | SSMGetMultipleOptionsTransformBinary ? Record<string, string> :
// eslint-disable-next-line @typescript-eslint/no-explicit-any
O extends SSMGetMultipleOptionsTransformJson ? Record<string, Record<string, any>> :
never;
O extends SSMGetMultipleOptionsTransformAuto ? Record<string, unknown> :
O extends SSMGetMultipleOptionsTransformJson ? Record<string, Record<string, unknown>> :
never
: Record<string, T>;

/**
* Options for the SSMProvider getParametersByName method.
@@ -152,7 +170,7 @@ interface SSMGetParametersByNameOptionsInterface {
maxAge?: number
throwOnError?: boolean
decrypt?: boolean
transform?: TransformOptions
transform?: Exclude<TransformOptions, 'auto'>
}

/**
@@ -179,10 +197,14 @@ type SSMGetParametersByNameFromCacheOutputType = {
toFetch: Record<string, SSMGetParametersByNameOptionsInterface>
};

type SSMGetParametersByNameOutput<T = undefined> =
undefined extends T ?
Record<string, unknown> & { _errors?: string[] } :
Record<string, T> & { _errors?: string[] };

export type {
SSMProviderOptions,
SSMGetOptions,
SSMGetOptionsUnion,
SSMGetOutput,
SSMGetMultipleOptions,
SSMGetMultipleOptionsUnion,
@@ -191,4 +213,5 @@ export type {
SSMSplitBatchAndDecryptParametersOutputType,
SSMGetParametersByNameOutputInterface,
SSMGetParametersByNameFromCacheOutputType,
SSMGetParametersByNameOutput,
};