From c19bdfe074e9f07e96c320c34909d95d26fb9070 Mon Sep 17 00:00:00 2001 From: Swopnil Dangol Date: Wed, 13 Aug 2025 17:13:38 +0100 Subject: [PATCH] Passed am empty string as the default value for idempotency key prefix when initializing the persistence layer class --- .../src/persistence/BasePersistenceLayer.ts | 4 ++-- .../persistence/BasePersistenceLayer.test.ts | 21 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/packages/idempotency/src/persistence/BasePersistenceLayer.ts b/packages/idempotency/src/persistence/BasePersistenceLayer.ts index 55c9e34418..e55e38feae 100644 --- a/packages/idempotency/src/persistence/BasePersistenceLayer.ts +++ b/packages/idempotency/src/persistence/BasePersistenceLayer.ts @@ -1,7 +1,7 @@ import { createHash, type Hash } from 'node:crypto'; import type { JSONValue } from '@aws-lambda-powertools/commons/types'; -import { LRUCache } from '@aws-lambda-powertools/commons/utils/lru-cache'; import { getStringFromEnv } from '@aws-lambda-powertools/commons/utils/env'; +import { LRUCache } from '@aws-lambda-powertools/commons/utils/lru-cache'; import { search } from '@aws-lambda-powertools/jmespath'; import type { JMESPathParsingOptions } from '@aws-lambda-powertools/jmespath/types'; import { IdempotencyRecordStatus } from '../constants.js'; @@ -39,7 +39,7 @@ abstract class BasePersistenceLayer implements BasePersistenceLayerInterface { public constructor() { this.idempotencyKeyPrefix = getStringFromEnv({ key: 'AWS_LAMBDA_FUNCTION_NAME', - errorMessage: 'AWS_LAMBDA_FUNCTION_NAME environment variable is required', + defaultValue: '', }); } diff --git a/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts b/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts index acd40bcb99..6344a9246a 100644 --- a/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts +++ b/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts @@ -63,6 +63,27 @@ describe('Class: BasePersistenceLayer', () => { }) ); }); + + it('initializes with empty key prefix if AWS_LAMBDA_FUNCTION_NAME is not in the environment variable', () => { + // Prepare + vi.stubEnv('AWS_LAMBDA_FUNCTION_NAME', undefined); + + // Act + const persistenceLayer = new PersistenceLayerTestClass(); + + // Assess + expect(persistenceLayer.idempotencyKeyPrefix).toBe(''); + expect(persistenceLayer).toEqual( + expect.objectContaining({ + configured: false, + expiresAfterSeconds: 3600, + hashFunction: 'md5', + payloadValidationEnabled: false, + throwOnNoIdempotencyKey: false, + useLocalCache: false, + }) + ); + }); }); describe('Method: configure', () => {