-
Notifications
You must be signed in to change notification settings - Fork 153
/
Copy pathmakeIdempotent.ts
110 lines (101 loc) · 3.05 KB
/
makeIdempotent.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import type { Context, Handler } from 'aws-lambda';
import type {
AnyFunction,
ItempotentFunctionOptions,
IdempotencyLambdaHandlerOptions,
} from './types/IdempotencyOptions.js';
import { IdempotencyHandler } from './IdempotencyHandler.js';
import { IdempotencyConfig } from './IdempotencyConfig.js';
const isContext = (arg: unknown): arg is Context => {
return (
arg !== undefined &&
arg !== null &&
typeof arg === 'object' &&
'getRemainingTimeInMillis' in arg
);
};
const isFnHandler = (
fn: AnyFunction,
args: Parameters<AnyFunction>
): fn is Handler => {
// get arguments of function
return (
fn !== undefined &&
fn !== null &&
typeof fn === 'function' &&
isContext(args[1])
);
};
const isOptionsWithDataIndexArgument = (
options: unknown
): options is IdempotencyLambdaHandlerOptions & {
dataIndexArgument: number;
} => {
return (
options !== undefined &&
options !== null &&
typeof options === 'object' &&
'dataIndexArgument' in options
);
};
/**
* Use function wrapper to make your function idempotent.
* @example
* ```ts
* // this is your processing function with an example record { transactionId: '123', foo: 'bar' }
* const processRecord = (record: Record<string, unknown>): any => {
* // you custom processing logic
* return result;
* };
*
* // we use wrapper to make processing function idempotent with DynamoDBPersistenceLayer
* const processIdempotently = makeIdempotent(processRecord, {
* persistenceStore: new DynamoDBPersistenceLayer()
* dataKeywordArgument: 'transactionId', // keyword argument to hash the payload and the result
* });
*
* export const handler = async (
* _event: EventRecords,
* _context: Context
* ): Promise<void> => {
* for (const record of _event.records) {
* const result = await processIdempotently(record);
* // do something with the result
* }
*
* return Promise.resolve();
* };
*
* ```
*/
// eslint-disable-next-line func-style
function makeIdempotent<Func extends AnyFunction>(
fn: Func,
options: ItempotentFunctionOptions<Parameters<Func>>
): (...args: Parameters<Func>) => ReturnType<Func> {
const { persistenceStore, config } = options;
const idempotencyConfig = config ? config : new IdempotencyConfig({});
if (!idempotencyConfig.isEnabled()) return fn;
return function (this: Handler, ...args: Parameters<Func>): ReturnType<Func> {
let functionPayloadToBeHashed;
if (isFnHandler(fn, args)) {
idempotencyConfig.registerLambdaContext(args[1]);
functionPayloadToBeHashed = args[0];
} else {
if (isOptionsWithDataIndexArgument(options)) {
functionPayloadToBeHashed = args[options.dataIndexArgument];
} else {
functionPayloadToBeHashed = args[0];
}
}
return new IdempotencyHandler({
functionToMakeIdempotent: fn,
idempotencyConfig: idempotencyConfig,
persistenceStore: persistenceStore,
functionArguments: args,
functionPayloadToBeHashed,
thisArg: this,
}).handle() as ReturnType<Func>;
};
}
export { makeIdempotent };