@@ -2,9 +2,9 @@ import { IdempotencyHandler } from '../IdempotencyHandler';
2
2
import { IdempotencyConfig } from '../IdempotencyConfig' ;
3
3
import { cleanupMiddlewares } from '@aws-lambda-powertools/commons/lib/middleware' ;
4
4
import {
5
+ IdempotencyInconsistentStateError ,
5
6
IdempotencyItemAlreadyExistsError ,
6
7
IdempotencyPersistenceLayerError ,
7
- IdempotencyInconsistentStateError ,
8
8
} from '../Exceptions' ;
9
9
import { IdempotencyRecord } from '../persistence' ;
10
10
import { MAX_RETRIES } from '../constants' ;
@@ -50,6 +50,9 @@ const makeHandlerIdempotent = (
50
50
config : idempotencyConfig ,
51
51
} ) ;
52
52
53
+ // keep the flag for after and onError checks
54
+ let shouldSkipIdempotency = false ;
55
+
53
56
/**
54
57
* Function called before the handler is executed.
55
58
*
@@ -72,6 +75,18 @@ const makeHandlerIdempotent = (
72
75
request : MiddyLikeRequest ,
73
76
retryNo = 0
74
77
) : Promise < unknown | void > => {
78
+ if (
79
+ IdempotencyHandler . shouldSkipIdempotency (
80
+ idempotencyConfig . eventKeyJmesPath ,
81
+ idempotencyConfig . throwOnNoIdempotencyKey ,
82
+ request . event as Record < string , unknown >
83
+ )
84
+ ) {
85
+ // set the flag to skip checks in after and onError
86
+ shouldSkipIdempotency = true ;
87
+
88
+ return ;
89
+ }
75
90
try {
76
91
await persistenceStore . saveInProgress (
77
92
request . event as Record < string , unknown > ,
@@ -114,7 +129,6 @@ const makeHandlerIdempotent = (
114
129
}
115
130
}
116
131
} ;
117
-
118
132
/**
119
133
* Function called after the handler has executed successfully.
120
134
*
@@ -125,6 +139,9 @@ const makeHandlerIdempotent = (
125
139
* @param request - The Middy request object
126
140
*/
127
141
const after = async ( request : MiddyLikeRequest ) : Promise < void > => {
142
+ if ( shouldSkipIdempotency ) {
143
+ return ;
144
+ }
128
145
try {
129
146
await persistenceStore . saveSuccess (
130
147
request . event as Record < string , unknown > ,
@@ -146,6 +163,9 @@ const makeHandlerIdempotent = (
146
163
* @param request - The Middy request object
147
164
*/
148
165
const onError = async ( request : MiddyLikeRequest ) : Promise < void > => {
166
+ if ( shouldSkipIdempotency ) {
167
+ return ;
168
+ }
149
169
try {
150
170
await persistenceStore . deleteRecord (
151
171
request . event as Record < string , unknown >
0 commit comments