Skip to content

Feature request: manipulating the idempotent response #2887

Closed
@am29d

Description

@am29d

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

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

Labels

completedThis item is complete and has been merged/shippedfeature-requestThis item refers to a feature request for an existing or new utilityidempotencyThis item relates to the Idempotency Utility

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions