Skip to content

Feature request: Add support for middlewares in event handler #953

Closed
@BVMiko

Description

@BVMiko

Is your feature request related to a problem? Please describe.
Event handlers (specifically ApiGatewayResolver) are incompatible with middleware decorators. The lambda_handler_decorator is almost exactly what would be needed, except for the function signature.

Describe the solution you'd like
I'd like to be able to add middleware when using a router, and for the middleware to have access to the processed event:

@router_decorator
def db_middleware(handler, router, **kwargs):
    db = router.current_event.get_query_string_value(name="dbname", default_value='default_value')
    return handler(db=db, **kwargs)
    
@router.get("/users")
@db_middleware(router=router)
def get_users(db, foo) -> Dict:
    pagination_limit = router.current_event.get_query_string_value(name="limit", default_value=10)
    return {"pagination_limit": pagination_limit, "db": db}

Describe alternatives you've considered
Using a regular lambda_handler_decorator outside of the router.get is executed even when the router isn't called. The decorator wrapper could be stored in individual project code, but since it's so similar to the existing one and would be useful for any router usage, it makes sense to consider it for the aws-lambda-powertools library.

Additional context

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions