Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
69 changes: 32 additions & 37 deletions packages/parameters/src/appconfig/AppConfigProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ import type {
} from '../types/AppConfigProvider.js';

/**
* ## Intro
* The Parameters utility provides an AppConfigProvider that allows to retrieve configuration profiles from AWS AppConfig.
*
* ## Getting started
* The Parameters utility provides an `AppConfigProvider` that allows to retrieve configuration profiles from AWS AppConfig.
*
* This utility supports AWS SDK v3 for JavaScript only (`@aws-sdk/client-appconfigdata`). This allows the utility to be modular, and you to install only
* the SDK packages you need and keep your bundle size small.
*
* ## Basic usage
* **Basic usage**
*
* @example
* ```typescript
Expand All @@ -41,9 +38,7 @@ import type {
* ```
* If you want to retrieve configs without customizing the provider, you can use the {@link getAppConfig} function instead.
*
* ## Advanced usage
*
* ### Caching
* **Caching**
*
* By default, the provider will cache parameters retrieved in-memory for 5 seconds.
* You can adjust how long values should be kept in cache by using the `maxAge` parameter.
Expand Down Expand Up @@ -82,7 +77,7 @@ import type {
* };
* ```
*
* ### Transformations
* **Transformations**
*
* 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.
*
Expand Down Expand Up @@ -118,7 +113,7 @@ import type {
* };
* ```
*
* ### Extra SDK options
* **Extra SDK options**
*
* When retrieving a configuration profile, you can pass extra options to the AWS SDK v3 for JavaScript client by using the `sdkOptions` parameter.
*
Expand All @@ -144,7 +139,7 @@ import type {
*
* 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).
*
* ### Customize AWS SDK v3 for JavaScript client
* **Customize AWS SDK v3 for JavaScript client**
*
* By default, the provider will create a new AppConfigData client using the default configuration.
*
Expand Down Expand Up @@ -192,21 +187,20 @@ class AppConfigProvider extends BaseProvider {
private readonly application?: string;
private readonly environment: string;

/**
* It initializes the AppConfigProvider class.
* *
* @param {AppConfigProviderOptions} options - The configuration object.
*/
public constructor(options: AppConfigProviderOptions) {
public constructor({
application,
environment,
clientConfig,
awsSdkV3Client,
}: AppConfigProviderOptions) {
super({
awsSdkV3ClientPrototype: AppConfigDataClient as new (
config?: unknown
) => AppConfigDataClient,
clientConfig: options.clientConfig,
awsSdkV3Client: options.awsSdkV3Client,
clientConfig,
awsSdkV3Client,
});

const { application, environment } = options;
this.application = application ?? getServiceName();
if (!this.application || this.application.trim().length === 0) {
throw new Error(
Expand Down Expand Up @@ -235,26 +229,23 @@ class AppConfigProvider extends BaseProvider {
* };
* ```
*
* You can customize the retrieval of the configuration profile by passing options to the function:
* * `maxAge` - The maximum age of the value in cache before fetching a new one (in seconds) (default: 5)
* * `forceFetch` - Whether to always fetch a new value from the store regardless if already available in cache
* * `transform` - Whether to transform the value before returning it. Supported values: `json`, `binary`
* * `sdkOptions` - Extra options to pass to the AWS SDK v3 for JavaScript client
*
* For usage examples check {@link AppConfigProvider}.
*
* @param {string} name - The name of the configuration profile or its ID
* @param {AppConfigGetOptions} options - Options to configure the provider
* @see https://docs.powertools.aws.dev/lambda/typescript/latest/features/parameters/
*
* @param name - The name of the configuration profile to retrieve
* @param options - Optional options to configure the provider
* @param options.maxAge - Optional maximum age of the value in the cache, in seconds (default: `5`)
* @param options.forceFetch - Optional flag to always fetch a new value from the store regardless if already available in cache (default: `false`)
* @param options.transform - Optional transform to be applied, can be `json` or `binary`
* @param options.sdkOptions - Optional additional options to pass to the AWS SDK v3 client, supports all options from {@link StartConfigurationSessionCommandInput | `StartConfigurationSessionCommandInput`} except `ApplicationIdentifier`, `EnvironmentIdentifier`, and `ConfigurationProfileIdentifier`
*/
public async get<
public get<
ExplicitUserProvidedType = undefined,
InferredFromOptionsType extends
| AppConfigGetOptions
| undefined = AppConfigGetOptions,
>(
name: string,
options?: InferredFromOptionsType & AppConfigGetOptions
options?: NonNullable<InferredFromOptionsType & AppConfigGetOptions>
): Promise<
| AppConfigGetOutput<ExplicitUserProvidedType, InferredFromOptionsType>
| undefined
Expand Down Expand Up @@ -287,12 +278,16 @@ class AppConfigProvider extends BaseProvider {
* polls the configuration multiple times, we return the most recent value by returning the cached
* one if an empty response is returned by AppConfig.
*
* @param {string} name - Name of the configuration or its ID
* @param {AppConfigGetOptions} options - SDK options to propagate to `StartConfigurationSession` API call
* @param name - Name of the configuration or its ID
* @param options - SDK options to propagate to `StartConfigurationSession` API call
* @param options.maxAge - Maximum age of the value in the cache, in seconds.
* @param options.forceFetch - Force fetch the value from the parameter store, ignoring the cache.
* @param options.sdkOptions - Additional options to pass to the AWS SDK v3 client. Supports all options from {@link StartConfigurationSessionCommandInput | `StartConfigurationSessionCommandInput`} except `ApplicationIdentifier`, `EnvironmentIdentifier`, and `ConfigurationProfileIdentifier`.
* @param options.transform - Optional transform to be applied, can be 'json' or 'binary'.
*/
protected async _get(
name: string,
options?: AppConfigGetOptions
options?: NonNullable<AppConfigGetOptions>
): Promise<Uint8Array | undefined> {
if (
!this.configurationTokenStore.has(name) ||
Expand Down Expand Up @@ -339,7 +334,7 @@ class AppConfigProvider extends BaseProvider {
/** When the response is not empty, stash the result locally before returning
* See AppConfig docs:
* {@link https://docs.aws.amazon.com/appconfig/latest/userguide/appconfig-retrieving-the-configuration.html}
**/
*/
if (
response.Configuration !== undefined &&
response.Configuration?.length > 0
Expand All @@ -358,7 +353,7 @@ class AppConfigProvider extends BaseProvider {
*
* @throws Not Implemented Error.
*/
protected async _getMultiple(
protected _getMultiple(
_path: string,
_sdkOptions?: unknown
): Promise<Record<string, unknown> | undefined> {
Expand Down
101 changes: 51 additions & 50 deletions packages/parameters/src/appconfig/getAppConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,30 @@ import type {
import { AppConfigProvider } from './AppConfigProvider.js';

/**
* ## Intro
* The Parameters utility provides an AppConfigProvider that allows to retrieve configuration profiles from AWS AppConfig.
* The Parameters utility provides an `AppConfigProvider` that allows to retrieve configuration profiles from AWS AppConfig.
*
* ## Getting started
*
* This utility supports AWS SDK v3 for JavaScript only. This allows the utility to be modular, and you to install only
* This utility supports AWS SDK v3 for JavaScript only (`@aws-sdk/client-appconfigdata`). This allows the utility to be modular, and you to install only
* the SDK packages you need and keep your bundle size small.
*
* To use the provider, you must install the Parameters utility and the AWS SDK v3 for JavaScript for AppConfig:
*
* ```sh
* npm install @aws-lambda-powertools/parameters @aws-sdk/client-appconfigdata
* ```
*
* ## Basic usage
* **Basic usage**
*
* @example
* ```typescript
* import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
*
* const encodedConfig = await getAppConfig('my-config', {
* application: 'my-app',
* environment: 'prod',
* });
* const config = new TextDecoder('utf-8').decode(encodedConfig);
*
* export const handler = async (): Promise<void> => {
* // Retrieve a configuration profile
* const encodedConfig = await getAppConfig('my-config', {
* application: 'my-app',
* environment: 'prod',
* });
* const config = new TextDecoder('utf-8').decode(encodedConfig);
* // Use the config variable as needed
* console.log(config);
* };
* ```
*
* ## Advanced usage
*
* ### Caching
* **Caching**
*
* By default, the provider will cache parameters retrieved in-memory for 5 seconds.
* You can adjust how long values should be kept in cache by using the `maxAge` parameter.
Expand All @@ -47,13 +38,15 @@ import { AppConfigProvider } from './AppConfigProvider.js';
* ```typescript
* import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
*
* const encodedConfig = await getAppConfig('my-config', {
* application: 'my-app',
* environment: 'prod',
* maxAge: 10, // Cache for 10 seconds
* });
* const config = new TextDecoder('utf-8').decode(encodedConfig);
*
* export const handler = async (): Promise<void> => {
* // Retrieve a configuration profile and cache it for 10 seconds
* const encodedConfig = await getAppConfig('my-config', {
* application: 'my-app',
* environment: 'prod',
* });
* const config = new TextDecoder('utf-8').decode(encodedConfig);
* // Use the config variable as needed
* };
* ```
*
Expand All @@ -63,32 +56,37 @@ import { AppConfigProvider } from './AppConfigProvider.js';
* ```typescript
* import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
*
* const encodedConfig = await getAppConfig('my-config', {
* application: 'my-app',
* environment: 'prod',
* forceFetch: true, // Always fetch the latest value
* });
* const config = new TextDecoder('utf-8').decode(encodedConfig);
*
* export const handler = async (): Promise<void> => {
* // Retrieve a config and always fetch the latest value
* const config = await getAppConfig('my-config', {
* application: 'my-app',
* environment: 'prod',
* forceFetch: true,
* });
* const config = new TextDecoder('utf-8').decode(encodedConfig);
* // Use the config variable as needed
* console.log
* };
* ```
*
* ### Transformations
* **Transformations**
*
* 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.
*
* @example
* ```typescript
* import { getAppConfig } from '@aws-lambda-powertools/parameters/appconfig';
*
* // Retrieve a JSON config and parse it as JSON
* const encodedConfig = await getAppConfig('my-config', {
* application: 'my-app',
* environment: 'prod',
* transform: 'json'
* });
*
* export const handler = async (): Promise<void> => {
* // Retrieve a JSON config or Feature Flag and parse it as JSON
* const config = await getAppConfig('my-config', {
* application: 'my-app',
* environment: 'prod',
* transform: 'json'
* });
* // Use the config variable as needed
* console.log(config);
* };
* ```
*
Expand All @@ -108,7 +106,7 @@ import { AppConfigProvider } from './AppConfigProvider.js';
* };
* ```
*
* ### Extra SDK options
* **Extra SDK options**
*
* When retrieving a configuration profile, you can pass extra options to the AWS SDK v3 for JavaScript client by using the `sdkOptions` parameter.
*
Expand All @@ -131,15 +129,18 @@ import { AppConfigProvider } from './AppConfigProvider.js';
*
* 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).
*
* ### Built-in provider class
* For greater flexibility such as configuring the underlying SDK client used by built-in providers, you can use the {@link AppConfigProvider | `AppConfigProvider`} class.
*
* For greater flexibility such as configuring the underlying SDK client used by built-in providers, you can use the {@link AppConfigProvider} class.
*
* For more usage examples, see [our documentation](https://docs.powertools.aws.dev/lambda/typescript/latest/features/parameters/).
*
* @param {string} name - The name of the configuration profile or its ID
* @param {GetAppConfigOptions} options - Options to configure the provider
* @see https://docs.powertools.aws.dev/lambda/typescript/latest/features/parameters/
*
* @param name - The name of the configuration profile to retrieve
* @param options - Options to configure the provider
* @param options.application - The application ID or the application name
* @param options.environment - The environment ID or the environment name
* @param options.maxAge - Optional maximum age of the value in the cache, in seconds (default: `5`)
* @param options.forceFetch - Optional flag to always fetch a new value from the store regardless if already available in cache (default: `false`)
* @param options.transform - Optional transform to be applied, can be `json` or `binary`
* @param options.sdkOptions - Optional additional options to pass to the AWS SDK v3 client, supports all options from {@link StartConfigurationSessionCommandInput | `StartConfigurationSessionCommandInput`} except `ApplicationIdentifier`, `EnvironmentIdentifier`, and `ConfigurationProfileIdentifier`
*/
const getAppConfig = <
ExplicitUserProvidedType = undefined,
Expand All @@ -148,7 +149,7 @@ const getAppConfig = <
| undefined = GetAppConfigOptions,
>(
name: string,
options: InferredFromOptionsType & GetAppConfigOptions
options: NonNullable<InferredFromOptionsType & GetAppConfigOptions>
): Promise<
| AppConfigGetOutput<ExplicitUserProvidedType, InferredFromOptionsType>
| undefined
Expand Down
Loading
Loading