Skip to content

feat(batch): add esmodule support #1737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module.exports = {
},
runner: 'groups',
preset: 'ts-jest',
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
'^.+\\.ts?$': 'ts-jest',
},
Expand Down
35 changes: 31 additions & 4 deletions packages/batch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,46 @@
"test:e2e:nodejs18x": "echo 'Not Implemented'",
"test:e2e": "echo 'Not Implemented'",
"watch": "jest --watch",
"build": "tsc --build --force",
"build:cjs": "tsc --build --force && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
"build:esm": "tsc --project tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
"build": "npm run build:esm & npm run build:cjs",
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
"prebuild": "rimraf ./lib",
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
"prepack": "rimraf ./lib/*.tsbuildinfo && node ../../.github/scripts/release_patch_package_json.js ."
},
"lint-staged": {
"*.{js,ts}": "npm run lint-fix"
},
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/batch#readme",
"license": "MIT-0",
"main": "./lib/index.js",
"types": "./lib/index.d.ts",
"type": "module",
"exports": {
".": {
"require": {
"types": "./lib/cjs/index.d.ts",
"default": "./lib/cjs/index.js"
},
"import": {
"types": "./lib/esm/index.d.ts",
"default": "./lib/esm/index.js"
}
},
"./types": {
"import": "./lib/esm/types.js",
"require": "./lib/cjs/types.js"
}
},
"typesVersions": {
"*": {
"types": [
"lib/cjs/types.d.ts",
"lib/esm/types.d.ts"
]
}
},
"types": "./lib/cjs/index.d.ts",
"main": "./lib/cjs/index.js",
"files": [
"lib"
],
Expand Down
12 changes: 8 additions & 4 deletions packages/batch/src/BasePartialBatchProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import type {
KinesisStreamRecord,
SQSRecord,
} from 'aws-lambda';
import { BasePartialProcessor } from './BasePartialProcessor';
import { DATA_CLASS_MAPPING, DEFAULT_RESPONSE, EventType } from './constants';
import { FullBatchFailureError } from './errors';
import { BasePartialProcessor } from './BasePartialProcessor.js';
import {
DATA_CLASS_MAPPING,
DEFAULT_RESPONSE,
EventType,
} from './constants.js';
import { FullBatchFailureError } from './errors.js';
import type {
EventSourceDataClassTypes,
PartialItemFailureResponse,
PartialItemFailures,
} from './types';
} from './types.js';

/**
* Process batch and partially report failed items
Expand Down
2 changes: 1 addition & 1 deletion packages/batch/src/BasePartialProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import type {
FailureResponse,
ResultType,
SuccessResponse,
} from './types';
} from './types.js';

/**
* Abstract class for batch processors.
Expand Down
6 changes: 3 additions & 3 deletions packages/batch/src/BatchProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor';
import { BatchProcessingError } from './errors';
import type { BaseRecord, FailureResponse, SuccessResponse } from './types';
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
import { BatchProcessingError } from './errors.js';
import type { BaseRecord, FailureResponse, SuccessResponse } from './types.js';

/**
* Process native partial responses from SQS, Kinesis Data Streams, and DynamoDB
Expand Down
6 changes: 3 additions & 3 deletions packages/batch/src/BatchProcessorSync.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor';
import { BatchProcessingError } from './errors';
import type { BaseRecord, FailureResponse, SuccessResponse } from './types';
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
import { BatchProcessingError } from './errors.js';
import type { BaseRecord, FailureResponse, SuccessResponse } from './types.js';

/**
* Process native partial responses from SQS, Kinesis Data Streams, and DynamoDB
Expand Down
8 changes: 4 additions & 4 deletions packages/batch/src/SqsFifoPartialProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BatchProcessorSync } from './BatchProcessorSync';
import { EventType } from './constants';
import { SqsFifoShortCircuitError } from './errors';
import type { FailureResponse, SuccessResponse } from './types';
import { BatchProcessorSync } from './BatchProcessorSync.js';
import { EventType } from './constants.js';
import { SqsFifoShortCircuitError } from './errors.js';
import type { FailureResponse, SuccessResponse } from './types.js';

/**
* Process native partial responses from SQS FIFO queues
Expand Down
2 changes: 1 addition & 1 deletion packages/batch/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
import type {
PartialItemFailureResponse,
EventSourceDataClassTypes,
} from './types';
} from './types.js';

const EventType = {
SQS: 'SQS',
Expand Down
2 changes: 1 addition & 1 deletion packages/batch/src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EventType } from './constants';
import { EventType } from './constants.js';

/**
* Base error thrown by the Batch Processing utility
Expand Down
23 changes: 13 additions & 10 deletions packages/batch/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
export * from './constants';
export * from './errors';
export * from './types';
export * from './BasePartialProcessor';
export * from './BasePartialBatchProcessor';
export * from './BatchProcessorSync';
export * from './BatchProcessor';
export * from './processPartialResponseSync';
export * from './processPartialResponse';
export * from './SqsFifoPartialProcessor';
export { EventType } from './constants.js';
export {
BatchProcessingError,
FullBatchFailureError,
SqsFifoShortCircuitError,
UnexpectedBatchTypeError,
} from './errors.js';
export { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
export { BatchProcessorSync } from './BatchProcessorSync.js';
export { BatchProcessor } from './BatchProcessor.js';
export { processPartialResponseSync } from './processPartialResponseSync.js';
export { processPartialResponse } from './processPartialResponse.js';
export { SqsFifoPartialProcessor } from './SqsFifoPartialProcessor.js';
6 changes: 3 additions & 3 deletions packages/batch/src/processPartialResponse.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor';
import { UnexpectedBatchTypeError } from './errors';
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
import { UnexpectedBatchTypeError } from './errors.js';
import type {
BaseRecord,
BatchProcessingOptions,
PartialItemFailureResponse,
} from './types';
} from './types.js';

/**
* Higher level function to handle batch event processing
Expand Down
6 changes: 3 additions & 3 deletions packages/batch/src/processPartialResponseSync.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor';
import { UnexpectedBatchTypeError } from './errors';
import { BasePartialBatchProcessor } from './BasePartialBatchProcessor.js';
import { UnexpectedBatchTypeError } from './errors.js';
import type {
BaseRecord,
BatchProcessingOptions,
PartialItemFailureResponse,
} from './types';
} from './types.js';

/**
* Higher level function to handle batch event processing
Expand Down
2 changes: 1 addition & 1 deletion packages/batch/tests/helpers/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import type {
DynamoDBRecord,
KinesisStreamRecord,
SQSRecord,
Context,
} from 'aws-lambda';
import type { Context } from 'aws-lambda';

const sqsRecordHandler = (record: SQSRecord): string => {
const body = record.body;
Expand Down
74 changes: 74 additions & 0 deletions packages/batch/tests/unit/BasePartialProcessor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Test BasePartialBatchProcessor class
*
* @group unit/batch/class/basepartialbatchprocessor
*/
import { BasePartialBatchProcessor, EventType } from '../../src/index.js';
import type {
BaseRecord,
FailureResponse,
SuccessResponse,
} from '../../src/types.js';
import { sqsRecordFactory } from '../helpers/factories.js';
import { sqsRecordHandler } from '../helpers/handlers.js';

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

beforeEach(() => {
jest.clearAllMocks();
jest.resetModules();
process.env = { ...ENVIRONMENT_VARIABLES };
});

afterAll(() => {
process.env = ENVIRONMENT_VARIABLES;
});

class MyPartialProcessor extends BasePartialBatchProcessor {
public constructor() {
super(EventType.SQS);
}

public async processRecord(
_record: BaseRecord
): Promise<SuccessResponse | FailureResponse> {
throw new Error('Not implemented');
}

public processRecordSync(
record: BaseRecord
): SuccessResponse | FailureResponse {
console.log('Processing record');

return this.successHandler(record, 'success');
}
}

describe('create custom batch partial processor', () => {
it('should create a custom batch partial processor', () => {
// Act
const processor = new MyPartialProcessor();

// Assess
expect(processor).toBeInstanceOf(BasePartialBatchProcessor);
});

it('should process a batch of records', () => {
// Prepare
const processor = new MyPartialProcessor();
const records = [sqsRecordFactory('success')];
const consoleSpy = jest.spyOn(console, 'log');

// Act
processor.register(records, sqsRecordHandler);
const processedMessages = processor.processSync();

// Assess
expect(processedMessages).toStrictEqual([
['success', records[0].body, records[0]],
]);
expect(consoleSpy).toHaveBeenCalledTimes(1);
});
});
});
19 changes: 11 additions & 8 deletions packages/batch/tests/unit/BatchProcessor.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
/**
* Test AsyncBatchProcessor class
* Test BatchProcessor class
*
* @group unit/batch/class/asyncBatchProcessor
* @group unit/batch/class/batchprocessor
*/
import type { Context } from 'aws-lambda';
import { ContextExamples as dummyContext } from '@aws-lambda-powertools/commons';
import { BatchProcessor } from '../../src/BatchProcessor';
import { EventType } from '../../src/constants';
import { BatchProcessingError, FullBatchFailureError } from '../../src/errors';
import type { BatchProcessingOptions } from '../../src/types';
import {
BatchProcessor,
EventType,
BatchProcessingError,
FullBatchFailureError,
} from '../../src/index.js';
import type { BatchProcessingOptions } from '../../src/types.js';
import {
dynamodbRecordFactory,
kinesisRecordFactory,
sqsRecordFactory,
} from '../helpers/factories';
} from '../helpers/factories.js';
import {
asyncDynamodbRecordHandler,
asyncKinesisRecordHandler,
asyncSqsRecordHandler,
asyncHandlerWithContext,
} from '../helpers/handlers';
} from '../helpers/handlers.js';

describe('Class: AsyncBatchProcessor', () => {
const ENVIRONMENT_VARIABLES = process.env;
Expand Down
19 changes: 11 additions & 8 deletions packages/batch/tests/unit/BatchProcessorSync.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,28 @@
/**
* Test BatchProcessor class
* Test BatchProcessorSync class
*
* @group unit/batch/class/batchprocessor
* @group unit/batch/class/batchprocessorsync
*/
import type { Context } from 'aws-lambda';
import { ContextExamples as dummyContext } from '@aws-lambda-powertools/commons';
import { BatchProcessorSync } from '../../src/BatchProcessorSync';
import { EventType } from '../../src/constants';
import { BatchProcessingError, FullBatchFailureError } from '../../src/errors';
import type { BatchProcessingOptions } from '../../src/types';
import {
BatchProcessorSync,
EventType,
BatchProcessingError,
FullBatchFailureError,
} from '../../src/index.js';
import type { BatchProcessingOptions } from '../../src/types.js';
import {
dynamodbRecordFactory,
kinesisRecordFactory,
sqsRecordFactory,
} from '../helpers/factories';
} from '../helpers/factories.js';
import {
dynamodbRecordHandler,
handlerWithContext,
kinesisRecordHandler,
sqsRecordHandler,
} from '../helpers/handlers';
} from '../helpers/handlers.js';

describe('Class: BatchProcessor', () => {
const ENVIRONMENT_VARIABLES = process.env;
Expand Down
6 changes: 3 additions & 3 deletions packages/batch/tests/unit/SqsFifoPartialProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {
SqsFifoPartialProcessor,
processPartialResponseSync,
SqsFifoShortCircuitError,
} from '../../src';
import { sqsRecordFactory } from '../helpers/factories';
import { sqsRecordHandler } from '../helpers/handlers';
} from '../../src/index.js';
import { sqsRecordFactory } from '../helpers/factories.js';
import { sqsRecordHandler } from '../helpers/handlers.js';

describe('Class: SqsFifoBatchProcessor', () => {
const ENVIRONMENT_VARIABLES = process.env;
Expand Down
Loading