http-micro
Version:
Micro-framework on top of node's http module
154 lines (153 loc) • 5.6 kB
TypeScript
import { Middleware, NextMiddleware } from "./core";
import { Context } from "./context";
import * as pathToRegexp from "path-to-regexp";
export declare type PathRouteMap<T extends Context> = Map<string, RouteDescriptor<T>>;
export declare type RegExpRouteMap<T extends Context> = Array<RouteDescriptor<T>>;
export declare class RouteDescriptor<T extends Context> {
test: RegExp;
definitions: IRouteDefinitionMap<T>;
constructor(test?: RegExp, definitions?: IRouteDefinitionMap<T>);
}
export interface IRouteDefinitionMap<T extends Context> {
[method: string]: RouteDefinition<T>;
}
export declare class RouteDefinition<T extends Context> {
handler: Middleware<T>;
paramKeys: any;
constructor(handler: Middleware<T>, paramKeys?: any);
}
export interface MatchResult<T extends Context> {
path: string;
router: Router<T>;
route: RouteDefinition<T>;
descriptor: RouteDescriptor<T>;
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<T extends Context = Context> {
static Defaults: RouterOpts;
private _routes;
private _middlewares;
private _opts;
constructor(opts?: RouterOpts);
readonly opts: RouterOpts;
readonly routes: RouteDescriptor<T>[];
/**
* 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<T>, 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<T>, opts?: RouterOpts): this;
/**
* Add a route for Http GET method
* @param route
* @param handler
*/
get(route: Route, handler: Middleware<T>, opts?: RouterOpts): this;
/**
* Add a route for Http PUT method
* @param route
* @param handler
*/
put(route: Route, handler: Middleware<T>, opts?: RouterOpts): this;
/**
* Add a route for Http POST method
* @param route
* @param handler
*/
post(route: Route, handler: Middleware<T>, opts?: RouterOpts): this;
/**
* Add a route for Http DELETE method
* @param route
* @param handler
*/
delete(route: Route, handler: Middleware<T>, opts?: RouterOpts): this;
/**
* Add a route for Http PATCH method
* @param route
* @param handler
*/
patch(route: Route, handler: Middleware<T>, 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<T>): Router<T>;
use(path: Route, middleware: Middleware<T>): Router<T>;
use(...middleware: (Middleware<T> | Router<T>)[]): Router<T>;
/**
* 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<T>, 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<T>|null}
*
*/
match(path: string, method: string): MatchResult<T>;
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<T>;
}
export declare function createRouteHandler<T extends Context>(router: Router<T>): Middleware<T>;
export declare function routeHandler<T extends Context>(router: Router<T>, context: T, next: NextMiddleware): Promise<any>;
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;