Skip to content

Commit 7f8a1f4

Browse files
committed
feat(idempotency): add custom idempotency key support with keyPrefix option
1 parent 4479fa6 commit 7f8a1f4

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

packages/idempotency/src/IdempotencyHandler.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ export class IdempotencyHandler<Func extends AnyFunction> {
5151
* Idempotency configuration options.
5252
*/
5353
readonly #idempotencyConfig: IdempotencyConfig;
54+
/**
55+
* Custom prefix to be used when generating the idempotency key.
56+
*/
57+
readonly #keyPrefix: string | undefined;
5458
/**
5559
* Persistence layer used to store the idempotency records.
5660
*/
@@ -69,18 +73,21 @@ export class IdempotencyHandler<Func extends AnyFunction> {
6973
idempotencyConfig,
7074
functionArguments,
7175
persistenceStore,
76+
keyPrefix,
7277
thisArg,
7378
} = options;
7479
this.#functionToMakeIdempotent = functionToMakeIdempotent;
7580
this.#functionPayloadToBeHashed = functionPayloadToBeHashed;
7681
this.#idempotencyConfig = idempotencyConfig;
82+
this.#keyPrefix = keyPrefix;
7783
this.#functionArguments = functionArguments;
7884
this.#thisArg = thisArg;
7985

8086
this.#persistenceStore = persistenceStore;
8187

8288
this.#persistenceStore.configure({
8389
config: this.#idempotencyConfig,
90+
keyPrefix: this.#keyPrefix,
8491
});
8592
}
8693

packages/idempotency/src/makeIdempotent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function makeIdempotent<Func extends AnyFunction>(
7979
fn: Func,
8080
options: ItempotentFunctionOptions<Parameters<Func>>
8181
): (...args: Parameters<Func>) => ReturnType<Func> {
82-
const { persistenceStore, config } = options;
82+
const { persistenceStore, config, keyPrefix } = options;
8383
const idempotencyConfig = config ? config : new IdempotencyConfig({});
8484

8585
if (!idempotencyConfig.isEnabled()) return fn;
@@ -102,6 +102,7 @@ function makeIdempotent<Func extends AnyFunction>(
102102
functionToMakeIdempotent: fn,
103103
idempotencyConfig: idempotencyConfig,
104104
persistenceStore: persistenceStore,
105+
keyPrefix: keyPrefix,
105106
functionArguments: args,
106107
functionPayloadToBeHashed,
107108
thisArg: this,

packages/idempotency/src/persistence/BasePersistenceLayer.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,15 @@ abstract class BasePersistenceLayer implements BasePersistenceLayerInterface {
4848
*
4949
* @param {BasePersistenceLayerConfigureOptions} options - configuration object for the persistence layer
5050
*/
51-
public configure(options: BasePersistenceLayerOptions): void {
52-
// Extracting the idempotency config from the config object for easier access
53-
const { config: idempotencyConfig } = options;
54-
55-
if (options?.functionName && options.functionName.trim() !== '') {
56-
this.idempotencyKeyPrefix = `${this.idempotencyKeyPrefix}.${options.functionName}`;
57-
}
51+
public configure(options: BasePersistenceLayerOptions): void {
52+
// Extracting the idempotency configuration from the options for easier access
53+
const { config: idempotencyConfig, keyPrefix, functionName } = options;
54+
55+
if (keyPrefix?.trim()) {
56+
this.idempotencyKeyPrefix = keyPrefix.trim();
57+
} else if (functionName?.trim()) {
58+
this.idempotencyKeyPrefix = `${this.idempotencyKeyPrefix}.${functionName.trim()}`;
59+
}
5860

5961
// Prevent reconfiguration
6062
if (this.configured) {

packages/idempotency/src/types/IdempotencyOptions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import type { IdempotencyRecord } from '../persistence/IdempotencyRecord.js';
1919
type IdempotencyLambdaHandlerOptions = {
2020
persistenceStore: BasePersistenceLayer;
2121
config?: IdempotencyConfig;
22+
keyPrefix?: string;
2223
};
2324

2425
/**
@@ -137,6 +138,10 @@ type IdempotencyHandlerOptions = {
137138
* Idempotency configuration options.
138139
*/
139140
idempotencyConfig: IdempotencyConfig;
141+
/**
142+
* The custom idempotency key prefix.
143+
*/
144+
keyPrefix?: string;
140145
/**
141146
* Persistence layer used to store the idempotency records.
142147
*/

0 commit comments

Comments
 (0)