Skip to content

Commit c02149e

Browse files
committed
docs: wrote api docs for BaseProvider
1 parent 7ca0aaa commit c02149e

File tree

6 files changed

+135
-12
lines changed

6 files changed

+135
-12
lines changed

Diff for: packages/parameters/src/BaseProvider.ts

+59-12
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,54 @@ import type { BaseProviderInterface, GetMultipleOptionsInterface, GetOptionsInte
99
// These providers are dinamycally intialized on first use of the helper functions
1010
const DEFAULT_PROVIDERS: Record<string, BaseProvider> = {};
1111

12+
/**
13+
* Base class for all providers.
14+
*
15+
* As an abstract class, it should not be used directly, but rather extended by other providers.
16+
*
17+
* It implements the common logic for all providers, such as caching, transformation, etc.
18+
* Each provider that extends this class must implement the `_get` and `_getMultiple` abstract methods.
19+
*
20+
* These methods are responsible for retrieving the values from the underlying parameter store. They are
21+
* called by the `get` and `getMultiple` methods, which are responsible for caching and transformation.
22+
*
23+
* If there are multiple calls to the same parameter but in a different transform, they will be stored multiple times.
24+
* This allows us to optimize by transforming the data only once per retrieval, thus there is no need to transform cached values multiple times.
25+
*
26+
* However, this means that we need to make multiple calls to the underlying parameter store if we need to return it in different transforms.
27+
*
28+
* Since the number of supported transform is small and the probability that a given parameter will always be used in a specific transform,
29+
* this should be an acceptable tradeoff.
30+
*/
1231
abstract class BaseProvider implements BaseProviderInterface {
1332
protected store: Map<string, ExpirableValue>;
1433

1534
public constructor() {
1635
this.store = new Map();
1736
}
1837

38+
/**
39+
* Add a value to the cache.
40+
*
41+
* @param {string} key - Key of the cached value
42+
* @param {string | Uint8Array | Record<string, unknown>} value - Value to be cached
43+
* @param {number} maxAge - Maximum age in seconds for the value to be cached
44+
*/
1945
public addToCache(key: string, value: string | Uint8Array | Record<string, unknown>, maxAge: number): void {
2046
if (maxAge <= 0) return;
2147

2248
this.store.set(key, new ExpirableValue(value, maxAge));
2349
}
2450

51+
/**
52+
* Clear the cache.
53+
*/
2554
public clearCache(): void {
2655
this.store.clear();
2756
}
2857

2958
/**
30-
* Retrieve a parameter value or return the cached value
31-
*
32-
* If there are multiple calls to the same parameter but in a different transform, they will be stored multiple times.
33-
* This allows us to optimize by transforming the data only once per retrieval, thus there is no need to transform cached values multiple times.
34-
*
35-
* However, this means that we need to make multiple calls to the underlying parameter store if we need to return it in different transforms.
36-
*
37-
* Since the number of supported transform is small and the probability that a given parameter will always be used in a specific transform,
38-
* this should be an acceptable tradeoff.
59+
* Retrieve a parameter value or return the cached value.
3960
*
4061
* @param {string} name - Parameter name
4162
* @param {GetOptionsInterface} options - Options to configure maximum age, trasformation, AWS SDK options, or force fetch
@@ -68,6 +89,13 @@ abstract class BaseProvider implements BaseProviderInterface {
6889
return value;
6990
}
7091

92+
/**
93+
* Retrieve multiple parameter values or return the cached values.
94+
*
95+
* @param {string} path - Parameters path
96+
* @param {GetMultipleOptionsInterface} options - Options to configure maximum age, trasformation, AWS SDK options, or force fetch
97+
* @returns
98+
*/
7199
public async getMultiple(path: string, options?: GetMultipleOptionsInterface): Promise<undefined | Record<string, unknown>> {
72100
const configs = new GetMultipleOptions(options || {});
73101
const key = [ path, configs.transform ].toString();
@@ -97,7 +125,7 @@ abstract class BaseProvider implements BaseProviderInterface {
97125
}
98126

99127
/**
100-
* Check whether a key has expired in the cache or not
128+
* Check whether a key has expired in the cache or not.
101129
*
102130
* It returns true if the key is expired or not present in the cache.
103131
*
@@ -111,15 +139,15 @@ abstract class BaseProvider implements BaseProviderInterface {
111139
}
112140

113141
/**
114-
* Retrieve parameter value from the underlying parameter store
142+
* Retrieve parameter value from the underlying parameter store.
115143
*
116144
* @param {string} name - Parameter name
117145
* @param {unknown} options - Options to pass to the underlying implemented method
118146
*/
119147
protected abstract _get(name: string, options?: unknown): Promise<string | Uint8Array | undefined>;
120148

121149
/**
122-
* Retrieve multiple parameter values from the underlying parameter store
150+
* Retrieve multiple parameter values from the underlying parameter store.
123151
*
124152
* @param {string} path - Parameter name
125153
* @param {unknown} options - Options to pass to the underlying implementated method
@@ -128,6 +156,16 @@ abstract class BaseProvider implements BaseProviderInterface {
128156

129157
}
130158

159+
/**
160+
* Utility function to transform a value.
161+
*
162+
* It supports JSON and binary transformations, as well as an 'auto' mode that will try to transform the value based on the key.
163+
*
164+
* @param {string | Uint8Array | undefined} value - Value to be transformed
165+
* @param {TransformOptions} transform - Transform to be applied, can be 'json', 'binary', or 'auto'
166+
* @param {boolean} throwOnTransformError - Whether to throw an error if the transformation fails, when transforming multiple values this can be set to false
167+
* @param {string} key - Key of the value to be transformed, used to determine the transformation method when using 'auto'
168+
*/
131169
const transformValue = (value: string | Uint8Array | undefined, transform: TransformOptions, throwOnTransformError: boolean, key: string): string | Record<string, unknown> | undefined => {
132170
try {
133171
const normalizedTransform = transform.toLowerCase();
@@ -159,6 +197,15 @@ const transformValue = (value: string | Uint8Array | undefined, transform: Trans
159197
}
160198
};
161199

200+
/**
201+
* Utility function to transform multiple values.
202+
*
203+
* It iterates over the values and applies the transformation to each one by calling the `transformValue` function.
204+
*
205+
* @param {Record<string, string | undefined>} value - Values to be transformed
206+
* @param {TransformOptions} transform - Transform to be applied, can be 'json', 'binary', or 'auto'
207+
* @param {boolean} throwOnTransformError - Whether to throw an error if the transformation fails, when transforming multiple values this can be set to false
208+
*/
162209
const transformValues = (value: Record<string, string | undefined>, transform: TransformOptions, throwOnTransformError: boolean): Record<string, string | Record<string, unknown> | undefined> => {
163210
const transformedValues: Record<string, string | Record<string, unknown> | undefined> = {};
164211
for (const [ entryKey, entryValue ] of Object.entries(value)) {

Diff for: packages/parameters/src/Exceptions.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
class GetParameterError extends Error {}
22

3+
/**
4+
* Error thrown when a transform fails.
5+
*/
36
class TransformParameterError extends Error {
47
public constructor(transform: string, message: string) {
58
super(message);

Diff for: packages/parameters/src/ExpirableValue.ts

+16
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
import type { ExpirableValueInterface } from './types';
22

3+
/**
4+
* Class to represent a value that can expire.
5+
*
6+
* Upon creation, the value is assigned a TTL (time to live) that is calculated
7+
* by adding the current time with the maximum age.
8+
*/
39
class ExpirableValue implements ExpirableValueInterface {
410
public ttl: number;
511
public value: string | Uint8Array | Record<string, unknown>;
612

13+
/**
14+
*
15+
* @param value - Value to be cached
16+
* @param maxAge - Maximum age in seconds for the value to be cached
17+
*/
718
public constructor(value: string | Uint8Array | Record<string, unknown>, maxAge: number) {
819
this.value = value;
920
const timeNow = new Date();
1021
this.ttl = timeNow.setSeconds(timeNow.getSeconds() + maxAge);
1122
}
1223

24+
/**
25+
* Check if the value has expired.
26+
*
27+
* @returns {boolean} - True if the value has expired, false otherwise
28+
*/
1329
public isExpired(): boolean {
1430
return this.ttl < Date.now();
1531
}

Diff for: packages/parameters/src/GetMultipleOptions.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { DEFAULT_MAX_AGE_SECS } from './constants';
22
import type { GetMultipleOptionsInterface, TransformOptions } from './types';
33

4+
/**
5+
* Options for the `getMultiple` method.
6+
*
7+
* It merges the default options with the provided options.
8+
*/
49
class GetMultipleOptions implements GetMultipleOptionsInterface {
510
public forceFetch: boolean = false;
611
public maxAge: number = DEFAULT_MAX_AGE_SECS;

Diff for: packages/parameters/src/GetOptions.ts

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import { DEFAULT_MAX_AGE_SECS } from './constants';
22
import type { GetOptionsInterface, TransformOptions } from './types';
33

4+
/**
5+
* Options for the `get` method.
6+
*
7+
* It merges the default options with the provided options.
8+
*/
49
class GetOptions implements GetOptionsInterface {
510
public forceFetch: boolean = false;
611
public maxAge: number = DEFAULT_MAX_AGE_SECS;

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

+47
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,68 @@
1+
/**
2+
* Type for the transform option.
3+
*/
14
type TransformOptions = 'auto' | 'binary' | 'json';
25

6+
/**
7+
* Options for the `get` method.
8+
*
9+
* @property {number} maxAge - Maximum age of the value in the cache, in seconds.
10+
* @property {boolean} forceFetch - Force fetch the value from the parameter store, ignoring the cache.
11+
* @property {unknown} sdkOptions - Options to pass to the underlying SDK.
12+
* @property {TransformOptions} transform - Transform to be applied, can be 'json', 'binary', or 'auto'.
13+
*/
314
interface GetOptionsInterface {
15+
/**
16+
* Maximum age of the value in the cache, in seconds.
17+
*/
418
maxAge?: number
19+
/**
20+
* Force fetch the value from the parameter store, ignoring the cache.
21+
*/
522
forceFetch?: boolean
23+
/**
24+
* Options to pass to the underlying SDK.
25+
*/
626
sdkOptions?: unknown
27+
/**
28+
* Transform to be applied, can be 'json', 'binary', or 'auto'.
29+
*/
730
transform?: TransformOptions
831
}
932

33+
/**
34+
* Options for the `getMultiple` method.
35+
*
36+
* @property {number} maxAge - Maximum age of the value in the cache, in seconds.
37+
* @property {boolean} forceFetch - Force fetch the value from the parameter store, ignoring the cache.
38+
* @property {unknown} sdkOptions - Options to pass to the underlying SDK.
39+
* @property {TransformOptions} transform - Transform to be applied, can be 'json', 'binary', or 'auto'.
40+
* @property {boolean} throwOnTransformError - Whether to throw an error if a value cannot be transformed.
41+
*/
1042
interface GetMultipleOptionsInterface extends GetOptionsInterface {
43+
/**
44+
* Whether to throw an error if a value cannot be transformed.
45+
*/
1146
throwOnTransformError?: boolean
1247
}
1348

49+
/**
50+
* Interface for a value that can expire.
51+
*/
1452
interface ExpirableValueInterface {
53+
/**
54+
* Value of the parameter.
55+
*/
1556
value: string | Uint8Array | Record<string, unknown>
57+
/**
58+
* Expiration timestamp of the value.
59+
*/
1660
ttl: number
1761
}
1862

63+
/**
64+
* Interface for a parameter store provider.
65+
*/
1966
interface BaseProviderInterface {
2067
get(name: string, options?: GetOptionsInterface): Promise<undefined | string | Uint8Array | Record<string, unknown>>
2168
getMultiple(path: string, options?: GetMultipleOptionsInterface): Promise<void | Record<string, unknown>>

0 commit comments

Comments
 (0)