-
Notifications
You must be signed in to change notification settings - Fork 154
/
Copy pathIdempotencyConfig.ts
44 lines (39 loc) · 1.58 KB
/
IdempotencyConfig.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { EnvironmentVariablesService } from './config';
import type { Context } from 'aws-lambda';
import type { IdempotencyConfigOptions } from './types';
class IdempotencyConfig {
public eventKeyJmesPath: string;
public expiresAfterSeconds: number;
public hashFunction: string;
public lambdaContext?: Context;
public maxLocalCacheSize: number;
public payloadValidationJmesPath?: string;
public throwOnNoIdempotencyKey: boolean;
public useLocalCache: boolean;
readonly #envVarsService: EnvironmentVariablesService;
readonly #enabled: boolean = true;
public constructor(config: IdempotencyConfigOptions) {
this.eventKeyJmesPath = config.eventKeyJmesPath ?? '';
this.payloadValidationJmesPath = config.payloadValidationJmesPath;
this.throwOnNoIdempotencyKey = config.throwOnNoIdempotencyKey ?? false;
this.expiresAfterSeconds = config.expiresAfterSeconds ?? 3600; // 1 hour default
this.useLocalCache = config.useLocalCache ?? false;
this.maxLocalCacheSize = config.maxLocalCacheSize ?? 1000;
this.hashFunction = config.hashFunction ?? 'md5';
this.lambdaContext = config.lambdaContext;
this.#envVarsService = new EnvironmentVariablesService();
this.#enabled = this.#envVarsService.getIdempotencyEnabled();
}
/**
* Determines if the idempotency feature is enabled.
*
* @returns {boolean} Returns true if the idempotency feature is enabled.
*/
public isEnabled(): boolean {
return this.#enabled;
}
public registerLambdaContext(context: Context): void {
this.lambdaContext = context;
}
}
export { IdempotencyConfig };