forked from aws-powertools/powertools-lambda-typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessPartialResponse.ts
44 lines (38 loc) · 1.17 KB
/
processPartialResponse.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 {
BasePartialBatchProcessor,
BaseRecord,
BatchProcessingOptions,
EventType,
PartialItemFailureResponse,
} from '.';
/**
* Higher level function to handle batch event processing
* @param event Lambda's original event
* @param recordHandler Callable function to process each record from the batch
* @param processor Batch processor to handle partial failure cases
* @returns Lambda Partial Batch Response
*/
const processPartialResponse = async (
event: { Records: BaseRecord[] },
recordHandler: CallableFunction,
processor: BasePartialBatchProcessor,
options?: BatchProcessingOptions
): Promise<PartialItemFailureResponse> => {
if (!event.Records) {
const eventTypes: string = Object.values(EventType).toString();
throw new Error(
'Failed to convert event to record batch for processing.\nPlease ensure batch event is a valid ' +
eventTypes +
' event.'
);
}
const records = event['Records'];
if (options) {
processor.register(records, recordHandler, options);
} else {
processor.register(records, recordHandler);
}
await processor.process();
return processor.response();
};
export { processPartialResponse };