File tree Expand file tree Collapse file tree 4 files changed +23
-8
lines changed Expand file tree Collapse file tree 4 files changed +23
-8
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,10 @@ export class IdempotencyHandler<Func extends AnyFunction> {
51
51
* Idempotency configuration options.
52
52
*/
53
53
readonly #idempotencyConfig: IdempotencyConfig ;
54
+ /**
55
+ * Custom prefix to be used when generating the idempotency key.
56
+ */
57
+ readonly #keyPrefix: string | undefined ;
54
58
/**
55
59
* Persistence layer used to store the idempotency records.
56
60
*/
@@ -69,18 +73,21 @@ export class IdempotencyHandler<Func extends AnyFunction> {
69
73
idempotencyConfig,
70
74
functionArguments,
71
75
persistenceStore,
76
+ keyPrefix,
72
77
thisArg,
73
78
} = options ;
74
79
this . #functionToMakeIdempotent = functionToMakeIdempotent ;
75
80
this . #functionPayloadToBeHashed = functionPayloadToBeHashed ;
76
81
this . #idempotencyConfig = idempotencyConfig ;
82
+ this . #keyPrefix = keyPrefix ;
77
83
this . #functionArguments = functionArguments ;
78
84
this . #thisArg = thisArg ;
79
85
80
86
this . #persistenceStore = persistenceStore ;
81
87
82
88
this . #persistenceStore. configure ( {
83
89
config : this . #idempotencyConfig,
90
+ keyPrefix : this . #keyPrefix,
84
91
} ) ;
85
92
}
86
93
Original file line number Diff line number Diff line change @@ -79,7 +79,7 @@ function makeIdempotent<Func extends AnyFunction>(
79
79
fn : Func ,
80
80
options : ItempotentFunctionOptions < Parameters < Func > >
81
81
) : ( ...args : Parameters < Func > ) => ReturnType < Func > {
82
- const { persistenceStore, config } = options ;
82
+ const { persistenceStore, config, keyPrefix } = options ;
83
83
const idempotencyConfig = config ? config : new IdempotencyConfig ( { } ) ;
84
84
85
85
if ( ! idempotencyConfig . isEnabled ( ) ) return fn ;
@@ -102,6 +102,7 @@ function makeIdempotent<Func extends AnyFunction>(
102
102
functionToMakeIdempotent : fn ,
103
103
idempotencyConfig : idempotencyConfig ,
104
104
persistenceStore : persistenceStore ,
105
+ keyPrefix : keyPrefix ,
105
106
functionArguments : args ,
106
107
functionPayloadToBeHashed,
107
108
thisArg : this ,
Original file line number Diff line number Diff line change @@ -48,13 +48,15 @@ abstract class BasePersistenceLayer implements BasePersistenceLayerInterface {
48
48
*
49
49
* @param {BasePersistenceLayerConfigureOptions } options - configuration object for the persistence layer
50
50
*/
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
+ }
58
60
59
61
// Prevent reconfiguration
60
62
if ( this . configured ) {
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ import type { IdempotencyRecord } from '../persistence/IdempotencyRecord.js';
19
19
type IdempotencyLambdaHandlerOptions = {
20
20
persistenceStore : BasePersistenceLayer ;
21
21
config ?: IdempotencyConfig ;
22
+ keyPrefix ?: string ;
22
23
} ;
23
24
24
25
/**
@@ -137,6 +138,10 @@ type IdempotencyHandlerOptions = {
137
138
* Idempotency configuration options.
138
139
*/
139
140
idempotencyConfig : IdempotencyConfig ;
141
+ /**
142
+ * The custom idempotency key prefix.
143
+ */
144
+ keyPrefix ?: string ;
140
145
/**
141
146
* Persistence layer used to store the idempotency records.
142
147
*/
You can’t perform that action at this time.
0 commit comments