Skip to content

Commit 98217cf

Browse files
committed
docs(idempotency): add custom functions to docs
1 parent 9e7c8c0 commit 98217cf

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

docs/snippets/idempotency/makeIdempotentJmes.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ const createSubscriptionPayment = async (
2222
};
2323
};
2424

25-
// Extract the idempotency key from the request headers
25+
// Deserialize JSON string under the "body" key, then extract the "user" and "productId" keys
2626
const config = new IdempotencyConfig({
27-
eventKeyJmesPath: 'body',
27+
eventKeyJmesPath: 'powertools_json(body).["user", "productId"]',
2828
});
2929

3030
export const handler = makeIdempotent(

docs/utilities/idempotency.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,11 @@ Imagine the function executes successfully, but the client never receives the re
220220
???+ warning "Deserializing JSON strings in payloads for increased accuracy."
221221
The payload extracted by the `eventKeyJmesPath` is treated as a string by default. This means there could be differences in whitespace even when the JSON payload itself is identical.
222222

223+
To alter this behaviour, we can use the [JMESPath built-in function `powertools_json()`](jmespath.md#powertools_json-function) to treat the payload as a JSON object rather than a string.
224+
223225
=== "index.ts"
224226

225-
```typescript hl_lines="4 26-28 49"
227+
```typescript hl_lines="4 27 49"
226228
--8<-- "docs/snippets/idempotency/makeIdempotentJmes.ts"
227229
```
228230

packages/idempotency/README.md

+29-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ You can use the package in both TypeScript and JavaScript code bases.
1818
- [Becoming a reference customer](#becoming-a-reference-customer)
1919
- [Sharing your work](#sharing-your-work)
2020
- [Using Lambda Layer](#using-lambda-layer)
21-
- [Credits](#credits)
2221
- [License](#license)
2322

2423
## Intro
@@ -158,7 +157,33 @@ export const handler = makeIdempotent(myHandler, {
158157
config: new IdempotencyConfig({
159158
eventKeyJmespath: 'requestContext.identity.user',
160159
}),
161-
});
160+
});
161+
```
162+
163+
Additionally, you can also use one of the [JMESPath built-in functions](https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/jmespath/#built-in-jmespath-functions) like `powertools_json()` to decode keys and use parts of the payload as the idempotency key.
164+
165+
```ts
166+
import { makeIdempotent, IdempotencyConfig } from '@aws-lambda-powertools/idempotency';
167+
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
168+
import type { Context, APIGatewayProxyEvent } from 'aws-lambda';
169+
170+
const persistenceStore = new DynamoDBPersistenceLayer({
171+
tableName: 'idempotencyTableName',
172+
});
173+
174+
const myHandler = async (
175+
event: APIGatewayProxyEvent,
176+
_context: Context
177+
): Promise<void> => {
178+
// your code goes here here
179+
};
180+
181+
export const handler = makeIdempotent(myHandler, {
182+
persistenceStore,
183+
config: new IdempotencyConfig({
184+
eventKeyJmespath: 'powertools_json(body).["user", "productId"]',
185+
}),
186+
});
162187
```
163188

164189
Check the [docs](https://docs.powertools.aws.dev/lambda/typescript/latest/utilities/idempotency/) for more examples.
@@ -311,12 +336,8 @@ Share what you did with Powertools for AWS Lambda (TypeScript) 💞💞. Blog po
311336

312337
### Using Lambda Layer
313338

314-
This helps us understand who uses Powertools for AWS Lambda (TypeScript) in a non-intrusive way, and helps us gain future investments for other Powertools for AWS Lambda languages. When [using Layers](#lambda-layers), you can add Powertools as a dev dependency (or as part of your virtual env) to not impact the development process.
315-
316-
## Credits
317-
318-
Credits for the Lambda Powertools for AWS Lambda (TypeScript) idea go to [DAZN](https://github.com/getndazn) and their [DAZN Lambda Powertools](https://github.com/getndazn/dazn-lambda-powertools/).
339+
This helps us understand who uses Powertools for AWS Lambda (TypeScript) in a non-intrusive way, and helps us gain future investments for other Powertools for AWS Lambda languages. When [using Layers](https://docs.powertools.aws.dev/lambda/typescript/latest/#lambda-layer), you can add Powertools as a dev dependency to not impact the development process.
319340

320341
## License
321342

322-
This library is licensed under the MIT-0 License. See the LICENSE file.
343+
This library is licensed under the MIT-0 License. See the LICENSE file.

0 commit comments

Comments
 (0)