Hooks
Middy provides hooks into it’s core to allow for monitoring, setup, and cleaning that may not be possible within a middleware.
Hooks are provided via the second argument to middy(handler, pluginConfig).
In order of execution
beforePrefetch(): Triggered once before middlewares are attached and prefetches are executed.requestStart(request): Triggered on every request before the first middleware.beforeMiddleware/afterMiddleware(fctName): Triggered before/after everybefore,after, andonErrormiddleware function. The function name is passed in, this is why all middlewares use a verbose naming pattern.beforeHandler/afterHandler(): Triggered before/after the handler.requestEnd(request): Triggered right before the response is returned, including thrown errors. May be async.
Additional pluginConfig options
internal(object): Seed values merged intorequest.internalon each invocation. Defaults to an empty object.timeoutEarlyInMillis(integer >= 0): Reserves N milliseconds before Lambda times out sotimeoutEarlyResponsecan run. Set to0to disable (default5).timeoutEarlyResponse(function): Invoked when the early-timeout fires; its return value becomes the response. The default throws aTimeoutError.executionMode(function): Selects the runtime adapter. Provided modes:executionModeStandard(default),executionModeDurableContext,executionModeStreamifyResponse. Custom modes may be supplied.
Unknown keys in pluginConfig throw a TypeError when validated via the exported middyValidateOptions.
See Profiling for example usage.