Skip to content

Commit 2a5046e

Browse files
dreamorosiam29d
andauthored
docs(batch): review API docs & README (#2562)
Co-authored-by: Alexander Schueren <[email protected]>
1 parent f62ad63 commit 2a5046e

25 files changed

+634
-311
lines changed

docs/utilities/batch.md

+17-17
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ Processing batches from SQS works in three stages:
113113

114114
=== "index.ts"
115115

116-
```typescript hl_lines="1-5 14 17 29-31"
116+
```typescript hl_lines="1-5 9 12 21-23"
117117
--8<--
118-
examples/snippets/batch/gettingStartedSQS.ts::16
119-
examples/snippets/batch/gettingStartedSQS.ts:18:29
120-
examples/snippets/batch/gettingStartedSQS.ts:31:34
118+
examples/snippets/batch/gettingStartedSQS.ts::11
119+
examples/snippets/batch/gettingStartedSQS.ts:13:21
120+
examples/snippets/batch/gettingStartedSQS.ts:23:25
121121
--8<--
122122
```
123123

@@ -144,7 +144,7 @@ Processing batches from SQS works in three stages:
144144
When using [SQS FIFO queues](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/FIFO-queues.html){target="_blank"}, we will stop processing messages after the first failure, and return all failed and unprocessed messages in `batchItemFailures`.
145145
This helps preserve the ordering of messages in your queue.
146146

147-
```typescript hl_lines="1-4 13 28-30"
147+
```typescript hl_lines="1-4 8 20-22"
148148
--8<-- "examples/snippets/batch/gettingStartedSQSFifo.ts"
149149
```
150150

@@ -167,11 +167,11 @@ Processing batches from Kinesis works in three stages:
167167

168168
=== "index.ts"
169169

170-
```typescript hl_lines="1-5 14 17 27-29"
170+
```typescript hl_lines="1-5 9 12 19-21"
171171
--8<-- "examples/snippets/batch/gettingStartedKinesis.ts"
172172
```
173173

174-
1. **Step 1**. Creates a partial failure batch processor for Kinesis Data Streams. See [partial failure mechanics for details](#partial-failure-mechanics)
174+
1. Creates a partial failure batch processor for Kinesis Data Streams. See [partial failure mechanics for details](#partial-failure-mechanics)
175175

176176
=== "Sample response"
177177

@@ -200,11 +200,11 @@ Processing batches from DynamoDB Streams works in three stages:
200200

201201
=== "index.ts"
202202

203-
```typescript hl_lines="1-5 14 17 32-34"
203+
```typescript hl_lines="1-5 9 12 24-26"
204204
--8<-- "examples/snippets/batch/gettingStartedDynamoDBStreams.ts"
205205
```
206206

207-
1. **Step 1**. Creates a partial failure batch processor for DynamoDB Streams. See [partial failure mechanics for details](#partial-failure-mechanics)
207+
1. Creates a partial failure batch processor for DynamoDB Streams. See [partial failure mechanics for details](#partial-failure-mechanics)
208208

209209
=== "Sample response"
210210

@@ -226,17 +226,17 @@ By default, we catch any exception raised by your record handler function. This
226226

227227
=== "Sample error handling with custom exception"
228228

229-
```typescript hl_lines="30"
229+
```typescript hl_lines="25"
230230
--8<--
231-
examples/snippets/batch/gettingStartedErrorHandling.ts::29
232-
examples/snippets/batch/gettingStartedErrorHandling.ts:31:38
233-
examples/snippets/batch/gettingStartedErrorHandling.ts:40:43
231+
examples/snippets/batch/gettingStartedErrorHandling.ts::24
232+
examples/snippets/batch/gettingStartedErrorHandling.ts:26:30
233+
examples/snippets/batch/gettingStartedErrorHandling.ts:32:
234234
--8<--
235235
```
236236

237237
1. Any exception works here. See [extending BatchProcessorSync section, if you want to override this behavior.](#extending-batchprocessor)
238238

239-
2. Exceptions raised in `record_handler` will propagate to `process_partial_response`. <br/><br/> We catch them and include each failed batch item identifier in the response dictionary (see `Sample response` tab).
239+
2. Exceptions raised in `recordHandler` will propagate to `process_partial_response`. <br/><br/> We catch them and include each failed batch item identifier in the response dictionary (see `Sample response` tab).
240240

241241
=== "Sample response"
242242

@@ -397,7 +397,7 @@ Use the `BatchProcessor` directly in your function to access a list of all retur
397397
* **When successful**. We will include a tuple with `success`, the result of `recordHandler`, and the batch record
398398
* **When failed**. We will include a tuple with `fail`, exception as a string, and the batch record
399399

400-
```typescript hl_lines="25 27-28 30-33 38" title="Accessing processed messages"
400+
```typescript hl_lines="17 19-20 23 28" title="Accessing processed messages"
401401
--8<-- "examples/snippets/batch/accessProcessedMessages.ts"
402402
```
403403

@@ -410,7 +410,7 @@ Within your `recordHandler` function, you might need access to the Lambda contex
410410

411411
We can automatically inject the [Lambda context](https://docs.aws.amazon.com/lambda/latest/dg/typescript-context.html){target="_blank"} into your `recordHandler` as optional second argument if you register it when using `BatchProcessorSync` or the `processPartialResponseSync` function.
412412

413-
```typescript hl_lines="17 35"
413+
```typescript hl_lines="12 27"
414414
--8<-- "examples/snippets/batch/accessLambdaContext.ts"
415415
```
416416

@@ -425,7 +425,7 @@ For these scenarios, you can subclass `BatchProcessor` and quickly override `suc
425425

426426
Let's suppose you'd like to add a metric named `BatchRecordFailures` for each batch record that failed processing
427427

428-
```typescript hl_lines="3 20 24 31 37" title="Extending failure handling mechanism in BatchProcessor"
428+
```typescript hl_lines="3 15 19 26 32" title="Extending failure handling mechanism in BatchProcessor"
429429
--8<-- "examples/snippets/batch/extendingFailure.ts"
430430
```
431431

examples/snippets/batch/accessLambdaContext.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import {
44
processPartialResponse,
55
} from '@aws-lambda-powertools/batch';
66
import { Logger } from '@aws-lambda-powertools/logger';
7-
import type {
8-
SQSEvent,
9-
SQSRecord,
10-
Context,
11-
SQSBatchResponse,
12-
} from 'aws-lambda';
7+
import type { SQSRecord, Context, SQSHandler } from 'aws-lambda';
138

149
const processor = new BatchProcessor(EventType.SQS);
1510
const logger = new Logger();
@@ -27,11 +22,7 @@ const recordHandler = (record: SQSRecord, lambdaContext?: Context): void => {
2722
}
2823
};
2924

30-
export const handler = async (
31-
event: SQSEvent,
32-
context: Context
33-
): Promise<SQSBatchResponse> => {
34-
return processPartialResponse(event, recordHandler, processor, {
25+
export const handler: SQSHandler = async (event, context) =>
26+
processPartialResponse(event, recordHandler, processor, {
3527
context,
3628
});
37-
};

examples/snippets/batch/accessProcessedMessages.ts

+3-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { BatchProcessor, EventType } from '@aws-lambda-powertools/batch';
22
import { Logger } from '@aws-lambda-powertools/logger';
3-
import type {
4-
SQSEvent,
5-
SQSRecord,
6-
Context,
7-
SQSBatchResponse,
8-
} from 'aws-lambda';
3+
import type { SQSRecord, SQSHandler } from 'aws-lambda';
94

105
const processor = new BatchProcessor(EventType.SQS);
116
const logger = new Logger();
@@ -18,19 +13,14 @@ const recordHandler = (record: SQSRecord): void => {
1813
}
1914
};
2015

21-
export const handler = async (
22-
event: SQSEvent,
23-
context: Context
24-
): Promise<SQSBatchResponse> => {
16+
export const handler: SQSHandler = async (event, context) => {
2517
const batch = event.Records; // (1)!
2618

2719
processor.register(batch, recordHandler, { context }); // (2)!
2820
const processedMessages = await processor.process();
2921

3022
for (const message of processedMessages) {
31-
const status: 'success' | 'fail' = message[0];
32-
const error = message[1];
33-
const record = message[2];
23+
const [status, error, record] = message;
3424

3525
logger.info('Processed record', { status, record, error });
3626
}

examples/snippets/batch/advancedTracingRecordHandler.ts

+5-12
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@ import {
66
import { Tracer } from '@aws-lambda-powertools/tracer';
77
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer/middleware';
88
import middy from '@middy/core';
9-
import type {
10-
SQSEvent,
11-
SQSRecord,
12-
Context,
13-
SQSBatchResponse,
14-
} from 'aws-lambda';
9+
import type { SQSRecord, SQSHandler, SQSEvent } from 'aws-lambda';
1510

1611
const processor = new BatchProcessor(EventType.SQS);
1712
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
@@ -35,10 +30,8 @@ const recordHandler = async (record: SQSRecord): Promise<void> => {
3530
subsegment?.close(); // (3)!
3631
};
3732

38-
export const handler = middy(
39-
async (event: SQSEvent, context: Context): Promise<SQSBatchResponse> => {
40-
return processPartialResponse(event, recordHandler, processor, {
41-
context,
42-
});
43-
}
33+
export const handler: SQSHandler = middy(async (event: SQSEvent, context) =>
34+
processPartialResponse(event, recordHandler, processor, {
35+
context,
36+
})
4437
).use(captureLambdaHandler(tracer));

examples/snippets/batch/customPartialProcessor.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import type {
1414
FailureResponse,
1515
BaseRecord,
1616
} from '@aws-lambda-powertools/batch/types';
17-
import type { SQSEvent, Context, SQSBatchResponse } from 'aws-lambda';
17+
import type { SQSHandler } from 'aws-lambda';
1818

1919
const tableName = process.env.TABLE_NAME || 'table-not-found';
2020

@@ -89,11 +89,7 @@ const recordHandler = (): number => {
8989
return Math.floor(randomInt(1, 10));
9090
};
9191

92-
export const handler = async (
93-
event: SQSEvent,
94-
context: Context
95-
): Promise<SQSBatchResponse> => {
96-
return processPartialResponse(event, recordHandler, processor, {
92+
export const handler: SQSHandler = async (event, context) =>
93+
processPartialResponse(event, recordHandler, processor, {
9794
context,
9895
});
99-
};

examples/snippets/batch/extendingFailure.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ import type {
99
EventSourceDataClassTypes,
1010
} from '@aws-lambda-powertools/batch/types';
1111
import { Logger } from '@aws-lambda-powertools/logger';
12-
import type {
13-
SQSEvent,
14-
SQSRecord,
15-
Context,
16-
SQSBatchResponse,
17-
} from 'aws-lambda';
12+
import type { SQSRecord, SQSHandler } from 'aws-lambda';
1813

1914
class MyProcessor extends BatchProcessor {
2015
#metrics: Metrics;
@@ -45,11 +40,7 @@ const recordHandler = (record: SQSRecord): void => {
4540
}
4641
};
4742

48-
export const handler = async (
49-
event: SQSEvent,
50-
context: Context
51-
): Promise<SQSBatchResponse> => {
52-
return processPartialResponse(event, recordHandler, processor, {
43+
export const handler: SQSHandler = async (event, context) =>
44+
processPartialResponse(event, recordHandler, processor, {
5345
context,
5446
});
55-
};

examples/snippets/batch/gettingStartedDynamoDBStreams.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import {
44
processPartialResponse,
55
} from '@aws-lambda-powertools/batch';
66
import { Logger } from '@aws-lambda-powertools/logger';
7-
import type {
8-
DynamoDBStreamEvent,
9-
DynamoDBRecord,
10-
Context,
11-
DynamoDBBatchResponse,
12-
} from 'aws-lambda';
7+
import type { DynamoDBRecord, DynamoDBStreamHandler } from 'aws-lambda';
138

149
const processor = new BatchProcessor(EventType.DynamoDBStreams); // (1)!
1510
const logger = new Logger();
@@ -25,11 +20,7 @@ const recordHandler = async (record: DynamoDBRecord): Promise<void> => {
2520
}
2621
};
2722

28-
export const handler = async (
29-
event: DynamoDBStreamEvent,
30-
context: Context
31-
): Promise<DynamoDBBatchResponse> => {
32-
return processPartialResponse(event, recordHandler, processor, {
23+
export const handler: DynamoDBStreamHandler = async (event, context) =>
24+
processPartialResponse(event, recordHandler, processor, {
3325
context,
3426
});
35-
};

examples/snippets/batch/gettingStartedErrorHandling.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import {
44
processPartialResponse,
55
} from '@aws-lambda-powertools/batch';
66
import { Logger } from '@aws-lambda-powertools/logger';
7-
import type {
8-
SQSEvent,
9-
SQSRecord,
10-
Context,
11-
SQSBatchResponse,
12-
} from 'aws-lambda';
7+
import type { SQSRecord, SQSHandler } from 'aws-lambda';
138

149
const processor = new BatchProcessor(EventType.SQS);
1510
const logger = new Logger();
@@ -32,12 +27,8 @@ const recordHandler = async (record: SQSRecord): Promise<void> => {
3227
}
3328
};
3429

35-
export const handler = async (
36-
event: SQSEvent,
37-
context: Context
38-
): Promise<SQSBatchResponse> => {
30+
export const handler: SQSHandler = async (event, context) =>
3931
// prettier-ignore
40-
return processPartialResponse(event, recordHandler, processor, { // (2)!
32+
processPartialResponse(event, recordHandler, processor, { // (2)!
4133
context,
4234
});
43-
};

examples/snippets/batch/gettingStartedKinesis.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import {
44
processPartialResponse,
55
} from '@aws-lambda-powertools/batch';
66
import { Logger } from '@aws-lambda-powertools/logger';
7-
import type {
8-
KinesisStreamEvent,
9-
KinesisStreamRecord,
10-
Context,
11-
KinesisStreamBatchResponse,
12-
} from 'aws-lambda';
7+
import type { KinesisStreamHandler, KinesisStreamRecord } from 'aws-lambda';
138

149
const processor = new BatchProcessor(EventType.KinesisDataStreams); // (1)!
1510
const logger = new Logger();
@@ -20,11 +15,7 @@ const recordHandler = async (record: KinesisStreamRecord): Promise<void> => {
2015
logger.info('Processed item', { item: payload });
2116
};
2217

23-
export const handler = async (
24-
event: KinesisStreamEvent,
25-
context: Context
26-
): Promise<KinesisStreamBatchResponse> => {
27-
return processPartialResponse(event, recordHandler, processor, {
18+
export const handler: KinesisStreamHandler = async (event, context) =>
19+
processPartialResponse(event, recordHandler, processor, {
2820
context,
2921
});
30-
};

examples/snippets/batch/gettingStartedSQS.ts

+4-12
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ import {
44
processPartialResponse,
55
} from '@aws-lambda-powertools/batch';
66
import { Logger } from '@aws-lambda-powertools/logger';
7-
import type {
8-
SQSEvent,
9-
SQSRecord,
10-
Context,
11-
SQSBatchResponse,
12-
} from 'aws-lambda';
7+
import type { SQSRecord, SQSHandler } from 'aws-lambda';
138

149
const processor = new BatchProcessor(EventType.SQS); // (1)!
1510
const logger = new Logger();
@@ -23,13 +18,10 @@ const recordHandler = async (record: SQSRecord): Promise<void> => { // (2)!
2318
}
2419
};
2520

26-
export const handler = async (
27-
event: SQSEvent,
28-
context: Context
29-
): Promise<SQSBatchResponse> => {
21+
export const handler: SQSHandler = async (event, context) =>
3022
// prettier-ignore
31-
return processPartialResponse(event, recordHandler, processor, { // (3)!
23+
processPartialResponse(event, recordHandler, processor, { // (3)!
3224
context,
3325
});
34-
};
26+
3527
export { processor };

examples/snippets/batch/gettingStartedSQSFifo.ts

+3-12
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,7 @@ import {
33
processPartialResponseSync,
44
} from '@aws-lambda-powertools/batch';
55
import { Logger } from '@aws-lambda-powertools/logger';
6-
import type {
7-
SQSEvent,
8-
SQSRecord,
9-
Context,
10-
SQSBatchResponse,
11-
} from 'aws-lambda';
6+
import type { SQSHandler, SQSRecord } from 'aws-lambda';
127

138
const processor = new SqsFifoPartialProcessor(); // (1)!
149
const logger = new Logger();
@@ -21,11 +16,7 @@ const recordHandler = (record: SQSRecord): void => {
2116
}
2217
};
2318

24-
export const handler = async (
25-
event: SQSEvent,
26-
context: Context
27-
): Promise<SQSBatchResponse> => {
28-
return processPartialResponseSync(event, recordHandler, processor, {
19+
export const handler: SQSHandler = async (event, context) =>
20+
processPartialResponseSync(event, recordHandler, processor, {
2921
context,
3022
});
31-
};

examples/snippets/batch/testingYourCode.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('Function tests', () => {
3636
};
3737

3838
// Act
39-
const response = await handler(sqsEvent, context);
39+
const response = await handler(sqsEvent, context, () => {});
4040

4141
// Assess
4242
expect(response).toEqual(expectedResponse);

0 commit comments

Comments
 (0)