Skip to content

Conversation

@ricardo-devis-agullo
Copy link
Collaborator

Add Support for File-Based Route Handlers in Development Configuration

Overview

This PR extends the OpenComponents registry to support defining custom routes with file-based handlers in the development configuration. Previously, custom routes could only be defined programmatically with function handlers. Now developers can specify route handlers as file paths in their oc.json configuration, improving code organization and reusability.

Changes

Type System Updates:

  • Modified Config.routes.handler type to accept both string (file path) and function handlers
  • Added routes configuration option under development section in OpenComponentsConfig
  • Updated validation to accept both handler types

Configuration Processing:

  • Enhanced options-sanitiser.ts to automatically load handler files when handler is a string
  • Added support for both absolute and relative file paths
  • Implemented require cache clearing for fresh module loading during development
  • Added comprehensive error handling with fallback behavior

Router Improvements:

  • Added type guards in router to ensure handlers are functions before mounting
  • Enhanced base route handling to respect custom route definitions
  • Improved error messaging for invalid route configurations

Development Integration:

  • Integrated route loading into the development server startup process
  • Added proper error handling and logging for failed route handler loading

Technical Details

The implementation maintains backward compatibility while adding new functionality. When a route handler is specified as a string, the system:

  1. Resolves the file path (supports both absolute and relative paths)
  2. Clears the require cache to ensure fresh module loading
  3. Requires the module and extracts the handler function
  4. Falls back gracefully with warnings if file loading fails

Handler files should export their function using standard Node.js patterns:

module.exports = function(req, res) {
  // Route handler logic
};

Benefits

  • Better Code Organization: Route handlers can be separated into dedicated files
  • Improved Reusability: Handler files can be shared across different configurations
  • Enhanced Development Experience: Easier to manage complex routing logic
  • Backward Compatibility: Existing programmatic route definitions continue to work unchanged

This enhancement makes the OpenComponents registry more flexible for development scenarios where custom API endpoints or middleware are needed alongside component serving.

@ricardo-devis-agullo ricardo-devis-agullo merged commit 8fbd35d into master Sep 1, 2025
3 checks passed
@ricardo-devis-agullo ricardo-devis-agullo deleted the add-routes-conf branch September 1, 2025 10:26
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.

2 participants