Skip to content

Commit

Permalink
feat(logger): add esmodule support (#1734)
Browse files Browse the repository at this point in the history
* feat(logger): add esm build output

* fix(Logger): Remove barrel files update references

* test(Logger): update jest/ts-jest to use ESM

* chore(Logger): remove unused lodash.merge

* fix(logger): reinstate lodash.merge

* chore(logger): revert TS assertion

* chore(logger): revert format changes

* chore(logger): update postbuild to remove incremental tsbuildinfo files

* fix(logger): correct reference to types output

* feat(logging): add middleware export

* chore(logger): replace postbuild script with echo statement

* feat(logger): add typesVersions property and barrel files to /types

* chore(logger): file not used, can be added back if needed

* chore(logger): add space back to README

* chore(logger): revert space in README
  • Loading branch information
antstanley authored and dreamorosi committed Feb 20, 2024
1 parent 5c8b2b1 commit 8629016
Show file tree
Hide file tree
Showing 29 changed files with 165 additions and 93 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,8 @@ tmp

# TS build files
tsconfig.tsbuildinfo
.tsbuildinfo
<<<<<<< HEAD
.tsbuildinfo
=======
tsconfig.esm.tsbuildinfo
>>>>>>> 0bc7960c (feat(logger): add esmodule support (#1734))
2 changes: 1 addition & 1 deletion packages/logger/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,4 @@ Credits for the Powertools for AWS Lambda (TypeScript) idea go to [DAZN](https:/

## License

This library is licensed under the MIT-0 License. See the LICENSE file.
This library is licensed under the MIT-0 License. See the LICENSE file.
12 changes: 10 additions & 2 deletions packages/logger/jest.config.js → packages/logger/jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ module.exports = {
color: 'cyan',
},
runner: 'groups',
preset: 'ts-jest',
extensionsToTreatAsEsm: ['.ts'],
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
'^.+\\.ts?$': 'ts-jest',
'^.+\\.[tj]sx?$': [
'ts-jest',
{
useESM: true,
},
],
},
moduleFileExtensions: ['js', 'ts'],
collectCoverageFrom: ['**/src/**/*.ts', '!**/node_modules/**'],
Expand Down
44 changes: 40 additions & 4 deletions packages/logger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,55 @@
"test:e2e:nodejs20x": "RUNTIME=nodejs20x jest --group=e2e",
"test:e2e": "jest --group=e2e",
"watch": "jest --watch --group=unit",
"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",
"postbuild": "rimraf ./lib/*.tsbuildinfo",
"prepack": "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/logger#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"
}
},
"./middleware": {
"import": "./lib/esm/middleware/middy.js",
"require": "./lib/cjs/middleware/middy.js"
},
"./types": {
"import": "./lib/esm/types/index.js",
"require": "./lib/cjs/types/index.js"
}
},
"typesVersions": {
"*": {
"middleware": [
"lib/cjs/middleware/middy.d.ts",
"lib/esm/middleware/middy.d.ts"
],
"types": [
"lib/cjs/types/index.d.ts",
"lib/esm/types/index.d.ts"
]
}
},
"types": "./lib/cjs/index.d.ts",
"main": "./lib/cjs/index.js",
"devDependencies": {
"@aws-lambda-powertools/testing-utils": "file:../testing",
"@types/lodash.merge": "^4.6.9"
Expand Down Expand Up @@ -66,4 +102,4 @@
"serverless",
"nodejs"
]
}
}
23 changes: 13 additions & 10 deletions packages/logger/src/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@ import { Console } from 'node:console';
import { format } from 'node:util';
import type { Context, Handler } from 'aws-lambda';
import { Utility } from '@aws-lambda-powertools/commons';
import { LogFormatterInterface, PowertoolsLogFormatter } from './formatter';
import { LogItem } from './log';
import { PowertoolsLogFormatter } from './formatter/PowertoolsLogFormatter.js';
import { LogFormatterInterface } from './formatter/LogFormatterInterface.js';
import { LogItem } from './log/LogItem.js';
import merge from 'lodash.merge';
import { ConfigServiceInterface, EnvironmentVariablesService } from './config';
import { LogJsonIndent } from './types';
import { ConfigServiceInterface } from './config/ConfigServiceInterface.js';
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js';
import { LogJsonIndent } from './types/Logger.js';
import type {
ClassThatLogs,
Environment,
LogAttributes,
LogLevel,
LogLevelThresholds,
} from './types/Log.js';
import type {
ClassThatLogs,
HandlerMethodDecorator,
LambdaFunctionContext,
LogAttributes,
ConstructorOptions,
LogItemExtraInput,
LogItemMessage,
LogLevel,
LogLevelThresholds,
PowertoolLogData,
HandlerOptions,
} from './types';

} from './types/Logger.js';
/**
* ## Intro
* The Logger utility provides an opinionated logger with output structured as JSON.
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/config/EnvironmentVariablesService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfigServiceInterface } from './ConfigServiceInterface';
import { ConfigServiceInterface } from './ConfigServiceInterface.js';
import { EnvironmentVariablesService as CommonEnvironmentVariablesService } from '@aws-lambda-powertools/commons';

/**
Expand Down
2 changes: 0 additions & 2 deletions packages/logger/src/config/index.ts

This file was deleted.

7 changes: 4 additions & 3 deletions packages/logger/src/formatter/LogFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { LogFormatterInterface } from '.';
import { LogAttributes, UnformattedAttributes } from '../types';
import { LogItem } from '../log';
import { LogFormatterInterface } from './LogFormatterInterface.js';
import { LogAttributes } from '../types/Log.js';
import { UnformattedAttributes } from '../types/Logger.js';
import { LogItem } from '../log/LogItem.js';

/**
* Typeguard to monkey patch Error to add a cause property.
Expand Down
5 changes: 3 additions & 2 deletions packages/logger/src/formatter/LogFormatterInterface.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LogAttributes, UnformattedAttributes } from '../types';
import { LogItem } from '../log';
import { LogAttributes } from '../types/Log.js';
import { UnformattedAttributes } from '../types/Logger.js';
import { LogItem } from '../log/LogItem.js';

/**
* @interface
Expand Down
9 changes: 5 additions & 4 deletions packages/logger/src/formatter/PowertoolsLogFormatter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { LogFormatter } from '.';
import { LogAttributes, UnformattedAttributes } from '../types';
import { PowertoolsLog } from '../types/formats';
import { LogItem } from '../log';
import { LogFormatter } from './LogFormatter.js';
import { LogAttributes } from '../types/Log.js';
import { UnformattedAttributes } from '../types/Logger.js';
import { PowertoolsLog } from '../types/formats/PowertoolsLog.js';
import { LogItem } from '../log/LogItem.js';

/**
* This class is used to transform a set of log key-value pairs
Expand Down
3 changes: 0 additions & 3 deletions packages/logger/src/formatter/index.ts

This file was deleted.

8 changes: 5 additions & 3 deletions packages/logger/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './Logger';
export * from './middleware';
export * from './formatter';
export { Logger } from './Logger.js';
export { injectLambdaContext } from './middleware/middy.js';
export { PowertoolsLogFormatter } from './formatter/PowertoolsLogFormatter.js';
export { LogFormatter } from './formatter/LogFormatter.js';
export { LogFormatterInterface } from './formatter/LogFormatterInterface.js';
4 changes: 2 additions & 2 deletions packages/logger/src/log/LogItem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import merge from 'lodash.merge';
import { LogItemInterface } from '.';
import { LogAttributes } from '../types';
import { LogItemInterface } from './LogItemInterface.js';
import { LogAttributes } from '../types/Log.js';

class LogItem implements LogItemInterface {
private attributes: LogAttributes = {};
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/log/LogItemInterface.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { LogAttributes } from '../types';
import { LogAttributes } from '../types/Log.js';

interface LogItemInterface {
addAttributes(attributes: LogAttributes): void;
Expand Down
2 changes: 0 additions & 2 deletions packages/logger/src/log/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/logger/src/middleware/index.ts

This file was deleted.

5 changes: 3 additions & 2 deletions packages/logger/src/middleware/middy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Logger } from '../Logger';
import { HandlerOptions, LogAttributes } from '../types';
import { Logger } from '../Logger.js';
import { LogAttributes } from '../types/Log.js';
import { HandlerOptions } from '../types/Logger.js';
import { LOGGER_KEY } from '@aws-lambda-powertools/commons/lib/middleware';
import type {
MiddlewareLikeObj,
Expand Down
6 changes: 3 additions & 3 deletions packages/logger/src/types/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import {
SyncHandler,
} from '@aws-lambda-powertools/commons';
import { Handler } from 'aws-lambda';
import { ConfigServiceInterface } from '../config';
import { LogFormatterInterface } from '../formatter';
import { ConfigServiceInterface } from '../config/ConfigServiceInterface.js';
import { LogFormatterInterface } from '../formatter/LogFormatterInterface.js';
import {
Environment,
LogAttributes,
LogAttributesWithMessage,
LogLevel,
} from './Log';
} from './Log.js';

type ClassThatLogs = {
[key in Exclude<Lowercase<LogLevel>, 'silent'>]: (
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/types/formats/PowertoolsLog.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LogAttributes, LogLevel } from '..';
import type { LogAttributes, LogLevel } from '../Log.js';

type PowertoolsLog = LogAttributes & {
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/types/formats/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './PowertoolsLog';
export type { PowertoolsLog } from './PowertoolsLog.js';
22 changes: 20 additions & 2 deletions packages/logger/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
export * from './Log';
export * from './Logger';
export type {
LogAttributesWithMessage,
LogAttributeValue,
Environment,
LogLevelThresholds,
LogAttributes,
LogLevel,
} from './Log.js';

export type {
ClassThatLogs,
LogItemMessage,
LogItemExtraInput,
HandlerMethodDecorator,
LambdaFunctionContext,
UnformattedAttributes,
PowertoolLogData,
ConstructorOptions,
HandlerOptions,
} from './Logger.js';
17 changes: 7 additions & 10 deletions packages/logger/tests/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "../",
"noEmit": true
},
"include": [
"../src/**/*",
"./**/*",
]
}
"extends": "../tsconfig.json",
"compilerOptions": {
"rootDir": "../",
"noEmit": true
},
"include": ["../src/**/*", "./**/*"]
}
16 changes: 7 additions & 9 deletions packages/logger/tests/unit/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@ import {
Events as dummyEvent,
LambdaInterface,
} from '@aws-lambda-powertools/commons';
import { Logger } from '../../src';
import {
ConfigServiceInterface,
EnvironmentVariablesService,
} from '../../src/config';
import { PowertoolsLogFormatter, LogFormatter } from '../../src/formatter';
import { Logger } from '../../src/Logger.js';
import { ConfigServiceInterface } from '../../src/config/ConfigServiceInterface.js';
import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js';
import { PowertoolsLogFormatter } from '../../src/formatter/PowertoolsLogFormatter.js';
import { LogFormatter } from '../../src/formatter/LogFormatter.js';
import { LogLevelThresholds, LogLevel } from '../../src/types/Log.js';
import {
ClassThatLogs,
LogJsonIndent,
ConstructorOptions,
LogLevelThresholds,
LogLevel,
} from '../../src/types';
} from '../../src/types/Logger.js';
import type { Context } from 'aws-lambda';
import { Console } from 'console';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*
* @group unit/logger/all
*/
import { EnvironmentVariablesService } from '../../../src/config';
import { EnvironmentVariablesService } from '../../../src/config/EnvironmentVariablesService.js';

describe('Class: EnvironmentVariablesService', () => {
const ENVIRONMENT_VARIABLES = process.env;
Expand Down
14 changes: 6 additions & 8 deletions packages/logger/tests/unit/middleware/middy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ import {
Events as dummyEvent,
} from '@aws-lambda-powertools/commons';
import { cleanupMiddlewares } from '@aws-lambda-powertools/commons/lib/middleware';
import {
ConfigServiceInterface,
EnvironmentVariablesService,
} from '../../../src/config';
import { injectLambdaContext } from '../../../src/middleware/middy';
import { Logger } from './../../../src';
import { ConfigServiceInterface } from '../../../src/config/ConfigServiceInterface.js';
import { EnvironmentVariablesService } from '../../../src/config/EnvironmentVariablesService.js';
import { injectLambdaContext } from '../../../src/middleware/middy.js';
import { Logger } from './../../../src/Logger.js';
import middy from '@middy/core';
import { PowertoolsLogFormatter } from '../../../src/formatter';
import { Console } from 'console';
import { PowertoolsLogFormatter } from '../../../src/formatter/PowertoolsLogFormatter.js';
import { Console } from 'node:console';
import { Context } from 'aws-lambda';

const mockDate = new Date(1466424490000);
Expand Down
9 changes: 9 additions & 0 deletions packages/logger/tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.esm.json",
"compilerOptions": {
"baseUrl": ".",
"outDir": "./lib/esm",
"rootDir": "./src"
},
"include": ["./src/**/*"]
}
16 changes: 7 additions & 9 deletions packages/logger/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./lib",
"rootDir": "./src",
},
"include": [
"./src/**/*"
],
}
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./lib/cjs/",
"rootDir": "./src"
},
"include": ["./src/**/*"]
}
7 changes: 2 additions & 5 deletions packages/logger/typedoc.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"extends": ["../../typedoc.base.json"],
"entryPoints": [
"./src/index.ts",
"./src/types"
],
"entryPoints": ["./src/index.ts", "./src/types"],
"readme": "README.md"
}
}
Loading

0 comments on commit 8629016

Please sign in to comment.