Skip to content

Commit 4ee57bd

Browse files
committed
feat(batch): add esmodule support (#1737)
1 parent 3ba9d43 commit 4ee57bd

21 files changed

+230
-86
lines changed

Diff for: packages/batch/jest.config.js renamed to packages/batch/jest.config.cjs

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ module.exports = {
55
},
66
runner: 'groups',
77
preset: 'ts-jest',
8+
moduleNameMapper: {
9+
'^(\\.{1,2}/.*)\\.js$': '$1',
10+
},
811
transform: {
912
'^.+\\.ts?$': 'ts-jest',
1013
},

Diff for: packages/batch/package.json

+31-4
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,46 @@
1919
"test:e2e:nodejs20x": "echo 'Not Implemented'",
2020
"test:e2e": "echo 'Not Implemented'",
2121
"watch": "jest --watch",
22-
"build": "tsc --build --force",
22+
"build:cjs": "tsc --build --force && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
23+
"build:esm": "tsc --project tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
24+
"build": "npm run build:esm & npm run build:cjs",
2325
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
2426
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
2527
"prebuild": "rimraf ./lib",
26-
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
28+
"prepack": "rimraf ./lib/*.tsbuildinfo && node ../../.github/scripts/release_patch_package_json.js ."
2729
},
2830
"lint-staged": {
2931
"*.{js,ts}": "npm run lint-fix"
3032
},
3133
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/batch#readme",
3234
"license": "MIT-0",
33-
"main": "./lib/index.js",
34-
"types": "./lib/index.d.ts",
35+
"type": "module",
36+
"exports": {
37+
".": {
38+
"require": {
39+
"types": "./lib/cjs/index.d.ts",
40+
"default": "./lib/cjs/index.js"
41+
},
42+
"import": {
43+
"types": "./lib/esm/index.d.ts",
44+
"default": "./lib/esm/index.js"
45+
}
46+
},
47+
"./types": {
48+
"import": "./lib/esm/types.js",
49+
"require": "./lib/cjs/types.js"
50+
}
51+
},
52+
"typesVersions": {
53+
"*": {
54+
"types": [
55+
"lib/cjs/types.d.ts",
56+
"lib/esm/types.d.ts"
57+
]
58+
}
59+
},
60+
"types": "./lib/cjs/index.d.ts",
61+
"main": "./lib/cjs/index.js",
3562
"files": [
3663
"lib"
3764
],

Diff for: packages/batch/src/BasePartialBatchProcessor.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@ import type {
33
KinesisStreamRecord,
44
SQSRecord,
55
} from 'aws-lambda';
6-
import { BasePartialProcessor } from './BasePartialProcessor';
7-
import { DATA_CLASS_MAPPING, DEFAULT_RESPONSE, EventType } from './constants';
8-
import { FullBatchFailureError } from './errors';
6+
import { BasePartialProcessor } from './BasePartialProcessor.js';
7+
import {
8+
DATA_CLASS_MAPPING,
9+
DEFAULT_RESPONSE,
10+
EventType,
11+
} from './constants.js';
12+
import { FullBatchFailureError } from './errors.js';
913
import type {
1014
EventSourceDataClassTypes,
1115
PartialItemFailureResponse,
1216
PartialItemFailures,
13-
} from './types';
17+
} from './types.js';
1418

1519
/**
1620
* Process batch and partially report failed items

Diff for: packages/batch/src/BasePartialProcessor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type {
55
FailureResponse,
66
ResultType,
77
SuccessResponse,
8-
} from './types';
8+
} from './types.js';
99

1010
/**
1111
* Abstract class for batch processors.

Diff for: packages/batch/src/BatchProcessor.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor';
2-
import { BatchProcessingError } from './errors';
3-
import type { BaseRecord, FailureResponse, SuccessResponse } from './types';
1+
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
2+
import { BatchProcessingError } from './errors.js';
3+
import type { BaseRecord, FailureResponse, SuccessResponse } from './types.js';
44

55
/**
66
* Process native partial responses from SQS, Kinesis Data Streams, and DynamoDB

Diff for: packages/batch/src/BatchProcessorSync.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor';
2-
import { BatchProcessingError } from './errors';
3-
import type { BaseRecord, FailureResponse, SuccessResponse } from './types';
1+
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
2+
import { BatchProcessingError } from './errors.js';
3+
import type { BaseRecord, FailureResponse, SuccessResponse } from './types.js';
44

55
/**
66
* Process native partial responses from SQS, Kinesis Data Streams, and DynamoDB

Diff for: packages/batch/src/SqsFifoPartialProcessor.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { BatchProcessorSync } from './BatchProcessorSync';
2-
import { EventType } from './constants';
3-
import { SqsFifoShortCircuitError } from './errors';
4-
import type { FailureResponse, SuccessResponse } from './types';
1+
import { BatchProcessorSync } from './BatchProcessorSync.js';
2+
import { EventType } from './constants.js';
3+
import { SqsFifoShortCircuitError } from './errors.js';
4+
import type { FailureResponse, SuccessResponse } from './types.js';
55

66
/**
77
* Process native partial responses from SQS FIFO queues

Diff for: packages/batch/src/constants.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
import type {
77
PartialItemFailureResponse,
88
EventSourceDataClassTypes,
9-
} from './types';
9+
} from './types.js';
1010

1111
const EventType = {
1212
SQS: 'SQS',

Diff for: packages/batch/src/errors.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { EventType } from './constants';
1+
import { EventType } from './constants.js';
22

33
/**
44
* Base error thrown by the Batch Processing utility

Diff for: packages/batch/src/index.ts

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
export * from './constants';
2-
export * from './errors';
3-
export * from './types';
4-
export * from './BasePartialProcessor';
5-
export * from './BasePartialBatchProcessor';
6-
export * from './BatchProcessorSync';
7-
export * from './BatchProcessor';
8-
export * from './processPartialResponseSync';
9-
export * from './processPartialResponse';
10-
export * from './SqsFifoPartialProcessor';
1+
export { EventType } from './constants.js';
2+
export {
3+
BatchProcessingError,
4+
FullBatchFailureError,
5+
SqsFifoShortCircuitError,
6+
UnexpectedBatchTypeError,
7+
} from './errors.js';
8+
export { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
9+
export { BatchProcessorSync } from './BatchProcessorSync.js';
10+
export { BatchProcessor } from './BatchProcessor.js';
11+
export { processPartialResponseSync } from './processPartialResponseSync.js';
12+
export { processPartialResponse } from './processPartialResponse.js';
13+
export { SqsFifoPartialProcessor } from './SqsFifoPartialProcessor.js';

Diff for: packages/batch/src/processPartialResponse.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor';
2-
import { UnexpectedBatchTypeError } from './errors';
1+
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
2+
import { UnexpectedBatchTypeError } from './errors.js';
33
import type {
44
BaseRecord,
55
BatchProcessingOptions,
66
PartialItemFailureResponse,
7-
} from './types';
7+
} from './types.js';
88

99
/**
1010
* Higher level function to handle batch event processing

Diff for: packages/batch/src/processPartialResponseSync.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor';
2-
import { UnexpectedBatchTypeError } from './errors';
1+
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
2+
import { UnexpectedBatchTypeError } from './errors.js';
33
import type {
44
BaseRecord,
55
BatchProcessingOptions,
66
PartialItemFailureResponse,
7-
} from './types';
7+
} from './types.js';
88

99
/**
1010
* Higher level function to handle batch event processing

Diff for: packages/batch/tests/helpers/handlers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import type {
22
DynamoDBRecord,
33
KinesisStreamRecord,
44
SQSRecord,
5+
Context,
56
} from 'aws-lambda';
6-
import type { Context } from 'aws-lambda';
77

88
const sqsRecordHandler = (record: SQSRecord): string => {
99
const body = record.body;
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/**
2+
* Test BasePartialBatchProcessor class
3+
*
4+
* @group unit/batch/class/basepartialbatchprocessor
5+
*/
6+
import { BasePartialBatchProcessor, EventType } from '../../src/index.js';
7+
import type {
8+
BaseRecord,
9+
FailureResponse,
10+
SuccessResponse,
11+
} from '../../src/types.js';
12+
import { sqsRecordFactory } from '../helpers/factories.js';
13+
import { sqsRecordHandler } from '../helpers/handlers.js';
14+
15+
describe('Class: BasePartialBatchProcessor', () => {
16+
const ENVIRONMENT_VARIABLES = process.env;
17+
18+
beforeEach(() => {
19+
jest.clearAllMocks();
20+
jest.resetModules();
21+
process.env = { ...ENVIRONMENT_VARIABLES };
22+
});
23+
24+
afterAll(() => {
25+
process.env = ENVIRONMENT_VARIABLES;
26+
});
27+
28+
class MyPartialProcessor extends BasePartialBatchProcessor {
29+
public constructor() {
30+
super(EventType.SQS);
31+
}
32+
33+
public async processRecord(
34+
_record: BaseRecord
35+
): Promise<SuccessResponse | FailureResponse> {
36+
throw new Error('Not implemented');
37+
}
38+
39+
public processRecordSync(
40+
record: BaseRecord
41+
): SuccessResponse | FailureResponse {
42+
console.log('Processing record');
43+
44+
return this.successHandler(record, 'success');
45+
}
46+
}
47+
48+
describe('create custom batch partial processor', () => {
49+
it('should create a custom batch partial processor', () => {
50+
// Act
51+
const processor = new MyPartialProcessor();
52+
53+
// Assess
54+
expect(processor).toBeInstanceOf(BasePartialBatchProcessor);
55+
});
56+
57+
it('should process a batch of records', () => {
58+
// Prepare
59+
const processor = new MyPartialProcessor();
60+
const records = [sqsRecordFactory('success')];
61+
const consoleSpy = jest.spyOn(console, 'log');
62+
63+
// Act
64+
processor.register(records, sqsRecordHandler);
65+
const processedMessages = processor.processSync();
66+
67+
// Assess
68+
expect(processedMessages).toStrictEqual([
69+
['success', records[0].body, records[0]],
70+
]);
71+
expect(consoleSpy).toHaveBeenCalledTimes(1);
72+
});
73+
});
74+
});

Diff for: packages/batch/tests/unit/BatchProcessor.test.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
/**
2-
* Test AsyncBatchProcessor class
2+
* Test BatchProcessor class
33
*
4-
* @group unit/batch/class/asyncBatchProcessor
4+
* @group unit/batch/class/batchprocessor
55
*/
66
import type { Context } from 'aws-lambda';
77
import { ContextExamples as dummyContext } from '@aws-lambda-powertools/commons';
8-
import { BatchProcessor } from '../../src/BatchProcessor';
9-
import { EventType } from '../../src/constants';
10-
import { BatchProcessingError, FullBatchFailureError } from '../../src/errors';
11-
import type { BatchProcessingOptions } from '../../src/types';
8+
import {
9+
BatchProcessor,
10+
EventType,
11+
BatchProcessingError,
12+
FullBatchFailureError,
13+
} from '../../src/index.js';
14+
import type { BatchProcessingOptions } from '../../src/types.js';
1215
import {
1316
dynamodbRecordFactory,
1417
kinesisRecordFactory,
1518
sqsRecordFactory,
16-
} from '../helpers/factories';
19+
} from '../helpers/factories.js';
1720
import {
1821
asyncDynamodbRecordHandler,
1922
asyncKinesisRecordHandler,
2023
asyncSqsRecordHandler,
2124
asyncHandlerWithContext,
22-
} from '../helpers/handlers';
25+
} from '../helpers/handlers.js';
2326

2427
describe('Class: AsyncBatchProcessor', () => {
2528
const ENVIRONMENT_VARIABLES = process.env;

Diff for: packages/batch/tests/unit/BatchProcessorSync.test.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,28 @@
11
/**
2-
* Test BatchProcessor class
2+
* Test BatchProcessorSync class
33
*
4-
* @group unit/batch/class/batchprocessor
4+
* @group unit/batch/class/batchprocessorsync
55
*/
66
import type { Context } from 'aws-lambda';
77
import { ContextExamples as dummyContext } from '@aws-lambda-powertools/commons';
8-
import { BatchProcessorSync } from '../../src/BatchProcessorSync';
9-
import { EventType } from '../../src/constants';
10-
import { BatchProcessingError, FullBatchFailureError } from '../../src/errors';
11-
import type { BatchProcessingOptions } from '../../src/types';
8+
import {
9+
BatchProcessorSync,
10+
EventType,
11+
BatchProcessingError,
12+
FullBatchFailureError,
13+
} from '../../src/index.js';
14+
import type { BatchProcessingOptions } from '../../src/types.js';
1215
import {
1316
dynamodbRecordFactory,
1417
kinesisRecordFactory,
1518
sqsRecordFactory,
16-
} from '../helpers/factories';
19+
} from '../helpers/factories.js';
1720
import {
1821
dynamodbRecordHandler,
1922
handlerWithContext,
2023
kinesisRecordHandler,
2124
sqsRecordHandler,
22-
} from '../helpers/handlers';
25+
} from '../helpers/handlers.js';
2326

2427
describe('Class: BatchProcessor', () => {
2528
const ENVIRONMENT_VARIABLES = process.env;

Diff for: packages/batch/tests/unit/SqsFifoPartialProcessor.test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
SqsFifoPartialProcessor,
88
processPartialResponseSync,
99
SqsFifoShortCircuitError,
10-
} from '../../src';
11-
import { sqsRecordFactory } from '../helpers/factories';
12-
import { sqsRecordHandler } from '../helpers/handlers';
10+
} from '../../src/index.js';
11+
import { sqsRecordFactory } from '../helpers/factories.js';
12+
import { sqsRecordHandler } from '../helpers/handlers.js';
1313

1414
describe('Class: SqsFifoBatchProcessor', () => {
1515
const ENVIRONMENT_VARIABLES = process.env;

0 commit comments

Comments
 (0)