Skip to content

Commit 4f6cc3c

Browse files
authored
style(idempotency): apply stricter linting (#4546)
1 parent ec5a111 commit 4f6cc3c

15 files changed

+52
-74
lines changed

packages/idempotency/src/IdempotencyHandler.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -383,20 +383,20 @@ export class IdempotencyHandler<Func extends AnyFunction> {
383383
if (error.name === 'IdempotencyItemAlreadyExistsError') {
384384
let idempotencyRecord = (error as IdempotencyItemAlreadyExistsError)
385385
.existingRecord;
386-
if (idempotencyRecord !== undefined) {
387-
// If the error includes the existing record, we can use it to validate
388-
// the record being processed and cache it in memory.
389-
idempotencyRecord = this.#persistenceStore.processExistingRecord(
390-
idempotencyRecord,
391-
this.#functionPayloadToBeHashed
392-
);
386+
if (idempotencyRecord === undefined) {
393387
// If the error doesn't include the existing record, we need to fetch
394388
// it from the persistence layer. In doing so, we also call the processExistingRecord
395389
// method to validate the record and cache it in memory.
396-
} else {
397390
idempotencyRecord = await this.#persistenceStore.getRecord(
398391
this.#functionPayloadToBeHashed
399392
);
393+
} else {
394+
// If the error includes the existing record, we can use it to validate
395+
// the record being processed and cache it in memory.
396+
idempotencyRecord = this.#persistenceStore.processExistingRecord(
397+
idempotencyRecord,
398+
this.#functionPayloadToBeHashed
399+
);
400400
}
401401

402402
returnValue.isIdempotent = true;

packages/idempotency/src/idempotencyDecorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const idempotent = (
7070
) {
7171
const childFunction = descriptor.value;
7272

73-
descriptor.value = async function (this: Handler, ...args: unknown[]) {
73+
descriptor.value = function (this: Handler, ...args: unknown[]) {
7474
return makeIdempotent(childFunction, options).bind(this)(...args);
7575
};
7676

packages/idempotency/src/middleware/makeHandlerIdempotent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ const makeHandlerIdempotent = (
112112
*
113113
* @param request - The Middy request object
114114
*/
115-
const before = async (request: MiddyLikeRequest): Promise<unknown> => {
115+
const before = (request: MiddyLikeRequest): unknown => {
116116
const idempotencyConfig = options.config
117117
? options.config
118118
: new IdempotencyConfig({});

packages/idempotency/src/persistence/DynamoDBPersistenceLayer.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
8888
config.staticPkValue ?? `idempotency#${this.idempotencyKeyPrefix}`;
8989

9090
if (config.awsSdkV3Client) {
91-
if (!isSdkClient(config.awsSdkV3Client)) {
91+
if (isSdkClient(config.awsSdkV3Client)) {
92+
this.client = config.awsSdkV3Client;
93+
} else {
9294
console.warn(
9395
'awsSdkV3Client is not an AWS SDK v3 client, using default client'
9496
);
9597
this.client = new DynamoDBClient(config.clientConfig ?? {});
96-
} else {
97-
this.client = config.awsSdkV3Client;
9898
}
9999
} else {
100100
this.client = new DynamoDBClient(config.clientConfig ?? {});
@@ -168,9 +168,9 @@ class DynamoDBPersistenceLayer extends BasePersistenceLayer {
168168
* | (in_progress_expiry) (expiry)
169169
*
170170
* Conditions to successfully save a record:
171-
* * The idempotency key does not exist:
172-
* - first time that this invocation key is used
173-
* - previous invocation with the same key was deleted due to TTL
171+
* - The idempotency key does not exist:
172+
* - first time that this invocation key is used
173+
* - previous invocation with the same key was deleted due to TTL
174174
*/
175175
const idempotencyKeyDoesNotExist = 'attribute_not_exists(#id)';
176176
// * The idempotency key exists but it is expired

packages/idempotency/tests/e2e/idempotentDecorator.test.FunctionCode.ts

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { LambdaInterface } from '@aws-lambda-powertools/commons/types';
22
import { Logger } from '@aws-lambda-powertools/logger';
33
import type { Context } from 'aws-lambda';
44
import { IdempotencyConfig } from '../../src/IdempotencyConfig.js';
5-
import { idempotent } from '../../src/idempotencyDecorator';
5+
import { idempotent } from '../../src/idempotencyDecorator.js';
66
import { DynamoDBPersistenceLayer } from '../../src/persistence/DynamoDBPersistenceLayer.js';
77

88
const IDEMPOTENCY_TABLE_NAME =
@@ -35,19 +35,13 @@ class DefaultLambda implements LambdaInterface {
3535
logger.info(`${this.message} ${JSON.stringify(_event)}`);
3636
// sleep to enforce error with parallel execution
3737
await new Promise((resolve) => setTimeout(resolve, 1000));
38-
39-
// We return void to test that the utility handles it correctly
40-
return;
4138
}
4239

4340
@idempotent({
4441
persistenceStore: dynamoDBPersistenceLayerCustomized,
4542
config: config,
4643
})
47-
public async handlerCustomized(
48-
event: { foo: string },
49-
context: Context
50-
): Promise<string> {
44+
public handlerCustomized(event: { foo: string }, context: Context) {
5145
config.registerLambdaContext(context);
5246
logger.info('Processed event', { details: event.foo });
5347

@@ -62,10 +56,10 @@ class DefaultLambda implements LambdaInterface {
6256
eventKeyJmesPath: 'foo',
6357
}),
6458
})
65-
public async handlerExpired(
59+
public handlerExpired(
6660
event: { foo: string; invocation: number },
6761
context: Context
68-
): Promise<{ foo: string; invocation: number }> {
62+
) {
6963
logger.addContext(context);
7064

7165
logger.info('Processed event', { details: event.foo });
@@ -77,10 +71,7 @@ class DefaultLambda implements LambdaInterface {
7771
}
7872

7973
@idempotent({ persistenceStore: dynamoDBPersistenceLayer })
80-
public async handlerParallel(
81-
event: { foo: string },
82-
context: Context
83-
): Promise<string> {
74+
public async handlerParallel(event: { foo: string }, context: Context) {
8475
logger.addContext(context);
8576

8677
await new Promise((resolve) => setTimeout(resolve, 1500));
@@ -99,7 +90,7 @@ class DefaultLambda implements LambdaInterface {
9990
public async handlerTimeout(
10091
event: { foo: string; invocation: number },
10192
context: Context
102-
): Promise<{ foo: string; invocation: number }> {
93+
) {
10394
logger.addContext(context);
10495

10596
if (event.invocation === 0) {
@@ -130,12 +121,9 @@ const handlerExpired = defaultLambda.handlerExpired.bind(defaultLambda);
130121
const logger = new Logger();
131122

132123
class LambdaWithKeywordArgument implements LambdaInterface {
133-
public async handler(
134-
event: { id: string },
135-
_context: Context
136-
): Promise<string> {
124+
public handler(event: { id: string }, _context: Context) {
137125
config.registerLambdaContext(_context);
138-
await this.process(event.id, 'bar');
126+
this.process(event.id, 'bar');
139127

140128
return 'Hello World Keyword Argument';
141129
}
@@ -145,7 +133,7 @@ class LambdaWithKeywordArgument implements LambdaInterface {
145133
config: config,
146134
dataIndexArgument: 1,
147135
})
148-
public async process(id: string, foo: string): Promise<string> {
136+
public process(id: string, foo: string) {
149137
logger.info('Got test event', { id, foo });
150138

151139
return `idempotent result: ${foo}`;

packages/idempotency/tests/e2e/makeHandlerIdempotent.test.FunctionCode.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ const logger = new Logger();
1616
/**
1717
* Test handler with sequential execution.
1818
*/
19-
export const handler = middy(
20-
async (event: { foo: string }, context: Context) => {
21-
logger.addContext(context);
22-
logger.info('foo', { details: event.foo });
19+
export const handler = middy((event: { foo: string }, context: Context) => {
20+
logger.addContext(context);
21+
logger.info('foo', { details: event.foo });
2322

24-
return event.foo;
25-
}
26-
).use(
23+
return event.foo;
24+
}).use(
2725
makeHandlerIdempotent({
2826
persistenceStore: dynamoDBPersistenceLayer,
2927
})
@@ -97,7 +95,7 @@ export const handlerTimeout = middy(
9795
* was processed by looking at the value in the stored idempotency record.
9896
*/
9997
export const handlerExpired = middy(
100-
async (event: { foo: string; invocation: number }, context: Context) => {
98+
(event: { foo: string; invocation: number }, context: Context) => {
10199
logger.addContext(context);
102100

103101
logger.info('Processed event', { details: event.foo });

packages/idempotency/tests/e2e/makeHandlerIdempotent.test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,13 @@ describe('Idempotency E2E tests, middy middleware usage', () => {
192192
* We filter the logs to find which one was successful and which one failed, then we check
193193
* that they contain the expected logs.
194194
*/
195-
const successfulInvocationLogs = functionLogs.find(
196-
(functionLog) =>
197-
functionLog.find((log) => log.includes('Processed event')) !== undefined
195+
const successfulInvocationLogs = functionLogs.find((functionLog) =>
196+
functionLog.some((log) => log.includes('Processed event'))
198197
);
199-
const failedInvocationLogs = functionLogs.find(
200-
(functionLog) =>
201-
functionLog.find((log) =>
202-
log.includes('There is already an execution in progress')
203-
) !== undefined
198+
const failedInvocationLogs = functionLogs.find((functionLog) =>
199+
functionLog.some((log) =>
200+
log.includes('There is already an execution in progress')
201+
)
204202
);
205203
expect(successfulInvocationLogs).toHaveLength(1);
206204
expect(failedInvocationLogs).toHaveLength(1);

packages/idempotency/tests/e2e/makeIdempotent.test.FunctionCode.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const handlerCustomized = async (
9090
* Test idempotent Lambda handler with JMESPath expression to extract event key.
9191
*/
9292
export const handlerLambda = makeIdempotent(
93-
async (event: { body: string }, context: Context) => {
93+
(event: { body: string }, context: Context) => {
9494
logger.addContext(context);
9595
const body = JSON.parse(event.body);
9696
logger.info('foo', { details: body.foo });

packages/idempotency/tests/unit/IdempotencyConfig.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ describe('Class: IdempotencyConfig', () => {
8787
});
8888

8989
describe('Method: registerLambdaContext', () => {
90-
it('stores the provided context', async () => {
90+
it('stores the provided context', () => {
9191
// Prepare
9292
const config = new IdempotencyConfig({});
9393

packages/idempotency/tests/unit/IdempotencyHandler.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('Class IdempotencyHandler', () => {
6868
},
6969
])(
7070
'throws when the record is in progress and within expiry window ($case)',
71-
async ({ keys, expectedErrorMsg }) => {
71+
({ keys, expectedErrorMsg }) => {
7272
// Prepare
7373
const stubRecord = new IdempotencyRecord({
7474
...keys,
@@ -89,7 +89,7 @@ describe('Class IdempotencyHandler', () => {
8989
}
9090
);
9191

92-
it('throws when the record is in progress and outside expiry window', async () => {
92+
it('throws when the record is in progress and outside expiry window', () => {
9393
// Prepare
9494
const stubRecord = new IdempotencyRecord({
9595
idempotencyKey: 'idempotencyKey',
@@ -109,7 +109,7 @@ describe('Class IdempotencyHandler', () => {
109109
expect(mockResponseHook).not.toHaveBeenCalled();
110110
});
111111

112-
it('throws when the idempotency record is expired', async () => {
112+
it('throws when the idempotency record is expired', () => {
113113
// Prepare
114114
const stubRecord = new IdempotencyRecord({
115115
idempotencyKey: 'idempotencyKey',

0 commit comments

Comments
 (0)