Skip to content

Commit 0c1c94c

Browse files
committed
feat: enable custom functions
1 parent f37cea5 commit 0c1c94c

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

packages/idempotency/src/IdempotencyHandler.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { IdempotencyRecord } from './persistence/IdempotencyRecord.js';
1717
import { IdempotencyConfig } from './IdempotencyConfig.js';
1818
import { MAX_RETRIES, IdempotencyRecordStatus } from './constants.js';
1919
import { search } from '@aws-lambda-powertools/jmespath';
20+
import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
2021

2122
/**
2223
* @internal
@@ -276,7 +277,8 @@ export class IdempotencyHandler<Func extends AnyFunction> {
276277
) {
277278
const selection = search(
278279
this.#idempotencyConfig.eventKeyJmesPath,
279-
this.#functionPayloadToBeHashed
280+
this.#functionPayloadToBeHashed,
281+
{ customFunctions: new PowertoolsFunctions() }
280282
);
281283

282284
return selection === undefined || selection === null;

packages/idempotency/src/persistence/BasePersistenceLayer.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createHash, Hash } from 'node:crypto';
22
import { search } from '@aws-lambda-powertools/jmespath';
3+
import { PowertoolsFunctions } from '@aws-lambda-powertools/jmespath/functions';
34
import type {
45
BasePersistenceLayerOptions,
56
BasePersistenceLayerInterface,
@@ -279,7 +280,9 @@ abstract class BasePersistenceLayer implements BasePersistenceLayerInterface {
279280
*/
280281
private getHashedIdempotencyKey(data: JSONValue): string {
281282
if (this.eventKeyJmesPath) {
282-
data = search(this.eventKeyJmesPath, data) as JSONValue;
283+
data = search(this.eventKeyJmesPath, data, {
284+
customFunctions: new PowertoolsFunctions(),
285+
}) as JSONValue;
283286
}
284287

285288
if (BasePersistenceLayer.isMissingIdempotencyKey(data)) {
@@ -305,7 +308,9 @@ abstract class BasePersistenceLayer implements BasePersistenceLayerInterface {
305308
*/
306309
private getHashedPayload(data: JSONValue): string {
307310
if (this.isPayloadValidationEnabled() && this.validationKeyJmesPath) {
308-
data = search(this.validationKeyJmesPath, data) as JSONValue;
311+
data = search(this.validationKeyJmesPath, data, {
312+
customFunctions: new PowertoolsFunctions(),
313+
}) as JSONValue;
309314

310315
return this.generateHash(JSON.stringify(data));
311316
} else {

packages/idempotency/tests/e2e/makeIdempotent.test.FunctionCode.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,17 @@ export const handlerCustomized = async (
9090
* Test idempotent Lambda handler with JMESPath expression to extract event key.
9191
*/
9292
export const handlerLambda = makeIdempotent(
93-
async (event: { foo: string }, context: Context) => {
93+
async (event: { body: string }, context: Context) => {
9494
logger.addContext(context);
95-
logger.info(`foo`, { details: event.foo });
95+
const body = JSON.parse(event.body);
96+
logger.info('foo', { details: body.foo });
9697

97-
return event.foo;
98+
return body.foo;
9899
},
99100
{
100101
persistenceStore: dynamoDBPersistenceLayer,
101102
config: new IdempotencyConfig({
102-
eventKeyJmesPath: 'foo',
103+
eventKeyJmesPath: 'powertools_json(body).foo',
103104
useLocalCache: true,
104105
}),
105106
}

packages/idempotency/tests/e2e/makeIdempotent.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -268,10 +268,12 @@ describe(`Idempotency E2E tests, wrapper function usage`, () => {
268268
async () => {
269269
// Prepare
270270
const payload = {
271-
foo: 'bar',
271+
body: JSON.stringify({
272+
foo: 'bar',
273+
}),
272274
};
273275
const payloadHash = createHash('md5')
274-
.update(JSON.stringify(payload.foo))
276+
.update(JSON.stringify('bar'))
275277
.digest('base64');
276278

277279
// Act

0 commit comments

Comments
 (0)