Skip to content

Commit 3d1255c

Browse files
guillemcomermaguillempcdreamorosi
authored
docs(idempotency): fix dataKeywordArgument reference and remove unused test code (#4537)
Co-authored-by: guillempc <guillem.comerma@banxware.com> Co-authored-by: Andrea Amorosi <dreamorosi@gmail.com>
1 parent f08b366 commit 3d1255c

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

packages/idempotency/src/makeIdempotent.ts

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,36 +44,73 @@ const isOptionsWithDataIndexArgument = (
4444
};
4545

4646
/**
47-
* Use function wrapper to make your function idempotent.
47+
* Function wrapper to make any function idempotent.
48+
*
49+
* The `makeIdempotent` function is a higher-order function that takes another function and returns a new version of that function with idempotency behavior.
50+
* This means that if the function is called multiple times with the same input, it will return the same result without re-executing the original function logic.
51+
*
52+
* By default, the entire first argument is hashed to create the idempotency key. You can customize this behavior:
53+
* - Use {@link IdempotencyConfig.eventKeyJmesPath | `eventKeyJmesPath`} to hash only a subset of the payload
54+
* - Use {@link ItempotentFunctionOptions.dataIndexArgument | `dataIndexArgument`} to hash a different function argument
55+
*
56+
*
57+
* **Using a subset of the payload**
58+
*
4859
* @example
49-
* ```ts
50-
* // this is your processing function with an example record { transactionId: '123', foo: 'bar' }
51-
* const processRecord = (record: Record<string, unknown>): any => {
52-
* // you custom processing logic
60+
* ```typescript
61+
* import { makeIdempotent, IdempotencyConfig } from '@aws-lambda-powertools/idempotency';
62+
* import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
63+
*
64+
* const processRecord = (record: Record<string, unknown>): unknown => {
65+
* // your processing logic
5366
* return result;
5467
* };
5568
*
56-
* // we use wrapper to make processing function idempotent with DynamoDBPersistenceLayer
5769
* const processIdempotently = makeIdempotent(processRecord, {
58-
* persistenceStore: new DynamoDBPersistenceLayer()
59-
* dataKeywordArgument: 'transactionId', // keyword argument to hash the payload and the result
70+
* persistenceStore: new DynamoDBPersistenceLayer({ tableName: 'idempotency-table' }),
71+
* config: new IdempotencyConfig({
72+
* eventKeyJmesPath: 'transactionId', // hash only this field as idempotency key
73+
* }),
6074
* });
6175
*
62-
* export const handler = async (
63-
* _event: EventRecords,
64-
* _context: Context
65-
* ): Promise<void> => {
66-
* for (const record of _event.records) {
67-
* const result = await processIdempotently(record);
68-
* // do something with the result
76+
* export const handler = async (event: { records: Record<string, unknown>[] }) => {
77+
* for (const record of event.records) {
78+
* // use the idempotent function
79+
* const result = await processIdempotently(record);
80+
* // ... do something with the result
6981
* }
82+
* };
83+
*```
84+
*
85+
* **Using a different function argument (useful for multi-parameter functions)**
86+
*
87+
* @example
88+
* ```typescript
89+
* import { makeIdempotent } from '@aws-lambda-powertools/idempotency';
90+
* import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
7091
*
71-
* return Promise.resolve();
92+
* const processRecord = (record: Record<string, unknown>, userId: string): unknown => {
93+
* // your processing logic
94+
* return result;
7295
* };
7396
*
97+
* const processIdempotently = makeIdempotent(processRecord, {
98+
* persistenceStore: new DynamoDBPersistenceLayer({ tableName: 'idempotency-table' }),
99+
* dataIndexArgument: 1, // hash the userId (second argument) instead of first (record)
100+
* });
101+
*
102+
* export const handler = async (event: { records: Record<string,unknown>[]; userId: string }) => {
103+
* for (const record of event.records) {
104+
* const userId = event.userId;
105+
* // use the idempotent function
106+
* const result = await processIdempotently(record, userId);
107+
* // ... do something with the result
108+
* }
109+
* };
110+
* ```
111+
*
74112
* @param fn - the function to make idempotent
75113
* @param options - the options to configure the idempotency behavior
76-
* ```
77114
*/
78115
function makeIdempotent<Func extends AnyFunction>(
79116
fn: Func,

packages/idempotency/tests/unit/IdempotencyHandler.test.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ const mockFunctionPayloadToBeHashed = {};
2020
const persistenceStore = new PersistenceLayerTestClass();
2121
const mockIdempotencyOptions = {
2222
persistenceStore,
23-
dataKeywordArgument: 'testKeywordArgument',
2423
config: new IdempotencyConfig({}),
2524
};
2625

0 commit comments

Comments
 (0)