Skip to content

Commit

Permalink
feat(commons): add esmodule support (#1735)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
dreamorosi committed Feb 5, 2024
1 parent 2759ad9 commit ddedc4d
Show file tree
Hide file tree
Showing 73 changed files with 248 additions and 192 deletions.
2 changes: 1 addition & 1 deletion examples/cdk/functions/common/powertools.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Logger } from '@aws-lambda-powertools/logger';
import { Metrics } from '@aws-lambda-powertools/metrics';
import { Tracer } from '@aws-lambda-powertools/tracer';
import { PT_VERSION } from '@aws-lambda-powertools/commons/lib/version';
import { PT_VERSION } from '@aws-lambda-powertools/commons';

const defaultValues = {
region: process.env.AWS_REGION || 'N/A',
Expand Down
2 changes: 1 addition & 1 deletion examples/cdk/functions/get-all-items.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { injectLambdaContext } from '@aws-lambda-powertools/logger';
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware';
import { logMetrics } from '@aws-lambda-powertools/metrics';
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer';
import { ScanCommand } from '@aws-sdk/lib-dynamodb';
Expand Down
2 changes: 1 addition & 1 deletion examples/cdk/functions/get-by-id.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LambdaInterface } from '@aws-lambda-powertools/commons';
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
import { GetCommand } from '@aws-sdk/lib-dynamodb';
import {
APIGatewayProxyEvent,
Expand Down
2 changes: 1 addition & 1 deletion examples/cdk/functions/uuid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { randomUUID } from 'node:crypto';

exports.handler = async (_event) => {
exports.handler = async () => {
return {
statusCode: 200,
body: JSON.stringify(randomUUID()),
Expand Down
2 changes: 1 addition & 1 deletion examples/sam/src/common/powertools.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Logger } from '@aws-lambda-powertools/logger';
import { Metrics } from '@aws-lambda-powertools/metrics';
import { Tracer } from '@aws-lambda-powertools/tracer';
import { PT_VERSION } from '@aws-lambda-powertools/commons/lib/version';
import { PT_VERSION } from '@aws-lambda-powertools/commons';

const defaultValues = {
region: process.env.AWS_REGION || 'N/A',
Expand Down
2 changes: 1 addition & 1 deletion examples/sam/src/get-all-items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import middy from '@middy/core';
import { tableName } from './common/constants';
import { logger, tracer, metrics } from './common/powertools';
import { logMetrics } from '@aws-lambda-powertools/metrics';
import { injectLambdaContext } from '@aws-lambda-powertools/logger';
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware';
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer';
import { docClient } from './common/dynamodb-client';
import { ScanCommand } from '@aws-sdk/lib-dynamodb';
Expand Down
2 changes: 1 addition & 1 deletion examples/sam/src/get-by-id.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LambdaInterface } from '@aws-lambda-powertools/commons';
import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
import { GetCommand } from '@aws-sdk/lib-dynamodb';
import {
APIGatewayProxyEvent,
Expand Down
2 changes: 1 addition & 1 deletion examples/sam/src/get-uuid.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { randomUUID } from 'node:crypto';

exports.handler = async (_event) => {
exports.handler = async () => {
return {
statusCode: 200,
body: JSON.stringify(randomUUID()),
Expand Down
6 changes: 4 additions & 2 deletions packages/batch/tests/unit/BatchProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @group unit/batch/class/asyncBatchProcessor
*/
import type { Context } from 'aws-lambda';
import { helloworldContext as dummyContext } from '@aws-lambda-powertools/commons/lib/samples/resources/contexts';
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';
Expand All @@ -23,7 +23,9 @@ import {

describe('Class: AsyncBatchProcessor', () => {
const ENVIRONMENT_VARIABLES = process.env;
const options: BatchProcessingOptions = { context: dummyContext };
const options: BatchProcessingOptions = {
context: dummyContext.helloworldContext,
};

beforeEach(() => {
jest.clearAllMocks();
Expand Down
6 changes: 4 additions & 2 deletions packages/batch/tests/unit/BatchProcessorSync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @group unit/batch/class/batchprocessor
*/
import type { Context } from 'aws-lambda';
import { helloworldContext as dummyContext } from '@aws-lambda-powertools/commons/lib/samples/resources/contexts';
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';
Expand All @@ -23,7 +23,9 @@ import {

describe('Class: BatchProcessor', () => {
const ENVIRONMENT_VARIABLES = process.env;
const options: BatchProcessingOptions = { context: dummyContext };
const options: BatchProcessingOptions = {
context: dummyContext.helloworldContext,
};

beforeEach(() => {
jest.clearAllMocks();
Expand Down
22 changes: 13 additions & 9 deletions packages/batch/tests/unit/processPartialResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import type {
KinesisStreamEvent,
SQSEvent,
} from 'aws-lambda';
import { helloworldContext as dummyContext } from '@aws-lambda-powertools/commons/lib/samples/resources/contexts';
import { Custom as dummyEvent } from '@aws-lambda-powertools/commons/lib/samples/resources/events';
import {
ContextExamples as dummyContext,
Events as dummyEvent,
} from '@aws-lambda-powertools/commons';
import { BatchProcessor } from '../../src/BatchProcessor';
import { processPartialResponse } from '../../src/processPartialResponse';
import { EventType } from '../../src/constants';
Expand All @@ -33,7 +35,9 @@ import {
describe('Function: processPartialResponse()', () => {
const ENVIRONMENT_VARIABLES = process.env;
const context = dummyContext;
const options: BatchProcessingOptions = { context: dummyContext };
const options: BatchProcessingOptions = {
context: dummyContext.helloworldContext,
};

beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -106,7 +110,7 @@ describe('Function: processPartialResponse()', () => {
};

// Act
const result = await handler(event, context);
const result = await handler(event, context.helloworldContext);

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

// Act
const result = await handler(event, context);
const result = await handler(event, context.helloworldContext);

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

// Act
const result = await handler(event, context);
const result = await handler(event, context.helloworldContext);

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

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

// Act & Assess
await expect(() =>
handler(event as unknown as SQSEvent, context)
handler(event as unknown as SQSEvent, context.helloworldContext)
).rejects.toThrowError(
`Unexpected batch type. Possible values are: ${Object.keys(
EventType
Expand Down Expand Up @@ -216,7 +220,7 @@ describe('Function: processPartialResponse()', () => {
};

// Act
const result = await handler(event, context);
const result = await handler(event, context.helloworldContext);

// Assess
expect(result).toStrictEqual({ batchItemFailures: [] });
Expand Down
24 changes: 15 additions & 9 deletions packages/batch/tests/unit/processPartialResponseSync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import type {
KinesisStreamEvent,
SQSEvent,
} from 'aws-lambda';
import { helloworldContext as dummyContext } from '@aws-lambda-powertools/commons/lib/samples/resources/contexts';
import { Custom as dummyEvent } from '@aws-lambda-powertools/commons/lib/samples/resources/events';
import {
ContextExamples as dummyContext,
Events as dummyEvent,
} from '@aws-lambda-powertools/commons';
import { BatchProcessorSync } from '../../src/BatchProcessorSync';
import { processPartialResponseSync } from '../../src/processPartialResponseSync';
import { EventType } from '../../src/constants';
Expand All @@ -33,7 +35,9 @@ import {
describe('Function: processPartialResponse()', () => {
const ENVIRONMENT_VARIABLES = process.env;
const context = dummyContext;
const options: BatchProcessingOptions = { context: dummyContext };
const options: BatchProcessingOptions = {
context: dummyContext.helloworldContext,
};

beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -106,7 +110,7 @@ describe('Function: processPartialResponse()', () => {
};

// Act
const result = handler(event, context);
const result = handler(event, context.helloworldContext);

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

// Act
const result = handler(event, context);
const result = handler(event, context.helloworldContext);

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

// Act
const result = handler(event, context);
const result = handler(event, context.helloworldContext);

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

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

// Act & Assess
expect(() => handler(event as unknown as SQSEvent, context)).toThrowError(
expect(() =>
handler(event as unknown as SQSEvent, context.helloworldContext)
).toThrowError(
`Unexpected batch type. Possible values are: ${Object.keys(
EventType
).join(', ')}`
Expand Down Expand Up @@ -210,7 +216,7 @@ describe('Function: processPartialResponse()', () => {
};

// Act
const result = handler(event, context);
const result = handler(event, context.helloworldContext);

// Assess
expect(result).toStrictEqual({ batchItemFailures: [] });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ module.exports = {
color: 'red',
},
preset: 'ts-jest',
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
'^.+\\.ts?$': 'ts-jest',
},
Expand All @@ -13,7 +16,7 @@ module.exports = {
roots: ['<rootDir>/src', '<rootDir>/tests'],
testPathIgnorePatterns: ['/node_modules/'],
testEnvironment: 'node',
coveragePathIgnorePatterns: ['/node_modules/'],
coveragePathIgnorePatterns: ['/node_modules/', 'src/types/index.ts'],
coverageThreshold: {
global: {
statements: 100,
Expand Down
36 changes: 31 additions & 5 deletions packages/commons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,45 @@
"test:e2e": "echo 'Not Applicable'",
"watch": "jest --watch",
"generateVersionFile": "echo \"// this file is auto generated, do not modify\nexport const PT_VERSION = '$(jq -r '.version' package.json)';\" > src/version.ts",
"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/metrics#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/index.js",
"require": "./lib/cjs/types/index.js"
}
},
"typesVersions": {
"*": {
"types": [
"lib/cjs/types/index.d.ts",
"lib/esm/types/index.d.ts"
]
}
},
"types": "./lib/cjs/index.d.ts",
"main": "./lib/cjs/index.js",
"files": [
"lib"
],
Expand Down
2 changes: 0 additions & 2 deletions packages/commons/src/awsSdk/index.ts

This file was deleted.

25 changes: 0 additions & 25 deletions packages/commons/src/awsSdk/utils.ts

This file was deleted.

Loading

0 comments on commit ddedc4d

Please sign in to comment.