Description
Use case
I wan to have an option to call a function when idempotency is triggered. In some cases I want to either change payload to signal to the caller that this request was idempotent (x-idempotency: true
) or to have side effects, i.e. add custom metric.
While we have this option in python with idempotency_hook
there is no option available int TypeScript.
Solution/User Experience
import type { IdempotencyRecord } from '@aws-lambda-powertools/idempotency/persistence';
import {
IdempotencyConfig,
makeIdempotent,
} from '@aws-lambda-powertools/idempotency';
import type { Context } from 'aws-lambda';
import { DynamoDBPersistenceLayer } from '@aws-lambda-powertools/idempotency/dynamodb';
interface HandlerReponse {
message: string;
statusCode: number;
headers?: Record<string, string>;
}
const myCustomHook = async (
response: HandlerReponse,
record: IdempotencyRecord
) => {
response.headers['x-idempotency-key'] = record.idempotencyKey;
return response;
};
const config = new IdempotencyConfig({
reponseHook: myCustomHook,
});
const hanlder = async (event: unknown, context: Context) => {
// ... process your event
return {
message: 'success',
statusCode: 200,
};
};
const persistenceStore = new DynamoDBPersistenceLayer({
tableName: 'idempotencyTableName',
});
const idempotentHandler = makeIdempotent(hanlder, {
config: config,
persistenceStore: persistenceStore,
});
Following the Python implementation, we would add resposneHook
option to the config, that takes a function as a parameters. The response hook function has signature with response
and idempotencyRecord
and returns a modified version of the response. The response
type should match the return type of the handler function.
Alternative solutions
No response
Acknowledgment
- This feature request meets Powertools for AWS Lambda (TypeScript) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Python, Java, and .NET
Future readers
Please react with 👍 and your use case to help us understand customer demand.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status