UNPKG

ufiber

Version:

Next-gen webserver for node-js developer

111 lines (110 loc) 3.57 kB
import { $404Handler, ErrorHandler, Handler, Middleware, Router, RouterRoute } from "../types.js"; //#region src/router/index.d.ts declare const kNotFound: unique symbol; declare const kErrorHandler: unique symbol; /** * Lightweight, router built on top of a RegExp/Trie based matcher. * Supports single or multiple route matchers (Trie, RegExp, or both). * like multi-router delegation and single-router optimization. */ declare class Router$1 { #private; routes: RouterRoute[]; router: Router<[Handler, RouterRoute]>; [kNotFound]: $404Handler; [kErrorHandler]: ErrorHandler; /** Register a GET route. */ get: (path: string, ...handlers: Handler[]) => this; /** Register a POST route. */ post: (path: string, ...handlers: Handler[]) => this; /** Register a PUT route. */ put: (path: string, ...handlers: Handler[]) => this; /** Register a DELETE route. */ delete: (path: string, ...handlers: Handler[]) => this; /** Register a PATCH route. */ patch: (path: string, ...handlers: Handler[]) => this; /** Register a HEAD route. */ head: (path: string, ...handlers: Handler[]) => this; /** Register an OPTIONS route. */ options: (path: string, ...handlers: Handler[]) => this; /** Register a route matching any HTTP method. */ all: (path: string, ...handlers: Handler[]) => this; constructor(); /** * Register a route for one or more HTTP methods and paths. * * @param method - A single method or an array of methods (e.g. `'get'`, `'post'`). * @param path - A single path or an array of paths. * @param handlers - One or more handlers to attach. * @returns This router instance (for chaining). * * @example * ```ts * router.on(['get', 'post'], ['/user', '/account'], handler); * ``` */ on: (method: string | string[], path: string | string[], ...handlers: Handler[]) => Router$1; /** * Registers middleware handlers. * Works similarly to `app.use()` in Express. * * @param arg1 - Path string or the first handler function. * @param handlers - Additional handler functions. * @returns This router instance. * * @example * ```ts * router.use(authMiddleware); * router.use('/api', apiMiddleware); * ``` */ use: (arg1: string | Middleware, ...handlers: Middleware[]) => Router$1; /** * `.onError()` handles an error and returns a customized Response. * * @param {ErrorHandler} handler - request Handler for error * @returns {Hono} changed Hono instance * * @example * ```ts * app.onError((err, c) => { * console.error(`${err}`) * return c.text('Custom Error Message', 500) * }) * ``` */ onError: (handler: ErrorHandler) => Router$1; /** * `.notFound()` allows you to customize a Not Found Response. * * @param {$404Handler} handler - request handler for not-found * @returns {Hono} changed Hono instance * * @example * ```ts * app.notFound((c) => { * return c.text('Custom 404 Message', 404) * }) * ``` */ notFound: (handler: $404Handler) => Router$1; /** * `.route()` allows grouping other Fiber instance in routes. * * @param {string} path - base Path * @param {Router} router - other Router instance * @returns {Router} routed Router instance * * @example * ```ts * const app = new Router() * const app2 = new Router() * * app2.get("/user", (c) => c.text("user")) * app.route("/api", app2) // GET /api/user * ``` */ route(path: string, router: Router$1): void; } //#endregion export { Router$1 as Router };