Skip to content

Commit 7271890

Browse files
committed
merge conflicts
2 parents bcbbdeb + 63eeab4 commit 7271890

File tree

6 files changed

+72
-33
lines changed

6 files changed

+72
-33
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/parameters/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@aws-lambda-powertools/parameters",
3-
"version": "1.4.1",
3+
"version": "1.5.0",
44
"description": "The parameters package for the AWS Lambda Powertools for TypeScript library",
55
"author": {
66
"name": "Amazon Web Services",

packages/parameters/src/BaseProvider.ts

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import { GetParameterError, TransformParameterError } from './Exceptions';
77
import type { BaseProviderInterface, GetMultipleOptionsInterface, GetOptionsInterface, TransformOptions } from './types';
88
import type { SSMGetOptionsInterface, SSMGetMultipleOptionsInterface } from './types/SSMProvider';
99

10-
// These providers will be dynamically initialized on first use of the helper functions
11-
const DEFAULT_PROVIDERS: { [key: string]: BaseProvider } = {};
10+
// These providers are dinamycally intialized on first use of the helper functions
11+
const DEFAULT_PROVIDERS: Record<string, BaseProvider> = {};
1212

1313
abstract class BaseProvider implements BaseProviderInterface {
1414
protected store: Map<string, ExpirableValue>;
1515

16-
public constructor () {
16+
public constructor() {
1717
this.store = new Map();
1818
}
1919

20-
public addToCache(key: string, value: string | Record<string, unknown>, maxAge: number): void {
20+
public addToCache(key: string, value: string | Uint8Array | Record<string, unknown>, maxAge: number): void {
2121
if (maxAge <= 0) return;
2222

2323
this.store.set(key, new ExpirableValue(value, maxAge));
@@ -26,7 +26,7 @@ abstract class BaseProvider implements BaseProviderInterface {
2626
public clearCache(): void {
2727
this.store.clear();
2828
}
29-
29+
3030
/**
3131
* Retrieve a parameter value or return the cached value
3232
*
@@ -42,7 +42,7 @@ abstract class BaseProvider implements BaseProviderInterface {
4242
* @param {GetOptionsInterface|SSMGetOptionsInterface} options - Options to configure maximum age, trasformation, AWS SDK options, or force fetch
4343
*/
4444
public async get(name: string, options?: SSMGetOptionsInterface): Promise<undefined | string | Record<string, unknown>>;
45-
public async get(name: string, options?: GetOptionsInterface): Promise<undefined | string | Record<string, unknown>> {
45+
public async get(name: string, options?: GetOptionsInterface): Promise<undefined | string | Uint8Array | Record<string, unknown>> {
4646
const configs = new GetOptions(options);
4747
const key = [ name, configs.transform ].toString();
4848

@@ -82,7 +82,7 @@ abstract class BaseProvider implements BaseProviderInterface {
8282
return this.store.get(key)!.value as Record<string, unknown>;
8383
}
8484

85-
let values: Record<string, unknown> = {};
85+
let values = {};
8686
try {
8787
values = await this._getMultiple(path, options);
8888
} catch (error) {
@@ -119,44 +119,51 @@ abstract class BaseProvider implements BaseProviderInterface {
119119
* Retrieve parameter value from the underlying parameter store
120120
*
121121
* @param {string} name - Parameter name
122-
* @param {unknown} sdkOptions - Options to pass to the underlying AWS SDK
122+
* @param {unknown} options - Options to pass to the underlying implemented method
123123
*/
124-
protected abstract _get(name: string, sdkOptions?: unknown): Promise<string | undefined>;
124+
protected abstract _get(name: string, options?: unknown): Promise<string | Uint8Array | undefined>;
125125

126-
protected abstract _getMultiple(path: string, sdkOptions?: unknown): Promise<Record<string, string|undefined>>;
126+
/**
127+
* Retrieve multiple parameter values from the underlying parameter store
128+
*
129+
* @param {string} path - Parameter name
130+
* @param {unknown} options - Options to pass to the underlying implementated method
131+
*/
132+
protected abstract _getMultiple(path: string, options?: unknown): Promise<Record<string, string | undefined>>;
127133

128134
}
129135

130-
// TODO: revisit `value` type once we are clearer on the types returned by the various SDKs
131-
const transformValue = (value: unknown, transform: TransformOptions, throwOnTransformError: boolean, key: string = ''): string | Record<string, unknown> | undefined => {
136+
const transformValue = (value: string | Uint8Array | undefined, transform: TransformOptions, throwOnTransformError: boolean, key: string = ''): string | Record<string, unknown> | undefined => {
132137
try {
133138
const normalizedTransform = transform.toLowerCase();
134139
if (
135140
(normalizedTransform === TRANSFORM_METHOD_JSON ||
136-
(normalizedTransform === 'auto' && key.toLowerCase().endsWith(`.${TRANSFORM_METHOD_JSON}`))) &&
141+
(normalizedTransform === 'auto' && key.toLowerCase().endsWith(`.${TRANSFORM_METHOD_JSON}`))) &&
137142
typeof value === 'string'
138143
) {
139144
return JSON.parse(value) as Record<string, unknown>;
140145
} else if (
141146
(normalizedTransform === TRANSFORM_METHOD_BINARY ||
142-
(normalizedTransform === 'auto' && key.toLowerCase().endsWith(`.${TRANSFORM_METHOD_BINARY}`))) &&
143-
typeof value === 'string'
147+
(normalizedTransform === 'auto' && key.toLowerCase().endsWith(`.${TRANSFORM_METHOD_BINARY}`)))
144148
) {
145-
return new TextDecoder('utf-8').decode(fromBase64(value));
149+
if (typeof value === 'string') {
150+
return new TextDecoder('utf-8').decode(fromBase64(value));
151+
} else {
152+
return new TextDecoder('utf-8').decode(value);
153+
}
146154
} else {
147-
// TODO: revisit this type once we are clearer on types returned by SDKs
148155
return value as string;
149156
}
150157
} catch (error) {
151158
if (throwOnTransformError)
152159
throw new TransformParameterError(transform, (error as Error).message);
153-
160+
154161
return;
155162
}
156163
};
157164

158-
const transformValues = (value: Record<string, unknown>, transform: TransformOptions, throwOnTransformError: boolean): Record<string, unknown> => {
159-
const transformedValues: Record<string, unknown> = {};
165+
const transformValues = (value: Record<string, string | undefined>, transform: TransformOptions, throwOnTransformError: boolean): Record<string, string | Record<string, unknown> | undefined> => {
166+
const transformedValues: Record<string, string | Record<string, unknown> | undefined> = {};
160167
for (const [ entryKey, entryValue ] of Object.entries(value)) {
161168
try {
162169
transformedValues[entryKey] = transformValue(entryValue, transform, throwOnTransformError, entryKey);

packages/parameters/src/ExpirableValue.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import type { ExpirableValueInterface } from './types';
22

33
class ExpirableValue implements ExpirableValueInterface {
44
public ttl: number;
5-
public value: string | Record<string, unknown>;
5+
public value: string | Uint8Array | Record<string, unknown>;
66

7-
public constructor(value: string | Record<string, unknown>, maxAge: number) {
7+
public constructor(value: string | Uint8Array | Record<string, unknown>, maxAge: number) {
88
this.value = value;
99
const timeNow = new Date();
1010
this.ttl = timeNow.setSeconds(timeNow.getSeconds() + maxAge);
@@ -15,6 +15,6 @@ class ExpirableValue implements ExpirableValueInterface {
1515
}
1616
}
1717

18-
export {
18+
export {
1919
ExpirableValue
2020
};

packages/parameters/src/types/BaseProvider.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ interface GetMultipleOptionsInterface extends GetOptionsInterface {
1212
}
1313

1414
interface ExpirableValueInterface {
15-
value: string | Record<string, unknown>
15+
value: string | Uint8Array | Record<string, unknown>
1616
ttl: number
1717
}
1818

1919
interface BaseProviderInterface {
20-
get(name: string, options?: GetOptionsInterface): Promise<undefined | string | Record<string, unknown>>
20+
get(name: string, options?: GetOptionsInterface): Promise<undefined | string | Uint8Array | Record<string, unknown>>
2121
getMultiple(path: string, options?: GetMultipleOptionsInterface): Promise<void | Record<string, unknown>>
2222
}
2323

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

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ describe('Class: BaseProvider', () => {
7676
// Prepare
7777
const provider = new TestProvider();
7878

79-
// Act / Assess
79+
// Act & Assess
8080
await expect(provider.get('my-parameter')).rejects.toThrowError(GetParameterError);
8181

8282
});
@@ -154,7 +154,7 @@ describe('Class: BaseProvider', () => {
154154
const provider = new TestProvider();
155155
jest.spyOn(provider, '_get').mockImplementation(() => new Promise((resolve, _reject) => resolve(mockData)));
156156

157-
// Act / Assess
157+
// Act & Assess
158158
await expect(provider.get('my-parameter', { transform: 'json' })).rejects.toThrowError(TransformParameterError);
159159

160160
});
@@ -181,10 +181,42 @@ describe('Class: BaseProvider', () => {
181181
const provider = new TestProvider();
182182
jest.spyOn(provider, '_get').mockImplementation(() => new Promise((resolve, _reject) => resolve(mockData)));
183183

184-
// Act / Assess
184+
// Act & Assess
185185
await expect(provider.get('my-parameter', { transform: 'binary' })).rejects.toThrowError(TransformParameterError);
186186

187187
});
188+
189+
test('when called with no transform, and the value is a valid binary, it returns the binary as-is', async () => {
190+
191+
// Prepare
192+
const mockData = encoder.encode('my-value');
193+
const provider = new TestProvider();
194+
jest.spyOn(provider, '_get').mockImplementation(() => new Promise((resolve, _reject) => resolve(mockData as unknown as string)));
195+
196+
// Act
197+
const value = await provider.get('my-parameter');
198+
199+
// Assess
200+
expect(value).toBeInstanceOf(Uint8Array);
201+
expect(value).toEqual(mockData);
202+
203+
});
204+
205+
test('when called with a binary transform, and the value is a valid binary, it returns the decoded value', async () => {
206+
207+
// Prepare
208+
const mockData = encoder.encode('my-value');
209+
const provider = new TestProvider();
210+
jest.spyOn(provider, '_get').mockImplementation(() => new Promise((resolve, _reject) => resolve(mockData as unknown as string)));
211+
212+
// Act
213+
const value = await provider.get('my-parameter', { transform: 'binary' });
214+
215+
// Assess
216+
expect(typeof value).toBe('string');
217+
expect(value).toEqual('my-value');
218+
219+
});
188220

189221
});
190222

@@ -195,7 +227,7 @@ describe('Class: BaseProvider', () => {
195227
const provider = new TestProvider();
196228
jest.spyOn(provider, '_getMultiple').mockImplementation(() => new Promise((_resolve, reject) => reject(new Error('Some error.'))));
197229

198-
// Act / Assess
230+
// Act & Assess
199231
await expect(provider.getMultiple('my-parameter')).rejects.toThrowError(GetParameterError);
200232

201233
});
@@ -267,7 +299,7 @@ describe('Class: BaseProvider', () => {
267299
const provider = new TestProvider();
268300
jest.spyOn(provider, '_getMultiple').mockImplementation(() => new Promise((resolve, _reject) => resolve(mockData)));
269301

270-
// Act / Assess
302+
// Act & Assess
271303
await expect(provider.getMultiple('my-path', { transform: 'json', throwOnTransformError: true })).rejects.toThrowError(TransformParameterError);
272304

273305
});
@@ -316,7 +348,7 @@ describe('Class: BaseProvider', () => {
316348
const provider = new TestProvider();
317349
jest.spyOn(provider, '_getMultiple').mockImplementation(() => new Promise((resolve, _reject) => resolve(mockData)));
318350

319-
// Act / Assess
351+
// Act & Assess
320352
await expect(provider.getMultiple('my-path', { transform: 'binary', throwOnTransformError: true })).rejects.toThrowError(TransformParameterError);
321353

322354
});

0 commit comments

Comments
 (0)