diff --git a/docs/snippets/idempotency/idempotentDecoratorBase.ts b/docs/snippets/idempotency/idempotentDecoratorBase.ts index a14c2b26e1..852ddae075 100644 --- a/docs/snippets/idempotency/idempotentDecoratorBase.ts +++ b/docs/snippets/idempotency/idempotentDecoratorBase.ts @@ -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', diff --git a/docs/utilities/idempotency.md b/docs/utilities/idempotency.md index 2a770ec6ba..dc58487016 100644 --- a/docs/utilities/idempotency.md +++ b/docs/utilities/idempotency.md @@ -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. @@ -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. diff --git a/packages/idempotency/README.md b/packages/idempotency/README.md index c295c5ef1d..25c4a75a2d 100644 --- a/packages/idempotency/README.md +++ b/packages/idempotency/README.md @@ -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. @@ -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 { // your code goes here here } @@ -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'; @@ -207,18 +207,18 @@ const persistenceStore = new DynamoDBPersistenceLayer({ class MyHandler extends LambdaInterface { public async handler( - event: unknown, - context: Context + event: unknown, + context: Context ): Promise { for(const record of event.Records) { await this.processIdempotently(record); } } - @idempotent({ persistenceStore: dynamoDBPersistenceLayer }) - private async process(record: unknown): Promise { - // process each code idempotently - } + @idempotent({ persistenceStore: dynamoDBPersistenceLayer }) + private async process(record: unknown): Promise { + // process each code idempotently + } } const handlerClass = new MyHandler(); diff --git a/packages/idempotency/src/idempotencyDecorator.ts b/packages/idempotency/src/idempotencyDecorator.ts index 53e17f3ef3..6b20a38e21 100644 --- a/packages/idempotency/src/idempotencyDecorator.ts +++ b/packages/idempotency/src/idempotencyDecorator.ts @@ -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"; @@ -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){