-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(idempotency):
makeHandlerIdempotent
middy middleware (#1474)
* feat: makeHandlerIdempotent middy middleware
- Loading branch information
1 parent
faa9307
commit a558f10
Showing
12 changed files
with
577 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* Number of times to retry a request in case of `IdempotencyInconsistentStateError` | ||
* | ||
* Used in `IdempotencyHandler` and `makeHandlerIdempotent` | ||
* | ||
* @internal | ||
*/ | ||
const MAX_RETRIES = 2; | ||
|
||
export { MAX_RETRIES }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,58 @@ | ||
import type { Context } from 'aws-lambda'; | ||
import type { | ||
AnyFunctionWithRecord, | ||
AnyIdempotentFunction, | ||
GenericTempRecord, | ||
IdempotencyFunctionOptions, | ||
} from './types'; | ||
import { IdempotencyHandler } from './IdempotencyHandler'; | ||
import { IdempotencyConfig } from './IdempotencyConfig'; | ||
|
||
const isContext = (arg: unknown): arg is Context => { | ||
return ( | ||
arg !== undefined && | ||
arg !== null && | ||
typeof arg === 'object' && | ||
'getRemainingTimeInMillis' in arg | ||
); | ||
}; | ||
|
||
const makeFunctionIdempotent = function <U>( | ||
fn: AnyFunctionWithRecord<U>, | ||
options: IdempotencyFunctionOptions | ||
): AnyIdempotentFunction<U> { | ||
): AnyIdempotentFunction<U> | AnyFunctionWithRecord<U> { | ||
const idempotencyConfig = options.config | ||
? options.config | ||
: new IdempotencyConfig({}); | ||
|
||
const wrappedFn: AnyIdempotentFunction<U> = function ( | ||
record: GenericTempRecord | ||
...args: Parameters<AnyFunctionWithRecord<U>> | ||
): Promise<U> { | ||
const payload = args[0]; | ||
const context = args[1]; | ||
|
||
if (options.dataKeywordArgument === undefined) { | ||
throw new Error( | ||
`Missing data keyword argument ${options.dataKeywordArgument}` | ||
); | ||
} | ||
const idempotencyConfig = options.config | ||
? options.config | ||
: new IdempotencyConfig({}); | ||
if (isContext(context)) { | ||
idempotencyConfig.registerLambdaContext(context); | ||
} | ||
const idempotencyHandler: IdempotencyHandler<U> = new IdempotencyHandler<U>( | ||
{ | ||
functionToMakeIdempotent: fn, | ||
functionPayloadToBeHashed: record[options.dataKeywordArgument], | ||
functionPayloadToBeHashed: payload[options.dataKeywordArgument], | ||
idempotencyConfig: idempotencyConfig, | ||
persistenceStore: options.persistenceStore, | ||
fullFunctionPayload: record, | ||
fullFunctionPayload: payload, | ||
} | ||
); | ||
|
||
return idempotencyHandler.handle(); | ||
}; | ||
|
||
return wrappedFn; | ||
if (idempotencyConfig.isEnabled()) return wrappedFn; | ||
else return fn; | ||
}; | ||
|
||
export { makeFunctionIdempotent }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './makeHandlerIdempotent'; |
Oops, something went wrong.