1
1
import { BinaryToTextEncoding , createHash , Hash } from 'crypto' ;
2
2
import { IdempotencyRecordStatus } from '../types/IdempotencyRecordStatus' ;
3
- import { EnvironmentVariablesService } from '../EnvironmentVariablesService' ;
3
+ import type { PersistenceLayerConfigureOptions } from '../types/PersistenceLayer' ;
4
+ import { EnvironmentVariablesService } from '../config' ;
4
5
import { IdempotencyRecord } from './IdempotencyRecord' ;
5
6
import { PersistenceLayerInterface } from './PersistenceLayerInterface' ;
6
7
@@ -10,22 +11,32 @@ abstract class PersistenceLayer implements PersistenceLayerInterface {
10
11
private envVarsService ! : EnvironmentVariablesService ;
11
12
12
13
private expiresAfterSeconds : number ;
13
-
14
- private functionName : string = '' ;
15
-
14
+
16
15
private hashDigest : BinaryToTextEncoding ;
17
-
16
+
18
17
private hashFunction : string ;
18
+
19
+ private idempotencyKeyPrefix : string ;
19
20
20
21
public constructor ( ) {
21
22
this . setEnvVarsService ( ) ;
22
23
this . expiresAfterSeconds = 60 * 60 ; //one hour is the default expiration
23
24
this . hashFunction = 'md5' ;
24
25
this . hashDigest = 'base64' ;
25
-
26
+ this . idempotencyKeyPrefix = this . getEnvVarsService ( ) . getFunctionName ( ) ;
27
+
26
28
}
27
- public configure ( functionName : string = '' ) : void {
28
- this . functionName = this . getEnvVarsService ( ) . getLambdaFunctionName ( ) + '.' + functionName ;
29
+
30
+ /**
31
+ * Configures the persistence layer by passing the name of the idempotent function. This will be used
32
+ * in the prefix of the idempotency key
33
+ *
34
+ * @param {PersistenceLayerConfigureOptions } options - configuration object for the persistence layer
35
+ */
36
+ public configure ( options ?: PersistenceLayerConfigureOptions ) : void {
37
+ if ( options ?. functionName && options . functionName . trim ( ) !== '' ) {
38
+ this . idempotencyKeyPrefix = `${ this . idempotencyKeyPrefix } .${ options . functionName } ` ;
39
+ }
29
40
}
30
41
31
42
/**
@@ -136,7 +147,7 @@ abstract class PersistenceLayer implements PersistenceLayerInterface {
136
147
console . warn ( 'No data found for idempotency key' ) ;
137
148
}
138
149
139
- return this . functionName + '#' + this . generateHash ( JSON . stringify ( data ) ) ;
150
+ return ` ${ this . idempotencyKeyPrefix } # ${ this . generateHash ( JSON . stringify ( data ) ) } ` ;
140
151
}
141
152
142
153
/**
0 commit comments