@aws-lambda-powertools/idempotency
Version:
The idempotency package for the Powertools for AWS Lambda (TypeScript) library. It provides options to make your Lambda functions idempotent and safe to retry.
95 lines (94 loc) • 3.43 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.IdempotencyConfig = void 0;
const env_1 = require("@aws-lambda-powertools/commons/utils/env");
const functions_1 = require("@aws-lambda-powertools/jmespath/functions");
/**
* Configuration for the idempotency feature.
*/
class IdempotencyConfig {
/**
* The JMESPath expression used to extract the idempotency key from the event.
* @default ''
*/
eventKeyJmesPath;
/**
* The number of seconds the idempotency key is valid.
* @default 3600 (1 hour)
*/
expiresAfterSeconds;
/**
* The hash function used to generate the idempotency key.
* @see https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options
* @default 'md5'
*/
hashFunction;
/**
* Options for parsing JMESPath expressions.
*
* By default, you can use any of the {@link https://jmespath.org/specification.html | JMESPath built-in functions} as well as the
* {@link https://docs.powertools.aws.dev/lambda/typescript/latest/api/classes/_aws_lambda_powertools_jmespath.PowertoolsFunctions.PowertoolsFunctions.html | custom functions provided}
* by the `@aws-lambda-powertools/jmespath` package.
*/
jmesPathOptions;
/**
* The lambda context object.
*/
lambdaContext;
/**
* The maximum number of items to store in the local cache.
* @default 1000
*/
maxLocalCacheSize;
/**
* The JMESPath expression used to extract the payload to validate.
*/
payloadValidationJmesPath;
/**
* Throw an error if the idempotency key is not found in the event.
* In some cases, you may want to allow the request to continue without idempotency.
* If set to false and idempotency key is not found, the request will continue without idempotency.
* @default false
*/
throwOnNoIdempotencyKey;
/**
* A hook that runs when an idempotent request is made.
*/
responseHook;
/**
* Use the local cache to store idempotency keys.
*/
useLocalCache;
#enabled = true;
constructor(config) {
this.eventKeyJmesPath = config.eventKeyJmesPath ?? '';
this.payloadValidationJmesPath = config.payloadValidationJmesPath;
this.jmesPathOptions = {
customFunctions: config.jmesPathOptions ?? new functions_1.PowertoolsFunctions(),
};
this.throwOnNoIdempotencyKey = config.throwOnNoIdempotencyKey ?? false;
this.expiresAfterSeconds = config.expiresAfterSeconds ?? 3600; // 1 hour default
this.useLocalCache = config.useLocalCache ?? false;
this.maxLocalCacheSize = config.maxLocalCacheSize ?? 1000;
this.hashFunction = config.hashFunction ?? 'md5';
this.lambdaContext = config.lambdaContext;
this.responseHook = config.responseHook;
this.#enabled = !(0, env_1.getBooleanFromEnv)({
key: 'POWERTOOLS_IDEMPOTENCY_DISABLED',
defaultValue: false,
extendedParsing: true,
});
}
/**
* Determines if the idempotency feature is enabled.
*
* @returns {boolean} Returns true if the idempotency feature is enabled.
*/
isEnabled() {
return this.#enabled;
}
registerLambdaContext(context) {
this.lambdaContext = context;
}
}
exports.IdempotencyConfig = IdempotencyConfig;
;