Skip to content

Feature request: Be able to have more flexibility with how the idempotency key is being handled #3540

Closed
@wizardone

Description

@wizardone

Use case

Hello folks, while migrating a lambda function to use powertools I really needed to have slightly more control over the idempotency key and after reading the docs and code extensively I don't think there is currently support for what I'm after. In a nutshell:

  • I would like to be able to configure whether or not I apply a hash function
    • That would assume I have preconfigured the required idempotency key to what I need and the library can just use it.
  • Be able to specify the digest for the generated hash
    • Allows me to specify hex if I wanted to.

Both options are interconnected for my particular use case. In other words if I'm able to specify the digest, then powertools will give me everything I need. Otherwise I would like to have the freedom to generate what I need myself and use it.

I appreciate if that level of control is not something that you'd like to provide, my use cases are very specific and powertools already gives flexibility. Still, let me know if either option makes sense to you. Thank you.

Solution/User Experience

Implementing giving the option to set digest can be as low touch as adding a new configuration option to the IdempotencyConfig:
pseudo code:

baseDigest: 'hex' // default to base64
private generateHash(data: string): string {
    const hash: Hash = createHash(this.hashFunction);
    hash.update(data);
    const digest = this.baseDigest ?? 'base64'
    return hash.digest(digest);
}

Implementing the option to hash or not might be slightly more involving and I'm not aware of the nuances of the code, but a very rough implementation could be:

private getHashedIdempotencyKey(data: JSONValue): string {
    const payload = this.eventKeyJmesPath
      ? (search(
          this.eventKeyJmesPath,
          data,
          this.#jmesPathOptions
        ) as JSONValue)
      : data;

    if (BasePersistenceLayer.isMissingIdempotencyKey(payload)) {
      if (this.throwOnNoIdempotencyKey) {
        throw new IdempotencyKeyError(
          'No data found to create a hashed idempotency_key'
        );
      }
      console.warn(
        `No value found for idempotency_key. jmespath: ${this.eventKeyJmesPath}`
      );
    }
    const idempotencyHash = this.suppliedHashedKey ? this.generateHash(JSON.stringify(deepSort(payload))) : this.suppliedHashedKey
    return `${this.idempotencyKeyPrefix}#${idempotencyHash}`;
 }

Alternative solutions

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