1- import { fromBase64 } from '@aws-sdk/util-base64-node ' ;
1+ import { fromBase64 } from '@aws-sdk/util-base64' ;
22import { GetOptions } from './GetOptions' ;
33import { GetMultipleOptions } from './GetMultipleOptions' ;
44import { ExpirableValue } from './ExpirableValue' ;
55import { TRANSFORM_METHOD_BINARY , TRANSFORM_METHOD_JSON } from './constants' ;
66import { GetParameterError , TransformParameterError } from './Exceptions' ;
77import type { BaseProviderInterface , GetMultipleOptionsInterface , GetOptionsInterface , TransformOptions } from './types' ;
88
9+ // These providers are dinamycally intialized on first use of the helper functions
10+ const DEFAULT_PROVIDERS : Record < string , BaseProvider > = { } ;
11+
912abstract class BaseProvider implements BaseProviderInterface {
1013 protected store : Map < string , ExpirableValue > ;
1114
12- public constructor ( ) {
15+ public constructor ( ) {
1316 this . store = new Map ( ) ;
1417 }
1518
16- public addToCache ( key : string , value : string | Record < string , unknown > , maxAge : number ) : void {
19+ public addToCache ( key : string , value : string | Uint8Array | Record < string , unknown > , maxAge : number ) : void {
1720 if ( maxAge <= 0 ) return ;
1821
1922 this . store . set ( key , new ExpirableValue ( value , maxAge ) ) ;
@@ -22,7 +25,7 @@ abstract class BaseProvider implements BaseProviderInterface {
2225 public clearCache ( ) : void {
2326 this . store . clear ( ) ;
2427 }
25-
28+
2629 /**
2730 * Retrieve a parameter value or return the cached value
2831 *
@@ -37,7 +40,7 @@ abstract class BaseProvider implements BaseProviderInterface {
3740 * @param {string } name - Parameter name
3841 * @param {GetOptionsInterface } options - Options to configure maximum age, trasformation, AWS SDK options, or force fetch
3942 */
40- public async get ( name : string , options ?: GetOptionsInterface ) : Promise < undefined | string | Record < string , unknown > > {
43+ public async get ( name : string , options ?: GetOptionsInterface ) : Promise < undefined | string | Uint8Array | Record < string , unknown > > {
4144 const configs = new GetOptions ( options ) ;
4245 const key = [ name , configs . transform ] . toString ( ) ;
4346
@@ -49,7 +52,7 @@ abstract class BaseProvider implements BaseProviderInterface {
4952
5053 let value ;
5154 try {
52- value = await this . _get ( name , options ?. sdkOptions ) ;
55+ value = await this . _get ( name , options ) ;
5356 } catch ( error ) {
5457 throw new GetParameterError ( ( error as Error ) . message ) ;
5558 }
@@ -76,9 +79,9 @@ abstract class BaseProvider implements BaseProviderInterface {
7679 return this . store . get ( key ) ! . value as Record < string , unknown > ;
7780 }
7881
79- let values : Record < string , unknown > = { } ;
82+ let values = { } ;
8083 try {
81- values = await this . _getMultiple ( path , options ?. sdkOptions ) ;
84+ values = await this . _getMultiple ( path , options ) ;
8285 } catch ( error ) {
8386 throw new GetParameterError ( ( error as Error ) . message ) ;
8487 }
@@ -99,11 +102,17 @@ abstract class BaseProvider implements BaseProviderInterface {
99102 * Retrieve parameter value from the underlying parameter store
100103 *
101104 * @param {string } name - Parameter name
102- * @param {unknown } sdkOptions - Options to pass to the underlying AWS SDK
105+ * @param {unknown } options - Options to pass to the underlying implemented method
103106 */
104- protected abstract _get ( name : string , sdkOptions ?: unknown ) : Promise < string | undefined > ;
107+ protected abstract _get ( name : string , options ?: unknown ) : Promise < string | Uint8Array | undefined > ;
105108
106- protected abstract _getMultiple ( path : string , sdkOptions ?: unknown ) : Promise < Record < string , string | undefined > > ;
109+ /**
110+ * Retrieve multiple parameter values from the underlying parameter store
111+ *
112+ * @param {string } path - Parameter name
113+ * @param {unknown } options - Options to pass to the underlying implementated method
114+ */
115+ protected abstract _getMultiple ( path : string , options ?: unknown ) : Promise < Record < string , string | undefined > > ;
107116
108117 /**
109118 * Check whether a key has expired in the cache or not
@@ -115,42 +124,43 @@ abstract class BaseProvider implements BaseProviderInterface {
115124 private hasKeyExpiredInCache ( key : string ) : boolean {
116125 const value = this . store . get ( key ) ;
117126 if ( value ) return value . isExpired ( ) ;
118-
127+
119128 return true ;
120129 }
121130
122131}
123132
124- // TODO: revisit `value` type once we are clearer on the types returned by the various SDKs
125- const transformValue = ( value : unknown , transform : TransformOptions , throwOnTransformError : boolean , key : string = '' ) : string | Record < string , unknown > | undefined => {
133+ const transformValue = ( value : string | Uint8Array | undefined , transform : TransformOptions , throwOnTransformError : boolean , key : string = '' ) : string | Record < string , unknown > | undefined => {
126134 try {
127135 const normalizedTransform = transform . toLowerCase ( ) ;
128136 if (
129137 ( normalizedTransform === TRANSFORM_METHOD_JSON ||
130- ( normalizedTransform === 'auto' && key . toLowerCase ( ) . endsWith ( `.${ TRANSFORM_METHOD_JSON } ` ) ) ) &&
138+ ( normalizedTransform === 'auto' && key . toLowerCase ( ) . endsWith ( `.${ TRANSFORM_METHOD_JSON } ` ) ) ) &&
131139 typeof value === 'string'
132140 ) {
133141 return JSON . parse ( value ) as Record < string , unknown > ;
134142 } else if (
135143 ( normalizedTransform === TRANSFORM_METHOD_BINARY ||
136- ( normalizedTransform === 'auto' && key . toLowerCase ( ) . endsWith ( `.${ TRANSFORM_METHOD_BINARY } ` ) ) ) &&
137- typeof value === 'string'
144+ ( normalizedTransform === 'auto' && key . toLowerCase ( ) . endsWith ( `.${ TRANSFORM_METHOD_BINARY } ` ) ) )
138145 ) {
139- return new TextDecoder ( 'utf-8' ) . decode ( fromBase64 ( value ) ) ;
146+ if ( typeof value === 'string' ) {
147+ return new TextDecoder ( 'utf-8' ) . decode ( fromBase64 ( value ) ) ;
148+ } else {
149+ return new TextDecoder ( 'utf-8' ) . decode ( value ) ;
150+ }
140151 } else {
141- // TODO: revisit this type once we are clearer on types returned by SDKs
142152 return value as string ;
143153 }
144154 } catch ( error ) {
145155 if ( throwOnTransformError )
146156 throw new TransformParameterError ( transform , ( error as Error ) . message ) ;
147-
157+
148158 return ;
149159 }
150160} ;
151161
152- const transformValues = ( value : Record < string , unknown > , transform : TransformOptions , throwOnTransformError : boolean ) : Record < string , unknown > => {
153- const transformedValues : Record < string , unknown > = { } ;
162+ const transformValues = ( value : Record < string , string | undefined > , transform : TransformOptions , throwOnTransformError : boolean ) : Record < string , string | Record < string , unknown > | undefined > => {
163+ const transformedValues : Record < string , string | Record < string , unknown > | undefined > = { } ;
154164 for ( const [ entryKey , entryValue ] of Object . entries ( value ) ) {
155165 try {
156166 transformedValues [ entryKey ] = transformValue ( entryValue , transform , throwOnTransformError , entryKey ) ;
@@ -167,4 +177,5 @@ export {
167177 BaseProvider ,
168178 ExpirableValue ,
169179 transformValue ,
180+ DEFAULT_PROVIDERS ,
170181} ;
0 commit comments