-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathIdempotencyConfig.ts
80 lines (75 loc) · 2.59 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { EnvironmentVariablesService } from './config';
import type { Context } from 'aws-lambda';
import type { IdempotencyConfigOptions } from './types';
/**
* Configuration for the idempotency feature.
*/
class IdempotencyConfig {
/**
* The JMESPath expression used to extract the idempotency key from the event.
* @default ''
*/
public eventKeyJmesPath: string;
/**
* The number of seconds the idempotency key is valid.
* @default 3600 (1 hour)
*/
public expiresAfterSeconds: number;
/**
* The hash function used to generate the idempotency key.
* @see https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options
* @default 'md5'
*/
public hashFunction: string;
/**
* The lambda context object.
*/
public lambdaContext?: Context;
/**
* The maximum number of items to store in the local cache.
* @default 1000
*/
public maxLocalCacheSize: number;
/**
* The JMESPath expression used to extract the payload to validate.
*/
public payloadValidationJmesPath?: string;
/**
* Throw an error if the idempotency key is not found in the event.
* In some cases, you may want to allow the request to continue without idempotency.
* If set to false and idempotency key is not found, the request will continue without idempotency.
* @default false
*/
public throwOnNoIdempotencyKey: boolean;
/**
* Use the local cache to store idempotency keys.
* @see {@link LRUCache}
*/
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 };