Skip to content
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

Feature request: manipulating the idempotent response #2887

Closed
1 of 2 tasks
am29d opened this issue Aug 6, 2024 · 6 comments · Fixed by #3071
Closed
1 of 2 tasks

Feature request: manipulating the idempotent response #2887

am29d opened this issue Aug 6, 2024 · 6 comments · Fixed by #3071
Assignees
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility idempotency This item relates to the Idempotency Utility

Comments

@am29d
Copy link
Contributor

am29d commented Aug 6, 2024

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.

@am29d am29d added triage This item has not been triaged by a maintainer, please wait feature-request This item refers to a feature request for an existing or new utility labels Aug 6, 2024
@dreamorosi dreamorosi added help-wanted We would really appreciate some support from community for this one idempotency This item relates to the Idempotency Utility confirmed The scope is clear, ready for implementation and removed triage This item has not been triaged by a maintainer, please wait labels Aug 6, 2024
@dreamorosi dreamorosi moved this from Triage to Backlog in Powertools for AWS Lambda (TypeScript) Aug 6, 2024
@dreamorosi
Copy link
Contributor

Good point - we are missing this feature and it'd be good to have it for feature parity.

I'm going to mark it as contributions welcome.

Note

For those interested in contributing, please leave a comment below so that we can assign the issue to you and make sure we don't duplicate efforts. Also, if you have any further questions please don't hesitate to ask here or on our Discord channel.

@arnabrahman
Copy link
Contributor

I would like to contribute to this but I need some pointers.

I believe if the custom hook function is provided, it should be called from here. Something like this?

const resultFromIdempotency = IdempotencyHandler.determineResultFromIdempotencyRecord(
    idempotencyRecord
);
returnValue.result = hasHook? customHook(resultFromIdempotency, idempotencyRecord) : resultFromIdempotency;
        

Once again, I need your expert opinion on this, @dreamorosi. Thanks.

@dreamorosi
Copy link
Contributor

Hi @arnabrahman, thanks for giving some traction to this feature.

I've looked at the implementation in Powertools for AWS Lambda (Python) and it roughly matches the suggestion you shared above. In their implementation they call the hook in the equivalent of our IdempotencyHandler.determineResultFromIdempotencyRecord method just before returning, which is pretty much the same as you are suggesting1.

Normally I would advocate for consistency across projects for this type of feature, mainly to reduce the cognitive load for recurrent maintainers by having similar implementation.

In this case we might not be able to the same unless we change the determineResultFromIdempotencyRecord() method from static to normal, so that it can access the config object. Assuming we make the method non-static, I would be inclined to add the logic within the determineResultFromIdempotencyRecord() method instead.

Regarding the actual implementation, I think the logic you're suggesting makes sense, so from my side we're good to move forward if you're still interested.

Footnotes

  1. In their implementation they also do the equivalent of an undefined check - which is a bug that I have reported (Bug: response hook is not called when idempotent function returns None powertools-lambda-python#5150).

@arnabrahman
Copy link
Contributor

Yes, I am interested. Please assign me on this, @dreamorosi

Copy link
Contributor

⚠️ COMMENT VISIBILITY WARNING ⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added pending-release This item has been merged and will be released soon and removed help-wanted We would really appreciate some support from community for this one confirmed The scope is clear, ready for implementation labels Sep 19, 2024
Copy link
Contributor

github-actions bot commented Oct 8, 2024

This is now released under v2.9.0 version!

@github-actions github-actions bot added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Oct 8, 2024
@dreamorosi dreamorosi moved this from Coming soon to Shipped in Powertools for AWS Lambda (TypeScript) Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility idempotency This item relates to the Idempotency Utility
Projects
3 participants