import { Middleware, NextMiddleware } from "./core"; import { Context } from "./context"; import * as pathToRegexp from "path-to-regexp"; export declare type PathRouteMap = Map>; export declare type RegExpRouteMap = Array>; export declare class RouteDescriptor { test: RegExp; definitions: IRouteDefinitionMap; constructor(test?: RegExp, definitions?: IRouteDefinitionMap); } export interface IRouteDefinitionMap { [method: string]: RouteDefinition; } export declare class RouteDefinition { handler: Middleware; paramKeys: any; constructor(handler: Middleware, paramKeys?: any); } export interface MatchResult { path: string; router: Router; route: RouteDefinition; descriptor: RouteDescriptor; data: RegExpMatchArray; params: any; } export declare type Route = string | RegExp; export declare type RouterOpts = { strict?: boolean; sensitive?: boolean; end?: boolean; delimiter?: string; exhaustive?: boolean; }; export declare class Router { static Defaults: RouterOpts; private _routes; private _middlewares; private _opts; constructor(opts?: RouterOpts); readonly opts: RouterOpts; readonly routes: RouteDescriptor[]; /** * Add a route for all http action methods. Basically, * add the route for each method in HttpMethod.ActionMethods * * @param route * @param handler */ all(route: Route, handler: Middleware, opts?: RouterOpts): this; /** * Add a route for any action regardless of the method. * If a specific method for the same path is also provided, * that always takes precedence. * @param route * @param handler */ any(route: Route, handler: Middleware, opts?: RouterOpts): this; /** * Add a route for Http GET method * @param route * @param handler */ get(route: Route, handler: Middleware, opts?: RouterOpts): this; /** * Add a route for Http PUT method * @param route * @param handler */ put(route: Route, handler: Middleware, opts?: RouterOpts): this; /** * Add a route for Http POST method * @param route * @param handler */ post(route: Route, handler: Middleware, opts?: RouterOpts): this; /** * Add a route for Http DELETE method * @param route * @param handler */ delete(route: Route, handler: Middleware, opts?: RouterOpts): this; /** * Add a route for Http PATCH method * @param route * @param handler */ patch(route: Route, handler: Middleware, opts?: RouterOpts): this; /** * Adds a middleware to the router. This is just like adding a * middleware to the application itself, and is executed whenever * the execution reaches the router. * */ use(path: Route, router: Router): Router; use(path: Route, middleware: Middleware): Router; use(...middleware: (Middleware | Router)[]): Router; /** * Add a definition for a route for a Http method. This is the method * that's internally called for each of the 'get', 'post' etc, methods * on the router. They are just convenience methods for this method. * * @param route * @param method Preferably, use the HttpMethod helpers instead * of using raw strings. For breviety, only the common standard HTTP 1.1 * methods are given there. * * @param handler */ define(route: Route, method: string, handler: Middleware, opts?: RouterOpts): this; private _definePathMatchRoute(route, method, handler, opts?); private _defineRegExpRoute(route, method, handler, path, paramKeys); /** * Check if a path and method pair matches a defined route in * the router, and if so return the route. * * It tries to match a specific route first, and if no routes are * found, tries to see if there's an 'any' route that's provided, * to match all routes. * * If there are no matches, returns null. * * @param {string} path * @param {string} method * @returns {MatchResult|null} * */ match(path: string, method: string): MatchResult; private _match(routes, method, targetPath); /** * Returns a middleware function that executes the router. It composes * the middlewares of the router when called and returns one middleware * that executes the router pipeline. * */ build(): Middleware; } export declare function createRouteHandler(router: Router): Middleware; export declare function routeHandler(router: Router, context: T, next: NextMiddleware): Promise; export declare class HttpMethod { static Get: string; static Head: string; static Post: string; static Put: string; static Delete: string; static Patch: string; static Options: string; static Trace: string; static ActionMethods: string[]; static Wildcard: string; } export declare function createRouteParams(match: RegExpMatchArray, keys: pathToRegexp.Key[], params?: any): any; export declare function decodeRouteParam(param: string): string;