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
chore: added type annotation to tests to use as canary
dreamorosi committed Apr 5, 2023
commit 1b0cd0bac9a49e6d81b0574b2f81fbe9f9e65fc4
58 changes: 55 additions & 3 deletions packages/parameters/tests/unit/getParameter.test.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,10 @@ import { SSMClient, GetParameterCommand } from '@aws-sdk/client-ssm';
import { mockClient } from 'aws-sdk-client-mock';
import 'aws-sdk-client-mock-jest';

/**
* Note that the following tests include type annotations on the results of each call. This is to ensure that the
* generic types defined in the utility are working as expected. If they are not, the tests will fail to compile.
*/
describe('Function: getParameter', () => {

beforeEach(() => {
@@ -27,7 +31,7 @@ describe('Function: getParameter', () => {
});

// Act
const value = await getParameter(parameterName);
const value: string | undefined = await getParameter(parameterName);

// Assess
expect(client).toReceiveCommandWith(GetParameterCommand, {
@@ -36,7 +40,7 @@ describe('Function: getParameter', () => {
expect(value).toBe(parameterValue);

});

test('when called and a default provider exists, it uses it and returns the value', async () => {

// Prepare
@@ -51,7 +55,7 @@ describe('Function: getParameter', () => {
});

// Act
const value = await getParameter(parameterName);
const value: string | undefined = await getParameter(parameterName);

// Assess
expect(client).toReceiveCommandWith(GetParameterCommand, {
@@ -62,4 +66,52 @@ describe('Function: getParameter', () => {

});

test('when called and transform `JSON` is specified, it returns an object with correct type', async () => {

// Prepare
const provider = new SSMProvider();
DEFAULT_PROVIDERS.ssm = provider;
const parameterName = 'foo';
const parameterValue = JSON.stringify({ hello: 'world' });
const client = mockClient(SSMClient).on(GetParameterCommand).resolves({
Parameter: {
Value: parameterValue,
},
});

// Act
const value: Record<string, unknown> | undefined = await getParameter(parameterName, { transform: 'json' });

// Assess
expect(client).toReceiveCommandWith(GetParameterCommand, {
Name: parameterName,
});
expect(value).toStrictEqual(JSON.parse(parameterValue));

});

test('when called and transform `JSON` is specified as well as an explicit `K` type, it returns a result with correct type', async () => {

// Prepare
const provider = new SSMProvider();
DEFAULT_PROVIDERS.ssm = provider;
const parameterName = 'foo';
const parameterValue = JSON.stringify(5);
const client = mockClient(SSMClient).on(GetParameterCommand).resolves({
Parameter: {
Value: parameterValue,
},
});

// Act
const value: number | undefined = await getParameter<number>(parameterName, { transform: 'json' });

// Assess
expect(client).toReceiveCommandWith(GetParameterCommand, {
Name: parameterName,
});
expect(value).toBe(JSON.parse(parameterValue));

});

});
66 changes: 63 additions & 3 deletions packages/parameters/tests/unit/getParameters.test.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,10 @@ import { SSMClient, GetParametersByPathCommand } from '@aws-sdk/client-ssm';
import { mockClient } from 'aws-sdk-client-mock';
import 'aws-sdk-client-mock-jest';

/**
* Note that the following tests include type annotations on the results of each call. This is to ensure that the
* generic types defined in the utility are working as expected. If they are not, the tests will fail to compile.
*/
describe('Function: getParameters', () => {

beforeEach(() => {
@@ -28,7 +32,7 @@ describe('Function: getParameters', () => {
});

// Act
const parameters = await getParameters(parameterPath);
const parameters: Record<string, string> | undefined = await getParameters(parameterPath);

// Assess
expect(client).toReceiveCommandWith(GetParametersByPathCommand, {
@@ -39,7 +43,7 @@ describe('Function: getParameters', () => {
});

});

test('when called and a default provider exists, it uses it and returns the value', async () => {

// Prepare
@@ -55,7 +59,7 @@ describe('Function: getParameters', () => {
});

// Act
const parameters = await getParameters(parameterPath);
const parameters: Record<string, string> | undefined = await getParameters(parameterPath);

// Assess
expect(client).toReceiveCommandWith(GetParametersByPathCommand, {
@@ -68,4 +72,60 @@ describe('Function: getParameters', () => {

});

test('when called and transform `JSON` is specified, it returns an object with correct type', async () => {

// Prepare
const provider = new SSMProvider();
DEFAULT_PROVIDERS.ssm = provider;
const parameterPath = '/foo';
const parameterValue = JSON.stringify({ hello: 'world' });
const client = mockClient(SSMClient).on(GetParametersByPathCommand).resolves({
Parameters: [{
Name: '/foo/bar',
Value: parameterValue,
}],
});

// Act
const parameters: Record<string, Record<string, unknown>> | undefined = await getParameters(parameterPath);

// Assess
expect(client).toReceiveCommandWith(GetParametersByPathCommand, {
Path: parameterPath,
});
expect(parameters).toStrictEqual({
'bar': parameterValue,
});
expect(DEFAULT_PROVIDERS.ssm).toBe(provider);

});

test('when called and transform `JSON` is specified as well as an explicit `K` type, it returns a result with correct type', async () => {

// Prepare
const provider = new SSMProvider();
DEFAULT_PROVIDERS.ssm = provider;
const parameterPath = '/foo';
const parameterValue = JSON.stringify(5);
const client = mockClient(SSMClient).on(GetParametersByPathCommand).resolves({
Parameters: [{
Name: '/foo/bar',
Value: parameterValue,
}],
});

// Act
const parameters: Record<string, Record<string, number>> | undefined = await getParameters<Record<string, number>>(parameterPath);

// Assess
expect(client).toReceiveCommandWith(GetParametersByPathCommand, {
Path: parameterPath,
});
expect(parameters).toStrictEqual({
'bar': parameterValue,
});
expect(DEFAULT_PROVIDERS.ssm).toBe(provider);

});

});