The Middy request object
import middy from '@middy/core';
import { cleanupMiddlewares } from '@aws-lambda-powertools/commons/lib/middleware';
// Example middleware that returns early
const myCustomMiddleware = (): middy.MiddlewareObj => {
const before = async (request: middy.Request): Promise<undefined | string> => {
// If the request is a GET, return early (as an example)
if (request.event.httpMethod === 'GET') {
// Cleanup Powertools resources
await cleanupMiddlewares(request);
// Then return early
return 'GET method not supported';
}
};
return {
before,
};
};
Generated using TypeDoc
Function used to cleanup Powertools for AWS resources when a Middy middleware returns early and terminates the middleware chain.
When a middleware returns early, all the middleware lifecycle functions that come after it are not executed. This means that if a middleware was relying on certain logic to be run during the
after
oronError
lifecycle functions, that logic will not be executed.This is the case for the middlewares that are part of Powertools for AWS which rely on these lifecycle functions to perform cleanup operations like closing the current segment in the tracer or flushing any stored metrics.
When authoring a middleware that might return early, you can use this function to cleanup Powertools resources. This function will check if any cleanup function is present in the
request.internal
object and execute it.