Skip to content

Commit 8436fa9

Browse files
authored
style(batch): apply stricter linting (#4547)
1 parent e44f3e7 commit 8436fa9

File tree

6 files changed

+54
-24
lines changed

6 files changed

+54
-24
lines changed

packages/batch/src/BasePartialProcessor.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,7 @@ abstract class BasePartialProcessor {
239239
/**
240240
* Processes records in parallel using `Promise.all`.
241241
*/
242-
async #processRecordsInParallel(): Promise<
243-
(SuccessResponse | FailureResponse)[]
244-
> {
242+
#processRecordsInParallel(): Promise<(SuccessResponse | FailureResponse)[]> {
245243
return Promise.all(
246244
this.records.map((record) => this.processRecord(record))
247245
);

packages/batch/src/BatchProcessorSync.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ import type { BaseRecord, FailureResponse, SuccessResponse } from './types.js';
8888
*
8989
* @param _record The record to be processed
9090
*/
91-
public async processRecord(
91+
public processRecord(
9292
_record: BaseRecord
9393
): Promise<SuccessResponse | FailureResponse> {
9494
throw new BatchProcessingError('Not implemented. Use process() instead.');

packages/batch/tests/helpers/handlers.ts

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { setTimeout } from 'node:timers/promises';
12
import type {
3+
AttributeValue,
24
Context,
35
DynamoDBRecord,
46
KinesisStreamRecord,
@@ -8,50 +10,70 @@ import type {
810
const sqsRecordHandler = (record: SQSRecord): string => {
911
const body = record.body;
1012
if (body.includes('fail')) {
11-
throw Error('Failed to process record.');
13+
throw new Error('Failed to process record.');
1214
}
1315

1416
return body;
1517
};
1618

17-
const asyncSqsRecordHandler = async (record: SQSRecord): Promise<string> =>
18-
Promise.resolve(sqsRecordHandler(record));
19+
const asyncSqsRecordHandler = async (record: SQSRecord): Promise<string> => {
20+
const body = record.body;
21+
if (body.includes('fail')) {
22+
throw new Error('Failed to process record.');
23+
}
24+
await setTimeout(1); // simulate some processing time
25+
return body;
26+
};
1927

2028
const kinesisRecordHandler = (record: KinesisStreamRecord): string => {
2129
const body = record.kinesis.data;
2230
if (body.includes('fail')) {
23-
throw Error('Failed to process record.');
31+
throw new Error('Failed to process record.');
2432
}
2533

2634
return body;
2735
};
2836

2937
const asyncKinesisRecordHandler = async (
3038
record: KinesisStreamRecord
31-
): Promise<string> => Promise.resolve(kinesisRecordHandler(record));
39+
): Promise<string> => {
40+
const body = record.kinesis.data;
41+
if (body.includes('fail')) {
42+
throw new Error('Failed to process record.');
43+
}
44+
await setTimeout(1); // simulate some processing time
45+
return body;
46+
};
3247

33-
const dynamodbRecordHandler = (record: DynamoDBRecord): object => {
48+
const dynamodbRecordHandler = (record: DynamoDBRecord): AttributeValue => {
3449
const body = record.dynamodb?.NewImage?.Message || { S: 'fail' };
3550
if (body.S?.includes('fail')) {
36-
throw Error('Failed to process record.');
51+
throw new Error('Failed to process record.');
3752
}
3853

3954
return body;
4055
};
4156

4257
const asyncDynamodbRecordHandler = async (
4358
record: DynamoDBRecord
44-
): Promise<object> => {
45-
return Promise.resolve(dynamodbRecordHandler(record));
59+
): Promise<AttributeValue> => {
60+
const body = record.dynamodb?.NewImage?.Message || { S: 'fail' };
61+
if (body.S?.includes('fail')) {
62+
throw new Error('Failed to process record.');
63+
}
64+
await setTimeout(1); // simulate some processing time
65+
return body;
4666
};
4767

4868
const handlerWithContext = (record: SQSRecord, context: Context): string => {
4969
try {
5070
if (context.getRemainingTimeInMillis() === 0) {
51-
throw Error('No time remaining.');
71+
throw new Error('No time remaining.');
5272
}
5373
} catch {
54-
throw Error(`Context possibly malformed. Displaying context:\n${context}`);
74+
throw new Error(
75+
`Context possibly malformed. Displaying context:\n${context}`
76+
);
5577
}
5678

5779
return record.body;
@@ -61,7 +83,17 @@ const asyncHandlerWithContext = async (
6183
record: SQSRecord,
6284
context: Context
6385
): Promise<string> => {
64-
return Promise.resolve(handlerWithContext(record, context));
86+
try {
87+
if (context.getRemainingTimeInMillis() === 0) {
88+
throw new Error('No time remaining.');
89+
}
90+
} catch {
91+
throw new Error(
92+
`Context possibly malformed. Displaying context:\n${context}`
93+
);
94+
}
95+
await setTimeout(1); // simulate some processing time
96+
return record.body;
6597
};
6698

6799
export {

packages/batch/tests/unit/BasePartialProcessor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('Class: BasePartialBatchProcessor', () => {
2525
super(EventType.SQS);
2626
}
2727

28-
public async processRecord(
28+
public processRecord(
2929
_record: BaseRecord
3030
): Promise<SuccessResponse | FailureResponse> {
3131
throw new Error('Not implemented');

packages/batch/tests/unit/SqsFifoPartialProcessor.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe('SQS FIFO Processors', () => {
186186
});
187187
}
188188

189-
it('continues processing and moves to the next group when `skipGroupOnError` is true', async () => {
189+
it('continues processing and moves to the next group when `skipGroupOnError` is true', () => {
190190
// Prepare
191191
const firstRecord = sqsRecordFactory('fail', '1');
192192
const secondRecord = sqsRecordFactory('success', '2');

packages/batch/tests/unit/processPartialResponse.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ describe('Function: processPartialResponse()', () => {
3636
context,
3737
};
3838

39-
const handlerWithSqsEvent = async (
39+
const handlerWithSqsEvent = (
4040
event: SQSEvent,
4141
options: BatchProcessingOptions
42-
) => {
42+
): Promise<PartialItemFailureResponse> => {
4343
const processor = new BatchProcessor(EventType.SQS);
4444

4545
const handler = async (
@@ -51,10 +51,10 @@ describe('Function: processPartialResponse()', () => {
5151
return handler(event, context);
5252
};
5353

54-
const handlerWithKinesisEvent = async (
54+
const handlerWithKinesisEvent = (
5555
event: KinesisStreamEvent,
5656
options: BatchProcessingOptions
57-
) => {
57+
): Promise<PartialItemFailureResponse> => {
5858
const processor = new BatchProcessor(EventType.KinesisDataStreams);
5959

6060
const handler = async (
@@ -71,10 +71,10 @@ describe('Function: processPartialResponse()', () => {
7171
return handler(event, context);
7272
};
7373

74-
const handlerWithDynamoDBEvent = async (
74+
const handlerWithDynamoDBEvent = (
7575
event: DynamoDBStreamEvent,
7676
options: BatchProcessingOptions
77-
) => {
77+
): Promise<PartialItemFailureResponse> => {
7878
const processor = new BatchProcessor(EventType.DynamoDBStreams);
7979

8080
const handler = async (

0 commit comments

Comments
 (0)