Skip to content

Commit ca82e59

Browse files
committed
style(idempotency): address SonarCloud issues
1 parent 6427c89 commit ca82e59

File tree

5 files changed

+21
-29
lines changed

5 files changed

+21
-29
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/persistence/DynamoDBPersistenceLayer.ts

Lines changed: 3 additions & 3 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 ?? {});

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ 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({
@@ -124,12 +121,9 @@ const handlerExpired = defaultLambda.handlerExpired.bind(defaultLambda);
124121
const logger = new Logger();
125122

126123
class LambdaWithKeywordArgument implements LambdaInterface {
127-
public async handler(
128-
event: { id: string },
129-
_context: Context
130-
): Promise<string> {
124+
public handler(event: { id: string }, _context: Context) {
131125
config.registerLambdaContext(_context);
132-
await this.process(event.id, 'bar');
126+
this.process(event.id, 'bar');
133127

134128
return 'Hello World Keyword Argument';
135129
}

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/vitest.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default defineProject({
44
test: {
55
environment: 'node',
66
setupFiles: ['../testing/src/setupEnv.ts'],
7-
hookTimeout: 1_000 * 60 * 10, // 10 minutes
8-
testTimeout: 1_000 * 60 * 3, // 3 minutes
7+
hookTimeout: 1000 * 60 * 10, // 10 minutes
8+
testTimeout: 1000 * 60 * 3, // 3 minutes
99
},
1010
});

0 commit comments

Comments
 (0)