Skip to content

feat(host): add middleware registration tracking with unique identifiers #188

@j-d-ha

Description

@j-d-ha

Summary

Add a mechanism to track registered middleware by unique identifiers (ID or name), allowing users to check if a middleware has already been registered and enabling flexible ordering of middleware registration.

Problem Statement

Currently, middleware is registered in a fixed order without any mechanism to identify already-registered middleware. This prevents users from:

  • Checking if a specific middleware is already in the pipeline
  • Adding middleware out of the recommended order
  • Conditionally registering middleware based on what's already registered

Proposed Solution

Implement a middleware registration tracking system that:

  1. Generates unique identifiers for each registered middleware (based on type name or custom identifier)
  2. Tracks registered middleware - maintains a collection of registered middleware IDs/names
  3. Provides query methods - allows users to check if specific middleware has been registered
  4. Supports optional conflicts - can warn or prevent duplicate registrations if needed
  5. Documents the feature - provides clear guidance on usage and best practices

Implementation Details

Core Changes

  • Add a middleware registry/tracker to ILambdaInvocationBuilder
  • Extend middleware registration methods to support ID assignment
  • Create utility methods for checking if middleware is registered

API Design

Proposed extension methods:

// Check if middleware by ID is registered
bool IsMiddlewareRegistered(string middlewareId);

// Get list of registered middleware IDs
IReadOnlyList<string> GetRegisteredMiddleware();

// Register middleware with explicit ID
ILambdaInvocationBuilder UseMiddleware(
    string middlewareId, 
    Func<ILambdaHostContext, LambdaInvocationDelegate, Task> middleware
);

// Register middleware with auto-generated ID (e.g., from delegate type)
ILambdaInvocationBuilder UseMiddleware(
    Func<ILambdaHostContext, LambdaInvocationDelegate, Task> middleware
);

Middleware ID Generation

  • For inline delegates: Generate ID from delegate method name or provide explicit ID
  • For extension methods (e.g., UseClearLambdaOutputFormatting()): Use method name as ID prefix
  • Allow explicit ID override for custom middleware

Example Usage

var app = new LambdaApplication(host);

// Register with explicit ID
app.UseMiddleware("ClearLambdaOutputFormatting", async (ctx, next) => { /* ... */ });

// Check if middleware is registered
if (!app.IsMiddlewareRegistered("ClearLambdaOutputFormatting"))
{
    app.UseClearLambdaOutputFormatting();
}

// User can now add middleware in custom order
app.UseMiddleware(async (ctx, next) => { /* custom middleware */ });

Benefits

  • Enables conditional middleware registration
  • Supports flexible middleware ordering
  • Prevents accidental duplicate registrations
  • Improves debugging with middleware visibility
  • Facilitates middleware discovery

Related Issues

None

Type of Change

  • New feature
  • Breaking change
  • Requires documentation updates
  • Requires migration guide

Testing Considerations

  • Unit tests for middleware registry operations
  • Tests for ID generation and collision handling
  • Tests for ordering independence
  • Integration tests with existing middleware setup

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions