@@ -159,6 +159,8 @@ export async function idempotency(
159159 const originalJson = res . json . bind ( res ) ;
160160 const originalSend = res . send . bind ( res ) ;
161161
162+ let persisted = false ;
163+
162164 function saveIdempotency ( bodyToSave : unknown ) {
163165 if ( persisted ) return ;
164166 persisted = true ;
@@ -169,7 +171,7 @@ export async function idempotency(
169171 const v = res . getHeader ( h ) ;
170172 if ( v !== undefined ) headers [ h ] = String ( v ) ;
171173 }
172- const expiresAt = new Date ( Date . now ( ) + TTL_MS ) ;
174+ const expiresAt = new Date ( Date . now ( ) + IDEMPOTENCY_TTL_MS ) ;
173175 const valBody = typeof bodyToSave === "string" ? { content : bodyToSave } : bodyToSave ;
174176 db . insert ( idempotencyRecords )
175177 . values ( {
@@ -180,35 +182,12 @@ export async function idempotency(
180182 responseHeaders : headers ,
181183 expiresAt,
182184 } )
183- . catch ( ( err ) => logger . error ( { err, key, correlationId } , "idempotency_persist_failed" ) ) ;
185+ . catch ( ( err ) => logger . error ( { err, key, correlationId : reqId } , "idempotency_persist_failed" ) ) ;
184186 }
185187 }
186188
187189 res . json = function ( responseBody : unknown ) {
188- const status = res . statusCode ;
189- const headers : Record < string , string > = { } ;
190- for ( const h of REPLAY_HEADERS ) {
191- const v = res . getHeader ( h ) ;
192- if ( v ) headers [ h ] = String ( v ) ;
193- }
194-
195- // Only cache successful mutations; client / server errors are not stored.
196- if ( status >= 200 && status < 300 ) {
197- const expiresAt = new Date ( Date . now ( ) + IDEMPOTENCY_TTL_MS ) ;
198- db . insert ( idempotencyRecords )
199- . values ( {
200- key,
201- fingerprint,
202- responseStatus : status ,
203- responseBody,
204- responseHeaders : headers ,
205- expiresAt,
206- } )
207- . catch ( ( err ) =>
208- logger . error ( { err, reqId, key } , "idempotency_persist_failed" ) ,
209- ) ;
210- }
211-
190+ saveIdempotency ( responseBody ) ;
212191 return originalJson ( responseBody ) ;
213192 } ;
214193
0 commit comments