Skip to content

Commit 17845d0

Browse files
sdangoldreamorosi
andauthored
improv(idempotency): Prevent error when AWS_LAMBDA_FUNCTION_NAME is not passed in the Idempotency Persistence Layer (#4327)
Co-authored-by: Andrea Amorosi <dreamorosi@gmail.com>
1 parent 562747a commit 17845d0

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

packages/idempotency/src/persistence/BasePersistenceLayer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createHash, type Hash } from 'node:crypto';
22
import type { JSONValue } from '@aws-lambda-powertools/commons/types';
3-
import { LRUCache } from '@aws-lambda-powertools/commons/utils/lru-cache';
43
import { getStringFromEnv } from '@aws-lambda-powertools/commons/utils/env';
4+
import { LRUCache } from '@aws-lambda-powertools/commons/utils/lru-cache';
55
import { search } from '@aws-lambda-powertools/jmespath';
66
import type { JMESPathParsingOptions } from '@aws-lambda-powertools/jmespath/types';
77
import { IdempotencyRecordStatus } from '../constants.js';
@@ -39,7 +39,7 @@ abstract class BasePersistenceLayer implements BasePersistenceLayerInterface {
3939
public constructor() {
4040
this.idempotencyKeyPrefix = getStringFromEnv({
4141
key: 'AWS_LAMBDA_FUNCTION_NAME',
42-
errorMessage: 'AWS_LAMBDA_FUNCTION_NAME environment variable is required',
42+
defaultValue: '',
4343
});
4444
}
4545

packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,27 @@ describe('Class: BasePersistenceLayer', () => {
6363
})
6464
);
6565
});
66+
67+
it('initializes with empty key prefix if AWS_LAMBDA_FUNCTION_NAME is not in the environment variable', () => {
68+
// Prepare
69+
vi.stubEnv('AWS_LAMBDA_FUNCTION_NAME', undefined);
70+
71+
// Act
72+
const persistenceLayer = new PersistenceLayerTestClass();
73+
74+
// Assess
75+
expect(persistenceLayer.idempotencyKeyPrefix).toBe('');
76+
expect(persistenceLayer).toEqual(
77+
expect.objectContaining({
78+
configured: false,
79+
expiresAfterSeconds: 3600,
80+
hashFunction: 'md5',
81+
payloadValidationEnabled: false,
82+
throwOnNoIdempotencyKey: false,
83+
useLocalCache: false,
84+
})
85+
);
86+
});
6687
});
6788

6889
describe('Method: configure', () => {

0 commit comments

Comments
 (0)