///
///
declare module "@hatsy/router" {
import type { PathRoute, RouteMatch, RoutePattern, URLRoute } from "@hatsy/route-match";
/**
* Request routing means.
*
* @typeParam TRoute - A type of supported route.
*/
export interface RouterMeans {
/**
* Original route.
*
* This should never be changed.
*/
readonly fullRoute: TRoute;
/**
* Matching route.
*
* This is either an original route, or its tail.
*/
readonly route: TRoute;
/**
* A successful match of the route(s) against pattern(s).
*/
readonly routeMatch: RouteMatch;
/**
* A parser of route pattern string.
*/
routePattern(pattern: string): RoutePattern;
}
}
declare module "@hatsy/router" {
import type { RequestHandler, RequestHandlerMethod } from "@hatsy/hatsy/core.js";
/**
* Request processing handlers for route entry names.
*
* @typeParam TMeans - Supported route processing means.
*/
export interface DispatchNames {
/**
* Request handler method with entry name as its key.
*/
readonly [entry: string]: RequestHandlerMethod | undefined;
}
/**
* Dispatches request processing by route entry name.
*
* Builds a route processing handler that selects a route handler to dispatch to accordingly to the name of the first
* route entry.
*
* Target route handler receives a route tail without first entry.
*
* @typeParam TMeans - Supported route processing means.
* @param names - A map of request processing handlers for corresponding route entry names.
*
* @returns New route processing handler.
*/
export function dispatchByName(names: DispatchNames): RequestHandler;
}
declare module "@hatsy/router" {
import { RequestContext, RequestHandler, RequestHandlerMethod } from "@hatsy/hatsy/core.js";
import { PathRoute, RoutePattern, URLRoute } from "@hatsy/route-match";
/**
* Routing dispatch pattern.
*
* Declares a route handler to delegate request processing to when the route matches target {@link on pattern}.
*
* @typeParam TRoute - A type of supported route.
* @typeParam TMeans - A type of route processing means.
*/
export interface DispatchPattern = RouterMeans> {
/**
* A route pattern that should match the route in order to dispatch processing the the {@link to handler}.
*
* When specified as a string, the pre-configured {@link RouterMeans.routePattern pattern parser} is used to parse it.
*/
readonly on: RoutePattern | string;
/**
* A route handler to dispatch request processing to when the route matches the {@link on pattern}.
*
* This handler would receive a {@link tail} of the matching route.
*/
readonly to: RequestHandlerMethod;
/**
* Extracts route tail from matching route.
*
* The extracted route tail is passed to the {@link to handler}.
*
* @param context - Route processing context of the matching route.
*
* @returns Extracted tail of the matching route.
*
* @default Extracts a matching route tail starting from the first capture/wildcard. If no captures or wildcards
* present in pattern, then full route extracted.
*/
tail?(context: RequestContext): TRoute;
}
/**
* Dispatches request processing by matching route pattern.
*
* Builds a route processing handler that dispatcher to route handler(s) corresponding to pattern the route matches.
*
* Selects the first matching pattern and delegates request processing to its handler. If the handler not responded,
* then tries the next matching pattern, and so on until responded or no routes left.
*
* @typeParam TRoute - A type of supported route.
* @typeParam TMeans - A type of route processing means.
* @param routes - Either a routing dispatch pattern, or iterable of routing dispatch patterns.
*
* @returns Route processing handler.
*/
export function dispatchByPattern>(routes: DispatchPattern | Iterable>): RequestHandler;
}
declare module "@hatsy/router" {
import type { HttpMeans } from "@hatsy/hatsy";
import type { RequestContext } from "@hatsy/hatsy/core.js";
import type { PathRoute, RoutePattern, URLRoute } from "@hatsy/route-match";
/**
* Router configuration.
*
* @typeParam TMeans - A type of incoming request processing means.
* @typeParam TRoute - Supported route type.
*/
export type RouterConfig = RouterConfig.DefaultRoute | RouterConfig.CustomRoute;
export namespace RouterConfig {
/**
* Base router configuration.
*
* @typeParam TMeans - A type of incoming request processing means.
* @typeParam TRoute - Supported route type.
*/
interface Base {
/**
* A parser of route pattern string.
*
* The `this` parameter is bound to current request processing means.
*
* @param pattern - Pattern string in supported format.
* @param context - Current request processing context.
*
* @default Supports patterns in simple format (`simpleRoutePattern()`).
*/
routePattern?(pattern: string, context: RequestContext>): RoutePattern;
}
/**
* Router configuration with default route builder.
*
* @typeParam TMeans - A type of incoming request processing means.
* @typeParam TRoute - Supported route type.
*/
interface DefaultRoute extends Base {
readonly buildRoute?: undefined;
}
/**
* Router configuration with custom route builder.
*
* @typeParam TMeans - A type of incoming HTTP request processing means.
*/
interface CustomRoute extends Base {
/**
* Builds a route based on incoming request.
*
* @param context - Request processing context.
*
* @returns New URL route.
*
* @default Builds a route based on {@link HttpMeans.Addresses.url request URL} (for HTTP requests).
*/
buildRoute(context: RequestContext): TRoute;
}
}
}
declare module "@hatsy/router" {
import type { HttpMeans } from "@hatsy/hatsy";
import { RequestCapability } from "@hatsy/hatsy/core.js";
import { PathRoute, URLRoute } from "@hatsy/route-match";
/**
* Request routing capability.
*
* Provides {@link RouterMeans request routing means} for handlers.
*
* @typeParam TInput - A type of request processing means required in order to apply this capability.
* @typeParam TRoute - Supported route type.
*/
export interface Routing extends RequestCapability> {
/**
* Configures routing capability that constructs a route by incoming HTTP request.
*
* @param config - Router configuration without route build.
*
* @returns New request routing capability.
*/
with(config: RouterConfig.DefaultRoute): Routing;
/**
* Configures routing capability with custom route builder.
*
* @param config - Route configuration with custom route builder.
*
* @returns New request routing capability.
*/
with(config: RouterConfig.CustomRoute): Routing;
}
/**
* Request routing capability instance.
*
* Can be used directly (for HTTP requests), or {@link Routing.with configured} first.
*/
export const Routing: Routing;
}
//# sourceMappingURL=router.d.ts.map