diff --git a/packages/idempotency/jest.config.js b/packages/idempotency/jest.config.cjs similarity index 93% rename from packages/idempotency/jest.config.js rename to packages/idempotency/jest.config.cjs index 7a512f0d83..758a5e9f23 100644 --- a/packages/idempotency/jest.config.js +++ b/packages/idempotency/jest.config.cjs @@ -5,6 +5,9 @@ module.exports = { }, runner: 'groups', preset: 'ts-jest', + moduleNameMapper: { + '^(\\.{1,2}/.*)\\.js$': '$1', + }, transform: { '^.+\\.ts?$': 'ts-jest', }, diff --git a/packages/idempotency/package.json b/packages/idempotency/package.json index 26820ccb84..284d624bfe 100644 --- a/packages/idempotency/package.json +++ b/packages/idempotency/package.json @@ -19,57 +19,78 @@ "test:e2e:nodejs20x": "RUNTIME=nodejs20x jest --group=e2e", "test:e2e": "jest --group=e2e", "watch": "jest --watch", - "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/idempotency#readme", "license": "MIT-0", + "type": "module", "exports": { ".": { - "import": "./lib/index.js", - "require": "./lib/index.js" + "require": { + "types": "./lib/cjs/index.d.ts", + "default": "./lib/cjs/index.js" + }, + "import": { + "types": "./lib/esm/index.d.ts", + "default": "./lib/esm/index.js" + } }, "./persistence": { - "import": "./lib/persistence/index.js", - "require": "./lib/persistence/index.js" + "import": "./lib/esm/persistence/index.js", + "require": "./lib/cjs/persistence/index.js" }, "./dynamodb": { - "import": "./lib/persistence/DynamoDBPersistenceLayer.js", - "require": "./lib/persistence/DynamoDBPersistenceLayer.js" + "import": "./lib/esm/persistence/DynamoDBPersistenceLayer.js", + "require": "./lib/cjs/persistence/DynamoDBPersistenceLayer.js" + }, + "./dynamodb/types": { + "import": "./lib/esm/types/DynamoDBPersistence.js", + "require": "./lib/cjs/types/DynamoDBPersistence.js" }, "./middleware": { - "import": "./lib/middleware/index.js", - "require": "./lib/middleware/index.js" + "import": "./lib/esm/middleware/makeHandlerIdempotent.js", + "require": "./lib/cjs/middleware/makeHandlerIdempotent.js" }, "./types": { - "import": "./lib/types/index.d.ts", - "require": "./lib/types/index.d.ts" + "import": "./lib/esm/types/index.d.ts", + "require": "./lib/esm/types/index.d.ts" } }, "typesVersions": { "*": { "persistence": [ - "lib/persistence/index.d.ts" + "lib/esm/persistence/index.d.ts", + "lib/cjs/persistence/index.d.ts" ], "dynamodb": [ - "lib/persistence/DynamoDBPersistenceLayer.d.ts" + "lib/esm/persistence/DynamoDBPersistenceLayer.d.ts", + "lib/cjs/persistence/DynamoDBPersistenceLayer.d.ts" + ], + "dynamodb/types": [ + "lib/esm/types/DynamoDBPersistence.d.ts", + "lib/cjs/types/DynamoDBPersistence.d.ts" ], "middleware": [ - "lib/middleware/index.d.ts" + "lib/esm/middleware/makeHandlerIdempotent.d.ts", + "lib/cjs/middleware/makeHandlerIdempotent.d.ts" ], "types": [ - "lib/types/index.d.ts" + "lib/esm/types/index.d.ts", + "lib/cjs/types/index.d.ts" ] } }, - "main": "./lib/index.js", - "types": "./lib/index.d.ts", + "types": "./lib/cjs/index.d.ts", + "main": "./lib/cjs/index.js", "files": [ "lib" ], diff --git a/packages/idempotency/src/IdempotencyConfig.ts b/packages/idempotency/src/IdempotencyConfig.ts index 95b8a96a99..0f5afc6f09 100644 --- a/packages/idempotency/src/IdempotencyConfig.ts +++ b/packages/idempotency/src/IdempotencyConfig.ts @@ -1,6 +1,6 @@ -import { EnvironmentVariablesService } from './config'; +import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js'; import type { Context } from 'aws-lambda'; -import type { IdempotencyConfigOptions } from './types'; +import type { IdempotencyConfigOptions } from './types/IdempotencyOptions.js'; /** * Configuration for the idempotency feature. diff --git a/packages/idempotency/src/IdempotencyHandler.ts b/packages/idempotency/src/IdempotencyHandler.ts index 50facddb08..665d3f026d 100644 --- a/packages/idempotency/src/IdempotencyHandler.ts +++ b/packages/idempotency/src/IdempotencyHandler.ts @@ -2,16 +2,20 @@ import type { JSONValue, MiddyLikeRequest, } from '@aws-lambda-powertools/commons/types'; -import type { AnyFunction, IdempotencyHandlerOptions } from './types'; +import type { + AnyFunction, + IdempotencyHandlerOptions, +} from './types/IdempotencyOptions.js'; import { IdempotencyAlreadyInProgressError, IdempotencyInconsistentStateError, IdempotencyItemAlreadyExistsError, IdempotencyPersistenceLayerError, -} from './errors'; -import { BasePersistenceLayer, IdempotencyRecord } from './persistence'; -import { IdempotencyConfig } from './IdempotencyConfig'; -import { MAX_RETRIES, IdempotencyRecordStatus } from './constants'; +} from './errors.js'; +import { BasePersistenceLayer } from './persistence/BasePersistenceLayer.js'; +import { IdempotencyRecord } from './persistence/IdempotencyRecord.js'; +import { IdempotencyConfig } from './IdempotencyConfig.js'; +import { MAX_RETRIES, IdempotencyRecordStatus } from './constants.js'; import { search } from 'jmespath'; /** diff --git a/packages/idempotency/src/config/EnvironmentVariablesService.ts b/packages/idempotency/src/config/EnvironmentVariablesService.ts index f87635593b..a6d3e9b257 100644 --- a/packages/idempotency/src/config/EnvironmentVariablesService.ts +++ b/packages/idempotency/src/config/EnvironmentVariablesService.ts @@ -1,4 +1,4 @@ -import { ConfigServiceInterface } from './ConfigServiceInterface'; +import type { ConfigServiceInterface } from '../types/ConfigServiceInterface.js'; import { EnvironmentVariablesService as CommonEnvironmentVariablesService } from '@aws-lambda-powertools/commons'; /** diff --git a/packages/idempotency/src/config/index.ts b/packages/idempotency/src/config/index.ts deleted file mode 100644 index 3dfae2b192..0000000000 --- a/packages/idempotency/src/config/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './EnvironmentVariablesService'; diff --git a/packages/idempotency/src/idempotencyDecorator.ts b/packages/idempotency/src/idempotencyDecorator.ts index 8e2d7f9265..e912393a85 100644 --- a/packages/idempotency/src/idempotencyDecorator.ts +++ b/packages/idempotency/src/idempotencyDecorator.ts @@ -1,5 +1,8 @@ -import { AnyFunction, ItempotentFunctionOptions } from './types'; -import { makeIdempotent } from './makeIdempotent'; +import { + AnyFunction, + ItempotentFunctionOptions, +} from './types/IdempotencyOptions.js'; +import { makeIdempotent } from './makeIdempotent.js'; /** * Use this decorator to make your lambda handler itempotent. diff --git a/packages/idempotency/src/index.ts b/packages/idempotency/src/index.ts index 0b86f82a69..ced4539b1d 100644 --- a/packages/idempotency/src/index.ts +++ b/packages/idempotency/src/index.ts @@ -1,5 +1,14 @@ -export * from './errors'; -export * from './IdempotencyConfig'; -export * from './makeIdempotent'; -export * from './idempotencyDecorator'; -export { IdempotencyRecordStatus } from './constants'; +export { + IdempotencyItemAlreadyExistsError, + IdempotencyItemNotFoundError, + IdempotencyAlreadyInProgressError, + IdempotencyInvalidStatusError, + IdempotencyValidationError, + IdempotencyInconsistentStateError, + IdempotencyPersistenceLayerError, + IdempotencyKeyError, +} from './errors.js'; +export { IdempotencyConfig } from './IdempotencyConfig.js'; +export { makeIdempotent } from './makeIdempotent.js'; +export { idempotent } from './idempotencyDecorator.js'; +export { IdempotencyRecordStatus } from './constants.js'; diff --git a/packages/idempotency/src/makeIdempotent.ts b/packages/idempotency/src/makeIdempotent.ts index 8bae3cdf6d..dafb807e53 100644 --- a/packages/idempotency/src/makeIdempotent.ts +++ b/packages/idempotency/src/makeIdempotent.ts @@ -3,9 +3,9 @@ import type { AnyFunction, ItempotentFunctionOptions, IdempotencyLambdaHandlerOptions, -} from './types'; -import { IdempotencyHandler } from './IdempotencyHandler'; -import { IdempotencyConfig } from './IdempotencyConfig'; +} from './types/IdempotencyOptions.js'; +import { IdempotencyHandler } from './IdempotencyHandler.js'; +import { IdempotencyConfig } from './IdempotencyConfig.js'; const isContext = (arg: unknown): arg is Context => { return ( diff --git a/packages/idempotency/src/middleware/index.ts b/packages/idempotency/src/middleware/index.ts deleted file mode 100644 index c95b47d06a..0000000000 --- a/packages/idempotency/src/middleware/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './makeHandlerIdempotent'; diff --git a/packages/idempotency/src/middleware/makeHandlerIdempotent.ts b/packages/idempotency/src/middleware/makeHandlerIdempotent.ts index 55edb314dc..3ce92a73a2 100644 --- a/packages/idempotency/src/middleware/makeHandlerIdempotent.ts +++ b/packages/idempotency/src/middleware/makeHandlerIdempotent.ts @@ -1,10 +1,13 @@ -import { IdempotencyHandler } from '../IdempotencyHandler'; -import { IdempotencyConfig } from '../IdempotencyConfig'; +import { IdempotencyHandler } from '../IdempotencyHandler.js'; +import { IdempotencyConfig } from '../IdempotencyConfig.js'; import { cleanupMiddlewares, IDEMPOTENCY_KEY, } from '@aws-lambda-powertools/commons'; -import type { AnyFunction, IdempotencyLambdaHandlerOptions } from '../types'; +import type { + AnyFunction, + IdempotencyLambdaHandlerOptions, +} from '../types/IdempotencyOptions.js'; import type { MiddlewareLikeObj, MiddyLikeRequest, diff --git a/packages/idempotency/src/persistence/BasePersistenceLayer.ts b/packages/idempotency/src/persistence/BasePersistenceLayer.ts index 96b05fb657..aa23d1227d 100644 --- a/packages/idempotency/src/persistence/BasePersistenceLayer.ts +++ b/packages/idempotency/src/persistence/BasePersistenceLayer.ts @@ -1,15 +1,18 @@ import { createHash, Hash } from 'node:crypto'; import { search } from 'jmespath'; -import type { BasePersistenceLayerOptions } from '../types'; -import { IdempotencyRecordStatus } from '../constants'; -import { EnvironmentVariablesService } from '../config'; -import { IdempotencyRecord } from './IdempotencyRecord'; -import { BasePersistenceLayerInterface } from './BasePersistenceLayerInterface'; +import type { + BasePersistenceLayerOptions, + BasePersistenceLayerInterface, +} from '../types/BasePersistenceLayer.js'; +import { IdempotencyRecordStatus } from '../constants.js'; +import { EnvironmentVariablesService } from '../config/EnvironmentVariablesService.js'; +import { IdempotencyRecord } from './IdempotencyRecord.js'; import { IdempotencyItemAlreadyExistsError, + IdempotencyKeyError, IdempotencyValidationError, -} from '../errors'; -import { LRUCache } from './LRUCache'; +} from '../errors.js'; +import { LRUCache } from './LRUCache.js'; import type { JSONValue } from '@aws-lambda-powertools/commons/types'; /** @@ -258,7 +261,9 @@ abstract class BasePersistenceLayer implements BasePersistenceLayerInterface { if (BasePersistenceLayer.isMissingIdempotencyKey(data)) { if (this.throwOnNoIdempotencyKey) { - throw new Error('No data found to create a hashed idempotency_key'); + throw new IdempotencyKeyError( + 'No data found to create a hashed idempotency_key' + ); } console.warn( `No value found for idempotency_key. jmespath: ${this.eventKeyJmesPath}` diff --git a/packages/idempotency/src/persistence/BasePersistenceLayerInterface.ts b/packages/idempotency/src/persistence/BasePersistenceLayerInterface.ts deleted file mode 100644 index f4e792082a..0000000000 --- a/packages/idempotency/src/persistence/BasePersistenceLayerInterface.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { IdempotencyRecord } from './IdempotencyRecord'; -import type { BasePersistenceLayerOptions } from '../types/BasePersistenceLayer'; - -// TODO: move this to types folder -interface BasePersistenceLayerInterface { - configure(options?: BasePersistenceLayerOptions): void; - isPayloadValidationEnabled(): boolean; - saveInProgress(data: unknown, remainingTimeInMillis?: number): Promise; - saveSuccess(data: unknown, result: unknown): Promise; - deleteRecord(data: unknown): Promise; - getRecord(data: unknown): Promise; -} - -export { BasePersistenceLayerInterface }; diff --git a/packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts b/packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts index f30fa83b57..eacbe47e0a 100644 --- a/packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts +++ b/packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts @@ -1,9 +1,9 @@ import { IdempotencyItemAlreadyExistsError, IdempotencyItemNotFoundError, -} from '../errors'; -import { IdempotencyRecordStatus } from '../constants'; -import type { DynamoDBPersistenceOptions } from '../types'; +} from '../errors.js'; +import { IdempotencyRecordStatus } from '../constants.js'; +import type { DynamoDBPersistenceOptions } from '../types/DynamoDBPersistence.js'; import { AttributeValue, ConditionalCheckFailedException, @@ -15,8 +15,8 @@ import { UpdateItemCommand, } from '@aws-sdk/client-dynamodb'; import { marshall, unmarshall } from '@aws-sdk/util-dynamodb'; -import { IdempotencyRecord } from './IdempotencyRecord'; -import { BasePersistenceLayer } from './BasePersistenceLayer'; +import { IdempotencyRecord } from './IdempotencyRecord.js'; +import { BasePersistenceLayer } from './BasePersistenceLayer.js'; import { addUserAgentMiddleware, isSdkClient, diff --git a/packages/idempotency/src/persistence/IdempotencyRecord.ts b/packages/idempotency/src/persistence/IdempotencyRecord.ts index feb618ddf5..3dba4af73c 100644 --- a/packages/idempotency/src/persistence/IdempotencyRecord.ts +++ b/packages/idempotency/src/persistence/IdempotencyRecord.ts @@ -2,9 +2,9 @@ import type { JSONValue } from '@aws-lambda-powertools/commons/types'; import type { IdempotencyRecordOptions, IdempotencyRecordStatusValue, -} from '../types'; -import { IdempotencyRecordStatus } from '../constants'; -import { IdempotencyInvalidStatusError } from '../errors'; +} from '../types/IdempotencyRecord.js'; +import { IdempotencyRecordStatus } from '../constants.js'; +import { IdempotencyInvalidStatusError } from '../errors.js'; /** * Class representing an idempotency record. diff --git a/packages/idempotency/src/persistence/LRUCache.ts b/packages/idempotency/src/persistence/LRUCache.ts index 583456d4aa..45b8630d4a 100644 --- a/packages/idempotency/src/persistence/LRUCache.ts +++ b/packages/idempotency/src/persistence/LRUCache.ts @@ -1,4 +1,4 @@ -import type { LRUCacheOptions } from '../types'; +import type { LRUCacheOptions } from '../types/LRUCache.js'; const DEFAULT_MAX_SIZE = 100; const NEWER = Symbol('newer'); diff --git a/packages/idempotency/src/persistence/index.ts b/packages/idempotency/src/persistence/index.ts index 72ea36e01f..73d7815170 100644 --- a/packages/idempotency/src/persistence/index.ts +++ b/packages/idempotency/src/persistence/index.ts @@ -1,3 +1,2 @@ -export * from './BasePersistenceLayer'; -export * from './BasePersistenceLayerInterface'; -export * from './IdempotencyRecord'; +export { BasePersistenceLayer } from './BasePersistenceLayer.js'; +export { IdempotencyRecord } from './IdempotencyRecord.js'; diff --git a/packages/idempotency/src/types/BasePersistenceLayer.ts b/packages/idempotency/src/types/BasePersistenceLayer.ts index b11db6f6b7..7bd61689f8 100644 --- a/packages/idempotency/src/types/BasePersistenceLayer.ts +++ b/packages/idempotency/src/types/BasePersistenceLayer.ts @@ -1,8 +1,18 @@ -import { IdempotencyConfig } from '../IdempotencyConfig'; +import { IdempotencyRecord } from '../persistence/IdempotencyRecord.js'; +import { IdempotencyConfig } from '../IdempotencyConfig.js'; type BasePersistenceLayerOptions = { config: IdempotencyConfig; functionName?: string; }; -export { BasePersistenceLayerOptions }; +interface BasePersistenceLayerInterface { + configure(options?: BasePersistenceLayerOptions): void; + isPayloadValidationEnabled(): boolean; + saveInProgress(data: unknown, remainingTimeInMillis?: number): Promise; + saveSuccess(data: unknown, result: unknown): Promise; + deleteRecord(data: unknown): Promise; + getRecord(data: unknown): Promise; +} + +export { BasePersistenceLayerOptions, BasePersistenceLayerInterface }; diff --git a/packages/idempotency/src/config/ConfigServiceInterface.ts b/packages/idempotency/src/types/ConfigServiceInterface.ts similarity index 100% rename from packages/idempotency/src/config/ConfigServiceInterface.ts rename to packages/idempotency/src/types/ConfigServiceInterface.ts diff --git a/packages/idempotency/src/types/IdempotencyOptions.ts b/packages/idempotency/src/types/IdempotencyOptions.ts index ea929f765b..54487d3396 100644 --- a/packages/idempotency/src/types/IdempotencyOptions.ts +++ b/packages/idempotency/src/types/IdempotencyOptions.ts @@ -1,6 +1,6 @@ import type { Context } from 'aws-lambda'; -import { BasePersistenceLayer } from '../persistence/BasePersistenceLayer'; -import { IdempotencyConfig } from '../IdempotencyConfig'; +import { BasePersistenceLayer } from '../persistence/BasePersistenceLayer.js'; +import { IdempotencyConfig } from '../IdempotencyConfig.js'; import type { JSONValue } from '@aws-lambda-powertools/commons/types'; /** diff --git a/packages/idempotency/src/types/IdempotencyRecord.ts b/packages/idempotency/src/types/IdempotencyRecord.ts index b7fc48b0dc..af62b2ac22 100644 --- a/packages/idempotency/src/types/IdempotencyRecord.ts +++ b/packages/idempotency/src/types/IdempotencyRecord.ts @@ -1,5 +1,5 @@ import type { JSONValue } from '@aws-lambda-powertools/commons/types'; -import { IdempotencyRecordStatus } from '../constants'; +import { IdempotencyRecordStatus } from '../constants.js'; type IdempotencyRecordStatusValue = (typeof IdempotencyRecordStatus)[keyof typeof IdempotencyRecordStatus]; diff --git a/packages/idempotency/src/types/index.ts b/packages/idempotency/src/types/index.ts index 741a6fa409..c67b3e6fe6 100644 --- a/packages/idempotency/src/types/index.ts +++ b/packages/idempotency/src/types/index.ts @@ -1,5 +1,13 @@ -export * from './IdempotencyRecord'; -export * from './BasePersistenceLayer'; -export * from './IdempotencyOptions'; -export * from './DynamoDBPersistence'; -export * from './LRUCache'; +export { + IdempotencyRecordOptions, + IdempotencyRecordStatusValue, +} from './IdempotencyRecord.js'; +export { + BasePersistenceLayerInterface, + BasePersistenceLayerOptions, +} from './BasePersistenceLayer.js'; +export { + IdempotencyConfigOptions, + IdempotencyLambdaHandlerOptions, + IdempotencyHandlerOptions, +} from './IdempotencyOptions.js'; diff --git a/packages/idempotency/tests/e2e/idempotentDecorator.test.FunctionCode.ts b/packages/idempotency/tests/e2e/idempotentDecorator.test.FunctionCode.ts index 4a6a64f1a9..f55b445f8b 100644 --- a/packages/idempotency/tests/e2e/idempotentDecorator.test.FunctionCode.ts +++ b/packages/idempotency/tests/e2e/idempotentDecorator.test.FunctionCode.ts @@ -1,9 +1,9 @@ import type { Context } from 'aws-lambda'; import type { LambdaInterface } from '@aws-lambda-powertools/commons/types'; -import { idempotent } from '../../src'; -import { Logger } from '../../../logger'; -import { DynamoDBPersistenceLayer } from '../../src/persistence/DynamoDBPersistenceLayer'; -import { IdempotencyConfig } from '../../src/'; +import { idempotent } from '../../src/idempotencyDecorator'; +import { Logger } from '@aws-lambda-powertools/logger'; +import { DynamoDBPersistenceLayer } from '../../src/persistence/DynamoDBPersistenceLayer.js'; +import { IdempotencyConfig } from '../../src/IdempotencyConfig.js'; const IDEMPOTENCY_TABLE_NAME = process.env.IDEMPOTENCY_TABLE_NAME || 'table_name'; diff --git a/packages/idempotency/tests/e2e/idempotentDecorator.test.ts b/packages/idempotency/tests/e2e/idempotentDecorator.test.ts index ec740e0f03..83a863afaa 100644 --- a/packages/idempotency/tests/e2e/idempotentDecorator.test.ts +++ b/packages/idempotency/tests/e2e/idempotentDecorator.test.ts @@ -1,7 +1,7 @@ /** * Test idempotency decorator * - * @group e2e/idempotency + * @group e2e/idempotency/decorator */ import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; import { @@ -9,7 +9,7 @@ import { SETUP_TIMEOUT, TEARDOWN_TIMEOUT, TEST_CASE_TIMEOUT, -} from './constants'; +} from './constants.js'; import { ScanCommand } from '@aws-sdk/lib-dynamodb'; import { createHash } from 'node:crypto'; import { @@ -17,7 +17,7 @@ import { TestInvocationLogs, TestStack, } from '@aws-lambda-powertools/testing-utils'; -import { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources'; +import { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources.js'; import { join } from 'node:path'; import { Duration } from 'aws-cdk-lib'; import { AttributeType } from 'aws-cdk-lib/aws-dynamodb'; diff --git a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.FunctionCode.ts b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.FunctionCode.ts index 499eda7cca..b4aa178d56 100644 --- a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.FunctionCode.ts +++ b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.FunctionCode.ts @@ -1,7 +1,7 @@ import type { Context } from 'aws-lambda'; -import { DynamoDBPersistenceLayer } from '../../src/persistence/DynamoDBPersistenceLayer'; -import { makeHandlerIdempotent } from '../../src/middleware'; -import { IdempotencyConfig } from '../../src'; +import { DynamoDBPersistenceLayer } from '../../src/persistence/DynamoDBPersistenceLayer.js'; +import { makeHandlerIdempotent } from '../../src/middleware/makeHandlerIdempotent.js'; +import { IdempotencyConfig } from '../../src/IdempotencyConfig.js'; import { Logger } from '@aws-lambda-powertools/logger'; import middy from '@middy/core'; diff --git a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts index 1af54e589d..bf67669ab6 100644 --- a/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts +++ b/packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts @@ -13,13 +13,13 @@ import { ScanCommand } from '@aws-sdk/lib-dynamodb'; import { Duration } from 'aws-cdk-lib'; import { createHash } from 'node:crypto'; import { join } from 'node:path'; -import { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources'; +import { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources.js'; import { RESOURCE_NAME_PREFIX, SETUP_TIMEOUT, TEARDOWN_TIMEOUT, TEST_CASE_TIMEOUT, -} from './constants'; +} from './constants.js'; const ddb = new DynamoDBClient({}); diff --git a/packages/idempotency/tests/e2e/makeIdempotent.test.FunctionCode.ts b/packages/idempotency/tests/e2e/makeIdempotent.test.FunctionCode.ts index 9786ddea0e..c627184721 100644 --- a/packages/idempotency/tests/e2e/makeIdempotent.test.FunctionCode.ts +++ b/packages/idempotency/tests/e2e/makeIdempotent.test.FunctionCode.ts @@ -1,8 +1,8 @@ import type { Context } from 'aws-lambda'; -import { DynamoDBPersistenceLayer } from '../../src/persistence/DynamoDBPersistenceLayer'; -import { makeIdempotent } from '../../src'; +import { DynamoDBPersistenceLayer } from '../../src/persistence/DynamoDBPersistenceLayer.js'; +import { makeIdempotent } from '../../src/makeIdempotent.js'; import { Logger } from '@aws-lambda-powertools/logger'; -import { IdempotencyConfig } from '../../src'; +import { IdempotencyConfig } from '../../src/IdempotencyConfig.js'; const IDEMPOTENCY_TABLE_NAME = process.env.IDEMPOTENCY_TABLE_NAME || 'table_name'; diff --git a/packages/idempotency/tests/e2e/makeIdempotent.test.ts b/packages/idempotency/tests/e2e/makeIdempotent.test.ts index ff7c890f59..63da32a705 100644 --- a/packages/idempotency/tests/e2e/makeIdempotent.test.ts +++ b/packages/idempotency/tests/e2e/makeIdempotent.test.ts @@ -13,7 +13,7 @@ import { ScanCommand } from '@aws-sdk/lib-dynamodb'; import { AttributeType } from 'aws-cdk-lib/aws-dynamodb'; import { createHash } from 'node:crypto'; import { join } from 'node:path'; -import { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources'; +import { IdempotencyTestNodejsFunctionAndDynamoTable } from '../helpers/resources.js'; import { RESOURCE_NAME_PREFIX, SETUP_TIMEOUT, diff --git a/packages/idempotency/tests/helpers/idempotencyUtils.ts b/packages/idempotency/tests/helpers/idempotencyUtils.ts index efda071436..8b8eb04f09 100644 --- a/packages/idempotency/tests/helpers/idempotencyUtils.ts +++ b/packages/idempotency/tests/helpers/idempotencyUtils.ts @@ -1,4 +1,4 @@ -import { BasePersistenceLayer } from '../../src/persistence'; +import { BasePersistenceLayer } from '../../src/persistence/BasePersistenceLayer.js'; /** * Dummy class to test the abstract class BasePersistenceLayer. diff --git a/packages/idempotency/tests/unit/config/EnvironmentVariableService.test.ts b/packages/idempotency/tests/unit/EnvironmentVariableService.test.ts similarity index 92% rename from packages/idempotency/tests/unit/config/EnvironmentVariableService.test.ts rename to packages/idempotency/tests/unit/EnvironmentVariableService.test.ts index 85f44eb60a..a7176e7897 100644 --- a/packages/idempotency/tests/unit/config/EnvironmentVariableService.test.ts +++ b/packages/idempotency/tests/unit/EnvironmentVariableService.test.ts @@ -3,7 +3,7 @@ * * @group unit/idempotency/environment-variables-service */ -import { EnvironmentVariablesService } from '../../../src/config'; +import { EnvironmentVariablesService } from '../../src/config/EnvironmentVariablesService.js'; describe('Class: EnvironmentVariableService', () => { const ENVIRONMENT_VARIABLES = process.env; diff --git a/packages/idempotency/tests/unit/IdempotencyConfig.test.ts b/packages/idempotency/tests/unit/IdempotencyConfig.test.ts index f531972135..35551bc6d8 100644 --- a/packages/idempotency/tests/unit/IdempotencyConfig.test.ts +++ b/packages/idempotency/tests/unit/IdempotencyConfig.test.ts @@ -4,8 +4,8 @@ * @group unit/idempotency/config */ import context from '@aws-lambda-powertools/testing-utils/context'; -import { IdempotencyConfig } from '../../src'; -import type { IdempotencyConfigOptions } from '../../src/types'; +import { IdempotencyConfig } from '../../src/index.js'; +import type { IdempotencyConfigOptions } from '../../src/types/index.js'; describe('Class: IdempotencyConfig', () => { const ENVIRONMENT_VARIABLES = process.env; diff --git a/packages/idempotency/tests/unit/IdempotencyHandler.test.ts b/packages/idempotency/tests/unit/IdempotencyHandler.test.ts index 1ebb14e0be..d9e4e0deb7 100644 --- a/packages/idempotency/tests/unit/IdempotencyHandler.test.ts +++ b/packages/idempotency/tests/unit/IdempotencyHandler.test.ts @@ -3,17 +3,17 @@ * * @group unit/idempotency/IdempotencyHandler */ +import { IdempotencyRecord } from '../../src/persistence/index.js'; +import { IdempotencyHandler } from '../../src/IdempotencyHandler.js'; import { + IdempotencyConfig, IdempotencyAlreadyInProgressError, IdempotencyInconsistentStateError, IdempotencyItemAlreadyExistsError, IdempotencyPersistenceLayerError, -} from '../../src/errors'; -import { IdempotencyRecord } from '../../src/persistence'; -import { IdempotencyHandler } from '../../src/IdempotencyHandler'; -import { IdempotencyConfig } from '../../src/'; -import { MAX_RETRIES, IdempotencyRecordStatus } from '../../src/constants'; -import { PersistenceLayerTestClass } from '../helpers/idempotencyUtils'; +} from '../../src/index.js'; +import { MAX_RETRIES, IdempotencyRecordStatus } from '../../src/constants.js'; +import { PersistenceLayerTestClass } from '../helpers/idempotencyUtils.js'; const mockFunctionToMakeIdempotent = jest.fn(); const mockFunctionPayloadToBeHashed = {}; diff --git a/packages/idempotency/tests/unit/idempotencyDecorator.test.ts b/packages/idempotency/tests/unit/idempotencyDecorator.test.ts index acbee6e3bc..0ff10d1894 100644 --- a/packages/idempotency/tests/unit/idempotencyDecorator.test.ts +++ b/packages/idempotency/tests/unit/idempotencyDecorator.test.ts @@ -4,19 +4,22 @@ * @group unit/idempotency/decorator */ -import { BasePersistenceLayer, IdempotencyRecord } from '../../src/persistence'; -import { idempotent } from '../../src/'; -import type { IdempotencyRecordOptions } from '../../src/types'; import { + BasePersistenceLayer, + IdempotencyRecord, +} from '../../src/persistence/index.js'; +import { + idempotent, + IdempotencyConfig, IdempotencyAlreadyInProgressError, IdempotencyInconsistentStateError, IdempotencyItemAlreadyExistsError, IdempotencyPersistenceLayerError, -} from '../../src/errors'; -import { IdempotencyConfig } from '../../src'; +} from '../../src/index.js'; +import type { IdempotencyRecordOptions } from '../../src/types/index.js'; import { Context } from 'aws-lambda'; import context from '@aws-lambda-powertools/testing-utils/context'; -import { IdempotencyRecordStatus } from '../../src/constants'; +import { IdempotencyRecordStatus } from '../../src/constants.js'; const mockSaveInProgress = jest .spyOn(BasePersistenceLayer.prototype, 'saveInProgress') diff --git a/packages/idempotency/tests/unit/makeHandlerIdempotent.test.ts b/packages/idempotency/tests/unit/makeHandlerIdempotent.test.ts index a761a0f527..32854e1bb4 100644 --- a/packages/idempotency/tests/unit/makeHandlerIdempotent.test.ts +++ b/packages/idempotency/tests/unit/makeHandlerIdempotent.test.ts @@ -3,18 +3,19 @@ * * @group unit/idempotency/makeHandlerIdempotent */ -import { makeHandlerIdempotent } from '../../src/middleware'; +import { makeHandlerIdempotent } from '../../src/middleware/makeHandlerIdempotent.js'; import context from '@aws-lambda-powertools/testing-utils/context'; -import { IdempotencyRecord } from '../../src/persistence'; +import { IdempotencyRecord } from '../../src/persistence/index.js'; import { IdempotencyInconsistentStateError, IdempotencyItemAlreadyExistsError, IdempotencyPersistenceLayerError, -} from '../../src/errors'; -import { IdempotencyConfig } from '../../src/'; + IdempotencyConfig, + IdempotencyRecordStatus, +} from '../../src/index.js'; import middy from '@middy/core'; -import { MAX_RETRIES, IdempotencyRecordStatus } from '../../src/constants'; -import { PersistenceLayerTestClass } from '../helpers/idempotencyUtils'; +import { MAX_RETRIES } from '../../src/constants.js'; +import { PersistenceLayerTestClass } from '../helpers/idempotencyUtils.js'; import type { Context } from 'aws-lambda'; const mockIdempotencyOptions = { diff --git a/packages/idempotency/tests/unit/makeIdempotent.test.ts b/packages/idempotency/tests/unit/makeIdempotent.test.ts index d7737e16fd..40e5a3605b 100644 --- a/packages/idempotency/tests/unit/makeIdempotent.test.ts +++ b/packages/idempotency/tests/unit/makeIdempotent.test.ts @@ -3,17 +3,18 @@ * * @group unit/idempotency/makeIdempotent */ -import { IdempotencyRecord } from '../../src/persistence'; -import { makeIdempotent } from '../../src'; +import { IdempotencyRecord } from '../../src/persistence/index.js'; import { + makeIdempotent, IdempotencyInconsistentStateError, IdempotencyItemAlreadyExistsError, IdempotencyPersistenceLayerError, -} from '../../src/errors'; -import { IdempotencyConfig } from '../../src'; + IdempotencyConfig, + IdempotencyRecordStatus, +} from '../../src/index.js'; import context from '@aws-lambda-powertools/testing-utils/context'; -import { MAX_RETRIES, IdempotencyRecordStatus } from '../../src/constants'; -import { PersistenceLayerTestClass } from '../helpers/idempotencyUtils'; +import { MAX_RETRIES } from '../../src/constants.js'; +import { PersistenceLayerTestClass } from '../helpers/idempotencyUtils.js'; import type { Context } from 'aws-lambda'; const mockIdempotencyOptions = { diff --git a/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts b/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts index 61725076e1..7c2d13c3b8 100644 --- a/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts +++ b/packages/idempotency/tests/unit/persistence/BasePersistenceLayer.test.ts @@ -4,16 +4,18 @@ * @group unit/idempotency/persistence/base */ import context from '@aws-lambda-powertools/testing-utils/context'; -import { IdempotencyConfig, IdempotencyRecordStatus } from '../../../src'; -import { - BasePersistenceLayer, - IdempotencyRecord, -} from '../../../src/persistence'; import { + IdempotencyConfig, + IdempotencyRecordStatus, IdempotencyItemAlreadyExistsError, IdempotencyValidationError, -} from '../../../src/errors'; -import type { IdempotencyConfigOptions } from '../../../src/types'; + IdempotencyKeyError, +} from '../../../src/index.js'; +import { + BasePersistenceLayer, + IdempotencyRecord, +} from '../../../src/persistence/index.js'; +import type { IdempotencyConfigOptions } from '../../../src/types/index.js'; jest.mock('node:crypto', () => ({ createHash: jest.fn().mockReturnValue({ @@ -293,7 +295,9 @@ describe('Class: BasePersistenceLayer', () => { await expect( persistenceLayer.getRecord({ foo: { bar: [] } }) ).rejects.toThrow( - new Error('No data found to create a hashed idempotency_key') + new IdempotencyKeyError( + 'No data found to create a hashed idempotency_key' + ) ); }); diff --git a/packages/idempotency/tests/unit/persistence/DynamoDbPersistenceLayer.test.ts b/packages/idempotency/tests/unit/persistence/DynamoDbPersistenceLayer.test.ts index 4a0195c19e..29e8930161 100644 --- a/packages/idempotency/tests/unit/persistence/DynamoDbPersistenceLayer.test.ts +++ b/packages/idempotency/tests/unit/persistence/DynamoDbPersistenceLayer.test.ts @@ -3,14 +3,14 @@ * * @group unit/idempotency/persistence/dynamodb */ -import { DynamoDBPersistenceLayer } from '../../../src/persistence/DynamoDBPersistenceLayer'; +import { DynamoDBPersistenceLayer } from '../../../src/persistence/DynamoDBPersistenceLayer.js'; +import { IdempotencyRecord } from '../../../src/persistence/index.js'; +import type { DynamoDBPersistenceOptions } from '../../../src/types/DynamoDBPersistence.js'; import { + IdempotencyRecordStatus, IdempotencyItemAlreadyExistsError, IdempotencyItemNotFoundError, -} from '../../../src/errors'; -import { IdempotencyRecord } from '../../../src/persistence'; -import type { DynamoDBPersistenceOptions } from '../../../src/types'; -import { IdempotencyRecordStatus } from '../../../src'; +} from '../../../src/index.js'; import { ConditionalCheckFailedException, DynamoDBClient, diff --git a/packages/idempotency/tests/unit/persistence/IdempotencyRecord.test.ts b/packages/idempotency/tests/unit/persistence/IdempotencyRecord.test.ts index 00776ec246..38728bcef9 100644 --- a/packages/idempotency/tests/unit/persistence/IdempotencyRecord.test.ts +++ b/packages/idempotency/tests/unit/persistence/IdempotencyRecord.test.ts @@ -3,10 +3,12 @@ * * @group unit/idempotency/persistence/idempotencyRecord */ -import { IdempotencyInvalidStatusError } from '../../../src/errors'; -import { IdempotencyRecord } from '../../../src/persistence'; -import { IdempotencyRecordStatus } from '../../../src'; -import type { IdempotencyRecordStatusValue } from '../../../src/types'; +import { IdempotencyRecord } from '../../../src/persistence/IdempotencyRecord.js'; +import { + IdempotencyRecordStatus, + IdempotencyInvalidStatusError, +} from '../../../src/index.js'; +import type { IdempotencyRecordStatusValue } from '../../../src/types/index.js'; const mockIdempotencyKey = '123'; const mockData = undefined; diff --git a/packages/idempotency/tests/unit/persistence/LRUCache.test.ts b/packages/idempotency/tests/unit/persistence/LRUCache.test.ts index 83a4e445ff..5732c31a45 100644 --- a/packages/idempotency/tests/unit/persistence/LRUCache.test.ts +++ b/packages/idempotency/tests/unit/persistence/LRUCache.test.ts @@ -3,7 +3,7 @@ * * @group unit/idempotency/persistence/lru-cache */ -import { LRUCache } from '../../../src/persistence/LRUCache'; +import { LRUCache } from '../../../src/persistence/LRUCache.js'; describe('Class: LRUMap', () => { describe('Method: add', () => { diff --git a/packages/idempotency/tsconfig.esm.json b/packages/idempotency/tsconfig.esm.json new file mode 100644 index 0000000000..9bed8e4da4 --- /dev/null +++ b/packages/idempotency/tsconfig.esm.json @@ -0,0 +1,11 @@ +{ + "extends": "../../tsconfig.esm.json", + "compilerOptions": { + "baseUrl": ".", + "outDir": "./lib/esm", + "rootDir": "./src" + }, + "include": [ + "./src/**/*" + ] +} \ No newline at end of file diff --git a/packages/idempotency/tsconfig.json b/packages/idempotency/tsconfig.json index 1cb9d72773..a30fdead42 100644 --- a/packages/idempotency/tsconfig.json +++ b/packages/idempotency/tsconfig.json @@ -1,7 +1,7 @@ { "extends": "../../tsconfig.json", "compilerOptions": { - "outDir": "./lib", + "outDir": "./lib/cjs", "rootDir": "./src", }, "include": [