Skip to content

Conversation

karthaj
Copy link

@karthaj karthaj commented Sep 27, 2025

feat: Add Query Parameter Support to Router

This change adds comprehensive support for handling URL query parameters in the krouter package,
following the same patterns as existing header and path parameter handling.

Changes:

  • Added query parameter support to Handler struct and configuration
  • Enhanced Payload and HttpPayload structs with query parameter handling
  • Introduced HandlerWithQueryParam option for configuring query parameters
  • Added type-safe query parameter decoding and validation
  • Extended Route struct to include query parameters in re-route messages

New Features:

  1. Query Parameter Configuration:

    HandlerWithQueryParam("page", ParamTypeInt, true)    // required parameter
    HandlerWithQueryParam("sort", ParamTypeString, false) // optional parameter
  2. Type-Safe Query Access:

    • In HTTP handlers: payload.Query("paramName")
    • Supports all existing parameter types (Int, String, UUID, custom types)
    • Automatic type conversion and validation
  3. Re-routing Support:

    • Query parameters are preserved during re-routing
    • Maintains consistency with existing header and path parameter behavior
    • Example Usage:
    h := router.NewHandler("route1", encoder{}, preHandler, handler.Handle,
     HandlerWithQueryParam("page", ParamTypeInt, true),
     HandlerWithQueryParam("sort", ParamTypeString, false),
    )
    
    // Access in handler
    func (h *Handler) Handle(ctx context.Context, payload Payload) error {
       page := payload.Query("page").(int)
       sort := payload.Query("sort").(string)
       // ... handler logic
    }

Testing:

  • Added comprehensive test coverage for query parameter handling
  • Includes validation of required vs optional parameters
  • Tests for type conversion and error cases
  • Integration tests with the existing routing system
  • This enhancement maintains backward compatibility while adding new functionality that follows the established patterns in the codebase.

- Add query parameter handling in Handler struct
- Implement HandlerWithQueryParam option
- Add query param support in Payload and Route structs
- Include query params in re-routing
- Add type-safe query parameter access
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant