Skip to content

Commit 146720e

Browse files
committed
docs: wrote api docs for AppConfigProvider
1 parent b078161 commit 146720e

File tree

4 files changed

+309
-21
lines changed

4 files changed

+309
-21
lines changed

Diff for: packages/parameters/src/appconfig/AppConfigProvider.ts

+190-14
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,173 @@ import type {
1010
AppConfigGetOptionsInterface,
1111
} from '../types/AppConfigProvider';
1212

13+
/**
14+
* ## Intro
15+
* The Parameters utility provides an AppConfigProvider that allows to retrieve configuration profiles from AWS AppConfig.
16+
*
17+
* ## Getting started
18+
*
19+
* This utility supports AWS SDK v3 for JavaScript only. This allows the utility to be modular, and you to install only
20+
* the SDK packages you need and keep your bundle size small.
21+
*
22+
* To use the provider, you must install the Parameters utility and the AWS SDK v3 for JavaScript for AppConfig:
23+
*
24+
* ```sh
25+
* npm install @aws-lambda-powertools/parameters @aws-sdk/client-appconfigdata
26+
* ```
27+
*
28+
* ## Basic usage
29+
*
30+
* @example
31+
* ```typescript
32+
* import { AppConfigProvider } from '@aws-lambda-powertools/parameters/appconfig';
33+
*
34+
* const configProvider = new AppConfigProvider();
35+
*
36+
* export const handler = async (): Promise<void> => {
37+
* // Retrieve a configuration profile
38+
* const encodedConfig = await configProvider.get('my-config');
39+
* const config = new TextDecoder('utf-8').decode(encodedConfig);
40+
* };
41+
* ```
42+
* If you want to retrieve configs without customizing the provider, you can use the {@link getAppConfig} function instead.
43+
*
44+
* ## Advanced usage
45+
*
46+
* ### Caching
47+
*
48+
* By default, the provider will cache parameters retrieved in-memory for 5 seconds.
49+
* You can adjust how long values should be kept in cache by using the `maxAge` parameter.
50+
*
51+
* @example
52+
* ```typescript
53+
* import { AppConfigProvider } from '@aws-lambda-powertools/parameters/appconfig';
54+
*
55+
* const configProvider = new AppConfigProvider();
56+
*
57+
* export const handler = async (): Promise<void> => {
58+
* // Retrieve a configuration profile and cache it for 10 seconds
59+
* const encodedConfig = await configProvider.get('my-config');
60+
* const config = new TextDecoder('utf-8').decode(encodedConfig);
61+
* };
62+
* ```
63+
*
64+
* If instead you'd like to always ensure you fetch the latest parameter from the store regardless if already available in cache, use the `forceFetch` parameter.
65+
*
66+
* @example
67+
* ```typescript
68+
* import { AppConfigProvider } from '@aws-lambda-powertools/parameters/appconfig';
69+
*
70+
* const configProvider = new AppConfigProvider();
71+
*
72+
* export const handler = async (): Promise<void> => {
73+
* // Retrieve a config and always fetch the latest value
74+
* const config = await configProvider.get('my-config', { forceFetch: true });
75+
* const config = new TextDecoder('utf-8').decode(encodedConfig);
76+
* };
77+
* ```
78+
*
79+
* ### Transformations
80+
*
81+
* For configurations stored as freeform JSON, Freature Flag, you can use the transform argument for deserialization. This will return a JavaScript object instead of a string.
82+
*
83+
* @example
84+
* ```typescript
85+
* import { AppConfigProvider } from '@aws-lambda-powertools/parameters/appconfig';
86+
*
87+
* const configProvider = new AppConfigProvider();
88+
*
89+
* export const handler = async (): Promise<void> => {
90+
* // Retrieve a JSON config or Feature Flag and parse it as JSON
91+
* const config = await configProvider.get('my-config', { transform: 'json' });
92+
* };
93+
* ```
94+
*
95+
* For configurations that are instead stored as base64-encoded binary data, you can use the transform argument set to `binary` for decoding. This will return a decoded string.
96+
*
97+
* @example
98+
* ```typescript
99+
* import { AppConfigProvider } from '@aws-lambda-powertools/parameters/appconfig';
100+
*
101+
* const configProvider = new AppConfigProvider();
102+
*
103+
* export const handler = async (): Promise<void> => {
104+
* // Retrieve a base64-encoded string and decode it
105+
* const config = await configProvider.get('my-config', { transform: 'binary' });
106+
* };
107+
* ```
108+
*
109+
* ### Extra SDK options
110+
*
111+
* When retrieving a configuration profile, you can pass extra options to the AWS SDK v3 for JavaScript client by using the `sdkOptions` parameter.
112+
*
113+
* @example
114+
* ```typescript
115+
* import { AppConfigProvider } from '@aws-lambda-powertools/parameters/appconfig';
116+
*
117+
* const configProvider = new AppConfigProvider();
118+
*
119+
* export const handler = async (): Promise<void> => {
120+
* // Retrieve a config and pass extra options to the AWS SDK v3 for JavaScript client
121+
* const config = await configProvider.get('my-config', {
122+
* sdkOptions: {
123+
* RequiredMinimumPollIntervalInSeconds: 60,
124+
* },
125+
* });
126+
* const config = new TextDecoder('utf-8').decode(encodedConfig);
127+
* };
128+
* ```
129+
*
130+
* This object accepts the same options as the [AWS SDK v3 for JavaScript AppConfigData client](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-appconfigdata/interfaces/startconfigurationsessioncommandinput.html).
131+
*
132+
* ### Customize AWS SDK v3 for JavaScript client
133+
*
134+
* By default, the provider will create a new AppConfigData client using the default configuration.
135+
*
136+
* You can customize the client by passing a custom configuration object to the provider.
137+
*
138+
* @example
139+
* ```typescript
140+
* import { AppConfigProvider } from '@aws-lambda-powertools/parameters/appconfig';
141+
*
142+
* const configProvider = new AppConfigProvider({
143+
* clientConfig: { region: 'eu-west-1' },
144+
* });
145+
* ```
146+
*
147+
* This object accepts the same options as the [AWS SDK v3 for JavaScript AppConfig Data client](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-appconfigdata/interfaces/appconfigdataclientconfig.html).
148+
*
149+
* Otherwise, if you want to use a custom client altogether, you can pass it to the provider.
150+
*
151+
* @example
152+
* ```typescript
153+
* import { AppConfigProvider } from '@aws-lambda-powertools/parameters/appconfig';
154+
* import { AppConfigDataClient } from '@aws-sdk/client-appconfigdata';
155+
*
156+
* const client = new AppConfigDataClient({ region: 'eu-west-1' });
157+
* const configProvider = new AppConfigProvider({
158+
* awsSdkV3Client: client,
159+
* });
160+
* ```
161+
*
162+
* This object must be an instance of the [AWS SDK v3 for JavaScript AppConfig Data client](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-appconfigdata/classes/appconfigdataclient.html).
163+
*
164+
* For more usage examples, see [our documentation](https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/).
165+
*
166+
* @param {string} name - The name of the configuration profile or its ID
167+
* @param {GetAppConfigCombinedInterface} options - Options to configure the provider
168+
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/
169+
*/
13170
class AppConfigProvider extends BaseProvider {
14171
public client: AppConfigDataClient;
15172
protected configurationTokenStore: Map<string, string> = new Map();
16173
private application?: string;
17174
private environment: string;
18175

19176
/**
20-
* It initializes the AppConfigProvider class'.
177+
* It initializes the AppConfigProvider class.
21178
* *
22-
* @param {AppConfigProviderOptions} options
179+
* @param {AppConfigProviderOptions} options - The configuration object.
23180
*/
24181
public constructor(options: AppConfigProviderOptions) {
25182
super();
@@ -44,7 +201,32 @@ class AppConfigProvider extends BaseProvider {
44201
}
45202

46203
/**
47-
* Retrieve a configuration from AWS App config.
204+
* Retrieve a secret from AWS AppConfig.
205+
*
206+
* @example
207+
* ```typescript
208+
* import { AppConfigProvider } from '@aws-lambda-powertools/parameters/appconfig';
209+
*
210+
* const configProvider = new AppConfigProvider();
211+
*
212+
* export const handler = async (): Promise<void> => {
213+
* // Retrieve a configuration profile
214+
* const encodedConfig = await configProvider.get('my-config');
215+
* const config = new TextDecoder('utf-8').decode(encodedConfig);
216+
* };
217+
* ```
218+
*
219+
* You can customize the retrieval of the secret by passing options to the function:
220+
* * `maxAge` - The maximum age of the value in cache before fetching a new one (in seconds) (default: 5)
221+
* * `forceFetch` - Whether to always fetch a new value from the store regardless if already available in cache
222+
* * `transform` - Whether to transform the value before returning it. Supported values: `json`, `binary`
223+
* * `sdkOptions` - Extra options to pass to the AWS SDK v3 for JavaScript client
224+
*
225+
* For usage examples check {@link AppConfigProvider}.
226+
*
227+
* @param {string} name - The name of the configuration profile or its ID
228+
* @param {GetAppConfigCombinedInterface} options - Options to configure the provider
229+
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/
48230
*/
49231
public async get(
50232
name: string,
@@ -54,7 +236,7 @@ class AppConfigProvider extends BaseProvider {
54236
}
55237

56238
/**
57-
* Retrieving multiple configurations is not supported by AWS App Config Provider.
239+
* Retrieving multiple configurations is not supported by AWS AppConfig.
58240
*/
59241
public async getMultiple(
60242
path: string,
@@ -64,11 +246,10 @@ class AppConfigProvider extends BaseProvider {
64246
}
65247

66248
/**
67-
* Retrieve a configuration from AWS App config.
249+
* Retrieve a configuration from AWS AppConfig.
68250
*
69-
* @param {string} name - Name of the configuration
251+
* @param {string} name - Name of the configuration or its ID
70252
* @param {AppConfigGetOptionsInterface} options - SDK options to propagate to `StartConfigurationSession` API call
71-
* @returns {Promise<Uint8Array | undefined>}
72253
*/
73254
protected async _get(
74255
name: string,
@@ -80,7 +261,6 @@ class AppConfigProvider extends BaseProvider {
80261
* First we start the session and after that we retrieve the configuration
81262
* We need to store { name: token } pairs to use in the next execution
82263
**/
83-
84264
if (!this.configurationTokenStore.has(name)) {
85265

86266
const sessionOptions: StartConfigurationSessionCommandInput = {
@@ -117,19 +297,15 @@ class AppConfigProvider extends BaseProvider {
117297
}
118298

119299
/**
120-
* Retrieving multiple configurations is not supported by AWS App Config Provider API.
300+
* Retrieving multiple configurations is not supported by AWS AppConfig.
121301
*
122302
* @throws Not Implemented Error.
123303
*/
124304
protected async _getMultiple(
125305
_path: string,
126306
_sdkOptions?: unknown
127307
): Promise<Record<string, string | undefined>> {
128-
return this._notImplementedError();
129-
}
130-
131-
private _notImplementedError(): never {
132-
throw new Error('Not Implemented');
308+
throw new Error('Method not implemented.');
133309
}
134310
}
135311

Diff for: packages/parameters/src/appconfig/getAppConfig.ts

+113-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,120 @@ import { AppConfigProvider, DEFAULT_PROVIDERS } from './AppConfigProvider';
22
import type { GetAppConfigCombinedInterface } from '../types/AppConfigProvider';
33

44
/**
5-
* Gets the AppConfig data for the specified name.
5+
* ## Intro
6+
* The Parameters utility provides an AppConfigProvider that allows to retrieve configuration profiles from AWS AppConfig.
7+
*
8+
* ## Getting started
9+
*
10+
* This utility supports AWS SDK v3 for JavaScript only. This allows the utility to be modular, and you to install only
11+
* the SDK packages you need and keep your bundle size small.
12+
*
13+
* To use the provider, you must install the Parameters utility and the AWS SDK v3 for JavaScript for AppConfig:
14+
*
15+
* ```sh
16+
* npm install @aws-lambda-powertools/parameters @aws-sdk/client-appconfigdata
17+
* ```
618
*
7-
* @param {string} name - The configuration profile ID or the configuration profile name.
8-
* @param {GetAppConfigCombinedInterface} options - Options for the AppConfigProvider and the get method.
9-
* @returns {Promise<undefined | string | Uint8Array | Record<string, unknown>>} A promise that resolves to the AppConfig data or undefined if not found.
19+
* ## Basic usage
20+
*
21+
* @example
22+
* ```typescript
23+
* import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
24+
*
25+
* export const handler = async (): Promise<void> => {
26+
* // Retrieve a configuration profile
27+
* const encodedConfig = await getAppConfig('my-config');
28+
* const config = new TextDecoder('utf-8').decode(encodedConfig);
29+
* };
30+
* ```
31+
*
32+
* ## Advanced usage
33+
*
34+
* ### Caching
35+
*
36+
* By default, the provider will cache parameters retrieved in-memory for 5 seconds.
37+
* You can adjust how long values should be kept in cache by using the `maxAge` parameter.
38+
*
39+
* @example
40+
* ```typescript
41+
* import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
42+
*
43+
* export const handler = async (): Promise<void> => {
44+
* // Retrieve a configuration profile and cache it for 10 seconds
45+
* const encodedConfig = await getAppConfig('my-config');
46+
* const config = new TextDecoder('utf-8').decode(encodedConfig);
47+
* };
48+
* ```
49+
*
50+
* If instead you'd like to always ensure you fetch the latest parameter from the store regardless if already available in cache, use the `forceFetch` parameter.
51+
*
52+
* @example
53+
* ```typescript
54+
* import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
55+
*
56+
* export const handler = async (): Promise<void> => {
57+
* // Retrieve a config and always fetch the latest value
58+
* const config = await getAppConfig('my-config', { forceFetch: true });
59+
* const config = new TextDecoder('utf-8').decode(encodedConfig);
60+
* };
61+
* ```
62+
*
63+
* ### Transformations
64+
*
65+
* For configurations stored as freeform JSON, Freature Flag, you can use the transform argument for deserialization. This will return a JavaScript object instead of a string.
66+
*
67+
* @example
68+
* ```typescript
69+
* import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
70+
*
71+
* export const handler = async (): Promise<void> => {
72+
* // Retrieve a JSON config or Feature Flag and parse it as JSON
73+
* const config = await getAppConfig('my-config', { transform: 'json' });
74+
* };
75+
* ```
76+
*
77+
* For configurations that are instead stored as base64-encoded binary data, you can use the transform argument set to `binary` for decoding. This will return a decoded string.
78+
*
79+
* @example
80+
* ```typescript
81+
* import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
82+
*
83+
* export const handler = async (): Promise<void> => {
84+
* // Retrieve a base64-encoded string and decode it
85+
* const config = await getAppConfig('my-config', { transform: 'binary' });
86+
* };
87+
* ```
88+
*
89+
* ### Extra SDK options
90+
*
91+
* When retrieving a configuration profile, you can pass extra options to the AWS SDK v3 for JavaScript client by using the `sdkOptions` parameter.
92+
*
93+
* @example
94+
* ```typescript
95+
* import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
96+
*
97+
* export const handler = async (): Promise<void> => {
98+
* // Retrieve a config and pass extra options to the AWS SDK v3 for JavaScript client
99+
* const config = await getAppConfig('my-config', {
100+
* sdkOptions: {
101+
* RequiredMinimumPollIntervalInSeconds: 60,
102+
* },
103+
* });
104+
* const config = new TextDecoder('utf-8').decode(encodedConfig);
105+
* };
106+
* ```
107+
*
108+
* This object accepts the same options as the [AWS SDK v3 for JavaScript AppConfigData client](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-appconfigdata/interfaces/startconfigurationsessioncommandinput.html).
109+
*
110+
* ### Built-in provider class
111+
*
112+
* For greater flexibility such as configuring the underlying SDK client used by built-in providers, you can use the {@link AppConfigProvider} class.
113+
*
114+
* For more usage examples, see [our documentation](https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/).
115+
*
116+
* @param {string} name - The name of the configuration profile or its ID
117+
* @param {GetAppConfigCombinedInterface} options - Options to configure the provider
118+
* @see https://awslabs.github.io/aws-lambda-powertools-typescript/latest/utilities/parameters/
10119
*/
11120
const getAppConfig = (
12121
name: string,

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

+3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ type AppConfigProviderOptions = AppConfigProviderOptionsWithClientConfig | AppCo
5959
*
6060
* @interface AppConfigGetOptionsInterface
6161
* @extends {GetOptionsInterface}
62+
* @property {number} maxAge - Maximum age of the value in the cache, in seconds.
63+
* @property {boolean} forceFetch - Force fetch the value from the parameter store, ignoring the cache.
6264
* @property {StartConfigurationSessionCommandInput} [sdkOptions] - Required options to start configuration session.
65+
* @property {TransformOptions} transform - Transform to be applied, can be 'json' or 'binary'.
6366
*/
6467
interface AppConfigGetOptionsInterface extends Omit<GetOptionsInterface, 'sdkOptions'> {
6568
sdkOptions?: Omit<

0 commit comments

Comments
 (0)