Skip to content

Commit 0de2ffd

Browse files
authored
feat(tracer): add esmodule support (#1741)
* feat(tracer): add esmodule support * chore(docs): update imports
1 parent e5e42d0 commit 0de2ffd

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
@@ -16,19 +16,19 @@
1616
"test:e2e:nodejs18x": "RUNTIME=nodejs18x jest --group=e2e",
1717
"test:e2e": "jest --group=e2e",
1818
"watch": "jest --watch",
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 .",
2224
"prebuild": "rimraf ./lib",
23-
"prepack": "node ../../.github/scripts/release_patch_package_json.js ."
25+
"prepack": "rimraf ./lib/*.tsbuildinfo && node ../../.github/scripts/release_patch_package_json.js ."
2426
},
2527
"lint-staged": {
2628
"*.{js,ts}": "npm run lint-fix"
2729
},
2830
"homepage": "https://github.com/aws-powertools/powertools-lambda-typescript/tree/main/packages/tracer#readme",
2931
"license": "MIT-0",
30-
"main": "./lib/index.js",
31-
"types": "./lib/index.d.ts",
3232
"devDependencies": {
3333
"@aws-lambda-powertools/testing-utils": "file:../testing",
3434
"@aws-sdk/client-dynamodb": "^3.413.0",
@@ -46,6 +46,41 @@
4646
"optional": true
4747
}
4848
},
49+
"type": "module",
50+
"exports": {
51+
".": {
52+
"require": {
53+
"types": "./lib/cjs/index.d.ts",
54+
"default": "./lib/cjs/index.js"
55+
},
56+
"import": {
57+
"types": "./lib/esm/index.d.ts",
58+
"default": "./lib/esm/index.js"
59+
}
60+
},
61+
"./middleware": {
62+
"import": "./lib/esm/middleware/middy.js",
63+
"require": "./lib/cjs/middleware/middy.js"
64+
},
65+
"./types": {
66+
"import": "./lib/esm/types/index.js",
67+
"require": "./lib/cjs/types/index.js"
68+
}
69+
},
70+
"typesVersions": {
71+
"*": {
72+
"middleware": [
73+
"lib/cjs/middleware/middy.d.ts",
74+
"lib/esm/middleware/middy.d.ts"
75+
],
76+
"types": [
77+
"lib/cjs/types/index.d.ts",
78+
"lib/esm/types/index.d.ts"
79+
]
80+
}
81+
},
82+
"types": "./lib/cjs/index.d.ts",
83+
"main": "./lib/cjs/index.js",
4984
"files": [
5085
"lib"
5186
],

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
/**
@@ -461,7 +461,9 @@ class Tracer extends Utility implements TracerInterface {
461461
* @decorator Class
462462
* @param options - (_optional_) Options for the decorator
463463
*/
464-
public captureMethod(options?: CaptureMethodOptions): MethodDecorator {
464+
public captureMethod<T extends AnyClass>(
465+
options?: CaptureMethodOptions
466+
): MethodDecorator<T> {
465467
return (_target, propertyKey, descriptor) => {
466468
// The descriptor.value is the method this decorator decorates, it cannot be undefined.
467469
// 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)