Skip to content

docs(idempotency): address comments #1724

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/snippets/idempotency/idempotentDecoratorBase.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { Context } from 'aws-lambda';
import { LambdaInterface } from '@aws-lambda-powertools/commons';
import type { LambdaInterface } from '@aws-lambda-powertools/commons';
import {
IdempotencyConfig,
idempotent,
} from '@aws-lambda-powertools/idempotency';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
import { Request, Response } from './types';
import type { Request, Response } from './types';

const dynamoDBPersistenceLayer = new DynamoDBPersistenceLayer({
tableName: 'idempotencyTableName',
Expand Down
5 changes: 4 additions & 1 deletion docs/utilities/idempotency.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ After processing this request successfully, a second request containing the exac
See [Choosing a payload subset for idempotency](#choosing-a-payload-subset-for-idempotency) for more elaborate use cases.


You can also use the `makeIdempotent` function wrapper on any function that returns a response to make it idempotent. This is useful when you want to make a specific logic idempotent, for example when your Lambda handler performs multiple side effects and you only want to make a specific one idempotent.
You can also use the `makeIdempotent` function wrapper on any method that returns a response to make it idempotent. This is useful when you want to make a specific logic idempotent, for example when your Lambda handler performs multiple side effects and you only want to make a specific one idempotent.

???+ warning "Limitation"
Make sure to return a JSON serializable response from your function, otherwise you'll get an error.
Expand Down Expand Up @@ -173,6 +173,9 @@ You can also use the `@idempotent` decorator to make your Lambda handler idempot
=== "types.ts"

```typescript
--8<-- "docs/snippets/idempotency/types.ts"
```


You can use the decorator on your Lambda handler or on any function that returns a response to make it idempotent. This is useful when you want to make a specific logic idempotent, for example when your Lambda handler performs multiple side effects and you only want to make a specific one idempotent.
The configuration options for the `@idempotent` decorator are the same as the ones for the `makeIdempotent` function wrapper.
Expand Down
20 changes: 10 additions & 10 deletions packages/idempotency/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ You can use the package in both TypeScript and JavaScript code bases.
## Intro

This package provides a utility to implement idempotency in your Lambda functions.
You can either use it to wrap a function, decorate a function, or as Middy middleware to make your AWS Lambda handler idempotent.
You can either use it to wrap a function, decorate a method, or as Middy middleware to make your AWS Lambda handler idempotent.

The current implementation provides a persistence layer for Amazon DynamoDB, which offers a variety of configuration options. You can also bring your own persistence layer by extending the `BasePersistenceLayer` class.

Expand Down Expand Up @@ -181,8 +181,8 @@ const persistenceStore = new DynamoDBPersistenceLayer({
class MyHandler extends LambdaInterface {
@idempotent({ persistenceStore: dynamoDBPersistenceLayer })
public async handler(
event: APIGatewayProxyEvent,
context: Context
event: APIGatewayProxyEvent,
context: Context
): Promise<void> {
// your code goes here here
}
Expand All @@ -192,7 +192,7 @@ const handlerClass = new MyHandler();
export const handler = handlerClass.handler.bind(handlerClass);
```

Using the same decorator, you can also make any other arbitrary function idempotent.
Using the same decorator, you can also make any other arbitrary method idempotent.

```ts
import { idempotent } from '@aws-lambda-powertools/idempotency';
Expand All @@ -207,18 +207,18 @@ const persistenceStore = new DynamoDBPersistenceLayer({
class MyHandler extends LambdaInterface {

public async handler(
event: unknown,
context: Context
event: unknown,
context: Context
): Promise<void> {
for(const record of event.Records) {
await this.processIdempotently(record);
}
}

@idempotent({ persistenceStore: dynamoDBPersistenceLayer })
private async process(record: unknown): Promise<void> {
// process each code idempotently
}
@idempotent({ persistenceStore: dynamoDBPersistenceLayer })
private async process(record: unknown): Promise<void> {
// process each code idempotently
}
}

const handlerClass = new MyHandler();
Expand Down
10 changes: 6 additions & 4 deletions packages/idempotency/src/idempotencyDecorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import { makeIdempotent } from './makeIdempotent';
* import {
* DynamoDBPersistenceLayer,
* idempotentLambdaHandler
* } from '@aws-lambda-powertools/idempotency'
* } from '@aws-lambda-powertools/idempotency';
* import type { LambdaInterface } from '@aws-lambda-powertools/commons';
*
* class MyLambdaFunction {
* class MyLambdaFunction implements LambdaInterface{
* @idempotent({ persistenceStore: new DynamoDBPersistenceLayer() })
* async handler(event: any, context: any) {
* return "Hello World";
Expand All @@ -29,9 +30,10 @@ import { makeIdempotent } from './makeIdempotent';
* import {
* DynamoDBPersistenceLayer,
* idempotentFunction
* } from '@aws-lambda-powertools/idempotency'
* } from '@aws-lambda-powertools/idempotency';
* import type { LambdaInterface } from '@aws-lambda-powertools/commons';
*
* class MyClass {
* class MyClass implements LambdaInterface {
*
* public async handler(_event: any, _context: any) {
* for(const record of _event.records){
Expand Down