UNPKG

honestjs

Version:

HonestJS - a modern web framework built on top of Hono

144 lines (143 loc) 7.13 kB
import type { Context, Next } from 'hono'; import type { ArgumentMetadata, DiContainer, GuardType, IGuard, IPipe, MiddlewareType, PipeType } from '../interfaces'; import { type ComponentType, type ComponentTypeMap } from '../registries'; import type { Constructor } from '../types'; /** * Manager class for handling all component types in the Honest framework * Provides unified management of middleware, guards, pipes, and filters * Handles component registration, resolution, and execution at global, controller, and handler levels * Works with the dependency injection container to resolve component instances */ export declare class ComponentManager { private static container; /** * Initializes the ComponentManager with a dependency injection container * Must be called before using any other ComponentManager methods * @param container - The dependency injection container to use for resolving components */ static init(container: DiContainer): void; /** * Configures global components from application options * Global components are applied to all routes in the application * @param options - Application options containing component configurations * @param options.components - Optional component configuration object * @param options.components.middleware - Optional array of global middleware * @param options.components.guards - Optional array of global guards * @param options.components.pipes - Optional array of global pipes * @param options.components.filters - Optional array of global filters */ static setupGlobalComponents(options: { components?: { middleware?: any[]; guards?: any[]; pipes?: any[]; filters?: any[]; }; }): void; /** * Registers a component at the global level * @param type - The type of component to register * @param components - The component classes or instances to register */ static registerGlobal<T extends ComponentType>(type: T, ...components: ComponentTypeMap[T][]): void; /** * Registers a component at the controller level * @param type - The type of component to register * @param controller - The controller class to register the component for * @param components - The component classes or instances to register */ static registerController<T extends ComponentType>(type: T, controller: Constructor, ...components: ComponentTypeMap[T][]): void; /** * Registers a component at the handler level * @param type - The type of component to register * @param controller - The controller class * @param handlerName - The handler method name * @param components - The component classes or instances to register */ static registerHandler<T extends ComponentType>(type: T, controller: Constructor, handlerName: string | symbol, ...components: ComponentTypeMap[T][]): void; /** * Gets all components of a specific type for a handler * @param type - The type of component to get * @param controller - The controller class * @param handlerName - The handler method name * ..returns An array of component instances */ static getComponents<T extends ComponentType>(type: T, controller: Constructor, handlerName: string | symbol): ComponentTypeMap[T][]; /** * Resolves middleware classes or instances to middleware functions * @param middlewareItems - The middleware classes or instances to resolve * ..returns An array of middleware functions */ static resolveMiddleware(middlewareItems: MiddlewareType[]): ((c: Context, next: Next) => Promise<Response | void>)[]; /** * Gets middleware for a specific handler * @param controller - The controller class * @param handlerName - The handler method name * ..returns An array of middleware functions */ static getHandlerMiddleware(controller: Constructor, handlerName: string | symbol): ((c: Context, next: Next) => Promise<Response | void>)[]; /** * Gets global middleware * ..returns An array of middleware functions */ static getGlobalMiddleware(): ((c: Context, next: Next) => Promise<Response | void>)[]; /** * Resolves guard classes or instances to guard instances * @param guardItems - The guard classes or instances to resolve * ..returns An array of guard instances */ static resolveGuards(guardItems: GuardType[]): IGuard[]; /** * Gets guards for a specific handler * @param controller - The controller class * @param handlerName - The handler method name * ..returns An array of guard instances */ static getHandlerGuards(controller: Constructor, handlerName: string | symbol): IGuard[]; /** * Resolves pipe classes or instances to pipe instances * @param pipeItems - The pipe classes or instances to resolve * ..returns An array of pipe instances */ static resolvePipes(pipeItems: PipeType[]): IPipe[]; /** * Gets pipes for a specific handler * @param controller - The controller class * @param handlerName - The handler method name * ..returns An array of pipe instances */ static getHandlerPipes(controller: Constructor, handlerName: string | symbol): IPipe[]; /** * Executes a series of pipes on a value * Pipes are executed in sequence, with each pipe's output feeding into the next pipe * @param value - The initial value to transform * @param metadata - Metadata about the parameter being transformed * @param pipes - Array of pipes to execute * @returns The final transformed value after all pipes have executed * @throws {Error} If any pipe transformation fails */ static executePipes(value: any, metadata: ArgumentMetadata, pipes: IPipe[]): Promise<any>; /** * Handles an exception by passing it through registered exception filters * Filters are executed in sequence until one returns a response * @param exception - The error to handle * @param context - The Hono context object * @returns A Response object if a filter handles the exception, undefined otherwise */ static handleException(exception: Error, context: Context): Promise<Response | undefined>; /** * Executes a list of exception filters * @param filterItems - The exception filter classes or instances to execute * @param exception - The exception that was thrown * @param context - The Hono context object * ..returns The response from the first filter that handles the exception or undefined if no filter handled it */ private static executeFilters; /** * Registers a module and its dependencies with the container * @param moduleClass - The module class to register * @param container - The dependency injection container * @returns An array of controller classes registered from this module */ static registerModule(moduleClass: Constructor, container: DiContainer): Promise<Constructor[]>; }