Skip to content

Commit 8276cee

Browse files
committed
feat(tracer): add esmodule support (#1741)
* feat(tracer): add esmodule support * chore(docs): update imports
1 parent 3961d15 commit 8276cee

34 files changed

+251
-214
lines changed

examples/cdk/functions/get-all-items.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware';
22
import { logMetrics } from '@aws-lambda-powertools/metrics/middleware';
3-
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer';
3+
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer/middleware';
44
import { ScanCommand } from '@aws-sdk/lib-dynamodb';
55
import middy from '@middy/core';
66
import {

examples/sam/src/get-all-items.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { tableName } from './common/constants';
88
import { logger, tracer, metrics } from './common/powertools';
99
import { logMetrics } from '@aws-lambda-powertools/metrics/middleware';
1010
import { injectLambdaContext } from '@aws-lambda-powertools/logger/middleware';
11-
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer';
11+
import { captureLambdaHandler } from '@aws-lambda-powertools/tracer/middleware';
1212
import { docClient } from './common/dynamodb-client';
1313
import { ScanCommand } from '@aws-sdk/lib-dynamodb';
1414
import { getUuid } from './common/getUuid';

packages/tracer/jest.config.js renamed to packages/tracer/jest.config.cjs

Lines changed: 3 additions & 0 deletions
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
},

packages/tracer/package.json

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717
"test:e2e:nodejs20x": "RUNTIME=nodejs20x jest --group=e2e",
1818
"test:e2e": "jest --group=e2e",
1919
"watch": "jest --watch",
20-
"build": "tsc --build --force",
20+
"build:cjs": "tsc --build --force && echo '{ \"type\": \"commonjs\" }' > lib/cjs/package.json",
21+
"build:esm": "tsc --project tsconfig.esm.json && echo '{ \"type\": \"module\" }' > lib/esm/package.json",
22+
"build": "npm run build:esm & npm run build:cjs",
2123
"lint": "eslint --ext .ts,.js --no-error-on-unmatched-pattern .",
2224
"lint-fix": "eslint --fix --ext .ts,.js --no-error-on-unmatched-pattern .",
2325
"prebuild": "rimraf ./lib",
24-
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
26+
"prepack": "rimraf ./lib/*.tsbuildinfo && node ../../.github/scripts/release_patch_package_json.js ."
2527
},
2628
"lint-staged": {
2729
"*.{js,ts}": "npm run lint-fix"
2830
},
2931
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/tracer#readme",
3032
"license": "MIT-0",
31-
"main": "./lib/index.js",
32-
"types": "./lib/index.d.ts",
3333
"devDependencies": {
3434
"@aws-lambda-powertools/testing-utils": "file:../testing",
3535
"@aws-sdk/client-dynamodb": "^3.499.0",
@@ -47,6 +47,41 @@
4747
"optional": true
4848
}
4949
},
50+
"type": "module",
51+
"exports": {
52+
".": {
53+
"require": {
54+
"types": "./lib/cjs/index.d.ts",
55+
"default": "./lib/cjs/index.js"
56+
},
57+
"import": {
58+
"types": "./lib/esm/index.d.ts",
59+
"default": "./lib/esm/index.js"
60+
}
61+
},
62+
"./middleware": {
63+
"import": "./lib/esm/middleware/middy.js",
64+
"require": "./lib/cjs/middleware/middy.js"
65+
},
66+
"./types": {
67+
"import": "./lib/esm/types/index.js",
68+
"require": "./lib/cjs/types/index.js"
69+
}
70+
},
71+
"typesVersions": {
72+
"*": {
73+
"middleware": [
74+
"lib/cjs/middleware/middy.d.ts",
75+
"lib/esm/middleware/middy.d.ts"
76+
],
77+
"types": [
78+
"lib/cjs/types/index.d.ts",
79+
"lib/esm/types/index.d.ts"
80+
]
81+
}
82+
},
83+
"types": "./lib/cjs/index.d.ts",
84+
"main": "./lib/cjs/index.js",
5085
"files": [
5186
"lib"
5287
],

packages/tracer/src/Tracer.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ import { Utility } from '@aws-lambda-powertools/commons';
33
import type {
44
AsyncHandler,
55
SyncHandler,
6+
HandlerMethodDecorator,
67
} from '@aws-lambda-powertools/commons/types';
7-
import type { TracerInterface } from '.';
8-
import {
9-
type ConfigServiceInterface,
10-
EnvironmentVariablesService,
11-
} from './config';
8+
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js';
9+
import type { ConfigServiceInterface } from './types/ConfigServiceInterface.js';
1210
import type {
13-
HandlerMethodDecorator,
11+
TracerInterface,
1412
TracerOptions,
13+
AnyClass,
1514
MethodDecorator,
1615
CaptureLambdaHandlerOptions,
1716
CaptureMethodOptions,
18-
} from './types';
19-
import { ProviderService, type ProviderServiceInterface } from './provider';
17+
} from './types/Tracer.js';
18+
import { ProviderService } from './provider/ProviderService.js';
19+
import type { ProviderServiceInterface } from './types/ProviderServiceInterface.js';
2020
import { type Segment, Subsegment } from 'aws-xray-sdk-core';
2121

2222
/**
@@ -460,7 +460,9 @@ class Tracer extends Utility implements TracerInterface {
460460
* @decorator Class
461461
* @param options - (_optional_) Options for the decorator
462462
*/
463-
public captureMethod(options?: CaptureMethodOptions): MethodDecorator {
463+
public captureMethod<T extends AnyClass>(
464+
options?: CaptureMethodOptions
465+
): MethodDecorator<T> {
464466
return (_target, propertyKey, descriptor) => {
465467
// The descriptor.value is the method this decorator decorates, it cannot be undefined.
466468
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

packages/tracer/src/TracerInterface.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.

packages/tracer/src/config/EnvironmentVariablesService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ConfigServiceInterface } from './ConfigServiceInterface';
1+
import { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';
22
import { EnvironmentVariablesService as CommonEnvironmentVariablesService } from '@aws-lambda-powertools/commons';
33

44
class EnvironmentVariablesService

packages/tracer/src/config/index.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

packages/tracer/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
export * from './Tracer';
2-
export * from './TracerInterface';
3-
export * from './middleware/middy';
1+
export { Tracer } from './Tracer.js';

packages/tracer/src/middleware/middy.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { TRACER_KEY } from '@aws-lambda-powertools/commons';
2-
import type { Tracer } from '../Tracer';
2+
import type { Tracer } from '../Tracer.js';
33
import type { Segment, Subsegment } from 'aws-xray-sdk-core';
4-
import type { CaptureLambdaHandlerOptions } from '../types';
4+
import type { CaptureLambdaHandlerOptions } from '../types/Tracer.js';
55
import type {
66
MiddlewareLikeObj,
77
MiddyLikeRequest,

0 commit comments

Comments
 (0)