honestjs
Version:
HonestJS - a modern web framework built on top of Hono
110 lines (109 loc) • 4.67 kB
TypeScript
import type { Hono } from 'hono';
import { VERSION_NEUTRAL } from '../constants';
import type { DiContainer } from '../interfaces';
import type { Constructor } from '../types';
/**
* Manager class for handling route registration in the Honest framework
* Responsible for:
* - Registering controller routes with the Hono application
* - Managing route versioning and prefixing
* - Applying middleware and guards to routes
* - Handling parameter transformation and validation
*
* Versioning features:
* - Global version setting can be overridden at controller or method level
* - Controllers can opt out of versioning by setting version to null
* - Routes can use VERSION_NEUTRAL to be accessible with and without version prefix
* - Routes can specify an array of versions to support multiple versions
*
* Path handling:
* - Global prefix can be overridden at controller level
* - Paths are automatically normalized
* - Final path structure: prefix/version/controller-path/method-path
*/
export declare class RouteManager {
private hono;
private container;
private globalPrefix?;
private globalVersion?;
/**
* Creates a new RouteManager instance
* @param hono - The Hono application instance for route registration
* @param container - The dependency injection container for resolving controllers and dependencies
* @param options - Configuration options for the route manager
* @param options.prefix - Optional global prefix for all routes
* @param options.version - Optional global version or version array for all routes
*/
constructor(hono: Hono, container: DiContainer, options?: {
prefix?: string;
version?: number | typeof VERSION_NEUTRAL | number[];
});
/**
* Applies global middleware to the application
*/
private applyGlobalMiddleware;
/**
* Normalizes a path segment for route registration
* @param path - The path segment to normalize
* @returns The normalized path segment
*/
private normalizePath;
/**
* Registers a wrapper handler with middleware for a route
* @param method - HTTP method
* @param path - Full path for the route
* @param handlerMiddleware - Middleware for the handler
* @param wrapperHandler - The wrapper handler function
*/
private registerRouteHandler;
/**
* Builds a route path with the correct order: prefix, version, controller path, method path
* @param prefix - Global or controller-specific prefix
* @param version - Version string (e.g., '/v1') or empty string if no version
* @param controllerPath - Controller path
* @param methodPath - Method-specific path
* @returns Properly formatted full path
*/
private buildRoutePath;
/**
* Formats a version number or VERSION_NEUTRAL into a path segment
* @param version - Version number or VERSION_NEUTRAL
* @returns Formatted version string (e.g., '/v1') or empty string if null
*/
private formatVersionSegment;
/**
* Registers a controller and all its routes with the application
* Handles versioning, prefixing, and middleware application
*
* @param controllerClass - The controller class to register
* @throws {Error} If controller registration fails or if required dependencies cannot be resolved
*
* Route registration process:
* 1. Resolves controller instance and metadata
* 2. Processes controller-level options (prefix, version)
* 3. Registers each route with appropriate:
* - Path construction (prefix/version/controller-path/method-path)
* - Middleware application
* - Parameter processing
* - Guard validation
*/
registerController(controllerClass: Constructor): Promise<void>;
/**
* Registers a specific route with the application
* Handles middleware setup, parameter processing, and response handling
*
* @param controllerInstance - Instance of the controller containing the route handler
* @param route - Route metadata including path and HTTP method
* @param parameterMetadata - Metadata for parameter processing
* @param contextIndices - Map of context parameter indices
* @param controllerClass - The controller class
* @param prefix - Route prefix
* @param versionSegment - Version segment of the path
* @param controllerSegment - Controller path segment
* @param methodSegment - Method-specific path segment
* @param method - HTTP method for the route
*
* @throws {Error} If route registration fails
*/
private registerRoute;
}