1
- import type { Handler } from 'aws-lambda' ;
2
1
import type {
3
2
JSONValue ,
4
3
MiddyLikeRequest ,
5
4
} from '@aws-lambda-powertools/commons/types' ;
6
- import type {
7
- AnyFunction ,
8
- IdempotencyHandlerOptions ,
9
- } from './types/IdempotencyOptions .js' ;
5
+ import { search } from '@aws-lambda-powertools/jmespath' ;
6
+ import type { Handler } from 'aws-lambda' ;
7
+ import type { IdempotencyConfig } from './IdempotencyConfig.js' ;
8
+ import { IdempotencyRecordStatus , MAX_RETRIES } from './constants .js' ;
10
9
import {
11
10
IdempotencyAlreadyInProgressError ,
12
11
IdempotencyInconsistentStateError ,
13
- IdempotencyItemAlreadyExistsError ,
12
+ type IdempotencyItemAlreadyExistsError ,
14
13
IdempotencyPersistenceLayerError ,
15
14
IdempotencyUnknownError ,
16
15
} from './errors.js' ;
17
- import { BasePersistenceLayer } from './persistence/BasePersistenceLayer.js' ;
18
- import { IdempotencyRecord } from './persistence/IdempotencyRecord.js' ;
19
- import { IdempotencyConfig } from './IdempotencyConfig.js' ;
20
- import { MAX_RETRIES , IdempotencyRecordStatus } from './constants.js' ;
21
- import { search } from '@aws-lambda-powertools/jmespath' ;
16
+ import type { BasePersistenceLayer } from './persistence/BasePersistenceLayer.js' ;
17
+ import type { IdempotencyRecord } from './persistence/IdempotencyRecord.js' ;
18
+ import type {
19
+ AnyFunction ,
20
+ IdempotencyHandlerOptions ,
21
+ } from './types/IdempotencyOptions.js' ;
22
22
23
23
/**
24
24
* @internal
@@ -99,9 +99,8 @@ export class IdempotencyHandler<Func extends AnyFunction> {
99
99
throw new IdempotencyInconsistentStateError (
100
100
'Item has expired during processing and may not longer be valid.'
101
101
) ;
102
- } else if (
103
- idempotencyRecord . getStatus ( ) === IdempotencyRecordStatus . INPROGRESS
104
- ) {
102
+ }
103
+ if ( idempotencyRecord . getStatus ( ) === IdempotencyRecordStatus . INPROGRESS ) {
105
104
if (
106
105
idempotencyRecord . inProgressExpiryTimestamp &&
107
106
idempotencyRecord . inProgressExpiryTimestamp <
@@ -110,11 +109,10 @@ export class IdempotencyHandler<Func extends AnyFunction> {
110
109
throw new IdempotencyInconsistentStateError (
111
110
'Item is in progress but the in progress expiry timestamp has expired.'
112
111
) ;
113
- } else {
114
- throw new IdempotencyAlreadyInProgressError (
115
- `There is already an execution in progress with idempotency key: ${ idempotencyRecord . idempotencyKey } `
116
- ) ;
117
112
}
113
+ throw new IdempotencyAlreadyInProgressError (
114
+ `There is already an execution in progress with idempotency key: ${ idempotencyRecord . idempotencyKey } `
115
+ ) ;
118
116
}
119
117
120
118
return idempotencyRecord . getResponse ( ) ;
@@ -129,7 +127,7 @@ export class IdempotencyHandler<Func extends AnyFunction> {
129
127
* @returns The result of the function execution
130
128
*/
131
129
public async getFunctionResult ( ) : Promise < ReturnType < Func > > {
132
- let result ;
130
+ let result : ReturnType < Func > ;
133
131
try {
134
132
result = await this . #functionToMakeIdempotent. apply (
135
133
this . #thisArg,
@@ -168,7 +166,7 @@ export class IdempotencyHandler<Func extends AnyFunction> {
168
166
) ;
169
167
}
170
168
171
- let e ;
169
+ let e : Error | undefined ;
172
170
for ( let retryNo = 0 ; retryNo <= MAX_RETRIES ; retryNo ++ ) {
173
171
try {
174
172
const { isIdempotent, result } =
@@ -234,7 +232,7 @@ export class IdempotencyHandler<Func extends AnyFunction> {
234
232
public async handleMiddyBefore (
235
233
request : MiddyLikeRequest ,
236
234
callback : ( request : MiddyLikeRequest ) => Promise < void >
237
- ) : Promise < ReturnType < Func > | void > {
235
+ ) : Promise < ReturnType < Func > | undefined > {
238
236
for ( let retryNo = 0 ; retryNo <= MAX_RETRIES ; retryNo ++ ) {
239
237
try {
240
238
const { isIdempotent, result } =
@@ -309,9 +307,9 @@ export class IdempotencyHandler<Func extends AnyFunction> {
309
307
) ;
310
308
311
309
return selection === undefined || selection === null ;
312
- } else {
313
- return false ;
314
310
}
311
+
312
+ return false ;
315
313
}
316
314
317
315
/**
@@ -388,12 +386,11 @@ export class IdempotencyHandler<Func extends AnyFunction> {
388
386
) ;
389
387
390
388
return returnValue ;
391
- } else {
392
- throw new IdempotencyPersistenceLayerError (
393
- 'Failed to save in progress record to idempotency store' ,
394
- { cause : error }
395
- ) ;
396
389
}
390
+ throw new IdempotencyPersistenceLayerError (
391
+ 'Failed to save in progress record to idempotency store' ,
392
+ { cause : error }
393
+ ) ;
397
394
}
398
395
} ;
399
396
0 commit comments