Skip to content

Commit

Permalink
feat(idempotency): add esmodule support (#1743)
Browse files Browse the repository at this point in the history
* feat(idempotency): add esmodule support
  • Loading branch information
dreamorosi committed Jan 27, 2024
1 parent 8276cee commit 5473333
Show file tree
Hide file tree
Showing 41 changed files with 219 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ module.exports = {
},
runner: 'groups',
preset: 'ts-jest',
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
},
transform: {
'^.+\\.ts?$': 'ts-jest',
},
Expand Down
57 changes: 39 additions & 18 deletions packages/idempotency/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
],
Expand Down
4 changes: 2 additions & 2 deletions packages/idempotency/src/IdempotencyConfig.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
14 changes: 9 additions & 5 deletions packages/idempotency/src/IdempotencyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfigServiceInterface } from './ConfigServiceInterface';
import type { ConfigServiceInterface } from '../types/ConfigServiceInterface.js';
import { EnvironmentVariablesService as CommonEnvironmentVariablesService } from '@aws-lambda-powertools/commons';

/**
Expand Down
1 change: 0 additions & 1 deletion packages/idempotency/src/config/index.ts

This file was deleted.

7 changes: 5 additions & 2 deletions packages/idempotency/src/idempotencyDecorator.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
19 changes: 14 additions & 5 deletions packages/idempotency/src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
6 changes: 3 additions & 3 deletions packages/idempotency/src/makeIdempotent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 0 additions & 1 deletion packages/idempotency/src/middleware/index.ts

This file was deleted.

9 changes: 6 additions & 3 deletions packages/idempotency/src/middleware/makeHandlerIdempotent.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
21 changes: 13 additions & 8 deletions packages/idempotency/src/persistence/BasePersistenceLayer.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down Expand Up @@ -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}`
Expand Down

This file was deleted.

10 changes: 5 additions & 5 deletions packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions packages/idempotency/src/persistence/IdempotencyRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion packages/idempotency/src/persistence/LRUCache.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
5 changes: 2 additions & 3 deletions packages/idempotency/src/persistence/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './BasePersistenceLayer';
export * from './BasePersistenceLayerInterface';
export * from './IdempotencyRecord';
export { BasePersistenceLayer } from './BasePersistenceLayer.js';
export { IdempotencyRecord } from './IdempotencyRecord.js';
14 changes: 12 additions & 2 deletions packages/idempotency/src/types/BasePersistenceLayer.ts
Original file line number Diff line number Diff line change
@@ -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<void>;
saveSuccess(data: unknown, result: unknown): Promise<void>;
deleteRecord(data: unknown): Promise<void>;
getRecord(data: unknown): Promise<IdempotencyRecord>;
}

export { BasePersistenceLayerOptions, BasePersistenceLayerInterface };
4 changes: 2 additions & 2 deletions packages/idempotency/src/types/IdempotencyOptions.ts
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/idempotency/src/types/IdempotencyRecord.ts
Original file line number Diff line number Diff line change
@@ -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];
Expand Down
18 changes: 13 additions & 5 deletions packages/idempotency/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
Loading

0 comments on commit 5473333

Please sign in to comment.