UNPKG

honestjs

Version:

HonestJS - a modern web framework built on top of Hono

125 lines (124 loc) 4.24 kB
import { Hono } from 'hono'; import type { HonestOptions, RouteInfo } from './interfaces'; import type { Constructor } from './types'; /** * Main application class for the Honest framework * Serves as the entry point for creating and configuring web applications * * Features: * - Module-based architecture for organizing application components * - Dependency injection container integration * - Plugin system for extending functionality * - Route management with versioning support * - Global error handling * * @example * ```ts * const { app } = await Application.create(AppModule, { * routing: { prefix: '/api', version: 1 }, * plugins: [new LoggerPlugin()] * }); * ``` */ export declare class Application { private readonly hono; private readonly container; private readonly routeManager; private readonly options; /** * Creates a new Application instance with the specified configuration * @param options - Configuration options for the application * @param options.routing - Route configuration (prefix, versioning) * @param options.plugins - Array of plugins to extend functionality * @param options.container - Custom dependency injection container * @param options.hono - Hono-specific configuration options * @throws {Error} If options are invalid or initialization fails */ constructor(options?: HonestOptions); /** * Sets up global components from application options * Initializes the component manager and registers global middleware, * guards, pipes, and filters * @private */ private setupComponents; /** * Sets up global error handlers for the application * Configures handlers for 404 Not Found and general error cases * @private */ private setupErrorHandlers; /** * Resolves a plugin from either a constructor or instance * @param pluginType - Plugin constructor or instance * @returns Resolved plugin instance * @private * @throws {Error} If plugin instantiation fails */ private resolvePlugin; /** * Registers a module with the application * Processes the module's metadata and registers its controllers * * @param moduleClass - The module class to register * @returns The application instance for method chaining * @throws {Error} If module registration fails * * @example * ```ts * const app = new Application(); * await app.register(UsersModule) * .register(AuthModule); * ``` */ register(moduleClass: Constructor): Promise<Application>; /** * Creates and initializes a new application with a root module * * Process: * 1. Creates application instance with provided options * 2. Initializes and runs plugin lifecycle hooks * 3. Registers the root module * 4. Returns both the application and Hono instances * * @param rootModule - The root module class for the application * @param options - Application configuration options * @returns Object containing the application and Hono instances * @throws {Error} If application creation or module registration fails * * @example * ```ts * const { app, hono } = await Application.create(AppModule, { * routing: { prefix: '/api' } * }); * ``` */ static create(rootModule: Constructor, options?: HonestOptions): Promise<{ app: Application; hono: Hono; }>; /** * Gets the underlying Hono instance for direct access * Use this method when you need to access Hono-specific features * * @returns The Hono application instance * @example * ```ts * const hono = app.getApp(); * hono.use(someHonoMiddleware()); * ``` */ getApp(): Hono; /** * Gets information about all registered routes in the application * Useful for documentation and debugging purposes * * @returns Array of route information objects (read-only) * @example * ```ts * const routes = app.getRoutes(); * console.log(routes.map(r => r.path)); * ``` */ getRoutes(): ReadonlyArray<RouteInfo>; }