Skip to content

Commit 3977c60

Browse files
committed
feat(commons): add esmodule support (#1735)
* chore(logger): adapt logger to commons exports * feat(commons): add esmodule support * chore: address sonar findings * chore(commons): exported version * chore: fixed imports in examples * chore(parameters): fixed imports * chore(metrics): fixed imports * chore(tracer): fixed imports * chore(idempotency): fixed imports * chore(commons): test coverage * chore(batch): fix imports
1 parent 8629016 commit 3977c60

File tree

73 files changed

+248
-192
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+248
-192
lines changed

Diff for: examples/cdk/functions/common/powertools.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Logger } from '@aws-lambda-powertools/logger';
22
import { Metrics } from '@aws-lambda-powertools/metrics';
33
import { Tracer } from '@aws-lambda-powertools/tracer';
4-
import { PT_VERSION } from '@aws-lambda-powertools/commons/lib/version';
4+
import { PT_VERSION } from '@aws-lambda-powertools/commons';
55

66
const defaultValues = {
77
region: process.env.AWS_REGION || 'N/A',

Diff for: examples/cdk/functions/get-all-items.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { injectLambdaContext } from '@aws-lambda-powertools/logger';
1+
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware';
22
import { logMetrics } from '@aws-lambda-powertools/metrics';
33
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer';
44
import { ScanCommand } from '@aws-sdk/lib-dynamodb';

Diff for: examples/cdk/functions/get-by-id.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LambdaInterface } from '@aws-lambda-powertools/commons';
1+
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
22
import { GetCommand } from '@aws-sdk/lib-dynamodb';
33
import {
44
APIGatewayProxyEvent,

Diff for: examples/cdk/functions/uuid.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { randomUUID } from 'node:crypto';
22

3-
exports.handler = async (_event) => {
3+
exports.handler = async () => {
44
return {
55
statusCode: 200,
66
body: JSON.stringify(randomUUID()),

Diff for: examples/sam/src/common/powertools.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Logger } from '@aws-lambda-powertools/logger';
22
import { Metrics } from '@aws-lambda-powertools/metrics';
33
import { Tracer } from '@aws-lambda-powertools/tracer';
4-
import { PT_VERSION } from '@aws-lambda-powertools/commons/lib/version';
4+
import { PT_VERSION } from '@aws-lambda-powertools/commons';
55

66
const defaultValues = {
77
region: process.env.AWS_REGION || 'N/A',

Diff for: examples/sam/src/get-all-items.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import middy from '@middy/core';
77
import { tableName } from './common/constants';
88
import { logger, tracer, metrics } from './common/powertools';
99
import { logMetrics } from '@aws-lambda-powertools/metrics';
10-
import { injectLambdaContext } from '@aws-lambda-powertools/logger';
10+
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware';
1111
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer';
1212
import { docClient } from './common/dynamodb-client';
1313
import { ScanCommand } from '@aws-sdk/lib-dynamodb';

Diff for: examples/sam/src/get-by-id.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { LambdaInterface } from '@aws-lambda-powertools/commons';
1+
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
22
import { GetCommand } from '@aws-sdk/lib-dynamodb';
33
import {
44
APIGatewayProxyEvent,

Diff for: examples/sam/src/get-uuid.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { randomUUID } from 'node:crypto';
22

3-
exports.handler = async (_event) => {
3+
exports.handler = async () => {
44
return {
55
statusCode: 200,
66
body: JSON.stringify(randomUUID()),

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @group unit/batch/class/asyncBatchProcessor
55
*/
66
import type { Context } from 'aws-lambda';
7-
import { helloworldContext as dummyContext } from '@aws-lambda-powertools/commons/lib/samples/resources/contexts';
7+
import { ContextExamples as dummyContext } from '@aws-lambda-powertools/commons';
88
import { BatchProcessor } from '../../src/BatchProcessor';
99
import { EventType } from '../../src/constants';
1010
import { BatchProcessingError, FullBatchFailureError } from '../../src/errors';
@@ -23,7 +23,9 @@ import {
2323

2424
describe('Class: AsyncBatchProcessor', () => {
2525
const ENVIRONMENT_VARIABLES = process.env;
26-
const options: BatchProcessingOptions = { context: dummyContext };
26+
const options: BatchProcessingOptions = {
27+
context: dummyContext.helloworldContext,
28+
};
2729

2830
beforeEach(() => {
2931
jest.clearAllMocks();

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @group unit/batch/class/batchprocessor
55
*/
66
import type { Context } from 'aws-lambda';
7-
import { helloworldContext as dummyContext } from '@aws-lambda-powertools/commons/lib/samples/resources/contexts';
7+
import { ContextExamples as dummyContext } from '@aws-lambda-powertools/commons';
88
import { BatchProcessorSync } from '../../src/BatchProcessorSync';
99
import { EventType } from '../../src/constants';
1010
import { BatchProcessingError, FullBatchFailureError } from '../../src/errors';
@@ -23,7 +23,9 @@ import {
2323

2424
describe('Class: BatchProcessor', () => {
2525
const ENVIRONMENT_VARIABLES = process.env;
26-
const options: BatchProcessingOptions = { context: dummyContext };
26+
const options: BatchProcessingOptions = {
27+
context: dummyContext.helloworldContext,
28+
};
2729

2830
beforeEach(() => {
2931
jest.clearAllMocks();

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

+13-9
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import type {
99
KinesisStreamEvent,
1010
SQSEvent,
1111
} from 'aws-lambda';
12-
import { helloworldContext as dummyContext } from '@aws-lambda-powertools/commons/lib/samples/resources/contexts';
13-
import { Custom as dummyEvent } from '@aws-lambda-powertools/commons/lib/samples/resources/events';
12+
import {
13+
ContextExamples as dummyContext,
14+
Events as dummyEvent,
15+
} from '@aws-lambda-powertools/commons';
1416
import { BatchProcessor } from '../../src/BatchProcessor';
1517
import { processPartialResponse } from '../../src/processPartialResponse';
1618
import { EventType } from '../../src/constants';
@@ -33,7 +35,9 @@ import {
3335
describe('Function: processPartialResponse()', () => {
3436
const ENVIRONMENT_VARIABLES = process.env;
3537
const context = dummyContext;
36-
const options: BatchProcessingOptions = { context: dummyContext };
38+
const options: BatchProcessingOptions = {
39+
context: dummyContext.helloworldContext,
40+
};
3741

3842
beforeEach(() => {
3943
jest.clearAllMocks();
@@ -106,7 +110,7 @@ describe('Function: processPartialResponse()', () => {
106110
};
107111

108112
// Act
109-
const result = await handler(event, context);
113+
const result = await handler(event, context.helloworldContext);
110114

111115
// Assess
112116
expect(result).toStrictEqual({ batchItemFailures: [] });
@@ -133,7 +137,7 @@ describe('Function: processPartialResponse()', () => {
133137
};
134138

135139
// Act
136-
const result = await handler(event, context);
140+
const result = await handler(event, context.helloworldContext);
137141

138142
// Assess
139143
expect(result).toStrictEqual({ batchItemFailures: [] });
@@ -160,7 +164,7 @@ describe('Function: processPartialResponse()', () => {
160164
};
161165

162166
// Act
163-
const result = await handler(event, context);
167+
const result = await handler(event, context.helloworldContext);
164168

165169
// Assess
166170
expect(result).toStrictEqual({ batchItemFailures: [] });
@@ -169,7 +173,7 @@ describe('Function: processPartialResponse()', () => {
169173
test('Process partial response through handler for SQS records with incorrect event type', async () => {
170174
// Prepare
171175
const processor = new BatchProcessor(EventType.SQS);
172-
const event = dummyEvent;
176+
const event = dummyEvent.Custom;
173177

174178
const handler = async (
175179
event: SQSEvent,
@@ -184,7 +188,7 @@ describe('Function: processPartialResponse()', () => {
184188

185189
// Act & Assess
186190
await expect(() =>
187-
handler(event as unknown as SQSEvent, context)
191+
handler(event as unknown as SQSEvent, context.helloworldContext)
188192
).rejects.toThrowError(
189193
`Unexpected batch type. Possible values are: ${Object.keys(
190194
EventType
@@ -216,7 +220,7 @@ describe('Function: processPartialResponse()', () => {
216220
};
217221

218222
// Act
219-
const result = await handler(event, context);
223+
const result = await handler(event, context.helloworldContext);
220224

221225
// Assess
222226
expect(result).toStrictEqual({ batchItemFailures: [] });

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

+15-9
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import type {
99
KinesisStreamEvent,
1010
SQSEvent,
1111
} from 'aws-lambda';
12-
import { helloworldContext as dummyContext } from '@aws-lambda-powertools/commons/lib/samples/resources/contexts';
13-
import { Custom as dummyEvent } from '@aws-lambda-powertools/commons/lib/samples/resources/events';
12+
import {
13+
ContextExamples as dummyContext,
14+
Events as dummyEvent,
15+
} from '@aws-lambda-powertools/commons';
1416
import { BatchProcessorSync } from '../../src/BatchProcessorSync';
1517
import { processPartialResponseSync } from '../../src/processPartialResponseSync';
1618
import { EventType } from '../../src/constants';
@@ -33,7 +35,9 @@ import {
3335
describe('Function: processPartialResponse()', () => {
3436
const ENVIRONMENT_VARIABLES = process.env;
3537
const context = dummyContext;
36-
const options: BatchProcessingOptions = { context: dummyContext };
38+
const options: BatchProcessingOptions = {
39+
context: dummyContext.helloworldContext,
40+
};
3741

3842
beforeEach(() => {
3943
jest.clearAllMocks();
@@ -106,7 +110,7 @@ describe('Function: processPartialResponse()', () => {
106110
};
107111

108112
// Act
109-
const result = handler(event, context);
113+
const result = handler(event, context.helloworldContext);
110114

111115
// Assess
112116
expect(result).toStrictEqual({ batchItemFailures: [] });
@@ -133,7 +137,7 @@ describe('Function: processPartialResponse()', () => {
133137
};
134138

135139
// Act
136-
const result = handler(event, context);
140+
const result = handler(event, context.helloworldContext);
137141

138142
// Assess
139143
expect(result).toStrictEqual({ batchItemFailures: [] });
@@ -160,7 +164,7 @@ describe('Function: processPartialResponse()', () => {
160164
};
161165

162166
// Act
163-
const result = handler(event, context);
167+
const result = handler(event, context.helloworldContext);
164168

165169
// Assess
166170
expect(result).toStrictEqual({ batchItemFailures: [] });
@@ -169,7 +173,7 @@ describe('Function: processPartialResponse()', () => {
169173
test('Process partial response through handler for SQS records with incorrect event type', () => {
170174
// Prepare
171175
const processor = new BatchProcessorSync(EventType.SQS);
172-
const event = dummyEvent;
176+
const event = dummyEvent.Custom;
173177

174178
const handler = (
175179
event: SQSEvent,
@@ -179,7 +183,9 @@ describe('Function: processPartialResponse()', () => {
179183
};
180184

181185
// Act & Assess
182-
expect(() => handler(event as unknown as SQSEvent, context)).toThrowError(
186+
expect(() =>
187+
handler(event as unknown as SQSEvent, context.helloworldContext)
188+
).toThrowError(
183189
`Unexpected batch type. Possible values are: ${Object.keys(
184190
EventType
185191
).join(', ')}`
@@ -210,7 +216,7 @@ describe('Function: processPartialResponse()', () => {
210216
};
211217

212218
// Act
213-
const result = handler(event, context);
219+
const result = handler(event, context.helloworldContext);
214220

215221
// Assess
216222
expect(result).toStrictEqual({ batchItemFailures: [] });

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ module.exports = {
44
color: 'red',
55
},
66
preset: 'ts-jest',
7+
moduleNameMapper: {
8+
'^(\\.{1,2}/.*)\\.js$': '$1',
9+
},
710
transform: {
811
'^.+\\.ts?$': 'ts-jest',
912
},
@@ -13,7 +16,7 @@ module.exports = {
1316
roots: ['<rootDir>/src', '<rootDir>/tests'],
1417
testPathIgnorePatterns: ['/node_modules/'],
1518
testEnvironment: 'node',
16-
coveragePathIgnorePatterns: ['/node_modules/'],
19+
coveragePathIgnorePatterns: ['/node_modules/', 'src/types/index.ts'],
1720
coverageThreshold: {
1821
global: {
1922
statements: 100,

Diff for: packages/commons/package.json

+31-5
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,45 @@
1616
"test:e2e": "echo 'Not Applicable'",
1717
"watch": "jest --watch",
1818
"generateVersionFile": "echo \"// this file is auto generated, do not modify\nexport const PT_VERSION = '$(jq -r '.version' package.json)';\" > src/version.ts",
19-
"build": "tsc --build --force",
19+
"build:cjs": "tsc --build --force && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
20+
"build:esm": "tsc --project tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
21+
"build": "npm run build:esm & npm run build:cjs",
2022
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
2123
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
22-
"prebuild": "rimraf ./lib",
23-
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
24+
"prepack": "rimraf ./lib/*.tsbuildinfo && node ../../.github/scripts/release_patch_package_json.js ."
2425
},
2526
"lint-staged": {
2627
"*.{js,ts}": "npm run lint-fix"
2728
},
2829
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/metrics#readme",
2930
"license": "MIT-0",
30-
"main": "./lib/index.js",
31-
"types": "./lib/index.d.ts",
31+
"type": "module",
32+
"exports": {
33+
".": {
34+
"require": {
35+
"types": "./lib/cjs/index.d.ts",
36+
"default": "./lib/cjs/index.js"
37+
},
38+
"import": {
39+
"types": "./lib/esm/index.d.ts",
40+
"default": "./lib/esm/index.js"
41+
}
42+
},
43+
"./types": {
44+
"import": "./lib/esm/types/index.js",
45+
"require": "./lib/cjs/types/index.js"
46+
}
47+
},
48+
"typesVersions": {
49+
"*": {
50+
"types": [
51+
"lib/cjs/types/index.d.ts",
52+
"lib/esm/types/index.d.ts"
53+
]
54+
}
55+
},
56+
"types": "./lib/cjs/index.d.ts",
57+
"main": "./lib/cjs/index.js",
3258
"files": [
3359
"lib"
3460
],

Diff for: packages/commons/src/awsSdk/index.ts

-2
This file was deleted.

Diff for: packages/commons/src/awsSdk/utils.ts

-25
This file was deleted.

0 commit comments

Comments
 (0)